-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_processing.py
More file actions
216 lines (178 loc) · 8.53 KB
/
audio_processing.py
File metadata and controls
216 lines (178 loc) · 8.53 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
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
import os
import numpy as np
from demucs import pretrained
from demucs.apply import apply_model, BagOfModels
import torch
import soundfile as sf
import librosa
def process_in_chunks(model, wav, chunk_size=44100*15, overlap=0.1, progress_callback=None, status_callback=None):
"""Process audio in chunks to save memory"""
device = "cuda" if torch.cuda.is_available() else "cpu"
total_length = wav.shape[-1]
# Process first chunk to get output shape
with torch.no_grad():
first_chunk = wav[..., :chunk_size]
first_stems = apply_model(
model,
first_chunk.to(device),
shifts=1,
split=True,
overlap=0.1,
progress=False
)
# Initialize output tensor
n_stems = first_stems.shape[1] # Usually 4 for [drums, bass, other, vocals]
final_stems = torch.zeros((1, n_stems, 2, total_length), dtype=torch.float32)
final_stems[..., :first_stems.shape[-1]] = first_stems
# Process remaining chunks
overlap_frames = int(chunk_size * overlap)
current_pos = chunk_size - overlap_frames
for start in range(chunk_size - overlap_frames, total_length, chunk_size - overlap_frames):
if status_callback:
progress = (start / total_length) * 100
status_callback(f"Processing: {progress:.1f}%")
if progress_callback:
progress_callback(int((start / total_length) * 50))
end = min(start + chunk_size, total_length)
if end - start < chunk_size // 2: # Skip if chunk is too small
break
# Process chunk
with torch.no_grad():
chunk = wav[..., start:end]
stems = apply_model(
model,
chunk.to(device),
shifts=1,
split=True,
overlap=0.1,
progress=False
)
# Apply crossfade
chunk_length = stems.shape[-1]
fade_in = torch.linspace(0, 1, overlap_frames).view(1, 1, 1, -1)
fade_out = torch.linspace(1, 0, overlap_frames).view(1, 1, 1, -1)
# Crossfade region
final_stems[..., current_pos:current_pos + overlap_frames] *= fade_out
final_stems[..., current_pos:current_pos + overlap_frames] += stems[..., :overlap_frames] * fade_in
# Copy remaining part
if current_pos + overlap_frames < total_length:
end_pos = min(current_pos + chunk_length, total_length)
final_stems[..., current_pos + overlap_frames:end_pos] = stems[..., overlap_frames:end_pos-current_pos]
current_pos += chunk_size - overlap_frames
torch.cuda.empty_cache() if torch.cuda.is_available() else None
return final_stems
def separate_stems(input_file, output_dir, progress_callback=None, status_callback=None):
"""
Separates audio into stems using the advanced HTQ model of Demucs.
Args:
input_file (str): Path to the input audio file.
output_dir (str): Directory to save the separated stems.
"""
try:
if status_callback:
status_callback("Initializing...")
# Check GPU availability and provide detailed feedback
cuda_available = torch.cuda.is_available()
if cuda_available:
gpu_count = torch.cuda.device_count()
gpu_name = torch.cuda.get_device_name(0)
print(f"CUDA GPU detected: {gpu_name} (Device {0})")
if status_callback:
status_callback(f"GPU detected: {gpu_name}")
else:
print("CUDA GPU not available - using CPU (this will be slower)")
print("To enable GPU acceleration, install CUDA-enabled PyTorch:")
print("pip install torch==2.1.0+cu118 torchvision==0.16.0+cu118 torchaudio==2.1.0+cu118 --index-url https://download.pytorch.org/whl/cu118")
if status_callback:
status_callback("Using CPU - GPU not detected")
# Ensure the output directory exists
os.makedirs(output_dir, exist_ok=True)
print(f"Output directory created: {output_dir}")
# Load the pre-trained HTDemucs model with optimized settings
model = pretrained.get_model("htdemucs_ft") # Using fine-tuned model
if cuda_available:
model.cuda()
print(f"Model loaded on GPU: {torch.cuda.get_device_name(0)}")
else:
print("Model loaded on CPU")
print(f"Loaded model: htdemucs_ft, Model type: {type(model)}")
if status_callback:
status_callback("Loading audio file...")
# Load and prepare audio with optimal settings
print(f"Loading audio file: {input_file}")
wav, sr = librosa.load(input_file, sr=44100, mono=False, duration=None)
# Enhanced preprocessing
if wav.ndim == 1:
wav = np.expand_dims(wav, axis=0)
# Improved normalization with headroom
wav = torch.tensor(wav, dtype=torch.float32)
if wav.dim() == 2:
wav = wav.unsqueeze(0)
# Dynamic range compression for better vocal presence
wav = wav / (wav.abs().max() * 1.1) # Leave headroom
print(f"Audio file loaded: {input_file}, Sample rate: {sr}")
print(f"Audio tensor shape: {wav.shape}")
# Move to GPU if available
if torch.cuda.is_available():
wav = wav.cuda()
if status_callback:
status_callback("Performing stem separation...")
# Perform stem separation with optimized parameters
print(f"Performing stem separation")
sources = process_in_chunks(model, wav,
progress_callback=progress_callback,
status_callback=status_callback)
print(f"Sources shape: {sources.shape}") # Add debug print
stems = torch.split(sources.squeeze(0), 1, dim=0)
print(f"Stem separation completed")
if status_callback:
status_callback("Saving stems...")
# Extract base name from input file
base_name = os.path.splitext(os.path.basename(input_file))[0].replace(" ", "_")
# Save stems with enhanced quality
stem_names = ["drums", "bass", "other", "vocals"]
base_name = os.path.splitext(os.path.basename(input_file))[0]
for i, stem in enumerate(stems):
if status_callback:
status_callback(f"Saving {stem_names[i]}...")
if progress_callback:
progress_callback(50 + int((i / len(stem_names)) * 50)) # Last 50% of progress
output_path = os.path.join(output_dir, f"{base_name}_{stem_names[i]}.wav")
stem = stem.squeeze(0).cpu().numpy()
# Ensure proper shape and apply final processing
if stem.shape[0] == 2:
stem = stem.T
# Apply subtle enhancement for vocals
if stem_names[i] == "vocals":
stem = enhance_vocals(stem, sr)
print(f"Saving stem: {stem_names[i]} to {output_path}, shape: {stem.shape}")
sf.write(output_path, stem, sr, subtype='FLOAT')
print(f"Stem saved: {output_path}")
if status_callback:
status_callback("All stems saved successfully!")
if progress_callback:
progress_callback(100)
print(f"All stems saved in: {output_dir}")
except Exception as e:
if status_callback:
status_callback(f"Error: {str(e)}")
print(f"Error occurred: {str(e)}")
raise
def segment_size(audio_length):
"""Calculate optimal segment size based on audio length"""
base_segment = 44100 * 10 # 10 seconds base
return min(base_segment, audio_length // 4)
def enhance_vocals(vocals, sr):
"""Apply subtle enhancements to vocal stem"""
# Gentle normalization to preserve dynamics
max_val = np.abs(vocals).max()
if max_val > 0:
vocals = vocals / max_val * 0.95
# Preserve stereo field
if vocals.ndim == 2:
vocals = vocals.copy() # Prevent modifying original array
# Apply mild stereo enhancement
mid = np.mean(vocals, axis=1, keepdims=True)
side = vocals - mid
vocals = mid + side * 1.2 # Enhance stereo width by 20%
return vocals