From 3587adbe2fcefc8978ed6291ad16cc592d77dc71 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 3 Jun 2024 22:28:41 +0200 Subject: [PATCH] rewrite of get_frequencies to match new data structure --- debug.py | 9 ++++- main.py | 2 +- sound_process.py | 86 ++++++++++++++++++++++-------------------------- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/debug.py b/debug.py index 9321052..3ff75ac 100644 --- a/debug.py +++ b/debug.py @@ -213,6 +213,12 @@ def get_spacing(data, sample_rate=44100, show=False, retrieve=False): - dt is in ms ''' +def avg(data, i, j): + res = 0 + for e in range(i, min(len(data), j)): + res += data[e] + return (res/(min(len(data), j) - i)) + def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=False): ''' explaination : @@ -241,7 +247,7 @@ def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=Fa if(time_diff[i] > mintime): segments.append([current_left, i]) seglen.append(peak_times[i]-peak_times[current_left]) - res_peaks.append(500) + res_peaks.append(avg(data_peaks, current_left, i)) res_times.append(peak_times[i]) current_left = i @@ -268,5 +274,6 @@ def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=Fa plt.show() return (res_peaks, res_times) + """ res_times is in seconds """ \ No newline at end of file diff --git a/main.py b/main.py index 9811389..77c08aa 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ def main(): offset = timing.offset.total_seconds() * 1000 print(beatmap.audio_filename) - amplitudes, timings = sound_process.process_song(beatmap.audio_filename, bpm, offset0=offset, n_iter_2=-1) + amplitudes, timings, frequencies = sound_process.process_song(beatmap.audio_filename, bpm, offset0=offset, n_iter_2=-1) # NOTE : remove n_iter_2 to map the whole music timings = [timedelta(seconds=x) for x in timings] diff --git a/sound_process.py b/sound_process.py index 1a267de..079c588 100755 --- a/sound_process.py +++ b/sound_process.py @@ -92,46 +92,38 @@ def round_t(id, sample_rate, bpm, div, offset, k0): def compress(Zxx): res = [] -def get_freq(song_name, offset, step, songlen, data, display=False): +def get_freq(song_name, times, width=1000, display=False): """ - for a given list of amplitudes, returns the corresponding peak frequencies + for a given list of times (in seconds), returns the corresponding peak frequencies """ - fft_list = [] - times = [] - current_time = offset - k = 0 - subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(offset+songlen), "-i", song_name, "crop.wav"]) + subprocess.run(["ffmpeg", "-ss", str(0), "-t", str(max(np.array(times))), "-i", song_name, "crop.wav"]) - sample_rate, global_data = wavfile.read("crop.wav") - #blit = int(len(global_data) / len(data)) - blit = int(sample_rate*step) + sample_rate, global_data = wavfile.read(song_name) + #blit = int(sample_rate*step) subprocess.run(["clear"]) subprocess.run(["rm", "crop.wav"]) - pfreq = scipy.fft.rfftfreq(blit, 1/sample_rate) + pfreq = scipy.fft.rfftfreq(2*width, 1/sample_rate) - print("len : ", len(global_data)) - print("len : ", len(data)) - - frequencies = [0 for s in range(len(data))] + frequencies = [0 for s in range(len(times))] print(len(pfreq)) - for s in range(len(data)): - if(data[s] != 0): - pff = scipy.fft.rfft(global_data[int(s*len(global_data)/len(data)):int(44100*step+int(s*len(global_data)/len(data)))]) - - mx = max(np.abs(pff)) - for id in range(len(pff)): - if frequencies[s] == 0 and np.abs(pff[id]) == mx: - frequencies[s] = pfreq[id] + for s in range(len(times)): + left = max(0, int(times[s]*44100)-width) + right = min(len(global_data), int(times[s]*44100)+width) + pff = scipy.fft.rfft(global_data[left:right]) - elif s != 0: - frequencies[s] = 0 + #print(len(pff), len(pfreq)) + + mx = max(np.abs(pff)) + for id in range(len(pff)): + if frequencies[s] == 0 and np.abs(pff[id]) == mx: + frequencies[s] = pfreq[id] if(display): - plt.plot([offset+t/1000 for t in range(len(data))], frequencies) + plt.plot(times, frequencies) plt.grid() plt.xlabel("Time (s)") plt.ylabel("Dominant frequency (Hz)") @@ -267,24 +259,6 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, print("Done") -def get_tpts(data, sample_rate, thr): - res = [] - for i in range(len(data)): - if(data[i] > thr): - res.append(i/sample_rate) - - for i in res: - print(i) - return res - -def test_sample(timelist): - for i in range(1,len(timelist)): - #os.system('play -n synth %s sin %s' % (0.05, 440)) - for k in range(random.randint(1, 10)): - print("E", end="") - print("F") - sleep(timelist[i]-timelist[i-1]) - def convert_tuple(data, times): """ Takes data and converts it to a list of tuples (amplitude, datetimes) @@ -348,8 +322,10 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh (snapped_data, times) = debug.snap3(datares, mintime=50, initial_plot=True, after_plot=True) #frequencies=get_freq(filtered_name, offset, div_len, div_len*n_iter, snapped_data, True) + frequencies = get_freq(filtered_name, times, display=True) + Path(f"{filename}_trimmed.wav").unlink() - return snapped_data, times + return snapped_data, times, frequencies ''' datares = debug.snap2(datares, 44100, bpm, first_offset=offset, div=divisor, show=True, adjust=True) @@ -359,7 +335,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh ''' def main(): - data = process_song("tetris_4.wav", 160, n_iter_2=48) + aa, bb, cc = process_song("tetris_4.wav", 160, n_iter_2=48) #print(data) print("Program finished with return 0") @@ -875,4 +851,22 @@ def filter_n_percent(song_name, offset, length, threshold, reduce, show): plt.show() return song_data + +def get_tpts(data, sample_rate, thr): + res = [] + for i in range(len(data)): + if(data[i] > thr): + res.append(i/sample_rate) + + for i in res: + print(i) + return res + +def test_sample(timelist): + for i in range(1,len(timelist)): + #os.system('play -n synth %s sin %s' % (0.05, 440)) + for k in range(random.randint(1, 10)): + print("E", end="") + print("F") + sleep(timelist[i]-timelist[i-1]) '''