The Python audioop documentation states that most of the available functions require "sound fragments."
The audioop module contains some useful operations on sound fragments. It operates on sound fragments consisting of signed integer samples 8, 16 or 32 bits wide, stored in Python strings.
What exactly is a sound fragment and how can I turn an existing .wav file into one?
Thanks.
You can do so by using the wave module
The
open()
method opens the file andreadframes(n)
returns (maximum) n frames of audio as a string of bytes, just what audioop wants.For example, let's say you need to use the
avg()
method from audioop. This is how you could do it:Outputs:
Also, you may be interested in the
rewind()
method from the wave module. It puts the reading position back to the beginning of the wav file.If you need to read through your wav file twice you can write this:
Or alternatively you can cache the string: