示例#1
0
    def assert_amb(self, dtype, sample_rate, num_channels, normalize,
                   duration):
        """`sox_io_backend.load` can load amb format.

        This test takes the same strategy as mp3 to compare the result
        """
        path = self.get_temp_path('1.original.amb')
        ref_path = self.get_temp_path('2.reference.wav')

        # 1. Generate amb with sox
        sox_utils.gen_audio_file(path,
                                 sample_rate,
                                 num_channels,
                                 encoding=sox_utils.get_encoding(dtype),
                                 bit_depth=sox_utils.get_bit_depth(dtype),
                                 duration=duration)
        # 2. Convert to wav with sox
        sox_utils.convert_audio_file(path, ref_path)
        # 3. Load amb with torchaudio
        data, sr = sox_io_backend.load(path, normalize=normalize)
        # 4. Load wav with scipy
        data_ref = load_wav(ref_path, normalize=normalize)[0]
        # 5. Compare
        assert sr == sample_rate
        self.assertEqual(data, data_ref, atol=4e-05, rtol=1.3e-06)
示例#2
0
文件: common.py 项目: zkneupper/audio
def get_bits_per_sample(ext, dtype):
    bits_per_samples = {
        'flac': 24,
        'mp3': 0,
        'vorbis': 0,
    }
    return bits_per_samples.get(ext, sox_utils.get_bit_depth(dtype))
示例#3
0
 def test_amb(self, dtype, sample_rate, num_channels, normalize):
     """`sox_io_backend.load` can load amb format correctly."""
     bit_depth = sox_utils.get_bit_depth(dtype)
     encoding = sox_utils.get_encoding(dtype)
     self.assert_format("amb",
                        sample_rate,
                        num_channels,
                        bit_depth=bit_depth,
                        duration=1,
                        encoding=encoding,
                        normalize=normalize)
示例#4
0
    def _gen_file(self, ext, dtype, sample_rate, num_channels, num_frames):
        path = self.get_temp_path(f'test.{ext}')
        bit_depth = sox_utils.get_bit_depth(dtype)
        duration = num_frames / sample_rate

        sox_utils.gen_audio_file(
            path, sample_rate, num_channels=num_channels,
            encoding=sox_utils.get_encoding(dtype),
            bit_depth=bit_depth,
            duration=duration)
        return path
示例#5
0
 def test_wav_multiple_channels(self, dtype, sample_rate, num_channels):
     """`sox_io_backend.info` can check wav file with channels more than 2 correctly"""
     duration = 1
     path = self.get_temp_path('data.wav')
     data = get_wav_data(dtype, num_channels, normalize=False, num_frames=duration * sample_rate)
     save_wav(path, data, sample_rate)
     info = sox_io_backend.info(path)
     assert info.sample_rate == sample_rate
     assert info.num_frames == sample_rate * duration
     assert info.num_channels == num_channels
     assert info.bits_per_sample == sox_utils.get_bit_depth(dtype)
     assert info.encoding == get_encoding('wav', dtype)
示例#6
0
 def test_amb(self, dtype, sample_rate, num_channels):
     """`sox_io_backend.info` can check amb file correctly"""
     duration = 1
     path = self.get_temp_path('data.amb')
     sox_utils.gen_audio_file(path,
                              sample_rate,
                              num_channels,
                              bit_depth=sox_utils.get_bit_depth(dtype),
                              duration=duration)
     info = sox_io_backend.info(path)
     assert info.sample_rate == sample_rate
     assert info.num_frames == sample_rate * duration
     assert info.num_channels == num_channels
示例#7
0
    def _gen_file(self,
                  ext,
                  dtype,
                  sample_rate,
                  num_channels,
                  num_frames,
                  *,
                  comments=None):
        path = self.get_temp_path(f'test.{ext}')
        bit_depth = sox_utils.get_bit_depth(dtype)
        duration = num_frames / sample_rate
        comment_file = self._gen_comment_file(comments) if comments else None

        sox_utils.gen_audio_file(
            path,
            sample_rate,
            num_channels=num_channels,
            encoding=sox_utils.get_encoding(dtype),
            bit_depth=bit_depth,
            duration=duration,
            comment_file=comment_file,
        )
        return path