Skip to content

Instantly share code, notes, and snippets.

@jesseengel
Created June 12, 2019 19:25
Show Gist options
  • Save jesseengel/dedb8bc19212d4b982fb51f879728a0d to your computer and use it in GitHub Desktop.
Save jesseengel/dedb8bc19212d4b982fb51f879728a0d to your computer and use it in GitHub Desktop.
Wav2TFRecord
from cStringIO import StringIO
import os
import numpy as np
import scipy.io.wavfile
import tensorflow as tf
output_path = os.path.expanduser('~/Desktop/test.tfrecord')
sample_path = os.path.expanduser('~/Desktop/test.wav')
record_writer = tf.python_io.TFRecordWriter(output_path)
wav_contents = tf.gfile.Open(sample_path).read()
sample_rate, audio = scipy.io.wavfile.read(StringIO(wav_contents))
# Put in range [-1, 1]
float_normalizer = float(np.iinfo(np.int16).max)
audio = audio / float_normalizer
# Convert to mono
audio = np.mean(audio, axis=-1)
print(audio)
print(audio.shape)
example = tf.train.Example(features=tf.train.Features(feature={
'sample_rate': tf.train.Feature(
int64_list=tf.train.Int64List(value=[sample_rate])),
'audio': tf.train.Feature(float_list=tf.train.FloatList(value=audio.tolist())),
}))
record_writer.write(example.SerializeToString())
@jesseengel
Copy link
Author

jesseengel commented Jul 16, 2019 via email

@Faiza-K
Copy link

Faiza-K commented Jun 30, 2020

if we use BytesIO for this, will it work?? @jesseengel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment