97 lines
3.0 KiB
Python
Executable File
97 lines
3.0 KiB
Python
Executable File
"""
|
||
Preset mixing: combine separated stems into a single output file.
|
||
|
||
- Instrumental: all non-vocal stems mixed
|
||
- Static: all stems except user-selected ones mixed
|
||
- Dynamic: all stems mixed with per-stem volume multipliers (0.0–2.0)
|
||
"""
|
||
import math
|
||
from pathlib import Path
|
||
from typing import Dict, List
|
||
|
||
from pydub import AudioSegment
|
||
|
||
|
||
def _load(path: str) -> AudioSegment:
|
||
return AudioSegment.from_file(path)
|
||
|
||
|
||
def _apply_gain(audio: AudioSegment, multiplier: float) -> AudioSegment:
|
||
"""Convert a linear multiplier to dB and apply as gain."""
|
||
if multiplier <= 0.0:
|
||
return AudioSegment.silent(duration=len(audio), frame_rate=audio.frame_rate)
|
||
gain_db = 20 * math.log10(multiplier)
|
||
return audio + gain_db
|
||
|
||
|
||
def _export(audio: AudioSegment, dest: Path, output_format: str) -> None:
|
||
if output_format == 'mp3_320':
|
||
audio.export(str(dest), format='mp3', bitrate='320k')
|
||
elif output_format == 'mp3':
|
||
audio.export(str(dest), format='mp3', bitrate='256k')
|
||
elif output_format == 'flac':
|
||
audio.export(str(dest), format='flac')
|
||
else:
|
||
audio.export(str(dest), format='wav')
|
||
|
||
|
||
def _mix(paths: List[str]) -> AudioSegment:
|
||
"""Overlay all audio files into one mix."""
|
||
if not paths:
|
||
raise ValueError("No stems to mix")
|
||
result = _load(paths[0])
|
||
for p in paths[1:]:
|
||
result = result.overlay(_load(p))
|
||
return result
|
||
|
||
|
||
def make_instrumental(
|
||
stem_files: Dict[str, str],
|
||
output_path: Path,
|
||
output_format: str,
|
||
) -> str:
|
||
"""Mix all stems except those with 'vocal' in the stem name."""
|
||
keep = [p for name, p in stem_files.items() if 'vocal' not in name.lower()]
|
||
if not keep:
|
||
raise RuntimeError("No non-vocal stems available for Instrumental preset")
|
||
_export(_mix(keep), output_path, output_format)
|
||
return str(output_path)
|
||
|
||
|
||
def make_static(
|
||
stem_files: Dict[str, str],
|
||
muted_stems: List[str],
|
||
output_path: Path,
|
||
output_format: str,
|
||
) -> str:
|
||
"""Mix all stems except those listed in muted_stems."""
|
||
muted_lower = {s.lower() for s in muted_stems}
|
||
keep = [p for name, p in stem_files.items() if name.lower() not in muted_lower]
|
||
if not keep:
|
||
raise RuntimeError("All stems are muted — nothing to mix for Static preset")
|
||
_export(_mix(keep), output_path, output_format)
|
||
return str(output_path)
|
||
|
||
|
||
def make_dynamic(
|
||
stem_files: Dict[str, str],
|
||
volumes: Dict[str, float],
|
||
output_path: Path,
|
||
output_format: str,
|
||
) -> str:
|
||
"""
|
||
Mix stems with per-stem linear volume multipliers.
|
||
volumes keys are stem names (lowercase); missing stems default to 1.0.
|
||
"""
|
||
adjusted: List[AudioSegment] = []
|
||
for name, path in stem_files.items():
|
||
vol = volumes.get(name.lower(), 1.0)
|
||
adjusted.append(_apply_gain(_load(path), vol))
|
||
if not adjusted:
|
||
raise RuntimeError("No stems available for Dynamic preset")
|
||
result = adjusted[0]
|
||
for a in adjusted[1:]:
|
||
result = result.overlay(a)
|
||
_export(result, output_path, output_format)
|
||
return str(output_path)
|