File size: 7,041 Bytes
dfd1909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Audio super-resolution using FlashSR.

Independently written wrapper around the FlashSR model by Jaekwon Im and
Juhan Nam (KAIST). Supports files of arbitrary length via windowed processing
with overlap-add. No dependency on torchcodec or FFmpeg -- uses soundfile for
all I/O.

Paper: https://arxiv.org/abs/2501.10807
"""

from __future__ import annotations

import argparse
import math
import os
import sys
import time
from pathlib import Path

import numpy as np
import soundfile as sf
import torch
from scipy.signal import resample_poly

from FlashSR.FlashSR import FlashSR

# ---- constants ----------------------------------------------------------------

TARGET_SR = 48_000
WINDOW_LEN = 245_760          # samples per model call  (5.12 s at 48 kHz)
OVERLAP = 24_000              # crossfade region         (0.50 s)
HOP = WINDOW_LEN - OVERLAP   # advance per window       (4.62 s)

AUDIO_EXTENSIONS = {".wav", ".flac", ".mp3", ".ogg", ".opus"}


# ---- helpers ------------------------------------------------------------------

def _load_mono(path: str | Path) -> tuple[np.ndarray, int]:
    """Read an audio file, mix to mono, return (float32 array, sample_rate)."""
    data, sr = sf.read(str(path), dtype="float32")
    if data.ndim == 2:
        data = data.mean(axis=1)
    return data, sr


def _resample_if_needed(audio: np.ndarray, orig_sr: int) -> np.ndarray:
    """Polyphase resample to TARGET_SR when the source rate differs."""
    if orig_sr == TARGET_SR:
        return audio
    return resample_poly(audio, TARGET_SR, orig_sr).astype(np.float32)


def _build_fade(length: int) -> torch.Tensor:
    """Half-cosine fade-in ramp of *length* samples (0 -> 1)."""
    t = torch.linspace(0.0, math.pi / 2, length)
    return torch.sin(t) ** 2          # cos^2 fade is smooth at both ends


def _pad_to(tensor: torch.Tensor, n: int) -> torch.Tensor:
    """Right-zero-pad the last dimension to at least *n* samples."""
    deficit = n - tensor.shape[-1]
    if deficit <= 0:
        return tensor
    return torch.nn.functional.pad(tensor, (0, deficit))


# ---- core ---------------------------------------------------------------------

def build_model(weights_dir: str | Path, device: torch.device) -> FlashSR:
    """Instantiate FlashSR and load pretrained weights."""
    w = Path(weights_dir)
    model = FlashSR(
        student_ldm_ckpt_path=str(w / "student_ldm.pth"),
        sr_vocoder_ckpt_path=str(w / "sr_vocoder.pth"),
        autoencoder_ckpt_path=str(w / "vae.pth"),
    )
    return model.to(device).eval()


@torch.inference_mode()
def enhance(
    model: FlashSR,
    waveform: np.ndarray,
    *,
    device: torch.device,
    lowpass: bool = False,
) -> np.ndarray:
    """
    Run FlashSR on a mono waveform (numpy float32, 48 kHz).

    Long inputs are split into overlapping windows and reassembled with
    overlap-add using a raised-cosine crossfade.

    Returns enhanced waveform as numpy float32 at 48 kHz.
    """
    signal = torch.from_numpy(waveform).unsqueeze(0)  # (1, T)
    n_samples = signal.shape[-1]

    # --- short signal: single pass -------------------------------------------
    if n_samples <= WINDOW_LEN:
        chunk = _pad_to(signal, WINDOW_LEN).to(device)
        out = model(chunk, lowpass_input=lowpass)
        return out[0, :n_samples].cpu().numpy()

    # --- long signal: overlap-add --------------------------------------------
    fade = _build_fade(OVERLAP)
    accumulator = torch.zeros(n_samples)
    norm = torch.zeros(n_samples)

    offset = 0
    while offset < n_samples:
        end = min(offset + WINDOW_LEN, n_samples)
        segment = signal[:, offset:end]
        segment = _pad_to(segment, WINDOW_LEN).to(device)

        enhanced_seg = model(segment, lowpass_input=lowpass).cpu().squeeze(0)
        seg_len = min(WINDOW_LEN, n_samples - offset)
        enhanced_seg = enhanced_seg[:seg_len]

        # per-sample weights: 1.0 everywhere, faded in at overlap boundary
        w = torch.ones(seg_len)
        if offset > 0 and seg_len > OVERLAP:
            w[:OVERLAP] = fade

        accumulator[offset : offset + seg_len] += enhanced_seg * w
        norm[offset : offset + seg_len] += w
        offset += HOP

    norm.clamp_(min=1e-8)
    return (accumulator / norm).numpy()


# ---- file-level convenience ---------------------------------------------------

def enhance_file(
    model: FlashSR,
    src: str | Path,
    dst: str | Path,
    *,
    device: torch.device,
    lowpass: bool = False,
) -> float:
    """Enhance one file. Returns duration in seconds."""
    raw, sr = _load_mono(src)
    audio = _resample_if_needed(raw, sr)
    result = enhance(model, audio, device=device, lowpass=lowpass)
    os.makedirs(os.path.dirname(dst) or ".", exist_ok=True)
    sf.write(str(dst), result, TARGET_SR)
    return len(audio) / TARGET_SR


def collect_audio_files(root: str | Path) -> list[Path]:
    """Recursively find audio files under *root*."""
    root = Path(root)
    return sorted(p for p in root.rglob("*") if p.suffix.lower() in AUDIO_EXTENSIONS)


# ---- CLI ----------------------------------------------------------------------

def cli() -> None:
    ap = argparse.ArgumentParser(
        description="FlashSR audio super-resolution (by Im & Nam, KAIST)")
    ap.add_argument("--input", "-i", required=True,
                    help="Input audio file or directory")
    ap.add_argument("--output", "-o", required=True,
                    help="Output file or directory")
    ap.add_argument("--weights", "-w", default="./weights",
                    help="Directory containing the three .pth weight files")
    ap.add_argument("--lowpass", action="store_true",
                    help="Apply lowpass filter before enhancement")
    ap.add_argument("--device", default="cuda",
                    help="Torch device (default: cuda)")
    args = ap.parse_args()

    dev = torch.device(args.device if torch.cuda.is_available() else "cpu")
    print(f"Device: {dev}")

    print("Loading model...")
    t0 = time.monotonic()
    model = build_model(args.weights, dev)
    print(f"Loaded in {time.monotonic() - t0:.1f}s")

    # Resolve inputs
    inp = Path(args.input)
    out = Path(args.output)

    if inp.is_dir():
        files = collect_audio_files(inp)
        if not files:
            sys.exit(f"No audio files found in {inp}")
        pairs = [(f, out / f.relative_to(inp)) for f in files]
    else:
        pairs = [(inp, out)]

    total_dur = 0.0
    t_start = time.monotonic()

    for idx, (src, dst) in enumerate(pairs, 1):
        print(f"[{idx}/{len(pairs)}] {src} -> {dst}")
        dur = enhance_file(model, src, dst, device=dev, lowpass=args.lowpass)
        total_dur += dur
        print(f"  {dur:.1f}s of audio")

    elapsed = time.monotonic() - t_start
    rtf = total_dur / elapsed if elapsed > 0 else 0
    print(f"\nDone: {len(pairs)} file(s), {total_dur:.1f}s audio, "
          f"{elapsed:.1f}s wall-clock ({rtf:.1f}x realtime)")


if __name__ == "__main__":
    cli()