def generate_diffs(filea, fileb): wavea = read_wave_file(filea, True) waveb = read_wave_file(fileb, True) diffl = normalized_difference(*aligned_sublists(wavea[0], waveb[0])) diffr = normalized_difference(*aligned_sublists(wavea[1], waveb[1])) peakl = peak_diff(wavea[0], waveb[0]) peakr = peak_diff(wavea[1], waveb[1]) # for line in aligned_sublists(wavea[0], waveb[0]): # plt.plot(normalized(line[:10000])) # plt.show() # louder_a = wavea[0] if numpy.amax(wavea[0]) \ # > numpy.amax(wavea[1]) else wavea[1] # louder_b = waveb[0] if numpy.amax(waveb[0]) \ # > numpy.amax(waveb[1]) else waveb[1] # freqd = freq_diff(normalized(louder_a), normalized(louder_b)) return ( diffl, diffr, peakl, peakr, 0, # freqd, os.path.split(filea)[-1], os.path.split(fileb)[-1])
def concat_samples(regions, path, name=None): if name is None: output_filename = 'all_samples_%f.flac' % time.time() else: output_filename = '%s.flac' % name concat_filename = 'concat.txt' with open(concat_filename, 'w') as outfile: global_offset = 0 for region in regions: sample = region.attributes['sample'] sample_data = read_wave_file(full_path(path, sample)) sample_length = len(sample_data[0]) region.attributes['offset'] = global_offset region.attributes['end'] = (global_offset + sample_length - ANTI_CLICK_OFFSET) # TODO: make sure endpoint is a zero crossing to prevent clicks region.attributes['sample'] = output_filename outfile.write("file '%s'\n" % full_path(path, sample)) global_offset += sample_length create_flac(concat_filename, full_path(path, output_filename)) os.unlink(concat_filename) return regions
def normalize_file(filename): data = read_wave_file(filename, True) if len(data): normalized_data = normalized(data) * (2**(bit_depth - 1) - 1) else: normalized_data = data save_to_file(filename, 2, normalized_data)
def process_all(start, stop, *files): start = int(start) stop = int(stop) chunk_offset = ((-1 * start) % CHUNK_SIZE) for file in files: stereo = read_wave_file(file) left = stereo[0] right = stereo[1] plt.plot(left[start:stop]) plt.plot(right[start:stop]) plt.show()
def level_volume(regions, dirname): if len(regions) == 0: return None velcurve = {} velsorted = list( reversed(sorted(regions, key=lambda x: int(x.attributes['hivel'])))) for high, low in pairwise(velsorted): try: diff = (peak_rms( read_wave_file(full_path(dirname, low.attributes['sample']))) / peak_rms( read_wave_file( full_path(dirname, high.attributes['sample'])))) except ZeroDivisionError: print "Got ZeroDivisionError with high sample path: %s" % \ high.attributes['sample'] raise for attr in REMOVE_ATTRS: if attr in high.attributes: del high.attributes[attr] velcurve.update({ ('amp_velcurve_%d' % int(high.attributes['hivel'])): 1, ('amp_velcurve_%d' % (int(high.attributes['lovel']) + 1)): diff, }) # print the last region that didn't have a lower counterpart low = velsorted[-1] for attr in REMOVE_ATTRS: if attr in low.attributes: del low.attributes[attr] velcurve.update({ ('amp_velcurve_%d' % int(low.attributes['hivel'])): 1, ('amp_velcurve_%d' % (int(low.attributes['lovel']) + 1)): 0, }) return Group(velcurve, velsorted)
def freq_shift(): files = ['A1_v111_15.00s.aif', 'A1_v95_15.00s.aif'] wavea, waveb = [ read_wave_file(os.path.join(root_dir, file)) for file in files ] louder_a = wavea[0] if ( numpy.amax(wavea[0]) > numpy.amax(wavea[1])) else wavea[1] louder_b = waveb[0] if ( numpy.amax(waveb[0]) > numpy.amax(waveb[1])) else waveb[1] freqd = freq_diff(normalized(louder_a), normalized(louder_b)) waveb_shifted = [shift_freq(channel, freqd) for channel in waveb] louder_shifted_b = waveb_shifted[0] if (numpy.amax(waveb_shifted[0]) > numpy.amax(waveb_shifted[1])) \ else waveb_shifted[1] shifted_freqd = freq_diff(normalized(louder_a), normalized(louder_shifted_b)) # lefts_aligned = aligned_sublists(wavea[0], waveb[0]) rights_aligned = aligned_sublists(wavea[1], waveb[1]) # shifted_lefts_aligned = aligned_sublists(wavea[0], waveb_shifted[0]) diffl = normalized_difference(*aligned_sublists(wavea[0], waveb[0])) diffr = normalized_difference(*aligned_sublists(wavea[1], waveb[1])) plt.plot(normalized(rights_aligned[0][:10000])) plt.plot(normalized(rights_aligned[1][:10000])) plt.plot( numpy.absolute( normalized(rights_aligned[0][:10000]) - normalized(rights_aligned[1][:10000]))) plt.show() shifted_diffl = normalized_difference( *aligned_sublists(wavea[0], waveb_shifted[0])) shifted_diffr = normalized_difference( *aligned_sublists(wavea[1], waveb_shifted[1])) print files print 'diffs\t\t', diffl, diffr print 'shifted diffs\t', shifted_diffl, shifted_diffr print 'freqs', freqd print 'shifted freqs', shifted_freqd
def graph_ffts(): files = ['A1_v111_15.00s.aif', 'A2_v31_15.00s.aif'] for file in files: stereo = read_wave_file(os.path.join(root_dir, file)) left = stereo[0] right = stereo[1] list = left[:100000] w = numpy.fft.rfft(list) freqs = numpy.fft.fftfreq(len(w)) # Find the peak in the coefficients # idx = numpy.argmax(numpy.abs(w[:len(w) / 2])) idx = numpy.argmax(numpy.abs(w)) freq = freqs[idx] plt.plot(w) print freq print \ fundamental_frequency(normalized(list)), \ fundamental_frequency(normalized(left + right))
def starts_with_click(filename, threshold_samples=default_threshold_samples): sample_data = read_wave_file(filename) return (abs(sample_data[0][0]) > threshold_samples or abs(sample_data[0][1]) > threshold_samples)
def max_amp(filename): return numpy.amax(read_wave_file(filename)) / (2.**(bit_depth - 1))
def chop(aif): file = read_wave_file(aif) start, end = min([start_of(chan) for chan in file]), \ max([end_of(chan) for chan in file]) print aif, start, end, float(end) / len(file[0])
interpolation="none" ) plt.colorbar() plt.xlabel("time (s)") plt.ylabel("frequency (hz)") plt.xlim([0, timebins - 1]) plt.ylim([0, freqbins]) xlocs = np.float32(np.linspace(0, timebins - 1, 5)) plt.xticks(xlocs, [ "%.02f" % l for l in ( ((xlocs * len(samples) / timebins) + (0.5 * binsize)) / samplerate ) ]) ylocs = np.int16(np.round(np.linspace(0, freqbins - 1, 10))) plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs]) if plotpath: plt.savefig(plotpath, bbox_inches="tight") else: plt.show() plt.clf() if __name__ == "__main__": stereo = read_wave_file(sys.argv[1]) left = stereo[0] plotstft(48000, left)