def test_file_object(self): np_audio = self.create_np_audio() _, tmp_file = tempfile.mkstemp() self.write_wave_file(np_audio, tmp_file) class GFileWithSeekOnRead(tf.io.gfile.GFile): """Wrapper around GFile which is reusable across multiple read() calls. This is needed because assertFeature reuses the same FeatureExpectationItem several times. """ def read(self, *args, **kwargs): data_read = super(GFileWithSeekOnRead, self).read(*args, **kwargs) self.seek(0) return data_read with GFileWithSeekOnRead(tmp_file, "rb") as file_obj: self.assertFeature( feature=features.Audio(file_format="wav"), shape=(None,), dtype=tf.int64, tests=[ testing.FeatureExpectationItem( value=file_obj, expected=np_audio, ), ], )
def expectations(self): np_audio = np.random.randint(-2**10, 2**10, size=(10, ), dtype=np.int64) audio = pydub.AudioSegment.empty().set_sample_width(2) # See documentation for _spawn usage: # https://github.com/jiaaro/pydub/blob/master/API.markdown#audiosegmentget_array_of_samples audio = audio._spawn(array.array(audio.array_type, np_audio)) _, tmp_file = tempfile.mkstemp() audio.export(tmp_file, format="wav") return [ # Numpy array test_utils.FeatureExpectation( name="audio_np", feature=features.Audio(), shape=(None, ), dtype=tf.int64, tests=[ test_utils.FeatureExpectationItem( value=np_audio, expected=np_audio, ), ], ), # WAV file test_utils.FeatureExpectation( name="audio_np", feature=features.Audio(file_format="wav"), shape=(None, ), dtype=tf.int64, tests=[ test_utils.FeatureExpectationItem( value=tmp_file, expected=np_audio, ), ], ), ]
def test_numpy_array_float(self): np_audio = self.create_np_audio().astype(np.float32) self.assertFeature( feature=features.Audio(dtype=tf.float32), shape=(None,), dtype=tf.float32, tests=[ testing.FeatureExpectationItem( value=np_audio, expected=np_audio, ), ], )
def test_numpy_array(self): np_audio = self.create_np_audio() self.assertFeature( feature=features.Audio(), shape=(None,), dtype=tf.int64, tests=[ testing.FeatureExpectationItem( value=np_audio, expected=np_audio, ), ], )
def test_numpy_array_float(self, num_channels): np_audio = _create_np_audio(num_channels).astype(np.float32) self.assertFeature( feature=features.Audio(dtype=tf.float32, shape=_shape_for_channels(num_channels)), shape=_shape_for_channels(num_channels), dtype=tf.float32, tests=[ testing.FeatureExpectationItem( value=np_audio, expected=np_audio, ), ], )
def test_numpy_array(self): np_audio = self.create_np_audio() self.assertFeature(feature=features.Audio(sample_rate=1000), shape=(None, ), dtype=tf.int64, tests=[ testing.FeatureExpectationItem( value=np_audio, expected=np_audio, ), ], test_attributes=dict( _file_format=None, sample_rate=1000, ))
def test_wav_file(self): np_audio = self.create_np_audio() _, tmp_file = tempfile.mkstemp() self.write_wave_file(np_audio, tmp_file) self.assertFeature(feature=features.Audio(file_format="wav"), shape=(None, ), dtype=tf.int64, tests=[ testing.FeatureExpectationItem( value=tmp_file, expected=np_audio, ), ], test_attributes=dict(_file_format="wav", ))
def test_wav_file(self, num_channels): np_audio = _create_np_audio(num_channels) _, tmp_file = tempfile.mkstemp() _write_wave_file(np_audio, tmp_file) self.assertFeature(feature=features.Audio( file_format='wav', shape=_shape_for_channels(num_channels)), shape=_shape_for_channels(num_channels), dtype=tf.int64, tests=[ testing.FeatureExpectationItem( value=tmp_file, expected=np_audio, ), testing.FeatureExpectationItem( value=pathlib.Path(tmp_file), expected=np_audio, ), ], test_attributes=dict(_file_format='wav', ))
def test_sample_rate_property(self): self.assertEqual(features.Audio(sample_rate=1600).sample_rate, 1600)