-
-
Save fokx/1eb6df1f50dbfa43574f1e87c3ca0f8f to your computer and use it in GitHub Desktop.
Wav2TFRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment