Exemple #1
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)
Exemple #2
0
 def test_htk(self):
     """`sox_io_backend.info` can check HTK file correctly"""
     duration = 1
     num_channels = 1
     sample_rate = 8000
     path = self.get_temp_path('data.htk')
     sox_utils.gen_audio_file(
         path, sample_rate=sample_rate, num_channels=num_channels,
         bit_depth=16, 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
     assert info.bits_per_sample == 16
     assert info.encoding == "PCM_S"
Exemple #3
0
 def test_gsm(self):
     """`sox_io_backend.info` can check gsm file correctly"""
     duration = 1
     num_channels = 1
     sample_rate = 8000
     path = self.get_temp_path('data.gsm')
     sox_utils.gen_audio_file(path,
                              sample_rate=sample_rate,
                              num_channels=num_channels,
                              duration=duration)
     info = sox_io_backend.info(path)
     assert info.sample_rate == sample_rate
     assert info.num_channels == num_channels
     assert info.bits_per_sample == 0
     assert info.encoding == "GSM"
Exemple #4
0
 def test_vorbis(self, sample_rate, num_channels, quality_level):
     """`sox_io_backend.info` can check vorbis file correctly"""
     duration = 1
     path = self.get_temp_path('data.vorbis')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=quality_level,
         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
Exemple #5
0
 def test_sphere(self, sample_rate, num_channels, bits_per_sample):
     """`sox_io_backend.info` can check sph file correctly"""
     duration = 1
     path = self.get_temp_path('data.sph')
     sox_utils.gen_audio_file(path,
                              sample_rate,
                              num_channels,
                              duration=duration,
                              bit_depth=bits_per_sample)
     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 == bits_per_sample
     assert info.encoding == "PCM_S"
Exemple #6
0
 def test_flac(self, sample_rate, num_channels, compression_level):
     """`sox_io_backend.info` can check flac file correctly"""
     duration = 1
     path = self.get_temp_path('data.flac')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=compression_level,
         duration=duration,
     )
     info = sox_io_backend.info(path)
     assert info.get_sample_rate() == sample_rate
     assert info.get_num_frames() == sample_rate * duration
     assert info.get_num_channels() == num_channels
Exemple #7
0
 def test_mp3(self, sample_rate, num_channels, bit_rate):
     """`sox_io_backend.info` can check mp3 file correctly"""
     duration = 1
     path = self.get_temp_path('data.mp3')
     sox_utils.gen_audio_file(
         path, sample_rate, num_channels,
         compression=bit_rate, duration=duration,
     )
     info = sox_io_backend.info(path)
     assert info.sample_rate == sample_rate
     # mp3 does not preserve the number of samples
     # assert info.num_frames == sample_rate * duration
     assert info.num_channels == num_channels
     assert info.bits_per_sample == 0  # bit_per_sample is irrelevant for compressed formats
     assert info.encoding == "MP3"
Exemple #8
0
 def test_mp3(self, sample_rate, num_channels, bit_rate):
     """`sox_io_backend.info` can check mp3 file correctly"""
     duration = 1
     path = self.get_temp_path('data.mp3')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=bit_rate,
         duration=duration,
     )
     info = sox_io_backend.info(path)
     assert info.get_sample_rate() == sample_rate
     # mp3 does not preserve the number of samples
     # assert info.get_num_frames() == sample_rate * duration
     assert info.get_num_channels() == num_channels
Exemple #9
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(f'{dtype}_{sample_rate}_{num_channels}.wav')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         bit_depth=sox_utils.get_bit_depth(dtype),
         encoding=sox_utils.get_encoding(dtype),
         duration=duration,
     )
     info = sox_io_backend.info(path)
     assert info.get_sample_rate() == sample_rate
     assert info.get_num_samples() == sample_rate * duration
     assert info.get_num_channels() == num_channels
Exemple #10
0
 def test_vorbis(self, sample_rate, num_channels, quality_level):
     """`sox_io_backend.info` can check vorbis file correctly"""
     duration = 1
     path = self.get_temp_path(
         f'{sample_rate}_{num_channels}_{quality_level}.vorbis')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=quality_level,
         duration=duration,
     )
     info = sox_io_backend.info(path)
     assert info.get_sample_rate() == sample_rate
     assert info.get_num_samples() == sample_rate * duration
     assert info.get_num_channels() == num_channels
Exemple #11
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')
     bits_per_sample = sox_utils.get_bit_depth(dtype)
     sox_utils.gen_audio_file(path,
                              sample_rate,
                              num_channels,
                              bit_depth=bits_per_sample,
                              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
     assert info.bits_per_sample == bits_per_sample
     assert info.encoding == get_encoding("amb", dtype)
Exemple #12
0
 def test_alaw(self):
     """`sox_io_backend.info` can check alaw file correctly"""
     duration = 1
     num_channels = 1
     sample_rate = 8000
     path = self.get_temp_path('data.wav')
     sox_utils.gen_audio_file(
         path, sample_rate=sample_rate, num_channels=num_channels,
         bit_depth=8, encoding='a-law',
         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
     assert info.bits_per_sample == 8
     assert info.encoding == "ALAW"
Exemple #13
0
    def test_mp3(self):
        """Providing `format` allows to read mp3 without extension

        libsox does not check header for mp3

        https://github.com/pytorch/audio/issues/1040

        The file was generated with the following command
            ffmpeg -f lavfi -i "sine=frequency=1000:duration=5" -ar 16000 -f mp3 test_noext
        """
        path = get_asset_path("mp3_without_ext")
        sinfo = sox_io_backend.info(path, format="mp3")
        assert sinfo.sample_rate == 16000
        assert sinfo.num_frames == 81216
        assert sinfo.num_channels == 1
        assert sinfo.bits_per_sample == 0  # bit_per_sample is irrelevant for compressed formats
        assert sinfo.encoding == "MP3"
Exemple #14
0
 def test_vorbis(self, sample_rate, num_channels, quality_level):
     """`sox_io_backend.info` can check vorbis file correctly"""
     duration = 1
     path = self.get_temp_path('data.vorbis')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=quality_level,
         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
     assert info.bits_per_sample == 0  # bit_per_sample is irrelevant for compressed formats
     assert info.encoding == "VORBIS"
Exemple #15
0
 def test_flac(self, sample_rate, num_channels, compression_level):
     """`sox_io_backend.info` can check flac file correctly"""
     duration = 1
     path = self.get_temp_path('data.flac')
     sox_utils.gen_audio_file(
         path,
         sample_rate,
         num_channels,
         compression=compression_level,
         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
     assert info.bits_per_sample == 24  # FLAC standard
     assert info.encoding == "FLAC"
Exemple #16
0
 def _query_fileobj(self,
                    ext,
                    dtype,
                    sample_rate,
                    num_channels,
                    num_frames,
                    *,
                    comments=None):
     path = self._gen_file(ext,
                           dtype,
                           sample_rate,
                           num_channels,
                           num_frames,
                           comments=comments)
     format_ = ext if ext in ['mp3'] else None
     with open(path, 'rb') as fileobj:
         return sox_io_backend.info(fileobj, format_)
Exemple #17
0
def py_info_func(filepath: str) -> torch.classes.torchaudio.SignalInfo:
    return sox_io_backend.info(filepath)
Exemple #18
0
 def _query_bytesio(self, ext, dtype, sample_rate, num_channels, num_frames):
     path = self._gen_file(ext, dtype, sample_rate, num_channels, num_frames)
     format_ = ext if ext in ['mp3'] else None
     with open(path, 'rb') as file_:
         fileobj = io.BytesIO(file_.read())
     return sox_io_backend.info(fileobj, format_)