-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_midi.py
More file actions
77 lines (64 loc) · 2.42 KB
/
extract_midi.py
File metadata and controls
77 lines (64 loc) · 2.42 KB
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
from basic_pitch.inference import predict_and_save
import os
import shutil
def extract_midi(audio_path, output_path):
"""오디오 파일에서 MIDI 추출"""
print(f"Converting {audio_path} to MIDI...")
# 임시 디렉토리 생성
temp_dir = 'temp_midi'
os.makedirs(temp_dir, exist_ok=True)
try:
# Basic Pitch로 MIDI 예측 및 저장
predict_and_save(
audio_path_list=[audio_path],
output_dir=temp_dir,
save_midi=True,
sonify_midi=False,
min_note_length=0.05,
min_frequency=30,
max_frequency=1000,
multiple_pitch_bends=False
)
# 생성된 MIDI 파일 찾기
temp_midi = os.path.join(temp_dir, os.path.basename(audio_path).replace('.mp3', '_basic_pitch.mid'))
# 최종 위치로 이동
if os.path.exists(temp_midi):
os.makedirs(os.path.dirname(output_path), exist_ok=True)
shutil.move(temp_midi, output_path)
print(f"MIDI saved to {output_path}")
return True
except Exception as e:
print(f"Error converting {audio_path}: {str(e)}")
return False
finally:
# 임시 디렉토리 정리
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
return False
def process_all_stems(input_dir='separated', output_dir='midi_files'):
"""모든 스템 파일을 MIDI로 변환"""
os.makedirs(output_dir, exist_ok=True)
# 처리할 파일 매핑
stems = {
'vocals.mp3': 'vocals.mid',
'drums.mp3': 'drums.mid',
'bass.mp3': 'bass.mid',
'MR.mp3': 'mr.mid'
}
# 원본 파일도 처리
original_path = 'test.mp3'
if os.path.exists(original_path):
output_path = os.path.join(output_dir, 'original.mid')
success = extract_midi(original_path, output_path)
if success:
print(f"Original file converted successfully")
# 각 스템 처리
for input_file, output_file in stems.items():
input_path = os.path.join(input_dir, input_file)
if os.path.exists(input_path):
output_path = os.path.join(output_dir, output_file)
success = extract_midi(input_path, output_path)
if success:
print(f"{input_file} converted successfully")
if __name__ == "__main__":
process_all_stems()