Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2013 19:43
Show Gist options
  • Select an option

  • Save anonymous/5379760 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5379760 to your computer and use it in GitHub Desktop.
test
# wave_test.py
# python 2.7 doesn't support 'with' on the wave module
import wave
import struct
def clean(i):
strs = i.split('\t')[:2]
return [float(i) for i in strs]
filename = "snipped.wav"
info_filename = 'snipped_info'
'''read info file'''
f = open(info_filename)
sample_markers = [clean(i) for i in f.readlines()]
f.close()
mark_start, mark_end = sample_markers[0]
nseconds = mark_end - mark_start
# print(sample_markers)
# print('nseconds', nseconds)
# print('mark_start', mark_start)
# print('mark_end', mark_end)
# ----------------------------------------------------------
'''read original file'''
f = wave.open(filename)
wave_params = f.getparams()
nchannels, sampwidth, frate, nframes, comptype, compname = wave_params
duration_seconds = float(nframes)/float(frate)
# print(frate, nframes, duration_seconds)
# find start frame, end frame and n (n samples)
start_frame = int(mark_start * frate)
end_frame = int(mark_end * frate)
n = end_frame - start_frame
print(start_frame, end_frame, n)
f.setpos(start_frame)
data = f.readframes(n)
'''open destination for writing'''
wf = wave.open('destination.wav', 'wb')
wf.setparams(wave_params)
# wf.setpos(44100) # start at 1 second
for i in range(44100*2):
wf.writeframes(struct.pack('h', 0))
wf.writeframes(data)
wf.close()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment