def vocode_audiofiles(filepath_1, filepath_2, num_filters=20, chunk_size=1024): """ "Fuses" two audiofiles using the Vocoder-algorithm. Note: If the length of the audiofiles differ, the longer file gets cut to the length of the shorter one. If the framerate differs, the file with the lower framerate is converted to the higher one, using the average-algorithm. # TODO: Implement what is described up there /\ :param filepath_1 (str): Path to a .wav file :param filepath_2 (str): Path to a .wav file :param num_filters (int: Amount of filters the vocoder uses :param chunk_size (int): Size each chunk should have (debug) :return: toolset.Wave """ assert isinstance(filepath_1, str) and isinstance(filepath_1, str), "Filepaths must be of type %s" % type(" ") wave1 = toolset.to_wave(thinkdsp.read_wave(filepath_1)) wave2 = toolset.to_wave(thinkdsp.read_wave(filepath_2)) smaller_signal_length = min(wave1.length, wave2.length) result = np.zeros(smaller_signal_length - (smaller_signal_length % chunk_size)) print "Starting to vocode two signals with length %i..." % smaller_signal_length vocoder = vc.Vocoder(wave1.framerate, np.hamming, num_filters, chunk_size) debug_num_chunks = len(result) / chunk_size - 1 debug_factor = 100.0 / debug_num_chunks for i in range(len(result) / chunk_size - 1): # Status update: print "%g%% done, processing chunk no. %i out of %i " % (round(i * debug_factor, 2), i, debug_num_chunks) # Start - und ende des momentanen Chunks berechnen: start, end = i * chunk_size, (i + 1) * chunk_size # Modulator- und Carrier-Chunk "herausschneiden": modulator = toolset.Wave(wave1.ys[start:end], wave1.framerate) carrier = toolset.Wave(wave2.ys[start:end], wave2.framerate) # Vocoder-Effekt auf die beiden Signale Anwenden: result[start:end] = vocoder.vocode(modulator, carrier) print "~job's done~" return toolset.Wave(result, wave1.framerate)
def analyse(filename, resolution=100): print "Generating frequency-spectrogram for %s..." % filename low, high = 0, None try: wave = toolset.to_wave(thinkdsp.read_wave(filename)) except: print "Couldn't load file %s. Returning with a silten error." % filename return False len_spetrogram = len(wave)/resolution*2 spect = wave.make_spectrogram(len_spetrogram) _, plotarr = pyplot.subplots(2, 1) plotarr[0].plot(wave.ts, wave.ys) plotarr[0].set_xlabel("Zeit in s") plotarr[0].set_ylabel("Amplitude") ts = np.array(spect.times()) if high == None: high = len(spect.frequencies())-1 fs = np.array(spect.frequencies()[low:high]) # make the array size = len(fs), len(ts) array = np.zeros(size, dtype=np.float) # copy amplitude from each spectrum into a column of the array for i, t in enumerate(ts): spectrum = spect.spec_map[t] array[:,i] = np.array(spectrum.amps[low:high]) print("Generating graph...") plotarr[1].set_yscale("linear") plotarr[1].pcolor(ts, fs, array**0.5, cmap="nipy_spectral") plotarr[1].set_ylabel("Frequenz in Hz") plotarr[1].set_xlablel("Zeit in s") print("Saving image as \"res_%i_%s.png\"..." % (resolution, filename[:-4])) pyplot.savefig("Fourier-Results\\double\\res_%i_%s.png" % (framerate, filename.split("\\")[-1][:-4])) print("Done.") pyplot.cla() return True