File size: 9,569 Bytes
a0fcd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184639f
a0fcd39
184639f
a0fcd39
 
 
184639f
a0fcd39
 
184639f
a0fcd39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184639f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Audio processing for pitch shifting and time stretching."""

import subprocess
import tempfile
import os
from typing import Optional, Callable
from concurrent.futures import ProcessPoolExecutor, as_completed

import numpy as np
import soundfile as sf
import pyrubberband as pyrb

from ..models.session import StemData
from ..utils.audio_utils import normalize


def get_rubberband_options(stem_type: str) -> list[str]:
    """
    Get rubberband CLI flags optimized for stem type.

    Args:
        stem_type: Type of stem ("drums", "bass", or default)

    Returns:
        List of CLI flags
    """
    stem_type = stem_type.lower()

    if "drum" in stem_type or "percussion" in stem_type:
        return ["--crisp", "6"]  # Max transient preservation
    elif "bass" in stem_type:
        return ["--crisp", "3", "--fine"]  # Precise low-freq handling
    else:
        return ["--crisp", "4"]  # Default for guitar, synth, piano, etc.


def shift_and_stretch_single(
    audio: np.ndarray,
    sr: int,
    semitones: float,
    tempo_ratio: float,
    stem_type: str
) -> np.ndarray:
    """
    Single-pass pitch shift + time stretch using rubberband.

    Args:
        audio: Audio array
        sr: Sample rate
        semitones: Pitch shift amount (positive = up, negative = down)
        tempo_ratio: Tempo ratio (> 1.0 = faster, < 1.0 = slower)
        stem_type: Type of stem for optimization

    Returns:
        Processed audio array
    """
    # No change needed - return copy
    if semitones == 0 and tempo_ratio == 1.0:
        return audio.copy()

    # If only pitch change, use pyrubberband directly
    if tempo_ratio == 1.0:
        return pyrb.pitch_shift(audio, sr, n_steps=semitones)

    # If only tempo change, use pyrubberband directly
    if semitones == 0:
        return pyrb.time_stretch(audio, sr, rate=tempo_ratio)

    # Both changes - use rubberband CLI for single-pass
    return _rubberband_cli(audio, sr, semitones, tempo_ratio, stem_type)


def _rubberband_cli(
    audio: np.ndarray,
    sr: int,
    semitones: float,
    tempo_ratio: float,
    stem_type: str
) -> np.ndarray:
    """
    Use rubberband CLI for combined pitch+tempo processing.
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        input_path = os.path.join(tmpdir, "input.wav")
        output_path = os.path.join(tmpdir, "output.wav")

        # Write input
        sf.write(input_path, audio, sr)

        # Build command
        cmd = ["rubberband"]
        if semitones != 0:
            cmd += ["--pitch", str(semitones)]
        if tempo_ratio != 1.0:
            cmd += ["--tempo", str(tempo_ratio)]
        cmd += get_rubberband_options(stem_type)
        cmd += [input_path, output_path]

        # Run
        try:
            subprocess.run(cmd, check=True, capture_output=True)
        except subprocess.CalledProcessError as e:
            # Fall back to two-pass pyrubberband
            result = pyrb.pitch_shift(audio, sr, n_steps=semitones)
            result = pyrb.time_stretch(result, sr, rate=tempo_ratio)
            return result
        except FileNotFoundError:
            # rubberband CLI not available, use pyrubberband
            result = pyrb.pitch_shift(audio, sr, n_steps=semitones)
            result = pyrb.time_stretch(result, sr, rate=tempo_ratio)
            return result

        # Read output
        result, _ = sf.read(output_path)
        return result.astype(np.float32)


def _process_single_stem(args: tuple) -> tuple[str, np.ndarray]:
    """Worker function for parallel processing."""
    name, audio, sr, semitones, tempo_ratio, stem_type = args
    result = shift_and_stretch_single(audio, sr, semitones, tempo_ratio, stem_type)
    return name, result


def process_single_stem_standalone(
    stem_name: str,
    stem: StemData,
    semitones: float,
    tempo_ratio: float
) -> StemData:
    """
    Process a single stem (for use with async processing).

    Args:
        stem_name: Name of the stem
        stem: StemData object
        semitones: Pitch shift amount
        tempo_ratio: Tempo ratio

    Returns:
        Processed StemData object
    """
    # No change needed
    if semitones == 0 and tempo_ratio == 1.0:
        return StemData(
            name=stem_name,
            audio=stem.audio.copy(),
            sample_rate=stem.sample_rate
        )

    # Determine stem type
    name_lower = stem_name.lower()
    if "drum" in name_lower or "percussion" in name_lower:
        stem_type = "drums"
    elif "bass" in name_lower:
        stem_type = "bass"
    else:
        stem_type = "default"

    # Process
    processed_audio = shift_and_stretch_single(
        stem.audio, stem.sample_rate, semitones, tempo_ratio, stem_type
    )

    return StemData(
        name=stem_name,
        audio=processed_audio,
        sample_rate=stem.sample_rate
    )


