Skip to content

Instantly share code, notes, and snippets.

@aisipos
Created October 15, 2010 02:36
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 aisipos/627501 to your computer and use it in GitHub Desktop.
Save aisipos/627501 to your computer and use it in GitHub Desktop.
import sys
from collections import namedtuple
from struct import unpack, calcsize
Header = namedtuple('Header', 'id totallength wavefmt format pcm channels frequency bytes_per_second bytes_by_capture bits_per_sample data bytes_in_data')
header_format = '4si8sihhiihh4si'
header_len = calcsize(header_format)
def read_wav(fname):
"Read in a wav file with simple header parsing, and return the sum of the absolute value of its samples"
with open(fname,'rb') as f:
raw = f.read(header_len)
header = Header._make(unpack(header_format, raw))
if(header.id != 'RIFF'):
raise Exception("Unexpected id %s" % header.id)
body = f.read()
return sum(abs(ord(c)) for c in body)
if __name__ == '__main__':
print read_wav(sys.argv[0])
@calcium
Copy link

calcium commented Apr 2, 2014

sys.argv[0] should be sys.argv[1]

ps
thanks for the code snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment