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 and readframes(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:
import wave
import audioop
wav = wave.open("piano2.wav")
print(audioop.avg(wav.readframes(wav.getnframes()), wav.getsampwidth()))
Outputs:
-2
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:
wav = wave.open("piano2.wav")
print(audioop.avg(wav.readframes(wav.getnframes()), wav.getsampwidth()))
# if you don't call rewind, next readframes() call
# will return nothing and audioop will fail
wav.rewind()
print(audioop.max(wav.readframes(wav.getnframes()), wav.getsampwidth()))
Or alternatively you can cache the string:
wav = wave.open("piano2.wav")
string_wav = wav.readframes(wav.getnframes())
print(audioop.avg(string_wav, wav.getsampwidth()))
# wav.rewind()
print(audioop.max(string_wav, wav.getsampwidth()))