def save_wav_file(filename, wav_data, sample_rate): """Saves audio sample data to a .wav audio file. Args: filename: Path to save the file to. wav_data: 2D array of float PCM-encoded audio data. sample_rate: Samples per second to encode in the file. """ with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) ''' encode_wav: Encode audio data using the WAV file format. This operation will generate a string suitable to be saved out to create a .wav audio file. It will be encoded in the 16-bit PCM format. It takes in float values in the range -1.0f to 1.0f, and any outside that value will be clamped to that range. ''' wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) '''
def _save_op(self, iterator_resource): iterator_state_variant = gen_dataset_ops.serialize_iterator( iterator_resource) save_op = io_ops.write_file( self._iterator_checkpoint_prefix_local(), parsing_ops.serialize_tensor(iterator_state_variant)) return save_op
def testWriteFileCreateDir(self): cases = ['', 'Some contents'] for contents in cases: contents = compat.as_bytes(contents) subdir = os.path.join(self.get_temp_dir(), 'subdir1') filepath = os.path.join(subdir, 'subdir2', 'filename') with self.cached_session() as sess: w = io_ops.write_file(filepath, contents) self.evaluate(w) with open(filepath, 'rb') as f: file_contents = f.read() self.assertEqual(file_contents, contents) shutil.rmtree(subdir)
def save_wav_file(filename, wav_data, sample_rate): with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) sess.run(wav_saver, feed_dict={ wav_filename_placeholder: filename, sample_rate_placeholder: sample_rate, wav_data_placeholder: np.reshape(wav_data, (-1, 1)) })
def testWriteFile(self): cases = ['', 'Some contents'] for contents in cases: contents = compat.as_bytes(contents) with tempfile.NamedTemporaryFile( prefix='WriteFileTest', dir=self.get_temp_dir(), delete=False) as temp: pass with self.cached_session() as sess: w = io_ops.write_file(temp.name, contents) self.evaluate(w) with open(temp.name, 'rb') as f: file_contents = f.read() self.assertEqual(file_contents, contents) os.remove(temp.name)
def save_wav_file(filename, wav_data, sample_rate): """Saves audio sample data to a .wav audio file.""" with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) sess.run( wav_saver, feed_dict={ wav_filename_placeholder: filename, sample_rate_placeholder: sample_rate, wav_data_placeholder: np.reshape(wav_data, (-1, 1)) })
def save_my_test_file(self, model_settings): desired_samples = model_settings['desired_samples'] self.wav_filename_placeholder_ = tf.placeholder(tf.string, []) wav_loader = io_ops.read_file(self.wav_filename_placeholder_) wav_decoder = contrib_audio.decode_wav( # wav_loader, desired_channels=1, desired_samples=desired_samples) wav_loader, desired_channels=1, desired_samples=16000) # Allow the audio sample's volume to be adjusted. self.foreground_volume_placeholder_ = tf.placeholder(tf.float32, []) scaled_foreground = tf.multiply(wav_decoder.audio, self.foreground_volume_placeholder_) # Shift the sample's start position, and pad any gaps with zeros. self.time_shift_padding_placeholder_ = tf.placeholder(tf.int32, [2, 2]) self.time_shift_offset_placeholder_ = tf.placeholder(tf.int32, [2]) padded_foreground = tf.pad( scaled_foreground, self.time_shift_padding_placeholder_, mode='CONSTANT') sliced_foreground = tf.slice(padded_foreground, self.time_shift_offset_placeholder_, [desired_samples, -1]) # Mix in background noise. self.background_data_placeholder_ = tf.placeholder(tf.float32, [desired_samples, 1]) self.background_volume_placeholder_ = tf.placeholder(tf.float32, []) background_mul = tf.multiply(self.background_data_placeholder_, self.background_volume_placeholder_) background_add = tf.add(background_mul, sliced_foreground) background_clamp = tf.clip_by_value(background_add, -1.0, 1.0) self.filename_ = tf.placeholder(tf.string) # save_wav_file(self.filename_, np.array(background_clamp), 16000) wav_encoder = contrib_audio.encode_wav(background_clamp, 16000) self.wav_saver = io_ops.write_file(self.filename_, wav_encoder) # with tf.Session(graph=tf.Graph()) as sess: # sess.run(wav_saver) spectrogram = contrib_audio.audio_spectrogram( background_clamp, window_size=model_settings['window_size_samples'], stride=model_settings['window_stride_samples'], magnitude_squared=True) self.test_mfcc_ = contrib_audio.mfcc( spectrogram, wav_decoder.sample_rate, dct_coefficient_count=model_settings['dct_coefficient_count'])
def save_wav(filepath, wav_data, sample_rate=16000): """Defines a computational graph that saves an array of floats to a .wav file Parameters ---------- filepath : a tensor of dtype tf.string and shape (1,) representing the path of the saved wav file. wav_data : a tensor of dtype tf.float32 and shape (None, 1) representing the data to be encoded as a .wav file. sample_rate : a tensor of type tf.int32 and shape (1,) representing the desired sample rate at which to encode the data. Returns ------- wav_saver : a tensorflow operation that writes the encoded data to a file. """ wav_encoder = contrib_audio.encode_wav(data, sample_rate) wav_saver = io_ops.write_file(filename, wav_encoder) return wav_saver
def testReadWrite(self): with self.test_session() as sess: contents = "ASDASDASDASDASDAS" filename = "iptf://repo/root/foo" meta_filename = "iptf://meta/repo/root/foo" wf = io_ops.write_file(filename=constant_op.constant(filename), contents=constant_op.constant(contents)) reader = io_ops.WholeFileReader("test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) queue.enqueue_many([[filename]]).run() queue.close().run() with sess.graph.control_dependencies([wf]): key, value = sess.run(reader.read(queue)) self.assertEqual(key, compat.as_bytes(filename)) self.assertEqual(value, compat.as_bytes(contents)) queue2 = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) queue2.enqueue_many([[meta_filename]]).run() queue2.close().run() key, value = sess.run(reader.read(queue2)) d = json.loads(compat.as_str(value)) ipfs_path = d["IpfsPath"] queue3 = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) queue3.enqueue_many([[ipfs_path]]).run() queue3.close().run() with sess.graph.control_dependencies([wf]): key, value = sess.run(reader.read(queue3)) self.assertEqual(key, compat.as_bytes(ipfs_path)) self.assertEqual(value, compat.as_bytes(contents)) with gfile.Open(meta_filename, "wb") as f: f.write(compat.as_bytes('{"command": "publish"}')) ipns_path = d["IpnsPath"] queue4 = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) queue4.enqueue_many([[ipns_path]]).run() queue4.close().run() with sess.graph.control_dependencies([wf]): key, value = sess.run(reader.read(queue4)) self.assertEqual(key, compat.as_bytes(ipns_path)) self.assertEqual(value, compat.as_bytes(contents))
def save_wav_file(filename, wav_data, sample_rate): """Saves audio sample data to a .wav audio file. Args: filename: Path to save the file to. wav_data: 2D array of float PCM-encoded audio data. sample_rate: Samples per second to encode in the file. """ with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) sess.run(wav_saver, feed_dict={ wav_filename_placeholder: filename, sample_rate_placeholder: sample_rate, wav_data_placeholder: np.reshape(wav_data, (-1, 1)) })
def save_wav_file(filename, wav_data, sample_rate): """Saves audio sample data to a .wav audio file. Args: filename: Path to save the file to. wav_data: 2D array of float PCM-encoded audio data. sample_rate: Samples per second to encode in the file. """ with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) sess.run( wav_saver, feed_dict={ wav_filename_placeholder: filename, sample_rate_placeholder: sample_rate, wav_data_placeholder: np.reshape(wav_data, (-1, 1)) })
def save_wav_file(filename, wav_data, sample_rate): """ save audio sample data to a .wav file input: filename - file path wav_data - 2d array of audio data sample_rate - samples per second """ with tf.Session(graph=tf.Graph()) as sess: wav_filename_placeholder = tf.placeholder(tf.string, []) sample_rate_placeholder = tf.placeholder(tf.int32, []) wav_data_placeholder = tf.placeholder(tf.float32, [None, 1]) wav_encoder = contrib_audio.encode_wav(wav_data_placeholder, sample_rate_placeholder) wav_saver = io_ops.write_file(wav_filename_placeholder, wav_encoder) sess.run(wav_saver, feed_dict={ wav_filename_placeholder: filename, sample_rate_placeholder: sample_rate, wav_data_placeholder: np.reshape(wav_data, (-1, 1)) })
def _save_op(iterator_resource): iterator_state_variant = gen_dataset_ops.serialize_iterator( iterator_resource) save_op = io_ops.write_file( _path(), parsing_ops.serialize_tensor(iterator_state_variant)) return save_op