-
-
Save ufcolemanlab/cbf215d7119e43aad9516e3e80236b8c to your computer and use it in GitHub Desktop.
CSV to WAV: Needed a way to convert a list of numbers in a CSV file to a wave audio file. Go python.
This file contains hidden or 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
#!/usr/bin/python | |
import wave | |
import numpy | |
import struct | |
import sys | |
import csv | |
from scikits.samplerate import resample | |
def write_wav(data, filename, framerate, amplitude): | |
wavfile = wave.open(filename, "w") | |
nchannels = 1 | |
sampwidth = 2 | |
framerate = framerate | |
nframes = len(data) | |
comptype = "NONE" | |
compname = "not compressed" | |
wavfile.setparams((nchannels, | |
sampwidth, | |
framerate, | |
nframes, | |
comptype, | |
compname)) | |
print("Please be patient whilst the file is written") | |
frames = [] | |
for s in data: | |
mul = int(s * amplitude) | |
# print "s: %f mul: %d" % (s, mul) | |
frames.append(struct.pack('h', mul)) | |
# frames = (struct.pack('h', int(s*self.amp)) for s in sine_list) | |
frames = ''.join(frames) | |
for x in xrange(0, 7200): | |
wavfile.writeframes(frames) | |
wavfile.close() | |
print("%s written" %(filename)) | |
if __name__ == "__main__": | |
if len(sys.argv) <= 1: | |
print "You must supply a filename to generate" | |
exit(-1) | |
for fname in sys.argv[1:]: | |
data = [] | |
for time, value in csv.reader(open(fname, 'U'), delimiter=','): | |
try: | |
data.append(float(value)) | |
except ValueError: | |
pass # Just skip it | |
print "Generating wave file from %d samples" % (len(data),) | |
arr = numpy.array(data) | |
# Normalize data | |
arr /= numpy.max(numpy.abs(data)) | |
filename_head, extension = fname.rsplit(".", 1) | |
# Resample normalized data to 44.1 kHz | |
target_samplerate = 44100 | |
sampled = resample(arr, target_samplerate/100000.0, 'sinc_best') | |
write_wav(sampled, filename_head + ".wav", 100000, 32700) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment