if __name__ == '__main__': global dt # figure out the correct dt length based on the tempo of the output wave you want dt = compute_dt(bpm) # read and parse each sample wave for i in range(1, len(sys.argv) - 2): #read in the wave file and unpack its data wave_data = waveIO.read_wav_file(sys.argv[i]) wave_data = waveIO.unpack(wave_data) wave_time = len(wave_data) / sr note_length = dt * sr wave_chunks = split_wave(wave_data, int(note_length)) #test_sample_wave(wave_chunks) for i in range(len(wave_chunks)): chunk = wave_chunks[i] if len(chunk) != int(note_length): continue store_note(chunk) print_notes() plot_notes() # read and parse the music file musicfile = sys.argv[-2] song = build_song(musicfile, dt) waveIO.write_wav_file(sys.argv[-1], waveIO.pack(song))
wave_chunks = [] #Instead of multiplying the entire wave by the windowing function, we can simply examine window-sized pieces of the wave at a time #counting by WINDOW_LENGTH*(1-OVERLAP_PERCENT) allows adjustment of what percent of each window overlaps with the next #Example: OVERLAP_PERCENT = 0 means the next window will start where the previous window ended #Example: OVERLAP_PERCENT = 0.5 means the next window will start at the halfway mark of the previous window #This is necessary when using nonrectangular window functions, to prevent loss of data for i in range(0, len(wave_data), int(WINDOW_LENGTH*(1-OVERLAP_PERCENT))): window = [] window.extend(wave_data[i:i+WINDOW_LENGTH]) #multiply by the window function window = WINDOW_FUNC(window) wave_chunks.append(window) for i in range(len(wave_chunks)): chunk = wave_chunks[i] if len(chunk) != WINDOW_LENGTH: continue store_note(chunk) print_notes() plot_notes() # read and parse the music file musicfile = sys.argv[-2] song = build_song(musicfile, WINDOW_LENGTH / float(sr)) waveIO.write_wav_file(sys.argv[-1], waveIO.pack(song))
"A":440., "A#":466.16, "B":493.88, "C":261.63, "C#":277.18, "D":293.66, "D#":311.13, "E":329.63, "F":349.23, "F#":369.99, "G":391.99, "G#":415.31 } freqs5 = {} freqs6 = {} note_length = 8938 if __name__ == '__main__': for note in freqs4: freqs5[note] = 2*freqs4[note] freqs6[note] = 4*freqs4[note] musicfile = sys.argv[1] musicwave = build_song(musicfile) data, freqs, bins, im = plt.specgram(musicwave, NFFT=2048, Fs=44100, noverlap=900) plt.ylim(0,800) plt.title("Spectrogram of analytic_furelise_hanning.wav") plt.show() waveIO.write_wav_file("analytic_furelise.wav", waveIO.pack(musicwave))
"C": 261.63, "C#": 277.18, "D": 293.66, "D#": 311.13, "E": 329.63, "F": 349.23, "F#": 369.99, "G": 391.99, "G#": 415.31 } freqs5 = {} freqs6 = {} note_length = 8938 if __name__ == '__main__': for note in freqs4: freqs5[note] = 2 * freqs4[note] freqs6[note] = 4 * freqs4[note] musicfile = sys.argv[1] musicwave = build_song(musicfile) data, freqs, bins, im = plt.specgram(musicwave, NFFT=2048, Fs=44100, noverlap=900) plt.ylim(0, 800) plt.title("Spectrogram of analytic_furelise_hanning.wav") plt.show() waveIO.write_wav_file("analytic_furelise.wav", waveIO.pack(musicwave))
plt.xlim(0,1000) plt.xlabel("Frequency (Hz)") plt.ylabel("Absolute value of FFT Magnitude, divided by N") #plt.yscale("log") plt.title("Power spectrum of sample wave") plt.show() data, freqs, bins, im = plt.specgram(t_sin, NFFT=NFFT, Fs=fs, noverlap=NFFT/2) plt.title("Spectrogram, notes in reverse, window size = 1024, sr = 44100") plt.ylabel("Frequency (Hz)") plt.xlabel("Time (s)") plt.ylim(0,800) plt.show() #print(data) waveIO.write_wav_file("samplewave.wav", waveIO.pack(t_sin)) ############## # Now build a universal sample, with short sections for each note through a few octaves t = numpy.arange(0, 0.5, dt) big_sample = create_wave(440, t) big_sample = numpy.append(big_sample, create_wave(261, t)) big_sample = numpy.append(big_sample, create_wave(277, t)) big_sample = numpy.append(big_sample, create_wave(293, t)) big_sample = numpy.append(big_sample, create_wave(311, t)) big_sample = numpy.append(big_sample, create_wave(329, t)) big_sample = numpy.append(big_sample, create_wave(349, t)) big_sample = numpy.append(big_sample, create_wave(369, t)) big_sample = numpy.append(big_sample, create_wave(391, t)) big_sample = numpy.append(big_sample, create_wave(415, t))
plt.ylabel("Absolute value of FFT Magnitude, divided by N") #plt.yscale("log") plt.title("Power spectrum of sample wave") plt.show() data, freqs, bins, im = plt.specgram(t_sin, NFFT=NFFT, Fs=fs, noverlap=NFFT / 2) plt.title("Spectrogram, notes in reverse, window size = 1024, sr = 44100") plt.ylabel("Frequency (Hz)") plt.xlabel("Time (s)") plt.ylim(0, 800) plt.show() #print(data) waveIO.write_wav_file("samplewave.wav", waveIO.pack(t_sin)) ############## # Now build a universal sample, with short sections for each note through a few octaves t = numpy.arange(0, 0.5, dt) big_sample = create_wave(440, t) big_sample = numpy.append(big_sample, create_wave(261, t)) big_sample = numpy.append(big_sample, create_wave(277, t)) big_sample = numpy.append(big_sample, create_wave(293, t)) big_sample = numpy.append(big_sample, create_wave(311, t)) big_sample = numpy.append(big_sample, create_wave(329, t)) big_sample = numpy.append(big_sample, create_wave(349, t)) big_sample = numpy.append(big_sample, create_wave(369, t)) big_sample = numpy.append(big_sample, create_wave(391, t)) big_sample = numpy.append(big_sample, create_wave(415, t)) big_sample = numpy.append(big_sample, create_wave(466.16, t))