oddsilk commited on
Commit
996b71e
Β·
1 Parent(s): 1d567bd

change: spleeter to demucs

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. auto_mapper.py +93 -21
  3. requirements.txt +5 -2
  4. web_ui.py +1 -1
README.md CHANGED
@@ -4,9 +4,9 @@ emoji: πŸ”₯
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 6.9.0
8
- app_file: web_ui.py
9
  python_version: "3.10"
 
10
  pinned: false
11
  short_description: μΉΌλ‘œλ¦¬ν—Œν„°μŠ€μ— μ‚¬μš©ν•  midiνŒŒμΌμ„ μ œμž‘ν•˜λŠ” ν™ˆνŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
12
  ---
 
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.50.2
 
8
  python_version: "3.10"
9
+ app_file: web_ui.py
10
  pinned: false
11
  short_description: μΉΌλ‘œλ¦¬ν—Œν„°μŠ€μ— μ‚¬μš©ν•  midiνŒŒμΌμ„ μ œμž‘ν•˜λŠ” ν™ˆνŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
12
  ---
auto_mapper.py CHANGED
@@ -4,7 +4,7 @@
4
  .wav μ˜€λ””μ˜€ νŒŒμΌμ„ λΆ„μ„ν•˜μ—¬ μ •ν™•ν•œ 타이밍에 MIDI λ…ΈνŠΈλ₯Ό μžλ™ μƒμ„±ν•œλ‹€.
5
 
6
  νŒŒμ΄ν”„λΌμΈ:
7
- 1. Spleeter β†’ 4stems 뢄리 (λ“œλŸΌ, 베이슀, 보컬, 기타)
8
  2. librosa β†’ λ“œλŸΌ / λ©œλ‘œλ””(보컬+기타 병합) 각각 Onset 감지
9
  3. RMS μ—λ„ˆμ§€ 기반으둜 λ“œλŸΌ ν™œμ„±/λΉ„ν™œμ„± ꡬ간을 νŒλ³„ν•˜μ—¬ Onset을 슀마트 κ²°ν•©
10
  4. 50ms 이내 쀑볡 Onset 병합
@@ -24,47 +24,119 @@ import numpy as np
24
  import soundfile as sf
25
  from scipy.signal import butter, find_peaks, lfilter, sosfilt
26
 
27
- # Spleeter κ²½κ³  μ–΅μ œ
28
- warnings.filterwarnings("ignore")
 
 
29
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
30
 
31
 
32
  # ─────────────────────────────────────────────
33
- # 1. Spleeter μŠ€ν…œ 뢄리
34
  # ─────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def separate_stems(audio_path: str, output_dir: str) -> str:
36
  """
37
- Spleeterλ₯Ό μ‚¬μš©ν•΄ μ˜€λ””μ˜€λ₯Ό 4개의 μŠ€ν…œμœΌλ‘œ λΆ„λ¦¬ν•œλ‹€.
38
  이미 λΆ„λ¦¬λœ 파일이 μžˆλ‹€λ©΄ 뢄리 과정을 κ±΄λ„ˆλ›΄λ‹€.
39
  λ°˜ν™˜: μŠ€ν…œ νŒŒμΌλ“€μ΄ μ €μž₯된 디렉토리 경둜
40
  """
41
  basename = os.path.splitext(os.path.basename(audio_path))[0]
42
- stem_dir = os.path.join(output_dir, basename)
 
 
 
 
 
 
43
  expected_files = ["drums.wav", "bass.wav", "vocals.wav", "other.wav"]
44
 
45
  # 이미 λͺ¨λ“  μŠ€ν…œ 파일이 μ‘΄μž¬ν•˜λŠ”μ§€ 확인 (캐싱)
46
  all_exist = True
47
- for f in expected_files:
48
- if not os.path.exists(os.path.join(stem_dir, f)):
49
- all_exist = False
50
- break
 
 
 
51
 
52
  if all_exist:
53
- print(f"[1/5] Spleeter μŠ€ν…œ 파일이 이미 μ‘΄μž¬ν•©λ‹ˆλ‹€. μΊμ‹œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€: {stem_dir}")
54
  return stem_dir
55
 
56
- from spleeter.separator import Separator
57
-
58
- print("[1/5] Spleeter둜 μŠ€ν…œ 뢄리 쀑...")
59
- separator = Separator("spleeter:4stems")
60
- separator.separate_to_file(audio_path, output_dir)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- for f in expected_files:
63
- fpath = os.path.join(stem_dir, f)
64
- if not os.path.exists(fpath):
65
- print(f" ⚠ κ²½κ³ : {f} νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€ ({fpath})")
 
66
 
67
- print(f" βœ“ μŠ€ν…œ 뢄리 μ™„λ£Œ β†’ {stem_dir}")
68
  return stem_dir
69
 
70
 
 
4
  .wav μ˜€λ””μ˜€ νŒŒμΌμ„ λΆ„μ„ν•˜μ—¬ μ •ν™•ν•œ 타이밍에 MIDI λ…ΈνŠΈλ₯Ό μžλ™ μƒμ„±ν•œλ‹€.
