Handling 16-bit .WAV audio files

N

nigel

I'm trying to handle 16-bit .WAV format audio files. I've started
with the code below (from pythonapocrypha.com). I can successfully
read in the 16-bit code, using Numeric.fromstring, as they suggest in
the comments below.

However, I can't manage to write the samples, I always seem to get 32
bits per sample (yes, it is set to mono!).

It seems that I need to use Numeric.tostring, with an option to set it
to 'Numeric.Int16', but that is only available in Numeric.fromstring?

Do I need to convert the array somehow to a 16 bit signed integer
type?

What should I be doing to convert my final array to a suitable string
of 16-bit values?

Nigel


########################################
import Numeric
import wave

BUFFER_SIZE=5000
# NB: This is an 8-bit stereo .wav file. If it had a different
# sample size, such as 16-bits, we would need to convert the
# sequence of bytes into an array of 16-bit integers by
# calling Numeric.fromstring(Data,Numeric.Int16)
InFile=wave.open("LoudLeft.wav","rb")
OutFile=wave.open("QuietLeft.wav","wb")
OutFile.setparams(InFile.getparams())
while 1:
# Read audio data as a string of bytes:
Data=InFile.readframes(BUFFER_SIZE)
if len(Data)==0:
break
# Create an array based on the string:
Frames=Numeric.array(Data,
typecode=Numeric.UnsignedInt8,savespace=1)
# Take every other frame to get just the left side. And,
# divide each one by 2. (We would like to use
# Numeric.divide(Frames[::2],2), but we can't,
# because the returned array would have float type).
Frames[::2] = Frames[::2]/2
OutFile.writeframes(Frames.tostring())
InFile.close()
OutFile.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top