-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2701def
commit 514a656
Showing
1 changed file
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,63 @@ | ||
from importlib import import_module | ||
from pathlib import Path | ||
from typing import TypeAlias | ||
|
||
from pedalboard import Compressor, Gain, Pedalboard | ||
from pedalboard.io import AudioFile | ||
from pedalboard import Pedalboard | ||
from pedalboard.io import AudioFile # type: ignore | ||
|
||
T_fx_chain: TypeAlias = dict[str, dict[str, float]] | ||
|
||
fx_chain: T_fx_chain = { | ||
'NoiseGate': { | ||
'attack_ms': 100, | ||
'release_ms': 200, | ||
'threshold_db': -30, | ||
'ratio': 4, | ||
}, | ||
'Compressor': { | ||
'attack_ms': 0, | ||
'release_ms': 250, | ||
'threshold_db': -33.5, | ||
'ratio': 2, | ||
}, | ||
'Gain': {'gain_db': -12}, | ||
'Limiter': {'release_ms': 0, 'threshold_db': -1}, | ||
} | ||
|
||
|
||
def _get_board() -> Pedalboard: | ||
pedalboard = import_module('pedalboard') | ||
|
||
""" | ||
NOTE: Not implemented yet | ||
from json import loads | ||
from vmh import settings | ||
if settings.eq_config_path.exists(): | ||
effects: T_fx_chain = loads(settings.eq_config_path.read_text()) | ||
else: | ||
effects = fx_chain | ||
""" | ||
effects = fx_chain | ||
|
||
plugins = [ | ||
getattr(pedalboard, effect)(**effects[effect]) for effect in effects | ||
] | ||
|
||
return Pedalboard(plugins) | ||
|
||
|
||
def process_audio( | ||
input_file: str, output_file='output.wav', gain: int = 10 | ||
input_file: str, | ||
output_file: str = 'output.wav', | ||
board: Pedalboard = _get_board(), | ||
) -> Path: | ||
board = Pedalboard([Compressor(), Gain(gain)]) | ||
|
||
with AudioFile(input_file, mode='r') as f: | ||
with AudioFile(output_file, 'w', f.samplerate, f.num_channels) as o: | ||
while f.tell() < f.frames: | ||
chunk = f.read(int(f.samplerate)) | ||
effected = board(chunk, f.samplerate, reset=False) | ||
o.write(effected) | ||
with AudioFile(input_file, 'r') as ifile: | ||
with AudioFile( | ||
output_file, 'w', ifile.samplerate, ifile.num_channels | ||
) as ofile: | ||
while ifile.tell() < ifile.frames: | ||
chunk = ifile.read(ifile.samplerate) | ||
effected = board(chunk, ifile.samplerate, reset=False) | ||
ofile.write(effected) | ||
|
||
return Path(output_file) |