def _test_fileobj(self, ext, subtype, bits_per_sample): """Query audio via file-like object works""" duration = 2 sample_rate = 16000 num_channels = 2 num_frames = sample_rate * duration path = self.get_temp_path(f'test.{ext}') data = torch.randn(num_frames, num_channels).numpy() soundfile.write(path, data, sample_rate, subtype=subtype) with open(path, 'rb') as fileobj: info = soundfile_backend.info(fileobj) assert info.sample_rate == sample_rate assert info.num_frames == num_frames assert info.num_channels == num_channels assert info.bits_per_sample == bits_per_sample assert info.encoding == "FLAC" if ext == 'flac' else "PCM_S"
def _test_tarobj(self, ext, subtype, bits_per_sample): """Query compressed audio via file-like object works""" duration = 2 sample_rate = 16000 num_channels = 2 num_frames = sample_rate * duration audio_file = f'test.{ext}' audio_path = self.get_temp_path(audio_file) archive_path = self.get_temp_path('archive.tar.gz') data = torch.randn(num_frames, num_channels).numpy() soundfile.write(audio_path, data, sample_rate, subtype=subtype) 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) info = soundfile_backend.info(fileobj) assert info.sample_rate == sample_rate assert info.num_frames == num_frames assert info.num_channels == num_channels assert info.bits_per_sample == bits_per_sample assert info.encoding == "FLAC" if ext == 'flac' else "PCM_S"
def test_unknown_subtype_warning(self): """soundfile_backend.info issues a warning when the subtype is unknown This will happen if a new subtype is supported in SoundFile: the _SUBTYPE_TO_BITS_PER_SAMPLE dict should be updated. """ def _mock_info_func(_): class MockSoundFileInfo: samplerate = 8000 frames = 356 channels = 2 subtype = 'UNSEEN_SUBTYPE' format = 'UNKNOWN' return MockSoundFileInfo() with patch("soundfile.info", _mock_info_func): with warnings.catch_warnings(record=True) as w: info = soundfile_backend.info("foo") assert len(w) == 1 assert "UNSEEN_SUBTYPE subtype is unknown to TorchAudio" in str( w[-1].message) assert info.bits_per_sample == 0