|
| 1 | +function NNlib.stft(x; |
| 2 | + n_fft::Int, hop_length::Int = n_fft ÷ 4, window = nothing, |
| 3 | + center::Bool = true, normalized::Bool = false, |
| 4 | +) |
| 5 | + kab = get_backend(x) |
| 6 | + use_window = !isnothing(window) |
| 7 | + |
| 8 | + use_window && kab != get_backend(window) && throw(ArgumentError( |
| 9 | + "`window` must be on the same device as stft input `x` ($kab), \ |
| 10 | + instead: `$(get_backend(window))`.")) |
| 11 | + use_window && !(0 < length(window) ≤ n_fft) && throw(ArgumentError( |
| 12 | + "Expected `0 < length(window) ≤ n_fft=$n_fft`, \ |
| 13 | + but got `length(window)=$(length(window))`.")) |
| 14 | + hop_length < 0 && throw(ArgumentError( |
| 15 | + "Expected `hop_length > 0`, but got `hop_length=$hop_length`.")) |
| 16 | + |
| 17 | + # Pad window on both sides with `0` to `n_fft` length if needed. |
| 18 | + if use_window && length(window) < n_fft |
| 19 | + left = ((n_fft - length(window)) ÷ 2) + 1 |
| 20 | + tmp = KernelAbstractions.zeros(kab, eltype(window), n_fft) |
| 21 | + tmp[left:left + length(window) - 1] .= window |
| 22 | + window = tmp |
| 23 | + end |
| 24 | + |
| 25 | + if center |
| 26 | + pad_amount = n_fft ÷ 2 |
| 27 | + x = pad_reflect(x, pad_amount; dims=1) |
| 28 | + end |
| 29 | + |
| 30 | + n = size(x, 1) |
| 31 | + (0 < n_fft ≤ n) || throw(ArgumentError( |
| 32 | + "Expected `0 < n_fft ≤ size(x, 1)=$n`, but got `n_fft=$n_fft`.")) |
| 33 | + |
| 34 | + n_frames = 1 + (n - n_fft) ÷ hop_length |
| 35 | + |
| 36 | + # time2col. |
| 37 | + # Reshape `x` to (n_fft, n_frames, B) if needed. |
| 38 | + # Each row in `n_frames` is shifted by `hop_length`. |
| 39 | + if n_frames > 1 |
| 40 | + # TODO can be more efficient if we support something like torch.as_strided |
| 41 | + ids = [ |
| 42 | + row + hop_length * col |
| 43 | + for row in 1:n_fft, col in 0:(n_frames - 1)] |
| 44 | + x = x[ids, ntuple(_ -> Colon(), ndims(x) - 1)...] |
| 45 | + end |
| 46 | + |
| 47 | + region = 1 |
| 48 | + use_window && (x = x .* window;) |
| 49 | + y = eltype(x) <: Complex ? fft(x, region) : rfft(x, region) |
| 50 | + |
| 51 | + normalized && (y = y .* eltype(y)(n_fft^-0.5);) |
| 52 | + return y |
| 53 | +end |
| 54 | + |
| 55 | +function NNlib.istft(y; |
| 56 | + n_fft::Int, hop_length::Int = n_fft ÷ 4, window = nothing, |
| 57 | + center::Bool = true, normalized::Bool = false, |
| 58 | + return_complex::Bool = false, |
| 59 | + original_length::Union{Nothing, Int} = nothing, |
| 60 | +) |
| 61 | + kab = get_backend(y) |
| 62 | + use_window = !isnothing(window) |
| 63 | + |
| 64 | + use_window && kab != get_backend(window) && throw(ArgumentError( |
| 65 | + "`window` must be on the same device as istft input `y` ($kab), \ |
| 66 | + instead: `$(get_backend(window))`.")) |
| 67 | + use_window && !(0 < length(window) ≤ n_fft) && throw(ArgumentError( |
| 68 | + "Expected `0 < length(window) ≤ n_fft=$n_fft`, \ |
| 69 | + but got `length(window)=$(length(window))`.")) |
| 70 | + hop_length < 0 && throw(ArgumentError( |
| 71 | + "Expected `hop_length > 0`, but got `hop_length=$hop_length`.")) |
| 72 | + |
| 73 | + # TODO check `y` eltype is complex |
| 74 | + |
| 75 | + n_frames = size(y, 2) |
| 76 | + |
| 77 | + # Pad window on both sides with `0` to `n_fft` length if needed. |
| 78 | + if use_window && length(window) < n_fft |
| 79 | + left = ((n_fft - length(window)) ÷ 2) + 1 |
| 80 | + tmp = KernelAbstractions.zeros(kab, eltype(window), n_fft) |
| 81 | + tmp[left:left + length(window) - 1] .= window |
| 82 | + window = tmp |
| 83 | + end |
| 84 | + |
| 85 | + # Denormalize. |
| 86 | + normalized && (y = y .* eltype(y)(n_fft^0.5);) |
| 87 | + |
| 88 | + region = 1 |
| 89 | + x = return_complex ? ifft(y, region) : irfft(y, n_fft, region) |
| 90 | + |
| 91 | + # De-apply window. |
| 92 | + use_window && (x = x ./ window;) |
| 93 | + |
| 94 | + # col2time. |
| 95 | + expected_output_len = n_fft + hop_length * (n_frames - 1) |
| 96 | + |
| 97 | + ids = Vector{Int}(undef, expected_output_len) |
| 98 | + in_idx, out_idx = 0, 0 |
| 99 | + prev_e, v = 0, 0 |
| 100 | + |
| 101 | + for col in 0:(n_frames - 1) |
| 102 | + for row in 1:n_fft |
| 103 | + in_idx += 1 |
| 104 | + v = row + hop_length * col |
| 105 | + v > prev_e || continue |
| 106 | + |
| 107 | + out_idx += 1 |
| 108 | + ids[out_idx] = in_idx |
| 109 | + end |
| 110 | + prev_e = v |
| 111 | + end |
| 112 | + |
| 113 | + # In case of batched input, reshaped it (n_fft, n_frames, batch) -> (:, batch). |
| 114 | + nd = ntuple(_ -> Colon(), ndims(x) - 2) |
| 115 | + ndims(x) == 3 && (x = reshape(x, (:, size(x, 3)));) |
| 116 | + x = x[ids, nd...] |
| 117 | + |
| 118 | + # Trim padding. |
| 119 | + left = center ? (n_fft ÷ 2 + 1) : 1 |
| 120 | + right = if isnothing(original_length) |
| 121 | + center ? (size(x, 1) - n_fft ÷ 2) : expected_output_len |
| 122 | + else |
| 123 | + left + original_length - 1 |
| 124 | + end |
| 125 | + x = x[left:right, nd...] |
| 126 | + return x |
| 127 | +end |
0 commit comments