Last active
July 12, 2016 06:13
-
-
Save jkihgit/f7459e0a819fa00e948668d43a23cf05 to your computer and use it in GitHub Desktop.
Python code to open uncompressed wave files
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 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