import wave,struct,math
waveFile = wave.open("/home/pi/xmas/yolo2.wav", "rb") 
frames = waveFile.getnframes()
rate = waveFile.getframerate()
length = frames / int(rate)

I used this to get the audio file, but I'm not able to get any sort of values corresponding to each second of the file so that I can interface this to an led strip.

  • Welcome to the Raspberry Pi Stack Exchange! I, for one, would need a little (or rather a lot) more detail - it would seem that this is a python script but I do not see the connection with "an led strip". As it seems you are new here you may not realise that better questions are more likely to get better answers. so, please can you tell us more about the hardware and what you are trying to achieve?
    – SlySven
    Commented Dec 21, 2015 at 21:42
  • @SlySven i used pigpio to successfully interface the led strip. so what i need more is some sort of value from the sound input. i used a wave file in my sd card. used the wave library to open it. but i'm unable to find any good way to extract information i want from it. Commented Dec 23, 2015 at 14:20
  • Are you looking to produce a sort of "sound-to-light show"? What sort of control over the LED in the strip(s) do you have or want to use (global colour/intensity vs. individual LED control). You will probably want to look-up Fast Fourier Transform which is the way to convert Audio information in the time domain (instantaneous sound pressure levels - represent by an voltage level in an electrical circuit at an exact point in time or a "sample" value in an audio file) to the frequency domain...
    – SlySven
    Commented Dec 23, 2015 at 16:12
  • ... (frequency components over the audio spectrum for the duration of the sampling time) - like the display produce by a spectrum analyser. However the most basic approach would be to average the magnitude (distance from the mean value of all the samples in a delta {small} time interval) and use that value for each delta to set the brightness or colour of the LEDs for the same time delta... more processing power means instead of considering all frequencies as this would you could divide up the audio spectrum (from 0Hz to half the sample rate) and do different things with each band of...
    – SlySven
    Commented Dec 23, 2015 at 16:21
  • ...frequencies - this is what FFTs will give you. python-audioprocessing would appear to be related to this but the version number looks a bit low (0.0.7).
    – SlySven
    Commented Dec 23, 2015 at 16:37

1 Answer 1


2 Bytes = 16 Bits

Adding a bit of code so that I can understand how the wave module works:

#!/usr/bin/env python3
import wave,struct,math
waveFile = wave.open("/home/pi/Music/second_stupidest_birthday_ever.wav", "rb")
frames   = waveFile.getnframes()   # total number of frames / samples
rate     = waveFile.getframerate() # number of frames / samples per second (should be 44100 Hz (44.1 kHz) for CD-quality audio)
length   = frames / int(rate)      # length in seconds
channels = waveFile.getnchannels() # number of channels (should be 2 channels (stereo) for CD-quality audio)
width    = waveFile.getsampwidth() # sample width / bit depth (should be 2 bytes (16 bits) for CD-quality audio)

print("Total Number of Frames:   " + str(frames))
print("Frames Per Second:        " + str(rate))
print("Total Number of Seconds:  " + str(length))
print("Total Number of Channels: " + str(channels))
print("Bytes Per Frame:          " + str(width))

Running the program produces the following output:

pi@raspberrypi:~ $ bin/get_wav_info.py
Total Number of Frames:   2028600
Frames Per Second:        44100
Total Number of Seconds:  46.0
Total Number of Channels: 2
Bytes Per Frame:          2

I was surprised that wave expresses the bit depth in bytes, not bits!


Since you're asking about frequency specifically, waveFile.getframerate() returns the frequency in number of samples per second, generally expressed in hertz (Hz) or kilohertz (kHz).

Please note that the sample rate and the bit depth apply to each channel.
This means that the total bit rate of a 2-channel, 44.1kHz, 16-bit wav file will be:
2 x 44100 x 16 = 1411200 bits per second = 1411.200 megabits per second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.