-
-
Save zeffii/5380010 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # wave_test.py | |
| # python 2.7 doesn't support 'with' on the wave module | |
| ''' | |
| original wave: | |
| https://dl.dropboxusercontent.com/u/3397495/wav/we_must_build_upwards.wav | |
| original label file, generated by audacity | |
| https://dl.dropboxusercontent.com/u/3397495/wav/wmbu_labels | |
| ''' | |
| import wave | |
| import struct | |
| import collections | |
| filename = "we_must_build_upwards.wav" | |
| info_filename = 'wmbu_labels' | |
| def get_params_object(wave_params): | |
| '''returns a namedtuple object to allow attribute access''' | |
| wparams = 'nchannels sampwidth frate nframes comptype compname' | |
| param_dict = dict(zip(wparams.split(), wave_params)) | |
| param_object = collections.namedtuple('file_params', wparams) | |
| return param_object(**param_dict) | |
| def print_temp(a,b,c): | |
| print('nseconds', a) | |
| print('mark_start', b) | |
| print('mark_end', c) | |
| def get_sample_markers(info_filename): | |
| '''read info file''' | |
| def clean(i): | |
| strs = i.split('\t')[:2] | |
| return [float(i) for i in strs] | |
| with open(info_filename) as f: | |
| sample_markers = [clean(i) for i in f.readlines()] | |
| return sample_markers | |
| def get_duration_from_fparams(): | |
| duration_seconds = float(f_params.nframes)/float(f_params.frate) | |
| print(f_params.frate, f_params.nframes, duration_seconds) | |
| return duration_seconds | |
| def generate_silence(duration): | |
| '''duration in seconds, forces to be divisible by nchannels | |
| assuming here nchannels is 2 - which is fine for my needs''' | |
| dframes = int(f_params.frate * f_params.nchannels * duration) | |
| silent_frames = dframes if dframes%2 == 0 else dframes+1 | |
| return "".join((wave.struct.pack('h', 0) for i in range(silent_frames))) | |
| def get_slice_details(mark_start, mark_end): | |
| start_frame = int(mark_start * f_params.frate) | |
| end_frame = int(mark_end * f_params.frate) | |
| n = end_frame - start_frame | |
| print(start_frame, end_frame, n) | |
| return start_frame, end_frame, n | |
| sample_markers = get_sample_markers(info_filename) | |
| '''test on one marker''' | |
| mark_start, mark_end = sample_markers[0] | |
| # nseconds = mark_end - mark_start | |
| # print_temp(nseconds, mark_start, mark_end) | |
| '''read original file''' | |
| f = wave.open(filename) | |
| wave_params = f.getparams() | |
| f_params = get_params_object(wave_params) | |
| duration_seconds = get_duration_from_fparams() | |
| start_frame, end_frame, n = get_slice_details(mark_start, mark_end) | |
| f.setpos(start_frame) | |
| data = f.readframes(n) | |
| '''open destination for writing''' | |
| wf = wave.open('destination.wav', 'wb') | |
| wf.setparams(wave_params) | |
| silent_data = generate_silence(0) | |
| wf.writeframes(silent_data) | |
| 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