def process_all_stems(
    stems: dict[str, StemData],
    semitones: float,
    tempo_ratio: float,
    progress_callback: Optional[Callable[[str, str], None]] = None
) -> dict[str, StemData]:
    """
    Process all stems in parallel.

    Args:
        stems: Dict of StemData objects
        semitones: Pitch shift amount
        tempo_ratio: Tempo ratio (target_bpm / detected_bpm)
        progress_callback: Optional callback(stem_name, status)

    Returns:
        Dict of processed StemData objects
    """
    # No change needed - return copies
    if semitones == 0 and tempo_ratio == 1.0:
        return {
            name: StemData(
                name=name,
                audio=stem.audio.copy(),
                sample_rate=stem.sample_rate
            )
            for name, stem in stems.items()
        }

    # Determine stem types
    def get_stem_type(name: str) -> str:
        name_lower = name.lower()
        if "drum" in name_lower or "percussion" in name_lower:
            return "drums"
        elif "bass" in name_lower:
            return "bass"
        return "default"

    # Prepare arguments for parallel processing
    args_list = [
        (name, stem.audio, stem.sample_rate, semitones, tempo_ratio, get_stem_type(name))
        for name, stem in stems.items()
    ]

    results = {}
    max_workers = min(len(stems), 6)

    with ProcessPoolExecutor(max_workers=max_workers) as executor:
        futures = {
            executor.submit(_process_single_stem, args): args[0]
            for args in args_list
        }

        for future in as_completed(futures):
            name = futures[future]
            if progress_callback:
                progress_callback(name, "processing")

            try:
                stem_name, processed_audio = future.result()
                sr = stems[stem_name].sample_rate
                results[stem_name] = StemData(
                    name=stem_name,
                    audio=processed_audio,
                    sample_rate=sr
                )
                if progress_callback:
                    progress_callback(stem_name, "done")
            except Exception as e:
                # On error, keep original
                results[name] = stems[name]
                if progress_callback:
                    progress_callback(name, f"error: {e}")

    return results


def mix_stems(stems: dict[str, np.ndarray], sample_rate: int = 48000) -> np.ndarray:
    """
    Sum all stem arrays, apply mastering, and return final mix.

    Args:
        stems: Dict mapping stem names to audio arrays
        sample_rate: Sample rate in Hz (default 48000)

    Returns:
        Mastered mixed audio array
    """
    if not stems:
        return np.array([], dtype=np.float32)

    # Determine if any stem is stereo and find max length
    has_stereo = False
    max_length = 0
    for audio in stems.values():
        if audio.ndim == 2:
            has_stereo = True
            max_length = max(max_length, audio.shape[0])
        else:
            max_length = max(max_length, len(audio))

    # Initialize mixed array (stereo if any input is stereo)
    if has_stereo:
        mixed = np.zeros((max_length, 2), dtype=np.float64)
    else:
        mixed = np.zeros(max_length, dtype=np.float64)

    # Sum all stems (pad shorter ones, convert mono to stereo if needed)
    for audio in stems.values():
        # Get length based on array shape
        length = audio.shape[0] if audio.ndim == 2 else len(audio)

        if has_stereo:
            # Convert mono to stereo if needed
            if audio.ndim == 1:
                stereo_audio = np.column_stack([audio, audio])
            else:
                stereo_audio = audio

            if length < max_length:
                mixed[:length] += stereo_audio
            else:
                mixed += stereo_audio
        else:
            # All mono
            if length < max_length:
                mixed[:length] += audio
            else:
                mixed += audio

    # Convert to float32
    mixed = mixed.astype(np.float32)

    # Apply mastering using Pedalboard
    try:
        from pedalboard import Pedalboard, Compressor, Limiter

        board = Pedalboard([
            Compressor(
                threshold_db=-10,
                ratio=3,
                attack_ms=10,
                release_ms=150
            ),
            Limiter(
                threshold_db=-1,
                release_ms=100
            )
        ])

        mastered = board(mixed, sample_rate)
        return mastered

    except ImportError:
        # Fallback to normalize if pedalboard not installed
        return normalize(mixed, peak=0.95)