Created
October 15, 2010 02:36
-
-
Save aisipos/627501 to your computer and use it in GitHub Desktop.
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 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sys.argv[0] should be sys.argv[1]
ps
thanks for the code snippet.