Files
spleeter/tests/test_separator.py

98 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# coding: utf8
""" Unit testing for Separator class. """
__email__ = 'research@deezer.com'
__author__ = 'Deezer Research'
__license__ = 'MIT License'
import filecmp
import itertools
2019-11-23 15:42:40 -08:00
from os.path import splitext, basename, exists, join
from tempfile import TemporaryDirectory
2019-11-08 20:03:16 -05:00
import pytest
import numpy as np
2019-11-08 20:03:16 -05:00
2019-11-20 15:18:53 +01:00
from spleeter import SpleeterError
from spleeter.audio.adapter import get_default_audio_adapter
from spleeter.separator import Separator
TEST_AUDIO_DESCRIPTORS = ['audio_example.mp3', 'audio_example_mono.mp3']
BACKENDS = ["tensorflow", "librosa"]
MODELS = ['spleeter:2stems', 'spleeter:4stems', 'spleeter:5stems']
MODEL_TO_INST = {
'spleeter:2stems': ('vocals', 'accompaniment'),
'spleeter:4stems': ('vocals', 'drums', 'bass', 'other'),
'spleeter:5stems': ('vocals', 'drums', 'bass', 'piano', 'other'),
}
TEST_CONFIGURATIONS = list(itertools.product(TEST_AUDIO_DESCRIPTORS, MODELS, BACKENDS))
2020-03-26 14:27:19 +01:00
@pytest.mark.parametrize('test_file, configuration, backend', TEST_CONFIGURATIONS)
def test_separate(test_file, configuration, backend):
""" Test separation from raw data. """
2020-03-26 14:27:19 +01:00
instruments = MODEL_TO_INST[configuration]
adapter = get_default_audio_adapter()
waveform, _ = adapter.load(test_file)
separator = Separator(configuration, stft_backend=backend)
prediction = separator.separate(waveform, test_file)
2019-11-08 20:21:37 -05:00
assert len(prediction) == len(instruments)
2019-11-08 20:03:16 -05:00
for instrument in instruments:
assert instrument in prediction
for instrument in instruments:
track = prediction[instrument]
assert waveform.shape == track.shape
assert not np.allclose(waveform, track)
for compared in instruments:
if instrument != compared:
assert not np.allclose(track, prediction[compared])
2020-03-26 14:27:19 +01:00
@pytest.mark.parametrize('test_file, configuration, backend', TEST_CONFIGURATIONS)
def test_separate_to_file(test_file, configuration, backend):
""" Test file based separation. """
2020-03-26 14:27:19 +01:00
instruments = MODEL_TO_INST[configuration]
separator = Separator(configuration, stft_backend=backend)
basename = splitext(basename(test_file))
2019-11-08 20:03:16 -05:00
with TemporaryDirectory() as directory:
separator.separate_to_file(
test_file,
2019-11-08 20:03:16 -05:00
directory)
for instrument in instruments:
2019-11-20 15:18:53 +01:00
assert exists(join(
directory,
'{}/{}.wav'.format(basename, instrument)))
2019-11-20 15:18:53 +01:00
2020-03-26 14:27:19 +01:00
@pytest.mark.parametrize('test_file, configuration, backend', TEST_CONFIGURATIONS)
def test_filename_format(test_file, configuration, backend):
2019-11-20 15:18:53 +01:00
""" Test custom filename format. """
2020-03-26 14:27:19 +01:00
instruments = MODEL_TO_INST[configuration]
separator = Separator(configuration, stft_backend=backend)
basename = splitext(basename(test_file))
2019-11-20 15:18:53 +01:00
with TemporaryDirectory() as directory:
separator.separate_to_file(
test_file,
2019-11-20 15:18:53 +01:00
directory,
filename_format='export/{filename}/{instrument}.{codec}')
for instrument in instruments:
assert exists(join(
directory,
'export/{}/{}.wav'.format(basename, instrument)))
2019-11-20 15:18:53 +01:00
2020-03-26 14:27:19 +01:00
@pytest.mark.parametrize('test_file', TEST_AUDIO_DESCRIPTORS)
def test_filename_conflict(test_file):
2019-11-20 15:18:53 +01:00
""" Test error handling with static pattern. """
separator = Separator(TEST_CONFIGURATIONS[0][0])
with TemporaryDirectory() as directory:
with pytest.raises(SpleeterError):
separator.separate_to_file(
test_file,
2019-11-20 15:18:53 +01:00
directory,
filename_format='I wanna be your lover')