Reading/writing .wav data
Anton Vredegoor
anton at vredegoor.doge.nl
Mon Dec 30 11:55:26 EST 2002
On Mon, 30 Dec 2002 09:34:27 GMT, sj <jones57 at swbell.net> wrote:
>I have figured out how to use wave.open to get an instance of wave.Wave_read
>and then use wave.readframes(n) to read the first n data frames. The
>problem is I don't know how to convert the string returned by readframes to
>something numerically usefull. I want to convert the string to either a
>numeric array or list. The test file I have been using is a simple 1
>second sine with sr=44100 and 2 bytes per frame.
Module Struct can be used to unpack a string of bytes, see some code
below.
Regards,
Anton.
import wave
import struct
def test():
w = wave.open('sound.wav','r')
nf = w.getnframes()
sw = w.getsampwidth()
assert(sw==2)
rf = w.readframes(nf)
w.close()
data = struct.unpack("%sh" %nf,rf)
for i in range(2000):
print i,data[i]
if __name__=='__main__':
test()
More information about the Python-list
mailing list