You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

2 years ago
  1. #!/usr/bin/env python
  2. # coding: utf8
  3. """ Unit testing for audio adapter. """
  4. __email__ = 'spleeter@deezer.com'
  5. __author__ = 'Deezer Research'
  6. __license__ = 'MIT License'
  7. from os.path import join
  8. from tempfile import TemporaryDirectory
  9. from spleeter import SpleeterError
  10. from spleeter.audio.adapter import AudioAdapter
  11. from spleeter.audio.ffmpeg import FFMPEGProcessAudioAdapter
  12. # pyright: reportMissingImports=false
  13. # pylint: disable=import-error
  14. from pytest import fixture, raises
  15. import numpy as np
  16. import ffmpeg
  17. # pylint: enable=import-error
  18. TEST_AUDIO_DESCRIPTOR = 'audio_example.mp3'
  19. TEST_OFFSET = 0
  20. TEST_DURATION = 600.
  21. TEST_SAMPLE_RATE = 44100
  22. @fixture(scope='session')
  23. def adapter():
  24. """ Target test audio adapter fixture. """
  25. return AudioAdapter.default()
  26. @fixture(scope='session')
  27. def audio_data(adapter):
  28. """ Audio data fixture based on sample loading from adapter. """
  29. return adapter.load(
  30. TEST_AUDIO_DESCRIPTOR,
  31. TEST_OFFSET,
  32. TEST_DURATION,
  33. TEST_SAMPLE_RATE)
  34. def test_default_adapter(adapter):
  35. """ Test adapter as default adapter. """
  36. assert isinstance(adapter, FFMPEGProcessAudioAdapter)
  37. assert adapter is AudioAdapter._DEFAULT
  38. def test_load(audio_data):
  39. """ Test audio loading. """
  40. waveform, sample_rate = audio_data
  41. assert sample_rate == TEST_SAMPLE_RATE
  42. assert waveform is not None
  43. assert waveform.dtype == np.dtype('float32')
  44. assert len(waveform.shape) == 2
  45. assert waveform.shape[0] == 479832
  46. assert waveform.shape[1] == 2
  47. def test_load_error(adapter):
  48. """ Test load ffprobe exception """
  49. with raises(SpleeterError):
  50. adapter.load(
  51. 'Paris City Jazz',
  52. TEST_OFFSET,
  53. TEST_DURATION,
  54. TEST_SAMPLE_RATE)
  55. def test_save(adapter, audio_data):
  56. """ Test audio saving. """
  57. with TemporaryDirectory() as directory:
  58. path = join(directory, 'ffmpeg-save.mp3')
  59. adapter.save(
  60. path,
  61. audio_data[0],
  62. audio_data[1])
  63. probe = ffmpeg.probe(TEST_AUDIO_DESCRIPTOR)
  64. assert len(probe['streams']) == 1
  65. stream = probe['streams'][0]
  66. assert stream['codec_type'] == 'audio'
  67. assert stream['channels'] == 2
  68. assert stream['duration'] == '10.919184'