add resample module in preprocessing package, refactor demucs_wrapper.py

This commit is contained in:
Joel Mathew Thomas
2024-12-26 19:47:24 +05:30
parent 96b4468138
commit cfd30185bd
2 changed files with 30 additions and 43 deletions
+18
View File
@@ -0,0 +1,18 @@
import librosa
def resample(waveform, org_samplerate, new_samplerate):
"""
Reads a waveform and returns a waveform resampled to samplerate.
Args:
waveform: waveform of the target audio.
org_samplerate : original samplerate of the audio.
new_samplerate : samplerate to which the audio is to be resampled.
"""
try:
waveform = librosa.resample(waveform, orig_sr=org_samplerate, target_sr=new_samplerate)
return waveform, new_samplerate
except Exception as e:
raise RuntimeError(f"Error reasmpling the audio file: {e}")
+12 -43
View File
@@ -1,66 +1,35 @@
import subprocess
import os
import librosa
import soundfile as sf
import tempfile
from pathlib import Path
def preprocess_audio(input_file: str, target_sr: int = 41000):
"""
Preprocess the audio file by loading, converting to mono, and resampling.
Parameters:
input_file (str): Path to the input audio file to be processed.
target_sr (int): The target sample rate for the resampled audio.
Returns:
str: Path to the temporary processed audio file.
"""
# Load the audio with librosa, converting to mono and resampling
waveform, sr = librosa.load(input_file, sr=target_sr, mono=True)
# Create a temporary file to save the processed audio
temp_audio_path = tempfile.mktemp(suffix=".wav")
# Save the processed audio to the temporary file
sf.write(temp_audio_path, waveform, target_sr)
return temp_audio_path
def separate_audio_with_demucs(input_file: str, output_dir: str = './separated_output'):
def separate_audio_with_demucs(input_file, sample_rate, output_dir: str):
"""
Use subprocess to run Demucs separation on an input audio file.
Parameters:
input_file (str): Path to the input audio file to be separated.
output_dir (str): Directory where the separated output will be saved.
"""
# Create the output directory if it does not exist
os.makedirs(output_dir, exist_ok=True)
# Preprocess the audio file
processed_audio = preprocess_audio(input_file)
# Demucs command to separate the temp audio file
command = [ 'demucs', '--out', output_dir, input_file]
# Demucs command to separate the audio file
command = [
'demucs',
'--out', output_dir, # Output directory for separated tracks
processed_audio # Input processed file
]
try:
# Run the command as a subprocess
subprocess.run(command, check=True)
print(f"Separation completed. Output saved to {output_dir}")
except subprocess.CalledProcessError as e:
print(f"An error occurred while running Demucs: {e}")
print(f"An error occured while running demucs : {e}")
except FileNotFoundError:
print("Demucs is not installed or not found in the system PATH.")
print("Demucs is not installed or not found in the system PATH")
finally:
# Clean up the temporary file
os.remove(processed_audio)
# Cleanup the temporary file
os.remove(input_file)
if __name__ == "__main__":
# Example usage
input_audio_path = 'samples/pancakes_for_dinner.wav' # Path to your audio file
output_folder = './separated_output' # Where the separated files will be stored
separate_audio_with_demucs(input_audio_path, output_folder)