File size: 2,185 Bytes
157a4c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math

import torch
import torch.nn.functional as F
import torchaudio


def time_stretch_preserve_pitch(waveform: torch.Tensor, sample_rate: int, target_samples: int) -> torch.Tensor:
    """
    Pitch-preserving time stretch using STFT phase vocoder.

    waveform: [C, L] float tensor (CPU or GPU)
    returns:  [C, target_samples] float tensor
    """
    if waveform.dim() == 1:
        waveform = waveform.unsqueeze(0)

    waveform = waveform.to(torch.float32)

    src_len = waveform.shape[-1]
    if src_len == 0 or target_samples <= 0:
        return waveform[..., :0]

    if src_len == target_samples:
        return waveform

    # rate > 1.0 speeds up (shorter), rate < 1.0 slows down (longer)
    rate = float(src_len) / float(target_samples)

    # Use sample_rate to pick STFT params
    win_seconds = 0.046
    hop_seconds = 0.0115

    n_fft_target = int(sample_rate * win_seconds)
    n_fft = 1 << max(8, int(math.floor(math.log2(max(256, n_fft_target)))))  # >=256, pow2
    win_length = n_fft
    hop_length = max(64, int(sample_rate * hop_seconds))
    hop_length = min(hop_length, win_length // 2)

    window = torch.hann_window(win_length, device=waveform.device, dtype=waveform.dtype)

    stft = torch.stft(
        waveform,
        n_fft=n_fft,
        hop_length=hop_length,
        win_length=win_length,
        window=window,
        center=True,
        return_complex=True,
    )  # [C, F, T] complex

    # n_freq must match STFT's frequency bins (n_fft//2 + 1)
    stretcher = torchaudio.transforms.TimeStretch(
        n_freq=stft.shape[-2],
        hop_length=hop_length,
        fixed_rate=rate,
    ).to(waveform.device)

    stft_stretched = stretcher(stft)  # [C, F, T']

    stretched = torch.istft(
        stft_stretched,
        n_fft=n_fft,
        hop_length=hop_length,
        win_length=win_length,
        window=window,
        center=True,
        length=target_samples,
    )

    if stretched.shape[-1] > target_samples:
        stretched = stretched[..., :target_samples]
    elif stretched.shape[-1] < target_samples:
        stretched = F.pad(stretched, (0, target_samples - stretched.shape[-1]))

    return stretched