5
 
6
  νŒŒμ΄ν”„λΌμΈ:
7
+ 1. Demucs β†’ 4stems 뢄리 (λ“œλŸΌ, 베이슀, 보컬, 기타)
8
  2. librosa β†’ λ“œλŸΌ / λ©œλ‘œλ””(보컬+기타 병합) 각각 Onset 감지
9
  3. RMS μ—λ„ˆμ§€ 기반으둜 λ“œλŸΌ ν™œμ„±/λΉ„ν™œμ„± ꡬ간을 νŒλ³„ν•˜μ—¬ Onset을 슀마트 κ²°ν•©
10
  4. 50ms 이내 쀑볡 Onset 병합
 
24
  import soundfile as sf
25
  from scipy.signal import butter, find_peaks, lfilter, sosfilt
26
 
27
+ import shutil
28
+ from pathlib import Path
29
+
30
+ # Demucs μ—°μ‚° μ΅œμ ν™” (TF κ²½κ³  μ–΅μ œλŠ” μœ μ§€)
31
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
32
 
33
 
34
  # ─────────────────────────────────────────────
35
+ # 1. Demucs μŠ€ν…œ 뢄리 및 μΊμ‹œ 관리
36
  # ─────────────────────────────────────────────
37
+ def get_dir_size(path: str) -> int:
38
+ """λ””λ ‰ν† λ¦¬μ˜ 전체 μš©λŸ‰μ„ λ°”μ΄νŠΈ λ‹¨μœ„λ‘œ λ°˜ν™˜ν•œλ‹€."""
39
+ total = 0
40
+ for p in Path(path).rglob('*'):
41
+ if p.is_file():
42
+ total += p.stat().st_size
43
+ return total
44
+
45
+ def manage_cache(cache_dir: str, max_size_gb: float = 5.0):
46
+ """
47
+ μΊμ‹œ 디렉토리 μš©λŸ‰μ„ ν™•μΈν•˜κ³  섀정을 μ΄ˆκ³Όν•˜λ©΄ 였래된 폴더뢀터 μ‚­μ œν•œλ‹€.
48
+ """
49
+ if not os.path.exists(cache_dir):
50
+ return
51
+
52
+ current_size = get_dir_size(cache_dir)
53
+ max_size_bytes = max_size_gb * 1024 * 1024 * 1024
54
+
55
+ if current_size > max_size_bytes:
56
+ print(f" ⚠ μΊμ‹œ μš©λŸ‰ 초과 ({current_size/1024/1024/1024:.2f}GB > {max_size_gb}GB). 였래된 파일 정리 쀑...")
57
+
58
+ # 각 곑별 ν΄λ”μ˜ μˆ˜μ • μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬
59
+ subdirs = [os.path.join(cache_dir, d) for d in os.listdir(cache_dir)]
60
+ subdirs = [d for d in subdirs if os.path.isdir(d)]
61
+ subdirs.sort(key=lambda x: os.path.getmtime(x))
62
+
63
+ for d in subdirs:
64
+ if current_size <= max_size_bytes:
65
+ break
66
+ dir_size = get_dir_size(d)
67
+ shutil.rmtree(d)
68
+ current_size -= dir_size
69
+ print(f" - μ‚­μ œλ¨: {os.path.basename(d)} ({dir_size/1024/1024:.1f}MB)")
70
+
71
  def separate_stems(audio_path: str, output_dir: str) -> str:
72
  """
73
+ Demucsλ₯Ό μ‚¬μš©ν•΄ μ˜€λ””μ˜€λ₯Ό 4개의 μŠ€ν…œμœΌλ‘œ λΆ„λ¦¬ν•œλ‹€.
74
  이미 λΆ„λ¦¬λœ 파일이 μžˆλ‹€λ©΄ 뢄리 과정을 κ±΄λ„ˆλ›΄λ‹€.
75
  λ°˜ν™˜: μŠ€ν…œ νŒŒμΌλ“€μ΄ μ €μž₯된 디렉토리 경둜
76
  """
77
  basename = os.path.splitext(os.path.basename(audio_path))[0]
78
+ # μΊμ‹œ 디렉토리 μ„€μ • (ν”„λ‘œμ νŠΈ 루트의 stems 폴더)
79
+ stem_parent_dir = os.path.join(os.getcwd(), "stems")
80
+ stem_dir = os.path.join(stem_parent_dir, basename)
81
+
82
+ # μΊμ‹œ 관리 (5GB μ œν•œ)
83
+ manage_cache(stem_parent_dir, max_size_gb=5.0)
84
+
85
  expected_files = ["drums.wav", "bass.wav", "vocals.wav", "other.wav"]
86
 
87
  # 이미 λͺ¨λ“  μŠ€ν…œ 파일이 μ‘΄μž¬ν•˜λŠ”μ§€ 확인 (캐싱)
88
  all_exist = True
89
+ if os.path.exists(stem_dir):
90
+ for f in expected_files:
91
+ if not os.path.exists(os.path.join(stem_dir, f)):
92
+ all_exist = False
93
+ break
94
+ else:
95
+ all_exist = False
96
 
