Download auto_mapper.py from oddsilk/MidiMaker_For_Calorie_Hunters: direct link, hf CLI and curl.
- Browser
- Download file 31.9 kB
-
https://huggingface.co/spaces/oddsilk/MidiMaker_For_Calorie_Hunters/resolve/main/auto_mapper.py
- Command line
-
hf download hf://spaces/oddsilk/MidiMaker_For_Calorie_Hunters/auto_mapper.py
-
curl -L -o auto_mapper.py https://huggingface.co/spaces/oddsilk/MidiMaker_For_Calorie_Hunters/resolve/main/auto_mapper.py
31.9 kB
| """ | |
| ๋ฆฌ๋ฌ ๊ฒ์ ์คํ ๋งคํ ์คํฌ๋ฆฝํธ | |
| ========================= | |
| .wav ์ค๋์ค ํ์ผ์ ๋ถ์ํ์ฌ ์ ํํ ํ์ด๋ฐ์ MIDI ๋ ธํธ๋ฅผ ์๋ ์์ฑํ๋ค. | |
| ํ์ดํ๋ผ์ธ: | |
| 1. Demucs โ 4stems ๋ถ๋ฆฌ (๋๋ผ, ๋ฒ ์ด์ค, ๋ณด์ปฌ, ๊ธฐํ) | |
| 2. librosa โ ๋๋ผ / ๋ฉ๋ก๋(๋ณด์ปฌ+๊ธฐํ ๋ณํฉ) ๊ฐ๊ฐ Onset ๊ฐ์ง | |
| 3. RMS ์๋์ง ๊ธฐ๋ฐ์ผ๋ก ๋๋ผ ํ์ฑ/๋นํ์ฑ ๊ตฌ๊ฐ์ ํ๋ณํ์ฌ Onset์ ์ค๋งํธ ๊ฒฐํฉ | |
| 4. 50ms ์ด๋ด ์ค๋ณต Onset ๋ณํฉ | |
| 5. mido โ ๋จ์ผ ํธ๋ .mid ํ์ผ๋ก ์ ์ฅ | |
| """ | |
| import argparse | |
| import os | |
| import sys | |
| import time | |
| import warnings | |
| from typing import Dict, List, Tuple | |
| import librosa | |
| import mido | |
| import numpy as np | |
| import soundfile as sf | |
| from scipy.signal import butter, find_peaks, lfilter, sosfilt | |
| import shutil | |
| from pathlib import Path | |
| # Demucs ์ฐ์ฐ ์ต์ ํ (TF ๊ฒฝ๊ณ ์ต์ ๋ ์ ์ง) | |
| os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 1. Demucs ์คํ ๋ถ๋ฆฌ ๋ฐ ์บ์ ๊ด๋ฆฌ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def get_dir_size(path: str) -> int: | |
| """๋๋ ํ ๋ฆฌ์ ์ ์ฒด ์ฉ๋์ ๋ฐ์ดํธ ๋จ์๋ก ๋ฐํํ๋ค.""" | |
| total = 0 | |
| for p in Path(path).rglob('*'): | |
| if p.is_file(): | |
| total += p.stat().st_size | |
| return total | |
| def manage_cache(cache_dir: str, max_size_gb: float = 5.0): | |
| """ | |
| ์บ์ ๋๋ ํ ๋ฆฌ ์ฉ๋์ ํ์ธํ๊ณ ์ค์ ์ ์ด๊ณผํ๋ฉด ์ค๋๋ ํด๋๋ถํฐ ์ญ์ ํ๋ค. | |
| """ | |
| if not os.path.exists(cache_dir): | |
| return | |
| current_size = get_dir_size(cache_dir) | |
| max_size_bytes = max_size_gb * 1024 * 1024 * 1024 | |
| if current_size > max_size_bytes: | |
| print(f" โ ์บ์ ์ฉ๋ ์ด๊ณผ ({current_size/1024/1024/1024:.2f}GB > {max_size_gb}GB). ์ค๋๋ ํ์ผ ์ ๋ฆฌ ์ค...") | |
| # ๊ฐ ๊ณก๋ณ ํด๋์ ์์ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ | |
| subdirs = [os.path.join(cache_dir, d) for d in os.listdir(cache_dir)] | |
| subdirs = [d for d in subdirs if os.path.isdir(d)] | |
| subdirs.sort(key=lambda x: os.path.getmtime(x)) | |
| for d in subdirs: | |
| if current_size <= max_size_bytes: | |
| break | |
| dir_size = get_dir_size(d) | |
| shutil.rmtree(d) | |
| current_size -= dir_size | |
| print(f" - ์ญ์ ๋จ: {os.path.basename(d)} ({dir_size/1024/1024:.1f}MB)") | |
| def separate_stems(audio_path: str, output_dir: str) -> str: | |
| """ | |
| Demucs๋ฅผ ์ฌ์ฉํด ์ค๋์ค๋ฅผ 4๊ฐ์ ์คํ ์ผ๋ก ๋ถ๋ฆฌํ๋ค. | |
| ์ด๋ฏธ ๋ถ๋ฆฌ๋ ํ์ผ์ด ์๋ค๋ฉด ๋ถ๋ฆฌ ๊ณผ์ ์ ๊ฑด๋๋ด๋ค. | |
| ๋ฐํ: ์คํ ํ์ผ๋ค์ด ์ ์ฅ๋ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก | |
| """ | |
| basename = os.path.splitext(os.path.basename(audio_path))[0] | |
| # ์บ์ ๋๋ ํ ๋ฆฌ ์ค์ (ํ๋ก์ ํธ ๋ฃจํธ์ stems ํด๋) | |
| stem_parent_dir = os.path.join(os.getcwd(), "stems") | |
| stem_dir = os.path.join(stem_parent_dir, basename) | |
| # ์บ์ ๊ด๋ฆฌ (5GB ์ ํ) | |
| manage_cache(stem_parent_dir, max_size_gb=5.0) | |
| expected_files = ["drums.wav", "bass.wav", "vocals.wav", "other.wav"] | |
| # ์ด๋ฏธ ๋ชจ๋ ์คํ ํ์ผ์ด ์กด์ฌํ๋์ง ํ์ธ (์บ์ฑ) | |
| all_exist = True | |
| if os.path.exists(stem_dir): | |
| for f in expected_files: | |
| if not os.path.exists(os.path.join(stem_dir, f)): | |
| all_exist = False | |
| break | |
| else: | |
| all_exist = False | |
| if all_exist: | |
| print(f"[1/5] Demucs ์คํ ํ์ผ์ด ์ด๋ฏธ ์บ์์ ์กด์ฌํฉ๋๋ค: {stem_dir}") | |
| return stem_dir | |
| print("[1/5] Demucs๋ก ์คํ ๋ถ๋ฆฌ ์ค (์ด ์์ ์ ๋ค์ ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค)...") | |
| # Demucs ์คํ (subprocess ์ฌ์ฉ - ํ๊ฒฝ ์์กด์ฑ ์ต์ํ) | |
| try: | |
| import subprocess | |
| # htdemucs ๋ชจ๋ธ ์ฌ์ฉ (๊ธฐ๋ณธ 4์คํฌ) | |
| # ์ค์: --path ์ธ์๋ ์กด์ฌํ์ง ์์ผ๋ฏ๋ก ์ ๊ฑฐํ๊ณ positional argument๋ก ์ ๋ฌ | |
| cmd = [ | |
| sys.executable, "-m", "demucs.separate", | |
| "-n", "htdemucs", | |
| audio_path, | |
| "-o", stem_parent_dir | |
| ] | |
| print(f" โ ์คํ ๋ช ๋ น: {' '.join(cmd)}") | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode != 0: | |
| raise RuntimeError(f"Demucs ์คํ ์คํจ (Exit Code {result.returncode}):\n{result.stderr}") | |
| # Demucs์ ๊ธฐ๋ณธ ์ถ๋ ฅ ๊ฒฝ๋ก ๊ตฌ์กฐ: {output_dir}/htdemucs/{filename}/{stem}.wav | |
| # ์ด๋ฅผ ์ฐ๋ฆฌ๊ฐ ์ํ๋ {output_dir}/{filename}/{stem}.wav ๋ก ์ด๋/์ ๋ฆฌ | |
| demucs_out_path = os.path.join(stem_parent_dir, "htdemucs", basename) | |
| if not os.path.exists(demucs_out_path): | |
| # ํ์ผ๋ช ์ด ๋ค๋ฅผ ์ ์์ผ๋ฏ๋ก htdemucs ๋ด๋ถ ํด๋ ํ์ธ | |
| htdemucs_dir = os.path.join(stem_parent_dir, "htdemucs") | |
| if os.path.exists(htdemucs_dir): | |
| subdirs = [d for d in os.listdir(htdemucs_dir) if os.path.isdir(os.path.join(htdemucs_dir, d))] | |
| if subdirs: | |
| demucs_out_path = os.path.join(htdemucs_dir, subdirs[0]) | |
| print(f" โ basename ๋ถ์ผ์น ๊ฐ์ง. ํด๋๋ช ์ {subdirs[0]}๋ก ๋์ฒดํฉ๋๋ค.") | |
| if not os.path.exists(stem_dir): | |
| os.makedirs(stem_dir, exist_ok=True) | |
| for f in expected_files: | |
| src = os.path.join(demucs_out_path, f) | |
| dst = os.path.join(stem_dir, f) | |
| if os.path.exists(src): | |
| shutil.move(src, dst) | |
| else: | |
| print(f" โ ๊ฒฝ๊ณ : ์์๋ ์คํ ํ์ผ์ ์ฐพ์ ์ ์์: {src}") | |
| # ์ฌ์ฉ ํ ๋จ์ htdemucs ๋น ํด๋ ์ญ์ | |
| htdemucs_base = os.path.join(stem_parent_dir, "htdemucs") | |
| if os.path.exists(htdemucs_base): | |
| shutil.rmtree(htdemucs_base) | |
| except Exception as e: | |
| print(f" โ Demucs ๋ถ๋ฆฌ ์คํจ: {str(e)}") | |
| # ์๋ฌ๋ฅผ ๋ค์ ๋์ ธ์ ์์(Gradio UI)์์ ์ธ์งํ ์ ์๊ฒ ํจ | |
| raise RuntimeError(f"Demucs ๋ถ๋ฆฌ ๊ณผ์ ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}") | |
| print(f" โ ์คํ ๋ถ๋ฆฌ ๋ฐ ์บ์ฑ ์๋ฃ โ {stem_dir}") | |
| return stem_dir | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def load_audio(path: str, sr: int = 22050) -> tuple: | |
| """์ค๋์ค ํ์ผ์ ๋ชจ๋ ธ๋ก ๋ก๋ํ๋ค.""" | |
| y, sr_out = librosa.load(path, sr=sr, mono=True) | |
| return y, sr_out | |
| def apply_bandpass_filter(y: np.ndarray, lowcut: float, highcut: float, sr: int, order: int = 4) -> np.ndarray: | |
| """์ฃผ์ด์ง ์ฃผํ์ ๋์ญ๋ง ๋จ๊ธฐ๋ ๋ฒํฐ์์ค ํํฐ๋ฅผ ์ ์ฉํ๋ค.""" | |
| nyq = 0.5 * sr | |
| low = lowcut / nyq | |
| high = highcut / nyq | |
| # ๋ฎ์ ์ฃผํ์์์ ์์น ํญ์ฃผ(Instability)๋ฅผ ๋ง๊ธฐ ์ํด SOS(Second-Order Sections) ์ฌ์ฉ | |
| sos = butter(order, [low, high], btype='band', output='sos') | |
| y_filt = sosfilt(sos, y) | |
| # NaN ๋๋ Inf ๋ฐฉ์ด | |
| if not np.isfinite(y_filt).all(): | |
| y_filt = np.nan_to_num(y_filt, nan=0.0, posinf=0.0, neginf=0.0) | |
| return y_filt | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3. ์ ์ฒด ๋๋ผ Onset ์ ๋ฐ ๊ฐ์ง (๊ด๋์ญ์ฉ) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def detect_all_drum_onsets(y: np.ndarray, sr: int, delta_threshold: float = 0.07) -> np.ndarray: | |
| """๊ด๋์ญ ๋๋ผ ์ฌ์ด๋์์ ํ๊ฒฉ์์ ๊ฐ์งํ๋ค.""" | |
| print(f" โ ๋๋ผ ์คํ ์ ๊ด๋์ญ ์ ์ฒด Onset ๊ฐ์ง ์ค (๋ฏผ๊ฐ๋: {delta_threshold})...") | |
| onset_frames = librosa.onset.onset_detect( | |
| y=y, sr=sr, units="frames", hop_length=512, backtrack=True, | |
| pre_max=1, post_max=1, pre_avg=3, post_avg=5, delta=delta_threshold, wait=5 | |
| ) | |
| onset_times = librosa.frames_to_time(onset_frames, sr=sr, hop_length=512) | |
| return onset_times | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3.5. Kick ์ ์์ญ๋ ์ ์ฉ ํผํฌ ๊ฐ์ง | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def detect_kick_energy_peaks(y_kick: np.ndarray, sr: int, height_thresh: float = 0.15) -> np.ndarray: | |
| """ | |
| ์ ์ฃผํ ํํฐ๋ง๋ ์ ํธ๋ librosa.onset_detect๋ก ์ก๊ธฐ ์ด๋ ค์ฐ๋ฏ๋ก, | |
| RMS ์๋์ง๋ฅผ ๊ณ์ฐํ์ฌ ๊ทธ ํํ์ ์ต๊ณ ์ (Peak)์ Onset์ผ๋ก ์ ๋ฐํ๊ฒ ์ถ์ถํ๋ค. | |
| """ | |
| print(f" โ Kick ํํ ์๋์ง ํผํฌ(RMS) ๊ฐ์ง ์ค (์๊ณ์ : {height_thresh})...") | |
| # ์๋์ง ๊ณ์ฐ | |
| hop_length = 512 | |
| rms = librosa.feature.rms(y=y_kick, frame_length=1024, hop_length=hop_length)[0] | |
| rms_times = librosa.frames_to_time(np.arange(len(rms)), sr=sr, hop_length=hop_length) | |
| # ์ ๊ทํ | |
| rms_norm = rms / (np.max(rms) + 1e-8) | |
| # ํผํฌ ๊ฒ์ถ (height_thresh ์ด์, ์ต์ ๊ฐ๊ฒฉ 50ms) | |
| peaks, _ = find_peaks( | |
| rms_norm, | |
| height=height_thresh, | |
| distance=4, # ์ฝ 92ms ๊ฐ๊ฒฉ ์ ์ง | |
| prominence=0.05 | |
| ) | |
| onset_times = rms_times[peaks] | |
| print(f" ๊ฐ์ง๋ Kick Onset: {len(onset_times)}๊ฐ") | |
| return onset_times | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3.6. ๋ณด์ปฌ(๋ฉ) Onset ๊ฐ์ง | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def detect_vocal_onsets(y: np.ndarray, sr: int, delta_threshold: float = 0.05) -> np.ndarray: | |
| """ | |
| ๋น ๋ฅธ ๋ฉ์ด๋ ๋ณด์ปฌ์ ํธ๋์ง์ธํธ๋ฅผ ๊ฐ์งํ๋ค. | |
| ๋ณด์ปฌ์ ํ๊ฒฉ๊ฐ์ด ๋ํ๋ฏ๋ก ๋ฏผ๊ฐ๋(delta)๋ฅผ ๋ฎ์ถ๊ณ ์ดํ ํ์์ ์ ์ฐํ๊ฒ ์ก๋๋ค. | |
| """ | |
| print(f" โ ๋ณด์ปฌ ์คํ Onset ๊ฐ์ง ์ค (๋ฏผ๊ฐ๋: {delta_threshold})...") | |
| # ๋ฉ์ ํน์ฑ์ ํผํฌ๊ฐ ์ผ์ ํ์ง ์์ ์ ๊ทํ(Normalization)ํ์ฌ ์ผ๊ด๋ ๊ฐ์ง ์๋ | |
| if np.max(np.abs(y)) > 0: | |
| y = y / np.max(np.abs(y)) | |
| onset_frames = librosa.onset.onset_detect( | |
| y=y, sr=sr, units="frames", hop_length=512, backtrack=True, | |
| pre_max=1, post_max=1, pre_avg=3, post_avg=3, delta=delta_threshold, wait=3 | |
| ) | |
| onset_times = librosa.frames_to_time(onset_frames, sr=sr, hop_length=512) | |
| print(f" ๊ฐ์ง๋ ๋ณด์ปฌ Onset: {len(onset_times)}๊ฐ") | |
| return onset_times | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3.8. Kick/Snare ์ฃผํ์ ์๋์ง ๊ต์ฐจ ๊ฒ์ฆ (Cross-Validation) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def detect_kick_snare_cross_validation(y_drums: np.ndarray, sr: int) -> List[Tuple[float, int]]: | |
| """ | |
| Kick ๋์ญ(20-100Hz)๊ณผ Snare ๋์ญ(1000-4000Hz) ํํฐ๋ง ํ | |
| ์๋์ง(RMS)๋ฅผ ๋น๊ตํ์ฌ ํฅ/์ค๋ค์ด๋ฅผ ํ๋ณํ๋ค. | |
| """ | |
| print(f" โ ์ฃผํ์ ๋์ญ๋ณ ์๋์ง ๋น๊ต ๊ธฐ๋ฐ ๋๋ผ ๋ถ๋ฆฌ ๋ถ์ ์์...") | |
| hop_length = 128 | |
| # 1. Onset ํ๋ณด ๊ฐ์ง (๋ฏผ๊ฐ๋ ์ํ: 0.1) | |
| # [์์ ] backtrack=False ๋ก ๋ณ๊ฒฝํ์ฌ ํ๊ฒฉ์์ ๋งจ ์์ด ์๋ '๊ฐ์ฅ ์๋์ง๊ฐ ๊ฐํ ํผํฌ(์ ์ )'๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ์ ํํ์ด์ฆ ์งํฐ(Jitter)๋ฅผ ๋ฐฉ์งํฉ๋๋ค. | |
| # delta=0.1 ๋ก ์กฐ์ ํ์ฌ ํ๋ณด ๋ ธํธ๋ฅผ ๋ ๋ง์ด ์ฐพ์๋ด๋, ์๋์ Peak ํํฐ๋ก ์ ๋ฆฌํฉ๋๋ค. | |
| onset_frames = librosa.onset.onset_detect( | |
| y=y_drums, sr=sr, units="frames", hop_length=hop_length, backtrack=False, | |
| delta=0.1, wait=5, pre_max=3, post_max=3 | |
| ) | |
| onset_times = librosa.frames_to_time(onset_frames, sr=sr, hop_length=hop_length) | |
| # 2. ๋์ญ๋ณ ํํฐ๋ง (์ฌ์ฉ์ ์์ฒญ: 100Hz ์ดํ vs 1~4kHz) | |
| y_kick = apply_bandpass_filter(y_drums, 20.0, 100.0, sr) | |
| y_snare = apply_bandpass_filter(y_drums, 1000.0, 4000.0, sr) | |
| # 3. RMS ์๋์ง ์ถ์ถ | |
| rms_kick = librosa.feature.rms(y=y_kick, frame_length=512, hop_length=hop_length)[0] | |
| rms_snare = librosa.feature.rms(y=y_snare, frame_length=512, hop_length=hop_length)[0] | |
| rms_times = librosa.frames_to_time(np.arange(len(rms_kick)), sr=sr, hop_length=hop_length) | |
| final_notes: List[Tuple[float, int]] = [] | |
| # 4. ๊ฐ Onset ์์ ์ ์๋์ง ๋ฐ ์ ๋ ํผํฌ ๋น๊ต | |
| for t in onset_times: | |
| # [NEW] ํ๊ฒฉ ์๊ฐ์ ์ค์ ์ค๋์ค ์ํ ์ ๋๊ฐ(ํผํฌ) ํ์ธ (Transient Peak ํํฐ๋ง) | |
| # ํ๊ฒฉ ์ง์ ์ฃผ๋ณ ์ฝ 10ms ๋ด์์ ๊ฐ์ฅ ํฐ ํผํฌ๊ฐ ์ถ์ถ | |
| sample_idx = int(t * sr) | |
| win_size = int(sr * 0.01) | |
| if sample_idx + win_size < len(y_drums): | |
| peak_val = np.max(np.abs(y_drums[sample_idx : sample_idx + win_size])) | |
| else: | |
| peak_val = np.abs(y_drums[sample_idx]) | |
| # [์ฌ์ฉ์ ์์ฒญ] ์ ๋ ํผํฌ ์๊ณ๊ฐ(min_peak=0.5) ํํฐ๋ง | |
| # ์๋์ง๋ ํฌ์ง๋ง ํ๊ฒฉ ์๊ฐ์ ์ดํ(Transient)์ด ์ฝํ ๋ ธํธ๋ฅผ ์ ๊ฑฐํฉ๋๋ค. | |
| if peak_val < 0.5: | |
| continue | |
| idx = np.searchsorted(rms_times, t) | |
| if idx >= len(rms_kick): idx = len(rms_kick) - 1 | |
| # Onset ์งํ(์ฝ 30ms) ๊ตฌ๊ฐ์ ์ต๋ ์๋์ง ํ์ธ | |
| win = 5 | |
| e_k = np.max(rms_kick[idx : idx + win]) if idx + win < len(rms_kick) else rms_kick[idx] | |
| e_s = np.max(rms_snare[idx : idx + win]) if idx + win < len(rms_snare) else rms_snare[idx] | |
| # [์ฌ์ฉ์ ์ค์ ๊ตฌ๊ฐ] Kick๊ณผ Snare ์๋์ง ๋ถ๋ฆฌ ๊ธฐ์ค | |
| snare_weight = 1.0 | |
| if e_k > (e_s * snare_weight): | |
| pitch = 60 # C3 (Kick) | |
| else: | |
| pitch = 61 # C#3 (Snare) | |
| final_notes.append((float(t), int(pitch))) | |
| print(f" ๋๋ผ Onset ๊ฐ์ง ๋ถ๋ฅ ์๋ฃ: {len(final_notes)}๊ฐ ๋ ธํธ๋ฅผ ์ฐพ์์ต๋๋ค.") | |
| return final_notes | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3.7. ๊ต์ฐจ ์คํ ํฌํ ๋ถ์ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def vote_intersection_onsets(all_stem_onsets: dict, tolerance_ms: float = 50, min_votes: int = 2) -> np.ndarray: | |
| """ | |
| ์ฌ๋ฌ ์คํ ์ Onset ์ค ๊ต์ฐจ(ํฉ์)๋๋ ์ง์ ๋ง ์ถ์ถํ๋ค. | |
| all_stem_onsets: {stem_name: np.ndarray(onset_times)} | |
| """ | |
| print(f" โ ๊ต์ฐจ ์คํ ํฌํ ๋ถ์ ์ค (ํ์ฉ์ค์ฐจ: {tolerance_ms}ms, ์ต์๋ํ: {min_votes})...") | |
| tolerance = tolerance_ms / 1000.0 | |
| # ๋ชจ๋ Onset์ ํ๋์ ๋ฆฌ์คํธ๋ก ํตํฉํ์ฌ ์๊ฐ์ ์ ๋ ฌ | |
| master_list = [] | |
| for stem, onsets in all_stem_onsets.items(): | |
| for t in onsets: | |
| master_list.append({"time": t, "stem": stem}) | |
| master_list.sort(key=lambda x: x["time"]) | |
| final_onsets = [] | |
| used_indices = set() | |
| for i in range(len(master_list)): | |
| if i in used_indices: | |
| continue | |
| current = master_list[i] | |
| cluster = [current] | |
| cluster_stems = {current["stem"]} | |
| for j in range(i + 1, len(master_list)): | |
| candidate = master_list[j] | |
| # ์๊ฐ ์ฐจ์ด๊ฐ tolerance ์ด๋ด์ธ์ง ํ์ธ | |
| if candidate["time"] - current["time"] <= tolerance: | |
| # ๊ฐ์ ์คํ ์์ ์จ ์ค๋ณต Onset์ ์นด์ดํธ์์ ์ ์ธ (์ด๋ฏธ ํ๋ ๋ฝ์์ผ๋ฏ๋ก) | |
| if candidate["stem"] not in cluster_stems: | |
| cluster.append(candidate) | |
| cluster_stems.add(candidate["stem"]) | |
| used_indices.add(j) | |
| else: | |
| break | |
| if len(cluster_stems) >= min_votes: | |
| # ํฉ์๋ ์ง์ : ํด๋ฌ์คํฐ ํ๊ท ๊ฐ์ผ๋ก ๊ฒฐ์ | |
| avg_time = sum(c["time"] for c in cluster) / len(cluster) | |
| final_onsets.append(avg_time) | |
| print(f" ํฌํ ๊ฒฐ๊ณผ: {len(final_onsets)}๊ฐ ์ง์ ํฉ์ ์๋ฃ") | |
| return np.array(final_onsets) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 4. ํํฐ๋ง ์ ๊ฑฐ (๋ชจ๋ ๊ฐ์ง๋ Onset ์ ์ง) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def pass_all_onsets(onsets: List[Tuple[float, int]]) -> List[Tuple[float, int]]: | |
| """๋ณ๋์ ํํฐ๋ง ์์ด ๊ฐ์ง๋ ๋ชจ๋ Onset์ ๋ฐํํ๋ค.""" | |
| print("[3/5] ํํฐ๋ง ๊ฑด๋๋ฐ๊ธฐ (๋ชจ๋ Onset ์ ์ง)...") | |
| return onsets | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 6. 50ms ์ค๋ณต ๋ณํฉ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def merge_close_onsets( | |
| onsets: List[Tuple[float, int]], min_gap: float = 0.065 # 150BPM 16๋ถ์ํ(0.1s) ์์ ๊ฐ์ง๋ฅผ ์ํด 0.065s๋ก ์กฐ์ | |
| ) -> List[Tuple[float, int]]: | |
| """ | |
| min_gap ์ด๋ด๋ก ์ธ์ ํ Onset์ ํ๋๋ก ๋ณํฉํ์ฌ ์ํฅ/๊ผฌ๋ฆฌ ์ค๋ณต์ ๋ฐฉ์งํ๋ค. | |
| onsets: [(time, pitch), ...] | |
| """ | |
| if len(onsets) == 0: | |
| return onsets | |
| # ์๊ฐ์ ์ ๋ ฌ | |
| sorted_onsets: List[Tuple[float, int]] = sorted(onsets, key=lambda x: x[0]) | |
| if not sorted_onsets: | |
| return [] | |
| # ์ฌ๋ผ์ด์ฑ([1:]) ๋์ ๋ฐ๋ณต์(iterator)๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์ฒด์ปค ์ค๋ฅ ๋ฐฉ์ง | |
| it = iter(sorted_onsets) | |
| merged = [next(it)] | |
| for current in it: | |
| curr_time, curr_pitch = current | |
| last_time, last_pitch = merged[-1] | |
| # ์๊ฐ ๊ฐ๊ฒฉ์ด min_gap๋ณด๋ค ํฌ๋ฉด ์ ์ง | |
| if curr_time - last_time >= min_gap: | |
| merged.append(current) | |
| else: | |
| # ๋๋ฌด ๊ฐ๊น์ฐ๋ฉด: ๋ง์ฝ ํผ์น๊ฐ ๋ค๋ฅด๋ฉด(Kick+Snare ๋์ํ) ์ ์งํ ์๋ ์์ง๋ง, | |
| # ํ์ฌ๋ ๊ผฌ๋ฆฌ ๋ฐฉ์ง๊ฐ ๋ชฉ์ ์ด๋ฏ๋ก ๊ฐ์ ์์ ์ฐํ๋ ๋ฌด์ํจ. | |
| pass | |
| removed = len(onsets) - len(merged) | |
| print(f"[4/5] ๊ผฌ๋ฆฌ/์ค๋ณต ๋ณํฉ ({min_gap*1000:.0f}ms ๊ธฐ์ค): {removed}๊ฐ ์ ๊ฑฐ โ ์ต์ข {len(merged)}๊ฐ") | |
| return merged | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 7. ํํ์ด์ฆ (์ ํ์ ) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def quantize_onsets( | |
| onsets: List[Tuple[float, int]], | |
| bpm: float, | |
| strength: str = "1/16", | |
| offset: float = 0.0, | |
| ) -> List[Tuple[float, int]]: | |
| """ | |
| BPM๊ณผ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ์ ๋ง์ถฐ Onset์ ์๊ฐ์ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ทธ๋ฆฌ๋์ ์ค๋ ํ๋ค. | |
| offset์ ์ฃผ์ด ์ฒซ ๋ฐ์์ ๋๋ ์ด๋ฅผ ๋ณด์ํ ์ ์๋ค. | |
| onsets: [(time, pitch), ...] | |
| """ | |
| fraction_map = {"1/4": 1.0, "1/8": 0.5, "1/16": 0.25, "1/32": 0.125} | |
| if strength not in fraction_map: | |
| strength = "1/16" | |
| beat_duration = 60.0 / bpm | |
| grid_size = beat_duration * fraction_map[strength] | |
| quantized = [] | |
| seen = set() | |
| for t, p in onsets: | |
| # ์คํ์ ์์ ๊ธฐ์ค์ผ๋ก ํํ์ด์ฆ ํ ๋ค์ ์๋ ๊ณต๊ฐ์ผ๋ก ๋ณต์ | |
| qt = np.round((t - offset) / grid_size) * grid_size + offset | |
| # ์์ ์๊ฐ ๋ฐฉ์ง | |
| qt = max(0.0, qt) | |
| # ๊ฐ์ ์๊ฐ์ ๊ฐ์ ํผ์น๊ฐ ์ค๋ณต๋์ง ์๋๋ก ํจ | |
| if (qt, p) not in seen: | |
| quantized.append((qt, p)) | |
| seen.add((qt, p)) | |
| # ์๊ฐ์ ์ฌ์ ๋ ฌ | |
| quantized.sort(key=lambda x: float(x[0])) | |
| print(f" โ ํํ์ด์ฆ ์ ์ฉ: BPM={bpm}, 1/{int(1/fraction_map[strength])}, Offset={offset}s") | |
| return quantized | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 8. MIDI ์์ฑ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def create_midi( | |
| onsets: List[Tuple[float, int]], | |
| output_path: str, | |
| bpm: float = 120.0, | |
| velocity: int = 100, | |
| note_duration: float = 0.05, | |
| ) -> None: | |
| """ | |
| [(time, pitch), ...] ๋ฐฐ์ด์ ๊ธฐ๋ฐ์ผ๋ก ๋ฉํฐ ๋ ธํธ MIDI ํ์ผ์ ์์ฑํ๋ค. | |
| """ | |
| print("[5/5] MIDI ํ์ผ ์์ฑ ์ค...") | |
| mid = mido.MidiFile(type=0) | |
| track = mido.MidiTrack() | |
| mid.tracks.append(track) | |
| tempo = mido.bpm2tempo(bpm) | |
| track.append(mido.MetaMessage("set_tempo", tempo=tempo, time=0)) | |
| ticks_per_beat = mid.ticks_per_beat | |
| def seconds_to_ticks(sec: float) -> int: | |
| return int(mido.second2tick(sec, ticks_per_beat, tempo)) | |
| note_dur_ticks = seconds_to_ticks(note_duration) | |
| # MIDI ๋ฉ์์ง๋ค์ ๋ด์ ๋ฆฌ์คํธ (์ ๋ ํฑ ๊ธฐ์ค) | |
| events = [] | |
| for t_sec, pitch in onsets: | |
| abs_tick = seconds_to_ticks(t_sec) | |
| events.append({'tick': abs_tick, 'type': 'note_on', 'note': pitch, 'vel': velocity}) | |
| events.append({'tick': abs_tick + note_dur_ticks, 'type': 'note_off', 'note': pitch, 'vel': 0}) | |
| # ํฑ ์์๋๋ก ์ ๋ ฌ | |
| events.sort(key=lambda x: (x['tick'], 0 if x['type'] == 'note_off' else 1)) | |
| prev_tick = 0 | |
| for e in events: | |
| delta = max(0, e['tick'] - prev_tick) | |
| track.append(mido.Message(e['type'], note=e['note'], velocity=e['vel'], time=delta)) | |
| prev_tick = e['tick'] | |
| mid.save(output_path) | |
| # ๋ง์ง๋ง ์จ์ ์ ์๊ฐ์ผ๋ก ๊ณก ๊ธธ์ด ๊ณ์ฐ | |
| duration = 0 | |
| if len(onsets) > 0: | |
| last_item = onsets[-1] | |
| duration = last_item[0] | |
| print(f" โ MIDI ์ ์ฅ ์๋ฃ โ {output_path}") | |
| print(f" ์ด ๋ ธํธ ์: {len(onsets)}๊ฐ | ๊ณก ๊ธธ์ด: {duration:.2f}์ด") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 9. ์ฑํฌ ๊ฒ์ฆ ๋ก์ง (Verification) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def verify_sync( | |
| target_onsets: List[Tuple[float, int]], | |
| output_path: str, | |
| bpm: float | |
| ) -> None: | |
| """ | |
| ์์ฑ๋ MIDI ๋ ธํธ๋ฅผ ์๊ฐ(์ด)์ผ๋ก ๋ค์ ๋ก๋ํ์ฌ ์ฑํฌ ๊ฒ์ฆ. | |
| """ | |
| print("[6/6] ์์ฑ ์๊ฐ๊ณผ ๋ชฉํ ๊ทธ๋ฆฌ๋ ๊ฐ์ ์ฑํฌ ๊ฒ์ฆ ์ค...") | |
| mid = mido.MidiFile(output_path) | |
| tempo = mido.bpm2tempo(bpm) | |
| ticks_per_beat = mid.ticks_per_beat | |
| midi_onsets = [] | |
| for track in mid.tracks: | |
| track_time_sec = 0.0 | |
| for msg in track: | |
| track_time_sec += mido.tick2second(msg.time, ticks_per_beat, tempo) | |
| if msg.type == 'note_on' and msg.velocity > 0: | |
| midi_onsets.append((track_time_sec, msg.note)) | |
| # ์๊ฐ์ ์ ๋ ฌ | |
| midi_onsets.sort(key=lambda x: x[0]) | |
| target_times = [x[0] for x in target_onsets] | |
| midi_times = [x[0] for x in midi_onsets] | |
| if len(target_times) != len(midi_times): | |
| print(f" โ [๊ฒฝ๊ณ ] ๋ชฉํ ๊ฐ์({len(target_times)})์ ๋ฏธ๋ ๊ฐ์({len(midi_times)}) ์ฐจ์ด ๋ฐ์.") | |
| diffs = [abs(t - m) * 1000.0 for t, m in zip(target_times, midi_times)] | |
| if diffs: | |
| max_diff = max(diffs) | |
| avg_diff = sum(diffs) / len(diffs) | |
| print(f" โ ์ต๋ ์ฑํฌ ์ค์ฐจ: {max_diff:.3f} ms") | |
| print(f" โ ํ๊ท ์ฑํฌ ์ค์ฐจ: {avg_diff:.3f} ms") | |
| if max_diff < 2.0: | |
| print(" โ [๊ฒ์ฆ ์ฑ๊ณต] ์๋ฒฝํ ์ฑํฌ ์ ์ง.") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 10. ๋ฉ์ธ ํ์ดํ๋ผ์ธ | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def run_pipeline( | |
| audio_path: str, | |
| output_path: str, | |
| bpm: float, | |
| threshold_multiplier: float = 0.5, | |
| quantize_strength: str = "1/16", | |
| sr: int = 22050, | |
| target_stem: str = "drums", | |
| mode: str = "single", | |
| min_votes: int = 2, | |
| tolerance_ms: float = 50, | |
| offset: float = 0.0, | |
| shift: float = 0.0, | |
| ) -> List[Tuple[float, int]]: | |
| """์ ์ฒด ์คํ ๋งคํ ํ์ดํ๋ผ์ธ์ ์คํํ๋ค.""" | |
| print("=" * 60) | |
| print(" ๋ฆฌ๋ฌ ๊ฒ์ ์คํ ๋งคํ ์คํฌ๋ฆฝํธ (BPM ๊ธฐ๋ฐ ํํ์ด์ฆ ํ์)") | |
| print("=" * 60) | |
| print(f" ์ ๋ ฅ: {audio_path}") | |
| print(f" ์ถ๋ ฅ: {output_path}") | |
| print(f" ๋ชฉํ BPM: {bpm} (๊ฐ์ ํํ์ด์ฆ: {quantize_strength}) | Offset: {offset}s") | |
| print(f" ์๋ ๋ชจ๋: {mode} (ํ๊ฒ: {target_stem if mode == 'single' else 'ALL stems'})") | |
| print("=" * 60) | |
| # 1) ์คํ ๋ถ๋ฆฌ | |
| stem_output_dir = os.path.join(os.path.dirname(output_path) or ".", "stems_output") | |
| stem_dir = separate_stems(audio_path, stem_output_dir) | |
| if mode == "vote": | |
| # (ํฌํ ๋ชจ๋๋ ํ์ฌ ๋จ์ผ ํผ์น 60์ผ๋ก ์ ์งํ๊ฑฐ๋ ์ถํ ๋ฆฌํฉํ ๋ง ํ์) | |
| all_stem_onsets = {} | |
| # ... (์๋ต๋ ๊ธฐ์กด ํฌํ ๋ก์ง, ๋จ ๊ฒฐ๊ณผ๋ [(t, 60), ...]๋ก ๋ณํ ํ์) | |
| # ์ผ๋จ์ ๊ธฐ์กด๊ณผ ๋์ผํ๊ฒ ๋์ํ๊ฒ ํ๋ ํํ๋ก ๋ณํํจ | |
| # (์ค์ ์ฝ๋๋ ์๋์ ๊ฐ์ด ๊ตฌํ) | |
| all_stem_onsets_raw = {} | |
| for s_name in ["drums", "vocals", "bass", "other"]: | |
| p = os.path.join(stem_dir, f"{s_name}.wav") | |
| y_s, _ = load_audio(p, sr=sr) | |
| if s_name == "drums": | |
| all_stem_onsets_raw[s_name] = detect_all_drum_onsets(y_s, sr, 0.07) | |
| elif s_name == "vocals": | |
| all_stem_onsets_raw[s_name] = detect_vocal_onsets(y_s, sr, 0.08) | |
| else: | |
| all_stem_onsets_raw[s_name] = detect_all_drum_onsets(y_s, sr, 0.07) | |
| voted_times = vote_intersection_onsets(all_stem_onsets_raw, tolerance_ms, min_votes) | |
| target_onsets = [(t, 60) for t in voted_times] | |
| else: | |
| # ๊ธฐ์กด: ๋จ์ผ ์คํ ๋ถ์ | |
| target_path = os.path.join(stem_dir, f"{target_stem}.wav") | |
| print(f"[2/5] {target_stem} ์ค๋์ค ํธ๋ ๋ก๋ฉ ๋ฐ ๋ถ์ ์ค...") | |
| y_target, _ = load_audio(target_path, sr=sr) | |
| if target_stem == "drums": | |
| # [ํต์ฌ] Kick/Snare ๊ต์ฐจ ๊ฒ์ฆ ๋ก์ง ์ฌ์ฉ | |
| target_onsets = detect_kick_snare_cross_validation(y_target, sr) | |
| else: | |
| # ๋ณด์ปฌ ๋ฑ ๊ธฐํ ์คํ์ ๊ธฐ๋ณธ ํผ์น 60 ์ฌ์ฉ | |
| if target_stem == "vocals": | |
| times = detect_vocal_onsets(y_target, sr, 0.08) | |
| else: | |
| times = detect_all_drum_onsets(y_target, sr, 0.07) | |
| target_onsets = [(t, 60) for t in times] | |
| # 4) ํํฐ๋ง ๊ฑด๋๋ฐ๊ณ ๋ชจ๋ Onset ๋ณํฉ ๋จ๊ณ๋ก ๋๊น | |
| combined = pass_all_onsets(target_onsets) | |
| # 6) ๊ผฌ๋ฆฌ ์ค๋ณต ๋ฐฉ์ง (16๋ถ์ํ ์ฐํ๋ฅผ ์ํด ๋ ์ ๊ตํ ๊ฐ๊ฒฉ ์ ์ฉ) | |
| final_onsets = merge_close_onsets(combined, min_gap=0.065) | |
| if shift != 0.0: | |
| final_onsets = [(max(0.0, t + shift), p) for t, p in final_onsets] | |
| # 7) ํํ์ด์ฆ ๊ฐ์ ์ ์ฉ (์ฌ์ฉ์ ์์ฒญ: 1/8 ๋ฑ์์ ๋์ strength๋ก ๋ณต๊ตฌ) | |
| quantized_onsets = quantize_onsets(final_onsets, bpm=bpm, strength=quantize_strength, offset=offset) | |
| # 8) MIDI ์์ฑ | |
| if len(quantized_onsets) == 0: | |
| print(" โ ๊ฐ์ง๋ Onset์ด ์์ต๋๋ค. MIDI ํ์ผ์ ์์ฑํ์ง ์์ต๋๋ค.") | |
| return [] | |
| create_midi(quantized_onsets, output_path, bpm=bpm) | |
| # 9) ์์ฑ๋ MIDI ์ค์ฐจ ๊ฒ์ฆ(Verification) | |
| verify_sync(quantized_onsets, output_path, bpm=bpm) | |
| print("=" * 60) | |
| print(" โ ์คํ ๋งคํ ๋ฐ ๊ฒ์ฆ ์๋ฃ!") | |
| print("=" * 60) | |
| return quantized_onsets | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # CLI ์ธํฐํ์ด์ค | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="๋ฆฌ๋ฌ ๊ฒ์ ์คํ ๋งคํ: .wav โ .mid ๋ณํ ์คํฌ๋ฆฝํธ", | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| ์ฌ์ฉ ์์: | |
| python auto_mapper.py song.wav --bpm 150 -o output.mid | |
| python auto_mapper.py song.wav --bpm 150 -o output.mid --threshold-multiplier 0.3 --quantize-strength 1/8 | |
| """, | |
| ) | |
| parser.add_argument("input", help="์ ๋ ฅ ์ค๋์ค ํ์ผ ๊ฒฝ๋ก (.wav)") | |
| parser.add_argument( | |
| "-o", "--output", default="output.mid", help="์ถ๋ ฅ MIDI ํ์ผ ๊ฒฝ๋ก (๊ธฐ๋ณธ: output.mid)" | |
| ) | |
| parser.add_argument( | |
| "--bpm", | |
| type=float, | |
| required=True, | |
| help="[ํ์] ๊ณก์ ์๊ฒฉํ BPM (๋ฆฌ๋ฌ ๊ฒ์ ๊ฒฉ์๋ฅผ ์์ฑํ๊ธฐ ์ํด ๋ฐ๋์ ํ์ํฉ๋๋ค, ์: 150)", | |
| ) | |
| parser.add_argument( | |
| "--threshold-multiplier", | |
| type=float, | |
| default=0.5, | |
| help="๋๋ผ ์๋์ง ์๊ณ์น ๋ฐฐ์จ (๊ธฐ๋ณธ: 0.5, ๋ฎ์์๋ก ๋๋ผ Onset ๋ง์ด ์ฌ์ฉ)", | |
| ) | |
| parser.add_argument( | |
| "--quantize-strength", | |
| default="1/16", | |
| choices=["1/4", "1/8", "1/16", "1/32"], | |
| help="ํํ์ด์ฆ ๊ทธ๋ฆฌ๋ ๋จ์ (๊ธฐ๋ณธ: 1/16)", | |
| ) | |
| parser.add_argument( | |
| "--target-stem", | |
| default="drums", | |
| choices=["drums", "vocals", "bass", "other"], | |
| help="๋จ์ผ ์คํ ๋ถ์ ์ ๋์์ ์ง์ ํฉ๋๋ค (๊ธฐ๋ณธ: drums)", | |
| ) | |
| parser.add_argument( | |
| "--mode", | |
| default="single", | |
| choices=["single", "vote"], | |
| help="์๋ ๋ชจ๋: single(๋จ์ผ ์คํ ์ํ), vote(์ ์ฒด ์คํ ๊ต์ฐจ ํฌํ)", | |
| ) | |
| parser.add_argument( | |
| "--offset", | |
| type=float, | |
| default=0.0, | |
| help="๊ณก์ ์ฒซ ๋ฐ์ ์คํ์ (์ด ๋จ์). ์: 0.045", | |
| ) | |
| parser.add_argument( | |
| "--min-votes", | |
| type=int, | |
| default=2, | |
| help="ํฌํ ๋ชจ๋์์ Onset์ ์ธ์ ๋ฐ๊ธฐ ์ํ ์ต์ ํฉ์ ์คํ ์ (๊ธฐ๋ณธ: 2)", | |
| ) | |
| parser.add_argument( | |
| "--tolerance-ms", | |
| type=float, | |
| default=50.0, | |
| help="ํฌํ ๋ชจ๋์์ ๋ Onset์ ๊ฐ์ ์ง์ ์ผ๋ก ์ธ์ ํ ์๊ฐ ์ค์ฐจ(ms) (๊ธฐ๋ณธ: 50.0)", | |
| ) | |
| parser.add_argument( | |
| "--sr", | |
| type=int, | |
| default=22050, | |
| help="์ค๋์ค ์ํ๋ ์ดํธ (๊ธฐ๋ณธ: 22050)", | |
| ) | |
| parser.add_argument( | |
| "--shift", | |
| type=float, | |
| default=0.0, | |
| help="์ ์ฒด ๋ ธํธ ์๊ฐ์ ํต์งธ๋ก ์/๋ค๋ก ๊ฐ์ ์ด๋ํฉ๋๋ค. (์ด ๋จ์). ์: -0.025 (25ms ์๋น๊น)", | |
| ) | |
| args = parser.parse_args() | |
| if not os.path.exists(args.input): | |
| print(f"์ค๋ฅ: ์ ๋ ฅ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค โ {args.input}") | |
| sys.exit(1) | |
| run_pipeline( | |
| audio_path=args.input, | |
| output_path=args.output, | |
| bpm=args.bpm, | |
| threshold_multiplier=args.threshold_multiplier, | |
| quantize_strength=args.quantize_strength, | |
| sr=args.sr, | |
| target_stem=args.target_stem, | |
| mode=args.mode, | |
| min_votes=args.min_votes, | |
| tolerance_ms=args.tolerance_ms, | |
| offset=args.offset, | |
| shift=args.shift, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |