What type of file is the “sound clip” parameter for the audio player?

The Python audio file documentation states that most of the features available require "sound bites."

The audio player module contains some useful operations with fragments of sound. It works on sound bites consisting of signed integer samples of 8, 16 or 32 bits wide, stored in Python strings.

What is a sound fragment and how can I turn an existing .wav file into one?

Thanks.

+6
source share
3 answers

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())) # 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 you can cache the line:

 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())) 
+4
source

You might want to learn the wave module. You probably want to open the file in read mode and use readframes to get the sample you need for audiooop.

+3
source

To answer which particular fragment, this is a bytes object , which is just a string of bytes. I believe that for 8-bit audio files for each frame there will be one byte for 8-bit audio, two bytes per frame for 16-bit audio and four bytes for 32-bit audio.

+1
source

Source: https://habr.com/ru/post/980855/


All Articles