Skip to content

Instantly share code, notes, and snippets.

@dare0021
Last active July 12, 2016 06:13
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/f7459e0a819fa00e948668d43a23cf05 to your computer and use it in GitHub Desktop.
Save dare0021/f7459e0a819fa00e948668d43a23cf05 to your computer and use it in GitHub Desktop.
Python code to open uncompressed wave files
import wave
import struct
import sys
# the only non-default library
import numpy as np
input = "file_path_here.wav"
waveFile = wave.open(input, 'r')
if waveFile.getcompname() != 'not compressed':
waveFile.close()
print "NOT IMPLEMENTED"
sys.exit()
length = waveFile.getnframes()
leadingZeros = 0
out = "nothing yet"
for i in range(0, length):
waveData = waveFile.readframes(1)
data = struct.unpack("<i", 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("<i", waveData)[0]
out[i] = data
waveFile.close()
trailingZeros = 0
for i in range(length - leadingZeros - 1, 0, -1):
if out[i] != 0:
break
else:
trailingZeros += 1
print out[0 : length - leadingZeros - trailingZeros]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment