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2dd3
 
7965430
f5d2dd3
 
 
 
 
 
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
 
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
7965430
f5d2dd3
 
 
7965430
f5d2dd3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, Optional, Tuple

import torch
from einops import rearrange

from nemo.collections.asr.parts.preprocessing.features import make_seq_mask_like
from nemo.core.classes import NeuralModule, typecheck
from nemo.core.neural_types import AudioSignal, LengthsType, NeuralType, SpectrogramType
from nemo.utils import logging


class AudioToSpectrogram(NeuralModule):
    """Transform a batch of input multi-channel signals into a batch of
    STFT-based spectrograms.

    Args:
        fft_length: length of FFT
        hop_length: length of hops/shifts of the sliding window
        power: exponent for magnitude spectrogram. Default `None` will
               return a complex-valued spectrogram
        magnitude_power: Transform magnitude of the spectrogram as x^magnitude_power.
        scale: Positive scaling of the spectrogram.
    """

    def __init__(
        self, fft_length: int, hop_length: int, magnitude_power: float = 1.0, scale: float = 1.0, center: bool = True
    ):
        super().__init__()

        # For now, assume FFT length is divisible by two
        if fft_length % 2 != 0:
            raise ValueError(f'fft_length = {fft_length} must be divisible by 2')

        self.fft_length = fft_length
        self.hop_length = hop_length
        self.pad_mode = 'constant'
        window = torch.hann_window(self.win_length)
        self.register_buffer('window', window)

        self.num_subbands = fft_length // 2 + 1

        if magnitude_power <= 0:
            raise ValueError(f'Magnitude power needs to be positive: current value {magnitude_power}')
        self.magnitude_power = magnitude_power

        if scale <= 0:
            raise ValueError(f'Scale needs to be positive: current value {scale}')
        self.scale = scale
        self.center = center
        logging.debug('Initialized %s with:', self.__class__.__name__)
        logging.debug('\tfft_length:      %s', fft_length)
        logging.debug('\thop_length:      %s', hop_length)
        logging.debug('\tmagnitude_power: %s', magnitude_power)
        logging.debug('\tscale:           %s', scale)

    @property
    def win_length(self) -> int:
        return self.fft_length

    def stft(self, x: torch.Tensor):
        """Apply STFT as in torchaudio.transforms.Spectrogram(power=None)

        Args:
            x_spec: Input time-domain signal, shape (..., T)

        Returns:
            Time-domain signal ``x_spec = STFT(x)``, shape (..., F, N).
        """
        # pack batch
        B, C, T = x.size()
        x = rearrange(x, 'B C T -> (B C) T')

        x_spec = torch.stft(
            input=x,
            n_fft=self.fft_length,
            hop_length=self.hop_length,
            win_length=self.win_length,
            window=self.window,
            center=self.center,
            pad_mode=self.pad_mode,
            normalized=False,
            onesided=True,
            return_complex=True,
        )

        # unpack batch
        x_spec = rearrange(x_spec, '(B C) F N -> B C F N', B=B, C=C)

        return x_spec

    @property
    def input_types(self) -> Dict[str, NeuralType]:
        """Returns definitions of module output ports."""
        return {
            "input": NeuralType(('B', 'C', 'T'), AudioSignal()),
            "input_length": NeuralType(('B',), LengthsType(), optional=True),
        }

    @property
    def output_types(self) -> Dict[str, NeuralType]:
        """Returns definitions of module output ports."""
        return {
            "output": NeuralType(('B', 'C', 'D', 'T'), SpectrogramType()),
            "output_length": NeuralType(('B',), LengthsType()),
        }

    @typecheck()
    def forward(
        self, input: torch.Tensor, input_length: Optional[torch.Tensor] = None
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Convert a batch of C-channel input signals
        into a batch of complex-valued spectrograms.

        Args:
            input: Time-domain input signal with C channels, shape (B, C, T)
            input_length: Length of valid entries along the time dimension, shape (B,)

        Returns:
            Output spectrogram with F subbands and N time frames, shape (B, C, F, N)
            and output length with shape (B,).
        """
        B, T = input.size(0), input.size(-1)
        input = input.view(B, -1, T)

        # STFT output (B, C, F, N)
        with torch.amp.autocast(input.device.type, enabled=False):
            output = self.stft(input.float())

            if self.magnitude_power != 1:
                # apply power on the magnitude
                output = torch.pow(output.abs(), self.magnitude_power) * torch.exp(1j * output.angle())

            if self.scale != 1:
                # apply scaling of the coefficients
                output = self.scale * output

        if input_length is not None:
            # Mask padded frames
            output_length = self.get_output_length(input_length=input_length)

            length_mask: torch.Tensor = make_seq_mask_like(
                lengths=output_length, like=output, time_dim=-1, valid_ones=False
            )
            output = output.masked_fill(length_mask, 0.0)
        else:
            # Assume all frames are valid for all examples in the batch
            output_length = output.size(-1) * torch.ones(B, device=output.device).long()

        return output, output_length

    def get_output_length(self, input_length: torch.Tensor) -> torch.Tensor:
        """Get length of valid frames for the output.

        Args:
            input_length: number of valid samples, shape (B,)

        Returns:
            Number of valid frames, shape (B,)
        """
        # centered STFT results in (T // hop_length + 1) frames for T samples (cf. torch.stft)
        output_length = input_length.div(self.hop_length, rounding_mode='floor').add(1).long()
        return output_length


class SpectrogramToAudio(NeuralModule):
    """Transform a batch of input multi-channel spectrograms into a batch of
    time-domain multi-channel signals.

    Args:
        fft_length: length of FFT
        hop_length: length of hops/shifts of the sliding window
        magnitude_power: Transform magnitude of the spectrogram as x^(1/magnitude_power).
        scale: Spectrogram will be scaled with 1/scale before the inverse transform.

    Streaming usage (``center=False``)::

        # analysis should use the same window and center=False
        # Prefer hamming for center=False (see note below)
        window = torch.hamming_window(fft_length)
        spec2audio = SpectrogramToAudio(fft_length=fft_length, hop_length=hop_length, center=False)
        spec2audio.window = window
        spec2audio.use_streaming = True
        spec2audio.reset_streaming()

        parts = []
        for t in range(0, N, K):
            frames = spec[..., t : t + K]   # (B, C, F, K), complex
            out, _ = spec2audio(input=frames)
            parts.append(out)
        tail = spec2audio.stream_finalize()
        x_stream = torch.cat(parts + [tail], dim=-1)

    Notes: ``window`` must match analysis; call ``reset_streaming()`` before a new stream;
    ``stream_finalize()`` flushes the tail (empty if ``hop_length == win_length``).
    With ``center=False``, certain windows (e.g., Hann) may error in
    some PyTorch versions; Hamming works reliably. See
    `PyTorch issue #91309 <https://github.com/pytorch/pytorch/issues/91309>`_.
    """

    def __init__(
        self, fft_length: int, hop_length: int, magnitude_power: float = 1.0, scale: float = 1.0, center: bool = True
    ):
        super().__init__()

        # For now, assume FFT length is divisible by two
        if fft_length % 2 != 0:
            raise ValueError(f'fft_length = {fft_length} must be divisible by 2')

        self.fft_length = fft_length
        self.hop_length = hop_length
        window = torch.hann_window(self.win_length)
        self.register_buffer('window', window)

        self.num_subbands = fft_length // 2 + 1

        if magnitude_power <= 0:
            raise ValueError(f'Magnitude power needs to be positive: current value {magnitude_power}')
        self.magnitude_power = magnitude_power
        self.center = center
        if scale <= 0:
            raise ValueError(f'Scale needs to be positive: current value {scale}')
        self.scale = scale

        logging.debug('Initialized %s with:', self.__class__.__name__)
        logging.debug('\tfft_length:      %s', fft_length)
        logging.debug('\thop_length:      %s', hop_length)
        logging.debug('\tmagnitude_power: %s', magnitude_power)
        logging.debug('\tscale:           %s', scale)

        # --- Streaming state (initialized lazily) ---
        # Time-domain overlap-add buffers (initialized lazily)
        self._ola_accum: Optional[torch.Tensor] = None
        self._ola_weight: Optional[torch.Tensor] = None
        # Kept for backward compatibility; not used in OLA implementation
        self.use_streaming: bool = False

    @property
    def win_length(self) -> int:
        return self.fft_length

    def istft(self, x_spec: torch.Tensor):
        """Apply iSTFT as in torchaudio.transforms.InverseSpectrogram

        Args:
            x_spec: Input complex-valued spectrogram, shape (..., F, N)

        Returns:
            Time-domain signal ``x = iSTFT(x_spec)``, shape (..., T).
        """
        # pack batch
        B, C, F, N = x_spec.size()
        x_spec = rearrange(x_spec, 'B C F N -> (B C) F N')

        x = torch.istft(
            input=x_spec,
            n_fft=self.fft_length,
            hop_length=self.hop_length,
            win_length=self.win_length,
            window=self.window,
            center=self.center,
            normalized=False,
            onesided=True,
            length=None,
            return_complex=False,
        )

        # unpack batch
        x = rearrange(x, '(B C) T -> B C T', B=B, C=C)

        return x

    @property
    def input_types(self) -> Dict[str, NeuralType]:
        """Returns definitions of module output ports."""
        return {
            "input": NeuralType(('B', 'C', 'D', 'T'), SpectrogramType()),
            "input_length": NeuralType(('B',), LengthsType(), optional=True),
        }

    @property
    def output_types(self) -> Dict[str, NeuralType]:
        """Returns definitions of module output ports."""
        return {
            "output": NeuralType(('B', 'C', 'T'), AudioSignal()),
            "output_length": NeuralType(('B',), LengthsType()),
        }

    @typecheck()
    def forward(self, input: torch.Tensor, input_length: Optional[torch.Tensor] = None) -> torch.Tensor:
        """Convert input complex-valued spectrogram to a time-domain
        signal. Multi-channel IO is supported.

        Offline mode (default): processes the entire input spectrogram at once.
        Streaming mode: expects one or more frames (N>=1) and returns hop_length * N samples per call.

        Args:
            input: Input spectrogram for C channels, shape (B, C, F, N)
            input_length: Length of valid entries along the time dimension, shape (B,)

        Returns:
            - Offline: (B, C, T_total), lengths (B,)
            - Streaming (N=1): (B, C, hop_length), lengths (B,) filled with hop_length
        """
        B, F, N = input.size(0), input.size(-2), input.size(-1)
        assert F == self.num_subbands, f'Number of subbands F={F} not matching self.num_subbands={self.num_subbands}'
        input = input.view(B, -1, F, N)

        if not input.is_complex():
            raise ValueError("Expected `input` to be complex dtype.")

        # iSTFT output (B, C, T)
        with torch.amp.autocast(input.device.type, enabled=False):
            output = input.cfloat()

            if self.scale != 1:
                # apply 1/scale on the coefficients
                output = output / self.scale

            if self.magnitude_power != 1:
                # apply 1/power on the magnitude
                output = torch.pow(output.abs(), 1 / self.magnitude_power) * torch.exp(1j * output.angle())

            # --- Streaming mode ---
            if self.use_streaming:
                # Streaming expects a single frame at a time to avoid internal iteration.
                out_stream = self.stream_update(output)  # (B, C, <= hop_length)
                out_len = torch.full((B,), out_stream.size(-1), dtype=torch.long, device=out_stream.device)
                return out_stream, out_len

            output = self.istft(output)

        if input_length is not None:
            # Mask padded samples
            output_length = self.get_output_length(input_length=input_length)

            length_mask: torch.Tensor = make_seq_mask_like(
                lengths=output_length, like=output, time_dim=-1, valid_ones=False
            )
            output = output.masked_fill(length_mask, 0.0)
        else:
            # Assume all frames are valid for all examples in the batch
            output_length = output.size(-1) * torch.ones(B, device=output.device).long()

        return output, output_length

    def get_output_length(self, input_length: torch.Tensor) -> torch.Tensor:
        """Get length of valid samples for the output.

        Args:
            input_length: number of valid frames, shape (B,)

        Returns:
            Number of valid samples, shape (B,)
        """
        # centered STFT results in ((N-1) * hop_length) time samples for N frames (cf. torch.istft)
        output_length = input_length.sub(1).mul(self.hop_length).long()
        return output_length

    @property
    def _stream_initialized(self) -> bool:
        """Return True if streaming buffers are initialized."""
        return (self._ola_accum is not None) and (self._ola_weight is not None)

    @property
    def _eps(self) -> float:
        """Machine epsilon for the active streaming dtype."""
        dtype = self._ola_weight.dtype if self._ola_weight is not None else self.window.dtype
        return torch.finfo(dtype).eps

    # ------------------------------------------------------------------
    # Streaming iSTFT API (frame-by-frame with overlap-add buffering)
    # ------------------------------------------------------------------
    def _init_stream_buffers(self, shape_like: torch.Tensor) -> None:
        """Initialize streaming buffers based on an input tensor."""
        if self._stream_initialized:
            return
        if shape_like.dim() != 4:
            raise ValueError("Expected input of shape (B, C, F, N_frames) for streaming.")
        B, C = shape_like.size(0), shape_like.size(1)
        device = shape_like.device
        # Real-valued buffers for accumulated time-domain samples and weights
        dtype = torch.float32 if shape_like.dtype == torch.complex64 else torch.float64
        self._ola_accum = torch.zeros(B, C, self.win_length, device=device, dtype=dtype)
        self._ola_weight = torch.zeros(B, C, self.win_length, device=device, dtype=dtype)

    def reset_streaming(self) -> None:
        """Reset the internal streaming buffers.

        Re-initialization happens lazily on the next call to `stream_update`.
        """
        self._ola_accum = None
        self._ola_weight = None

    def _shift_left_inplace(self, buffer: torch.Tensor) -> None:
        """Shift buffer left by hop length and zero-fill the tail in-place."""
        hop = self.hop_length
        buffer[..., :-hop] = buffer[..., hop:].clone()
        buffer[..., -hop:] = 0.0

    @torch.no_grad()
    def stream_update(self, input: torch.Tensor) -> torch.Tensor:
        """Consume one or more spectrogram frames (N>=1) and return hop_length * N samples via OLA.

        Steps per frame:
        - inverse FFT
        - apply synthesis window
        - overlap-add into accumulation buffer
        - emit first hop_length samples normalized by window-sum-square
        - shift buffers left by hop_length
        """
        if not input.is_complex():
            raise ValueError("Expected `input` to be complex dtype for streaming.")

        if self.center:
            raise ValueError("Streaming iSTFT requires center=False.")

        # Lazily initialize buffers
        self._init_stream_buffers(input)

        B, C, F, num_frames = input.size()
        assert F == self.num_subbands, f"Number of subbands F={F} not matching self.num_subbands={self.num_subbands}"

        # Vectorized inverse FFT over frequency bins (dim=-2), yields (B, C, T, N)
        frames_time = torch.fft.irfft(input, n=self.fft_length, dim=-2)

        # Prepare window and ensure buffers are on correct device/dtype
        hop = self.hop_length
        emitted_parts = []
        # Window shaped for broadcasting over frames
        win = self.window.to(frames_time.device, dtype=frames_time.dtype).view(1, 1, self.win_length, 1)
        win_sq = win[..., 0].squeeze(-1).pow(2)  # (1, 1, T)
        frames_time_windowed = frames_time * win  # (B, C, T, N)

        # Ensure buffers on correct device/dtype
        self._ola_accum = self._ola_accum.to(frames_time_windowed.device, dtype=frames_time_windowed.dtype)
        self._ola_weight = self._ola_weight.to(frames_time_windowed.device, dtype=frames_time_windowed.dtype)

        # Iterate over frames for OLA
        for t in range(num_frames):
            frame_t = frames_time_windowed[..., t]  # (B, C, T)

            # Overlap-add accumulation and window-sum-square weights
            self._ola_accum.add_(frame_t)
            self._ola_weight.add_(win_sq)

            # Emit first hop_length samples with normalization
            denom = torch.clamp(self._ola_weight[..., :hop], min=self._eps)
            emitted = self._ola_accum[..., :hop] / denom
            emitted_parts.append(emitted)

            # Shift buffers left by hop_length
            self._shift_left_inplace(self._ola_accum)
            self._shift_left_inplace(self._ola_weight)

        return torch.cat(emitted_parts, dim=-1)

    @torch.no_grad()
    def stream_finalize(self) -> torch.Tensor:
        """Flush the remaining buffered samples (final tail for center=False).

        After processing the last frame, the streaming loop has emitted N*hop
        samples. The remaining tail corresponds to the last (win_length - hop)
        samples, which we return after proper window-sum-square normalization.
        """
        if not self._stream_initialized:
            return torch.tensor((), device=self.window.device)

        tail_len = self.win_length - self.hop_length
        if tail_len <= 0:
            return torch.tensor((), device=self.window.device)

        denom_tail = torch.clamp(self._ola_weight[..., :tail_len], min=self._eps)
        tail = self._ola_accum[..., :tail_len] / denom_tail
        return tail