deezer / spleeter

Deezer source separation library including pretrained models.

Home Page:https://research.deezer.com/projects/spleeter.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Discussion] How to export audio via API 'RAW waveform based separation'

wei-z-git opened this issue · comments

I want to only save separated vocals into a specific directory, and I found 'File based separation' API cannot choose which part to save, so I decide use API 'RAW waveform based separation', I use AudioSegment.export to export the audio.

Problem

But the exported audio sounds very piercing, it seems the audio quality is damaged. I don`t know if my way to export audio is wrong🤔, please let me know if anyone have the same problem or you have any solution🙏🏻

main code below:

from pydub import AudioSegment
from spleeter.separator import Separator
if __name__ == '__main__':
    separator = Separator('spleeter:2stems')
    sample_rate = 44100
    audio_loader = AudioAdapter.default()
    waveform, _ = audio_loader.load(audio_file_path)
    # Perform the separation :
    prediction = separator.separate(waveform)
    # Save separated audio
    vocals = AudioSegment(
        prediction['vocals'].tobytes(),
        frame_rate=sample_rate,
        sample_width=prediction['vocals'].dtype.itemsize,
        channels=2  
    )
    vocals_output_path = os.path.join(output_directory, f'{filename}_vocals.wav')
    vocals.export(vocals_output_path, format='wav')

But if I use 'File based separation' API, the exported audio sounds normal

if __name__ == '__main__':
    separator = Separator('spleeter:2stems')
    output_directory = "output" 
    separator.separate_to_file(audio_file_path, output_directory)