Created
August 23, 2013 17:16
-
-
Save nick-jonas/6321772 to your computer and use it in GitHub Desktop.
Analyze sound spectrum at 30 fps, from Mr. Doob's post: http://ricardocabello.com/blog/post/677
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import struct | |
import wave | |
import sys | |
w = wave.open(sys.argv[1], 'rb') | |
# We assume 44.1k @ 16-bit, can test with getframerate() and getsampwidth(). | |
sum = 0 | |
value = 0; | |
delta = 0; | |
amps = [ ] | |
for i in xrange(0, w.getnframes()): | |
# Assume stereo, mix the channels. | |
data = struct.unpack('<hh', w.readframes(1)) | |
sum += (data[0]*data[0] + data[1]*data[1]) / 2 | |
# 44100 / 30 = 1470 | |
if (i != 0 and (i % 1470) == 0): | |
value = int(math.sqrt(sum / 1470.0) / 10) | |
amps.append(value - delta) | |
delta = value | |
sum = 0 | |
print amps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment