Skip to content

Instantly share code, notes, and snippets.

@dare0021
Last active July 12, 2016 07:12
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 dare0021/c359fc67cfae766d86447a000a1aa72e to your computer and use it in GitHub Desktop.
Save dare0021/c359fc67cfae766d86447a000a1aa72e to your computer and use it in GitHub Desktop.
A more complex wave file opener. Just a bit more complex.
import numpy as np
import wave
import struct
waveFile = "No open file"
path = ""
# 0 is the least verbose
printLevel = 0
def open(input, verbose=0):
global waveFile, path
path = input
printLevel = verbose
waveFile = wave.open(input, 'r')
if waveFile.getcompname() != 'not compressed':
waveFile.close()
print "ERR: file uses compression"
print "NOT IMPLEMENTED"
return False
if printLevel > 0:
print "Opening: ",path
# all data in little endian (>)
# for stereo, use int (i)
# for mono, short (h)
# to think about: returning stereo data as an array of length 2?
print "Channels: ", waveFile.getnchannels()
return True
def close():
waveFile.close()
def read():
if printLevel > 0:
print "Reading: ",path
out = "nothing yet"
length = waveFile.getnframes()
leadingZeros = 0
for i in range(0, length):
waveData = waveFile.readframes(1)
data = struct.unpack("<h", waveData)[0]
if data != 0:
out = np.zeros(shape=(length - leadingZeros))
out[0] = data
break
else:
leadingZeros += 1
if out == "nothing yet":
out = np.zeros(shape=(length))
for i in range(1, length - leadingZeros):
waveData = waveFile.readframes(1)
data = struct.unpack("<h", waveData)[0]
out[i] = data
trailingZeros = 0
for i in range(length - leadingZeros - 1, 0, -1):
if out[i] != 0:
break
else:
trailingZeros += 1
return out[0 : length - leadingZeros - trailingZeros].astype(int)
def getFramerate():
return waveFile.getframerate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment