Spaces:
Running
on
A10G
Running
on
A10G
File size: 6,912 Bytes
a6c03bb |
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 |
"""
Effect Chain - Pedalboard ๊ธฐ๋ฐ ์ค๋์ค ์ดํํธ ์ฒ๋ฆฌ
=================================================
V2: ํ์ต ๋ฐ์ดํฐ ํ๋ผ๋ฏธํฐ ํค์ ํธํ์ฑ ๊ฐ์
"""
import numpy as np
import soundfile as sf
from typing import Dict, List, Optional
from pedalboard import (
Pedalboard,
Compressor,
Gain,
HighShelfFilter,
LowShelfFilter,
PeakFilter,
Delay,
Reverb,
Distortion,
Limiter
)
class EffectChain:
"""Pedalboard ๊ธฐ๋ฐ ์ดํํธ ์ฒด์ธ"""
def __init__(self, sample_rate: int = 44100):
self.sample_rate = sample_rate
self.available_effects = [
"eq_peak1", "eq_peak2",
"eq_lowshelf", "eq_highshelf",
"distortion", "delay", "compressor",
"reverb", "limiter"
]
def get_available_effects(self) -> List[str]:
"""์ฌ์ฉ ๊ฐ๋ฅํ ์ดํํธ ๋ชฉ๋ก"""
return self.available_effects
def _get_param(self, params: Dict[str, float], *keys, default=0.0) -> float:
"""
์ฌ๋ฌ ํค ์ค ํ๋๋ผ๋ ์์ผ๋ฉด ๋ฐํ (ํธํ์ฑ ์ฒ๋ฆฌ)
์: 'eq_peak1.params.Q' ๋๋ 'eq_peak1.params.q' ๋ ๋ค ์ง์
"""
for key in keys:
if key in params:
return params[key]
return default
def _build_pedalboard(self, params: Dict[str, float]) -> Pedalboard:
"""ํ๋ผ๋ฏธํฐ๋ก Pedalboard ๊ตฌ์ฑ"""
effects = []
# Compressor (ํญ์ ์ ์ฉ)
effects.append(Compressor(
threshold_db=-18.0,
ratio=2.0,
attack_ms=10.0,
release_ms=100.0
))
# EQ Peak 1 (Q/q ๋ ๋ค ์ง์)
freq1 = self._get_param(params, "eq_peak1.params.freq", default=1000.0)
gain1 = self._get_param(params, "eq_peak1.params.gain", default=0.0)
q1 = self._get_param(params, "eq_peak1.params.Q", "eq_peak1.params.q", default=1.0)
if abs(gain1) > 0.1:
effects.append(PeakFilter(
cutoff_frequency_hz=max(20, min(20000, freq1)),
gain_db=max(-12, min(12, gain1)),
q=max(0.1, min(10, q1))
))
# EQ Peak 2 (Q/q ๋ ๋ค ์ง์)
freq2 = self._get_param(params, "eq_peak2.params.freq", default=4000.0)
gain2 = self._get_param(params, "eq_peak2.params.gain", default=0.0)
q2 = self._get_param(params, "eq_peak2.params.Q", "eq_peak2.params.q", default=1.0)
if abs(gain2) > 0.1:
effects.append(PeakFilter(
cutoff_frequency_hz=max(20, min(20000, freq2)),
gain_db=max(-12, min(12, gain2)),
q=max(0.1, min(10, q2))
))
# Low Shelf
freq_low = self._get_param(params, "eq_lowshelf.params.freq", default=200.0)
gain_low = self._get_param(params, "eq_lowshelf.params.gain", default=0.0)
if abs(gain_low) > 0.1:
effects.append(LowShelfFilter(
cutoff_frequency_hz=max(20, min(2000, freq_low)),
gain_db=max(-12, min(12, gain_low)),
q=0.707
))
# High Shelf
freq_high = self._get_param(params, "eq_highshelf.params.freq", default=8000.0)
gain_high = self._get_param(params, "eq_highshelf.params.gain", default=0.0)
if abs(gain_high) > 0.1:
effects.append(HighShelfFilter(
cutoff_frequency_hz=max(1000, min(20000, freq_high)),
gain_db=max(-12, min(12, gain_high)),
q=0.707
))
# Distortion
dist_amount = self._get_param(params, "distortion_amount", default=0.0)
if dist_amount > 0.01:
# ํ์ต ๋ฐ์ดํฐ๋ sigmoid ํ 0.1 ๊ณฑํ๋ฏ๋ก ์ต๋ 0.1
# ์ฌ๊ธฐ์๋ drive_db๋ก ๋ณํ (0~20dB ๋ฒ์)
effects.append(Distortion(
drive_db=max(0, min(20, dist_amount * 100))
))
# Delay
delay_time = self._get_param(params, "delay.delay_time", default=0.02)
delay_feedback = self._get_param(params, "delay.feedback", default=0.3)
delay_mix = self._get_param(params, "delay.mix", default=0.2)
if delay_mix > 0.01:
effects.append(Delay(
delay_seconds=max(0.01, min(1.0, delay_time)),
feedback=max(0.0, min(0.9, delay_feedback)),
mix=max(0.0, min(1.0, delay_mix))
))
# Limiter (ํญ์ ๋ง์ง๋ง์)
effects.append(Limiter(threshold_db=-1.0))
return Pedalboard(effects)
def process(
self,
input_path: str,
output_path: str,
parameters: Dict[str, float]
) -> bool:
"""์ค๋์ค ํ์ผ ์ฒ๋ฆฌ"""
try:
# ์ค๋์ค ๋ก๋
audio, sr = sf.read(input_path)
# ๋ชจ๋
ธ/์คํ
๋ ์ค ์ฒ๋ฆฌ
if len(audio.shape) == 1:
audio = audio.reshape(-1, 1)
# float32๋ก ๋ณํ
audio = audio.astype(np.float32)
# Pedalboard ๊ตฌ์ฑ
board = self._build_pedalboard(parameters)
# ์ฒ๋ฆฌ
processed = board(audio, sr)
# Wet/Dry ๋ฏน์ค (final_wet_mix ํ๋ผ๋ฏธํฐ ์ฌ์ฉ)
wet_mix = self._get_param(parameters, "final_wet_mix", default=0.5)
wet_mix = max(0.0, min(1.0, wet_mix))
# ๊ธธ์ด ๋ง์ถ๊ธฐ
min_len = min(len(audio), len(processed))
output = audio[:min_len] * (1 - wet_mix) + processed[:min_len] * wet_mix
# ํด๋ฆฌํ ๋ฐฉ์ง
output = np.clip(output, -1.0, 1.0)
# ์ ์ฅ
sf.write(output_path, output, sr)
print(f"[EffectChain] โ
์ฒ๋ฆฌ ์๋ฃ: {output_path}")
print(f"[EffectChain] ์ ์ฉ๋ ํ๋ผ๋ฏธํฐ:")
print(f" - EQ Peak1 Gain: {self._get_param(parameters, 'eq_peak1.params.gain'):.2f} dB")
print(f" - EQ Peak2 Gain: {self._get_param(parameters, 'eq_peak2.params.gain'):.2f} dB")
print(f" - Low Shelf Gain: {self._get_param(parameters, 'eq_lowshelf.params.gain'):.2f} dB")
print(f" - High Shelf Gain: {self._get_param(parameters, 'eq_highshelf.params.gain'):.2f} dB")
print(f" - Distortion: {self._get_param(parameters, 'distortion_amount'):.3f}")
print(f" - Delay Mix: {self._get_param(parameters, 'delay.mix'):.2f}")
print(f" - Wet Mix: {wet_mix:.2f}")
return True
except Exception as e:
print(f"[EffectChain] โ ์ฒ๋ฆฌ ์คํจ: {e}")
import traceback
traceback.print_exc()
raise e
|