-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_processor.py
executable file
·521 lines (438 loc) · 19 KB
/
audio_processor.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
"""
Audio Processor Module for StablePackGen.
This module provides post-processing capabilities for audio samples,
including normalization, EQ shaping, and stereo enhancement.
"""
import os
import numpy as np
import librosa
import soundfile as sf
from typing import Dict, Any, List, Optional, Tuple, Union
import pyloudnorm as pyln
from scipy import signal
class AudioProcessor:
"""Handles post-processing of audio samples for quality control."""
def __init__(self, sample_rate: int = 44100):
"""
Initialize the audio processor.
Args:
sample_rate: Sample rate for audio processing (default: 44100 Hz)
"""
self.sample_rate = sample_rate
self.meter = pyln.Meter(self.sample_rate) # Initialize loudness meter
def process_sample(
self,
input_path: str,
output_path: Optional[str] = None,
processing_options: Dict[str, Any] = None
) -> str:
"""
Apply post-processing to an audio sample.
Args:
input_path: Path to the input audio file
output_path: Path to save the processed audio (if None, overwrites input)
processing_options: Dictionary of processing options
- normalize: Target LUFS level for normalization (e.g., -14.0)
- eq_preset: EQ preset to apply ('kick', 'snare', 'hat', etc.)
- stereo_width: Stereo width enhancement (0.0-2.0, 1.0 = no change)
- high_pass: High-pass filter cutoff frequency in Hz (or None)
- low_pass: Low-pass filter cutoff frequency in Hz (or None)
- transient_enhance: Transient enhancement amount (0.0-1.0)
Returns:
Path to the processed audio file
"""
# Set default output path if not provided
if output_path is None:
output_path = input_path
# Set default processing options if not provided
if processing_options is None:
processing_options = {
"normalize": -14.0,
"eq_preset": None,
"stereo_width": 1.0,
"high_pass": None,
"low_pass": None,
"transient_enhance": 0.0
}
# Load audio file
audio, sr = librosa.load(input_path, sr=self.sample_rate, mono=False)
# Convert mono to stereo if needed
if len(audio.shape) == 1:
audio = np.stack([audio, audio])
# Apply processing in the correct order
# 1. High-pass filter
if processing_options.get("high_pass"):
audio = self._apply_high_pass(audio, processing_options["high_pass"])
# 2. Low-pass filter
if processing_options.get("low_pass"):
audio = self._apply_low_pass(audio, processing_options["low_pass"])
# 3. EQ shaping
if processing_options.get("eq_preset"):
audio = self._apply_eq_preset(audio, processing_options["eq_preset"])
# 4. Transient enhancement
if processing_options.get("transient_enhance", 0.0) > 0:
audio = self._enhance_transients(audio, processing_options["transient_enhance"])
# 5. Stereo width adjustment
if processing_options.get("stereo_width", 1.0) != 1.0:
audio = self._adjust_stereo_width(audio, processing_options["stereo_width"])
# 6. Normalization (always apply as the last step)
if processing_options.get("normalize") is not None:
audio = self._normalize_loudness(audio, processing_options["normalize"])
# Create output directory if it doesn't exist
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
# Save processed audio
sf.write(output_path, audio.T, self.sample_rate)
return output_path
def process_sample_pack(
self,
directory: str,
processing_options: Dict[str, Dict[str, Any]] = None
) -> List[str]:
"""
Apply post-processing to all samples in a directory.
Args:
directory: Directory containing audio samples
processing_options: Dictionary mapping category names to processing options
Example: {
"kicks": {"normalize": -12.0, "eq_preset": "kick"},
"snares": {"normalize": -14.0, "eq_preset": "snare"},
"default": {"normalize": -16.0}
}
Returns:
List of paths to processed audio files
"""
if processing_options is None:
processing_options = {
"default": {"normalize": -14.0}
}
processed_files = []
# Walk through the directory
for root, dirs, files in os.walk(directory):
# Get the category from the directory name
category = os.path.basename(root)
# Skip if no audio files in this directory
audio_files = [f for f in files if f.endswith(('.wav', '.mp3', '.aif', '.aiff'))]
if not audio_files:
continue
print(f"Processing {len(audio_files)} samples in category: {category}")
# Get processing options for this category
category_options = processing_options.get(category, processing_options.get("default", {}))
# Process each audio file
for audio_file in audio_files:
input_path = os.path.join(root, audio_file)
try:
processed_path = self.process_sample(input_path, processing_options=category_options)
processed_files.append(processed_path)
print(f" ✓ Processed: {audio_file}")
except Exception as e:
print(f" ✗ Error processing {audio_file}: {str(e)}")
return processed_files
def _normalize_loudness(self, audio: np.ndarray, target_lufs: float) -> np.ndarray:
"""
Normalize audio to target LUFS loudness.
Args:
audio: Audio data (stereo)
target_lufs: Target loudness in LUFS
Returns:
Normalized audio data
"""
# Measure current loudness
current_loudness = self.meter.integrated_loudness(audio.T)
# Calculate gain needed
gain_db = target_lufs - current_loudness
# Apply gain
gain_linear = 10 ** (gain_db / 20.0)
# Ensure we don't clip
peak = np.max(np.abs(audio))
if peak * gain_linear > 1.0:
gain_linear = 0.99 / peak
return audio * gain_linear
def _apply_eq_preset(self, audio: np.ndarray, preset: str) -> np.ndarray:
"""
Apply EQ preset to audio.
Args:
audio: Audio data (stereo)
preset: EQ preset name
Returns:
EQ'd audio data
"""
# Define EQ presets for different sample types
presets = {
"kick": [
{"type": "high_shelf", "freq": 10000, "gain": -6, "q": 0.7},
{"type": "peaking", "freq": 100, "gain": 3, "q": 1.0},
{"type": "high_pass", "freq": 30, "q": 0.7}
],
"snare": [
{"type": "high_shelf", "freq": 8000, "gain": 3, "q": 0.7},
{"type": "peaking", "freq": 200, "gain": -3, "q": 1.0},
{"type": "peaking", "freq": 3500, "gain": 4, "q": 1.5}
],
"hat": [
{"type": "high_pass", "freq": 500, "q": 0.7},
{"type": "high_shelf", "freq": 10000, "gain": 6, "q": 0.7}
],
"percussion": [
{"type": "peaking", "freq": 400, "gain": -2, "q": 1.0},
{"type": "peaking", "freq": 3000, "gain": 3, "q": 1.0}
],
"bass": [
{"type": "low_shelf", "freq": 100, "gain": 3, "q": 0.7},
{"type": "high_pass", "freq": 40, "q": 0.7},
{"type": "high_shelf", "freq": 8000, "gain": -6, "q": 0.7}
],
"pad": [
{"type": "peaking", "freq": 300, "gain": -2, "q": 1.0},
{"type": "peaking", "freq": 3000, "gain": 2, "q": 1.0},
{"type": "high_shelf", "freq": 10000, "gain": 3, "q": 0.7}
],
"fx": [
{"type": "peaking", "freq": 1000, "gain": 2, "q": 2.0},
{"type": "high_shelf", "freq": 8000, "gain": 4, "q": 0.7}
]
}
# If preset doesn't exist, return unmodified audio
if preset not in presets:
return audio
# Apply each filter in the preset
processed_audio = audio.copy()
for filter_params in presets[preset]:
filter_type = filter_params["type"]
freq = filter_params["freq"]
if filter_type == "high_pass":
processed_audio = self._apply_high_pass(processed_audio, freq, filter_params.get("q", 0.7))
elif filter_type == "low_pass":
processed_audio = self._apply_low_pass(processed_audio, freq, filter_params.get("q", 0.7))
elif filter_type in ["peaking", "high_shelf", "low_shelf"]:
processed_audio = self._apply_parametric_eq(
processed_audio,
filter_type,
freq,
filter_params.get("gain", 0),
filter_params.get("q", 1.0)
)
return processed_audio
def _apply_high_pass(self, audio: np.ndarray, cutoff_freq: float, q: float = 0.7) -> np.ndarray:
"""Apply high-pass filter to audio."""
nyquist = self.sample_rate / 2.0
normal_cutoff = cutoff_freq / nyquist
# Design filter
b, a = signal.butter(2, normal_cutoff, btype='highpass', output='ba')
# Apply to each channel
return np.array([signal.filtfilt(b, a, channel) for channel in audio])
def _apply_low_pass(self, audio: np.ndarray, cutoff_freq: float, q: float = 0.7) -> np.ndarray:
"""Apply low-pass filter to audio."""
nyquist = self.sample_rate / 2.0
normal_cutoff = cutoff_freq / nyquist
# Design filter
b, a = signal.butter(2, normal_cutoff, btype='lowpass', output='ba')
# Apply to each channel
return np.array([signal.filtfilt(b, a, channel) for channel in audio])
def _apply_parametric_eq(
self,
audio: np.ndarray,
filter_type: str,
freq: float,
gain_db: float,
q: float
) -> np.ndarray:
"""Apply parametric EQ to audio."""
nyquist = self.sample_rate / 2.0
normal_freq = freq / nyquist
# Convert gain from dB to linear
gain_linear = 10 ** (gain_db / 20.0)
# Design filter based on type
if filter_type == "peaking":
b, a = signal.iirpeak(normal_freq, q, gain_linear)
elif filter_type == "low_shelf":
b, a = signal.iirfilter(
2, normal_freq, btype='lowpass',
ftype='butter', output='ba'
)
elif filter_type == "high_shelf":
b, a = signal.iirfilter(
2, normal_freq, btype='highpass',
ftype='butter', output='ba'
)
else:
return audio # Unknown filter type
# Apply to each channel
return np.array([signal.filtfilt(b, a, channel) for channel in audio])
def _adjust_stereo_width(self, audio: np.ndarray, width: float) -> np.ndarray:
"""
Adjust stereo width of audio.
Args:
audio: Stereo audio data [2, samples]
width: Stereo width factor (0.0 = mono, 1.0 = original, 2.0 = exaggerated)
Returns:
Width-adjusted audio
"""
# Ensure we have stereo audio
if audio.shape[0] != 2:
return audio
# Calculate mid and side signals
mid = (audio[0] + audio[1]) / 2
side = (audio[0] - audio[1]) / 2
# Adjust side level based on width
side_adjusted = side * width
# Recombine to stereo
left = mid + side_adjusted
right = mid - side_adjusted
return np.array([left, right])
def _enhance_transients(self, audio: np.ndarray, amount: float) -> np.ndarray:
"""
Enhance transients in audio.
Args:
audio: Audio data
amount: Enhancement amount (0.0-1.0)
Returns:
Transient-enhanced audio
"""
# Simple transient enhancement using differentiation
enhanced = np.zeros_like(audio)
for i, channel in enumerate(audio):
# Calculate derivative (difference)
diff = np.diff(channel, prepend=channel[0])
# Apply soft clipping to the difference signal
diff_clipped = np.tanh(diff * 3) / 3
# Mix original with enhanced version
enhanced[i] = channel + diff_clipped * amount * 0.5
# Normalize to prevent clipping
max_val = np.max(np.abs(enhanced[i]))
if max_val > 0.99:
enhanced[i] = enhanced[i] / max_val * 0.99
return enhanced
def get_default_processing_options() -> Dict[str, Dict[str, Any]]:
"""
Get default processing options for different sample categories.
Returns:
Dictionary of processing options by category
"""
return {
"kicks": {
"normalize": -12.0,
"eq_preset": "kick",
"stereo_width": 0.8,
"high_pass": 30,
"transient_enhance": 0.3
},
"snares": {
"normalize": -14.0,
"eq_preset": "snare",
"stereo_width": 1.2,
"high_pass": 100,
"transient_enhance": 0.5
},
"hats": {
"normalize": -16.0,
"eq_preset": "hat",
"stereo_width": 1.3,
"high_pass": 500,
"transient_enhance": 0.4
},
"percussion": {
"normalize": -15.0,
"eq_preset": "percussion",
"stereo_width": 1.1,
"high_pass": 200,
"transient_enhance": 0.3
},
"basses": {
"normalize": -13.0,
"eq_preset": "bass",
"stereo_width": 0.7,
"high_pass": 40
},
"synths": {
"normalize": -14.0,
"eq_preset": "bass",
"stereo_width": 1.1,
"high_pass": 80
},
"pads": {
"normalize": -18.0,
"eq_preset": "pad",
"stereo_width": 1.5
},
"atmospheres": {
"normalize": -18.0,
"eq_preset": "pad",
"stereo_width": 1.6
},
"fx": {
"normalize": -16.0,
"eq_preset": "fx",
"stereo_width": 1.4
},
"default": {
"normalize": -14.0,
"stereo_width": 1.0
}
}
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Audio Sample Post-Processor")
subparsers = parser.add_subparsers(dest="command", help="Command to execute")
# Process single file command
process_parser = subparsers.add_parser("process", help="Process a single audio file")
process_parser.add_argument("input", help="Input audio file path")
process_parser.add_argument("--output", help="Output audio file path")
process_parser.add_argument("--normalize", type=float, help="Target LUFS level for normalization")
process_parser.add_argument("--eq-preset", help="EQ preset to apply")
process_parser.add_argument("--stereo-width", type=float, help="Stereo width factor")
process_parser.add_argument("--high-pass", type=float, help="High-pass filter cutoff frequency")
process_parser.add_argument("--low-pass", type=float, help="Low-pass filter cutoff frequency")
process_parser.add_argument("--transient", type=float, help="Transient enhancement amount")
# Process directory command
batch_parser = subparsers.add_parser("batch", help="Process all audio files in a directory")
batch_parser.add_argument("directory", help="Directory containing audio files")
batch_parser.add_argument("--preset", choices=["default", "aggressive", "subtle"],
default="default", help="Processing preset to use")
args = parser.parse_args()
# Create audio processor
processor = AudioProcessor()
if args.command == "process":
# Build processing options from arguments
options = {}
if args.normalize is not None:
options["normalize"] = args.normalize
if args.eq_preset:
options["eq_preset"] = args.eq_preset
if args.stereo_width is not None:
options["stereo_width"] = args.stereo_width
if args.high_pass is not None:
options["high_pass"] = args.high_pass
if args.low_pass is not None:
options["low_pass"] = args.low_pass
if args.transient is not None:
options["transient_enhance"] = args.transient
# Process the file
output_path = processor.process_sample(args.input, args.output, options)
print(f"Processed audio saved to: {output_path}")
elif args.command == "batch":
# Get processing options based on preset
if args.preset == "aggressive":
# More extreme processing
options = get_default_processing_options()
for category, params in options.items():
if "stereo_width" in params:
params["stereo_width"] = min(2.0, params["stereo_width"] * 1.3)
if "transient_enhance" in params:
params["transient_enhance"] = min(1.0, params["transient_enhance"] * 1.5)
elif args.preset == "subtle":
# More subtle processing
options = get_default_processing_options()
for category, params in options.items():
if "stereo_width" in params:
params["stereo_width"] = 0.7 + (params["stereo_width"] - 0.7) * 0.5
if "transient_enhance" in params:
params["transient_enhance"] = params["transient_enhance"] * 0.5
else:
# Default processing
options = get_default_processing_options()
# Process the directory
processed_files = processor.process_sample_pack(args.directory, options)
print(f"Processed {len(processed_files)} audio files in {args.directory}")
else:
parser.print_help()