Skip to content

Commit

Permalink
test: add unit test for audio adapter
Browse files Browse the repository at this point in the history
fix: import issues
  • Loading branch information
Félix Voituret committed Nov 8, 2019
1 parent c4d1343 commit d19c13b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion spleeter/audio/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tensorflow.contrib.signal import stft, hann_window
# pylint: enable=import-error

from ..logging import get_logger
from ..utils.logging import get_logger

__email__ = 'research@deezer.com'
__author__ = 'Deezer Research'
Expand Down
2 changes: 1 addition & 1 deletion spleeter/audio/convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tensorflow as tf
# pylint: enable=import-error

from ..tensor import from_float32_to_uint8, from_uint8_to_float32
from ..utils.tensor import from_float32_to_uint8, from_uint8_to_float32

__email__ = 'research@deezer.com'
__author__ = 'Deezer Research'
Expand Down
2 changes: 1 addition & 1 deletion spleeter/audio/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# pylint: enable=import-error

from .adapter import AudioAdapter
from ..logging import get_logger
from ..utils.logging import get_logger

__email__ = 'research@deezer.com'
__author__ = 'Deezer Research'
Expand Down
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python
# coding: utf8

""" TO DOCUMENT """
77 changes: 77 additions & 0 deletions tests/test_ffmpeg_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# coding: utf8

""" Unit testing for audio adapter. """

__email__ = 'research@deezer.com'
__author__ = 'Deezer Research'
__license__ = 'MIT License'

from os.path import join
from tempfile import TemporaryDirectory

# pylint: disable=import-error
from pytest import fixture

import numpy as np
import ffmpeg
# pylint: enable=import-error

from spleeter.audio.adapter import AudioAdapter
from spleeter.audio.adapter import get_default_audio_adapter
from spleeter.audio.adapter import get_audio_adapter
from spleeter.audio.ffmpeg import FFMPEGProcessAudioAdapter

TEST_AUDIO_DESCRIPTOR = 'audio_example.mp3'
TEST_OFFSET = 0
TEST_DURATION = 600.
TEST_SAMPLE_RATE = 44100


@fixture(scope='session')
def adapter():
""" Target test audio adapter fixture. """
return get_default_audio_adapter()


@fixture(scope='session')
def audio_data(adapter):
""" Audio data fixture based on sample loading from adapter. """
return adapter.load(
TEST_AUDIO_DESCRIPTOR,
TEST_OFFSET,
TEST_DURATION,
TEST_SAMPLE_RATE)


def test_default_adapter(adapter):
""" Test adapter as default adapter. """
assert isinstance(adapter, FFMPEGProcessAudioAdapter)
assert adapter is AudioAdapter.DEFAULT


def test_load(audio_data):
""" Test audio loading. """
waveform, sample_rate = audio_data
assert sample_rate == TEST_SAMPLE_RATE
assert waveform is not None
assert waveform.dtype == np.dtype('float32')
assert len(waveform.shape) == 2
assert waveform.shape[0] == 479832
assert waveform.shape[1] == 2


def test_save(adapter, audio_data):
""" Test audio saving. """
with TemporaryDirectory() as directory:
path = join(directory, 'ffmpeg-save.mp3')
adapter.save(
path,
audio_data[0],
audio_data[1])
probe = ffmpeg.probe(TEST_AUDIO_DESCRIPTOR)
assert len(probe['streams']) == 1
stream = probe['streams'][0]
assert stream['codec_type'] == 'audio'
assert stream['channels'] == 2
assert stream['duration'] == '10.919184'

0 comments on commit d19c13b

Please sign in to comment.