Skip to content

Commit

Permalink
Create wavlibraryexample.py
Browse files Browse the repository at this point in the history
Re-implementations of the files to read/write wav data using the wave library instead of scipy. Used here to illustrate why you probably want to use the scipy library (it's much simpler).
  • Loading branch information
mchua committed Mar 18, 2013
1 parent 2ba9731 commit 17b9a37
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions wavlibraryexample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Using http://docs.python.org/2/library/wave.html
# aka "why we use scipy"
# an alternative implementation of the functions getwavdata and makewav
# as defined in sigproc-outline.py

import wave
import struct

# performs the same function as getwavdata
# using the wave library instead of scipy
def wgetwavdata(file):
# open a file for writing
infile = wave.open(file, 'r')
length = infile.getnframes()
data = []
for i in range(length):
nextframe = infile.readframes(1)
# this returns C-style little-endian shorts, so we convert...
data += struct.unpack("<h", nextframe)
return data

# performs ALMOST the same function as makewav
# using the wave library instead of scipy
# note that the 3rd argument is params rather than samplerate
# and that the 3rd element of params is the sample rate
# (see notes below)
def wmakewav(data, outfile, params=(1,2,44100, 0, 'NONE', 'not compressed')):
wavfile = wave.open(outfile, 'w')
wavfile.setparams(params)
# convert back to C-style little-endian shorts...
converted = [struct.pack("<h", i) for i in data]
outdata = ''
for i in converted:
outdata += i[0]
outdata += i[1]
wavfile.writeframes(outdata)
wavfile.close()

# Regarding params:
# arguments are (nchannels, sampwidth, framerate, nframes, comptype, compname)
# nchannels is 1 for stereo and 2 for mono
# sampwidth is the number of bytes per sample, usually 1 or 2
# framerate is the sample rate, 44100 is common for CDs
# nframes is the number of frames
# comptype is compression type
# compname is the human-readable name for comptype

0 comments on commit 17b9a37

Please sign in to comment.