You can do this using the wave module.
The open() method opens the file, and readframes(n) returns (maximum) n frames of audio as a string of bytes, exactly what the audio player wants.
For example, let's say you need to use the avg() method from audioop. Here 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
In addition, you might be interested in the rewind() method from the wave module. It returns the reading position to the beginning of the wav file.
If you need to read your wav file twice, you can write this:
wav = wave.open("piano2.wav") print(audioop.avg(wav.readframes(wav.getnframes()), wav.getsampwidth()))
Or you can cache the line:
wav = wave.open("piano2.wav") string_wav = wav.readframes(wav.getnframes()) print(audioop.avg(string_wav, wav.getsampwidth()))
source share