示例#1
0
 def assert_sphere(
     self,
     dtype,
     sample_rate,
     num_channels,
     channels_first=True,
     duration=1,
 ):
     """`soundfile_backend.load` can load SPHERE format correctly."""
     path = self.get_temp_path("reference.sph")
     num_frames = duration * sample_rate
     raw = get_wav_data(
         dtype,
         num_channels,
         num_frames=num_frames,
         normalize=False,
         channels_first=False,
     )
     soundfile.write(path,
                     raw,
                     sample_rate,
                     subtype=dtype2subtype(dtype),
                     format="NIST")
     expected = normalize_wav(raw.t() if channels_first else raw)
     data, sr = soundfile_backend.load(path, channels_first=channels_first)
     assert sr == sample_rate
     self.assertEqual(data, expected, atol=1e-4, rtol=1e-8)
示例#2
0
    def assert_wav(
        self,
        dtype,
        sample_rate,
        num_channels,
        normalize,
        channels_first=True,
        duration=1,
    ):
        """`soundfile_backend.load` can load wav format correctly.

        Wav data loaded with soundfile backend should match those with scipy
        """
        path = self.get_temp_path("reference.wav")
        num_frames = duration * sample_rate
        data = get_wav_data(
            dtype,
            num_channels,
            normalize=normalize,
            num_frames=num_frames,
            channels_first=channels_first,
        )
        save_wav(path, data, sample_rate, channels_first=channels_first)
        expected = load_wav(path,
                            normalize=normalize,
                            channels_first=channels_first)[0]
        data, sr = soundfile_backend.load(path,
                                          normalize=normalize,
                                          channels_first=channels_first)
        assert sr == sample_rate
        self.assertEqual(data, expected)
示例#3
0
    def _test_fileobj(self, ext):
        """Loading audio via file-like object works"""
        sample_rate = 16000
        path = self.get_temp_path(f'test.{ext}')

        data = get_wav_data('float32', num_channels=2).numpy().T
        soundfile.write(path, data, sample_rate)
        expected = soundfile.read(path, dtype='float32')[0].T

        with open(path, 'rb') as fileobj:
            found, sr = soundfile_backend.load(fileobj)
        assert sr == sample_rate
        self.assertEqual(expected, found)
示例#4
0
 def assert_dtype(self, ext, dtype, sample_rate, num_channels, normalize,
                  channels_first):
     """When format is WAV or NIST, normalize=False will return the native dtype Tensor, otherwise float32"""
     num_frames = 3 * sample_rate
     path = _get_mock_path(ext, dtype, sample_rate, num_channels,
                           num_frames)
     expected_dtype = (torch.float32 if normalize
                       or ext not in ["wav", "nist"] else getattr(
                           torch, dtype))
     with patch("soundfile.SoundFile", SoundFileMock):
         found, sr = soundfile_backend.load(path,
                                            normalize=normalize,
                                            channels_first=channels_first)
         assert found.dtype == expected_dtype
         assert sample_rate == sr
示例#5
0
    def _test_tarfile(self, ext):
        """Loading audio via file-like object works"""
        sample_rate = 16000
        audio_file = f'test.{ext}'
        audio_path = self.get_temp_path(audio_file)
        archive_path = self.get_temp_path('archive.tar.gz')

        data = get_wav_data('float32', num_channels=2).numpy().T
        soundfile.write(audio_path, data, sample_rate)
        expected = soundfile.read(audio_path, dtype='float32')[0].T

        with tarfile.TarFile(archive_path, 'w') as tarobj:
            tarobj.add(audio_path, arcname=audio_file)
        with tarfile.TarFile(archive_path, 'r') as tarobj:
            fileobj = tarobj.extractfile(audio_file)
            found, sr = soundfile_backend.load(fileobj)

        assert sr == sample_rate
        self.assertEqual(expected, found)
示例#6
0
 def _test_format(self, format_):
     """Providing format allows to read file without extension"""
     path, expected = self._make_file(format_)
     found, _ = soundfile_backend.load(path)
     self.assertEqual(found, expected)