diff --git a/cleaned_sp.py b/cleaned_sp.py index fafcce5..ee33a94 100644 --- a/cleaned_sp.py +++ b/cleaned_sp.py @@ -27,10 +27,11 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize): # remove high_pitched/low-pitched frequencies minfreq = 110 - maxfreq = 440*8 + maxfreq = 440*6 # cutting the song to only keep the one we're interested in subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False) + subprocess.run(["clear"]) # extracting data from cropped song sample_rate, raw_song_data = wavfile.read("crop.wav") @@ -50,6 +51,9 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize): else: song_data = raw_song_data + print("\nSampleRate : ", sample_rate) + print("SegSize : ", blit) + # remove the copy of the song subprocess.run(["rm", "crop.wav"], shell=False) @@ -77,6 +81,7 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize): # calculate the fft, append it to fft_list pff = scp.fft.rfft(song_data[int(current_time*sample_rate):int(sample_rate*(current_time+segsize))]) fft_list.append(pff) + #print("(k =", k, ") :", left_id, "to", right_id) # just to avoid what causes 0.1 + 0.1 == 0.2 to be False k += 1 @@ -96,9 +101,9 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize): for i in range(len(fft_list)): current_max = -1 current_fmax = 0 - + for j in range(len(fft_list[i])): - if(pfreq[j] < maxfreq and pfreq[j] >= minfreq and np.abs(fft_list[i][j]) > current_max): + if(j < len(pfreq) and pfreq[j] < maxfreq and pfreq[j] >= minfreq and np.abs(fft_list[i][j]) > current_max): current_max = np.abs(fft_list[i][j]) current_fmax = pfreq[j] @@ -111,17 +116,10 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize): def void_freq_clean(song_name, offset, songlen, segsize, minfreq, maxfreq, ampthr, output_name): # removes unnecessary frequencies/amps from a song - #ampthr is in [0, 1] - - # remove high_pitched/low-pitched frequencies - minfreq = 110 - maxfreq = 440*8 - - # cutting the song to only keep the one we're interested in - subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False) + # ampthr is in [0, 1] # extracting data from cropped song - sample_rate, raw_song_data = wavfile.read("crop.wav") + sample_rate, raw_song_data = wavfile.read(song_name) blit = int(sample_rate*segsize) # Te song_data = [0 for i in range(len(raw_song_data))] @@ -215,6 +213,7 @@ def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent): # cutting the song to only keep the one we're interested in subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False) + subprocess.run(["clear"]) # extracting data from cropped song sample_rate, raw_song_data = wavfile.read("crop.wav") @@ -291,7 +290,7 @@ def convert_to_wav(song_name:str, output_file="audio.wav") -> str: return output_file return song_name -def retrieve_all_from_song(filename, t0, t1, dt=0.001, threshold=0.1): +def retrieve_all_from_song(filename, t0, t1, dta=0.001, dtf=0.01, threshold=0.1, show=True): # dt = sample interval # threshold is in percent @@ -309,31 +308,39 @@ def retrieve_all_from_song(filename, t0, t1, dt=0.001, threshold=0.1): sample_rate, _ = wavfile.read("crop0.wav") print("Filtering song...") - void_freq_clean(new_fn, t0, t1-t0, dt, 200, 2500, 0.05, "crop1.wav") + #void_freq_clean(new_fn, t0, t1-t0, dt, 200, 2500, 0.05, "crop1.wav") print("Now retrieving the frequencies") - (maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1-t0, dt) + (maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1-t0, dtf) print("Now retrieving the amplitudes") - amps = retrieve_dominant_amps(new_fn, t0, t1-t0, dt, threshold) + amps = retrieve_dominant_amps(new_fn, t0, t1-t0, dta, threshold) print("Len of freqs : ", len(maxlist), "|", len(maxamps)) print("Len of amps : ", len(maxlist), "|", len(amps)) - timesF = [t0 + dt*k for k in range(len(maxlist))] - timesA = [t0 + dt*k for k in range(len(amps))] + maxa = amps[0] + for jj in amps: + if(jj > maxa): + maxa = jj - plt.plot(timesF, maxlist) - plt.show() + for i in range(len(amps)): + amps[i] = (amps[i] * 2000) / maxa - plt.plot(timesA, amps) - plt.show() + if(show): + timesF = [t0 + dtf*k for k in range(len(maxlist))] + timesA = [t0 + dta*k for k in range(len(amps))] + + plt.plot(timesA, amps) + plt.plot(timesF, maxlist) + plt.show() # free() subprocess.run(["rm", "crop0.wav"], shell=False) -retrieve_all_from_song("tetris_4.wav", 0, 5) +retrieve_all_from_song("tetris_4.wav", 0, 5, dtf=0.375/2) print("yipee") +print(1/(160/60)); diff --git a/crop1.wav b/crop1.wav index 31148cb..530107d 100644 Binary files a/crop1.wav and b/crop1.wav differ