Beispiel #1
0
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)
	'''
Beispiel #2
0
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))
                 })
Beispiel #3
0
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
Beispiel #6
0
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))
                 })
Beispiel #7
0
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))
        })
Beispiel #8
0
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))
                 })
Beispiel #9
0
 def _getWavData(self):
     with self.cached_session() as sess:
         sample_data = tf.zeros([32000, 2])
         wav_encoder = contrib_audio.encode_wav(sample_data, 16000)
         wav_data = self.evaluate(wav_encoder)
     return wav_data
 def _getWavData(self):
     with self.test_session() as sess:
         sample_data = tf.zeros([1000, 2])
         wav_encoder = contrib_audio.encode_wav(sample_data, 16000)
         wav_data = sess.run(wav_encoder)
     return wav_data
 def _getWavData(self):
   with self.cached_session() as sess:
     sample_data = tf.zeros([32000, 2])
     wav_encoder = contrib_audio.encode_wav(sample_data, 16000)
     wav_data = self.evaluate(wav_encoder)
   return wav_data
 def _getWavData(self):
   with self.test_session() as sess:
     sample_data = tf.zeros([1000, 2])
     wav_encoder = contrib_audio.encode_wav(sample_data, 16000)
     wav_data = sess.run(wav_encoder)
   return wav_data