# src/multivoice/lib/stt_separate_vocals.py
import logging
import multiprocessing
import os
[docs]
def separate_vocals(audio_file, temp_path, device):
"""
Separates vocals from the given audio file using the Demucs model and saves the result in the specified temporary path.
Parameters
----------
audio_file : str
The path to the input audio file from which vocals need to be separated.
temp_path : str
The directory where the separated vocal track will be saved.
device : str
The device (CPU or GPU) on which the separation process should run.
Returns
-------
str
The path to the separated vocal track if successful, otherwise the original audio file path.
Notes
-----
This function uses the `demucs.separate` command-line tool with a specific model (`htdemucs`) and extracts only the vocals.
If the separation fails (non-zero return code), it logs a warning and returns the original audio file path.
"""
logging.debug("Separating vocals from the rest of the audio...")
nprocs = multiprocessing.cpu_count() - 1
return_code = os.system(
f'python -m demucs.separate -n htdemucs --two-stems=vocals "{audio_file}" -o {temp_path} --device "{device}" --jobs {nprocs}'
)
if return_code != 0:
logging.warning(
"Source splitting failed, using original audio file. "
"Use --no-stem argument to disable it."
)
return audio_file
else:
return os.path.join(
temp_path,
"htdemucs",
os.path.splitext(os.path.basename(audio_file))[0],
"vocals.wav",
)