97
  if all_exist:
98
+ print(f"[1/5] Demucs μŠ€ν…œ 파일이 이미 μΊμ‹œμ— μ‘΄μž¬ν•©λ‹ˆλ‹€: {stem_dir}")
99
  return stem_dir
100
 
101
+ print("[1/5] Demucs둜 μŠ€ν…œ 뢄리 쀑 (이 μž‘μ—…μ€ λ‹€μ†Œ μ‹œκ°„μ΄ 걸릴 수 μžˆμŠ΅λ‹ˆλ‹€)...")
102
+
103
+ # Demucs μ‹€ν–‰ (subprocess μ‚¬μš© - ν™˜κ²½ μ˜μ‘΄μ„± μ΅œμ†Œν™”)
104
+ try:
105
+ import subprocess
106
+ # htdemucs λͺ¨λΈ μ‚¬μš© (κΈ°λ³Έ 4μŠ€νƒ¬)
107
+ cmd = [
108
+ sys.executable, "-m", "demucs.separate",
109
+ "-n", "htdemucs",
110
+ "--path", audio_path,
111
+ "-o", stem_parent_dir
112
+ ]
113
+ # output_dir μΈμžκ°€ 있으면 그곳에 μž„μ‹œ μ €μž₯ ν›„ 정리
114
+ subprocess.run(cmd, check=True)
115
+
116
+ # Demucs의 κΈ°λ³Έ 좜λ ₯ 경둜 ꡬ쑰: {output_dir}/htdemucs/{filename}/{stem}.wav
117
+ # 이λ₯Ό μš°λ¦¬κ°€ μ›ν•˜λŠ” {output_dir}/{filename}/{stem}.wav 둜 이동/정리
118
+ demucs_out_path = os.path.join(stem_parent_dir, "htdemucs", basename)
119
+
120
+ if not os.path.exists(stem_dir):
121
+ os.makedirs(stem_dir, exist_ok=True)
122
+
123
+ for f in expected_files:
124
+ src = os.path.join(demucs_out_path, f)
125
+ dst = os.path.join(stem_dir, f)
126
+ if os.path.exists(src):
127
+ shutil.move(src, dst)
128
+
129
+ # μ‚¬μš© ν›„ 남은 htdemucs 빈 폴더 μ‚­μ œ
130
+ if os.path.exists(demucs_out_path):
131
+ shutil.rmtree(os.path.join(stem_parent_dir, "htdemucs"))
132
 
133
+ except Exception as e:
134
+ print(f" ❌ Demucs 뢄리 μ‹€νŒ¨: {str(e)}")
135
+ # μ‹€νŒ¨ μ‹œμ—λ„ 디렉토리 ꡬ쑰라도 λ§žμΆ”κΈ° μœ„ν•΄ μ‹œλ„
136
+ if not os.path.exists(stem_dir):
137
+ os.makedirs(stem_dir, exist_ok=True)
138
 
139
+ print(f" βœ“ μŠ€ν…œ 뢄리 및 캐싱 μ™„λ£Œ β†’ {stem_dir}")
140
  return stem_dir
141
 
142
 
requirements.txt CHANGED
@@ -5,6 +5,9 @@ soundfile
5
  numpy
6
  scipy
7
  pandas
8
- spleeter==2.4.0
 
 
9
  huggingface_hub
10
- setuptools
 
 
5
  numpy
6
  scipy
7
  pandas
8
+ demucs
9
+ torch
10
+ torchaudio
11
  huggingface_hub
12
+ setuptools
13
+ ffmpeg-python
web_ui.py CHANGED
@@ -78,7 +78,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate"),
78
  gr.Markdown("# 🎡 UV MIDI Maker - Pro")
79
  gr.Markdown(
80
  "### μ˜€λ””μ˜€λ₯Ό λΆ„μ„ν•˜μ—¬ μ •λ°€ν•œ 리듬 κ²Œμž„ MIDIλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.\n"
81
- "이 λ„κ΅¬λŠ” Spleeter AIλ₯Ό 톡해 μ•…κΈ°λ₯Ό λΆ„λ¦¬ν•˜κ³ , Librosaλ₯Ό 톡해 타격점을 μ •λ°€ν•˜κ²Œ μΆ”μΆœν•©λ‹ˆλ‹€."
82
  )
83
 
84
  with gr.Row():
 
78
  gr.Markdown("# 🎡 UV MIDI Maker - Pro")
79
  gr.Markdown(
80
  "### μ˜€λ””μ˜€λ₯Ό λΆ„μ„ν•˜μ—¬ μ •λ°€ν•œ 리듬 κ²Œμž„ MIDIλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.\n"
81
+ "이 λ„κ΅¬λŠ” Meta의 Demucs AIλ₯Ό 톡해 μ•…κΈ°λ₯Ό 고음질둜 λΆ„λ¦¬ν•˜κ³ , Librosaλ₯Ό 톡해 타격점을 μ •λ°€ν•˜κ²Œ μΆ”μΆœν•©λ‹ˆλ‹€."
82
  )
83
 
84
  with gr.Row():