Skip to content

Instantly share code, notes, and snippets.

@kirkegaard
Last active April 4, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkegaard/9e23632902a1a76d0ee4ad20e6403714 to your computer and use it in GitHub Desktop.
Save kirkegaard/9e23632902a1a76d0ee4ad20e6403714 to your computer and use it in GitHub Desktop.
import math
import struct
import wave
import sys
w = wave.open(sys.argv[1], 'rb')
sum = 0
value = 0;
delta = 0;
amps = []
for i in range(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