From 0fcad4174a6795f981d129623b0b90f33ab0ad87 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Wed, 29 May 2024 21:32:38 +0200 Subject: [PATCH 1/3] Fixed snap error --- main.py | 2 +- sound_process.py | 124 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 86 insertions(+), 40 deletions(-) diff --git a/main.py b/main.py index 3d29283..e795421 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ def main(): offset = timing.offset.total_seconds() * 1000 print(beatmap.audio_filename) - data = sound_process.process_song(beatmap.audio_filename, int(bpm), offset0=offset) + data = sound_process.process_song(beatmap.audio_filename, int(bpm), offset0=offset, n_iter_2=48) # NOTE : remove n_iter_2 to map the whole music timings, amplitudes, freqs = [x[0] for x in data], [x[1] for x in data], [x[2] for x in data] diff --git a/sound_process.py b/sound_process.py index 9df7306..da8b1c6 100755 --- a/sound_process.py +++ b/sound_process.py @@ -195,44 +195,6 @@ def round_t(id, sample_rate, bpm, div, offset, k0): return t return (t - 1/(bpm*div), 0) -def snap(data, sample_rate, bpm, divisor, show=False): - # adjust time amplitudes to match the given BPM - new = [0 for x in range(int(1000*len(data)/sample_rate))] # 1pt per millisecond - print("old =", len(data)) - print("len =", 1000*len(data)/sample_rate) - k = 0 - t = 0 - percent = 0 - for i in range(len(data)): - - while(t < i/sample_rate): - t = k/(bpm*divisor) - k += 60 - - ''' - if(np.abs(i/sample_rate - k/(bpm*divisor)) > np.abs(i/sample_rate - (k-60)/(bpm*divisor))): - k -= 60 - t = k/(bpm*divisor)''' - - if(i%(len(data)//100) == 0): - print(percent, "%") - percent += 1 - - if(int(t*1000) < len(new)): - new[int(t*1000)] = max(data[i], new[int(t*1000)]) - else: - new[len(new)-1] = max(data[i], new[len(new)-1]) - - if(show): - t = [j/1000 for j in range(len(new))] - plt.plot(t, new) - plt.xlabel("Time (e)") - plt.ylabel("Amplitude") - plt.grid() - plt.show() - - return new - def compress(Zxx): res = [] @@ -328,10 +290,16 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, subprocess.run(["clear"]) subprocess.run(["rm", "crop.wav"]) + a = 0 + if(is_data_stereo(raw_global_data)): print("Converting to mono...") for x in range(len(raw_global_data)): global_data[x] = raw_global_data[x][0]/2 + raw_global_data[x][1]/2 + + if(x % (int(len(raw_global_data)/100)) == 0): + print(a, "/ 100") + a += 1 else: global_data = raw_global_data @@ -438,6 +406,83 @@ def get_songlen(filename): return (len(global_data)/sample_rate) +def snap(data, sample_rate, bpm, divisor, show=False): + # adjust time amplitudes to match the given BPM + new = [0 for x in range(int(1000*len(data)/sample_rate))] # 1pt per millisecond + print("old =", len(data)) + print("len =", 1000*len(data)/sample_rate) + k = 0 + t = 0 + percent = 0 + for i in range(len(data)): + + while(t < i/sample_rate): + t = k/(bpm*divisor) + k += 60 + + ''' + if(np.abs(i/sample_rate - k/(bpm*divisor)) > np.abs(i/sample_rate - (k-60)/(bpm*divisor))): + k -= 60 + t = k/(bpm*divisor)''' + + if(i%(len(data)//100) == 0): + print(percent, "%") + percent += 1 + + if(int(t*1000) < len(new)): + new[int(t*1000)] = max(data[i], new[int(t*1000)]) + else: + new[len(new)-1] = max(data[i], new[len(new)-1]) + + if(show): + t = [j/1000 for j in range(len(new))] + plt.plot(t, new) + plt.xlabel("Time (e)") + plt.ylabel("Amplitude") + plt.grid() + plt.show() + + return new + +def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): + """ + data : list(int) + sample_rate : int + bpm = float + """ + + new = [0 for i in range(int(1000*len(data)/sample_rate))] + + k = 0 + current_t = first_offset + + for i in range(len(data)): + + if(i/sample_rate > current_t): + k += 1 + current_t = first_offset + k*60/(bpm*div) + + x = int(current_t*1000) + if(x < len(new)): + new[x] = max(new[x], data[i]) + + if(show): + t = [j/1000+first_offset for j in range(len(new))] + beats = [0 for j in range(len(new))] + k = 0 + while((first_offset + k*60/(bpm*div)*1000 < len(new))): + beats[int(first_offset + k*60/(bpm*div)*1000)] = 16384 + k += 1 + + plt.plot(t, beats) + plt.plot(t, new) + plt.xlabel("Time (s)") + plt.ylabel("Amplitude") + plt.grid() + plt.show() + + return new + def convert_to_wav(song_name:str, output_file="audio.wav") -> str: """ Converts the song to .wav, only if it's not already in wave format. @@ -480,7 +525,8 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh void_freq(filename, offset, min(song_len, offset+div_len*(n_iter+1)+0.01), 4*60/bpm, minfreq=0, maxfreq=220, upperthr=5000, ampthr=60, ampfreq = 1200, ampval = 5.0, leniency = 0.005, write=True, linear=False, output_file=filtered_name) #void_freq(filename, offset, offset+div_len*(n_iter+1)+0.01, 4*60/bpm, minfreq=0, maxfreq=330, upperthr=2500, ampthr=60, ampfreq = 1200, ampval = 1/2000, leniency = 0.0, write=True, linear=True, output_file=filtered_name) datares = filter_n_percent_serial(filtered_name, offset, n_iter, div_len, threshold) - datares = snap(datares, 44100, bpm, 4, True) + #datares = snap(datares, 44100, bpm, 4, True) + datares = snap2(datares, 44100, bpm, first_offset=offset, div=4, show=True) frequencies = get_freq(filtered_name, offset, div_len, div_len*n_iter, datares, True) Path(f"{filename}_trimmed.wav").unlink() return convert_tuple(datares, frequencies) From e2e207c2940b1f075ebc026b27252c1326bbb7f1 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Thu, 30 May 2024 19:58:35 +0200 Subject: [PATCH 2/3] Attempt to fix snap --- sound_process.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/sound_process.py b/sound_process.py index da8b1c6..53325a8 100755 --- a/sound_process.py +++ b/sound_process.py @@ -419,7 +419,6 @@ def snap(data, sample_rate, bpm, divisor, show=False): while(t < i/sample_rate): t = k/(bpm*divisor) k += 60 - ''' if(np.abs(i/sample_rate - k/(bpm*divisor)) > np.abs(i/sample_rate - (k-60)/(bpm*divisor))): k -= 60 @@ -451,28 +450,54 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): bpm = float """ - new = [0 for i in range(int(1000*len(data)/sample_rate))] + song_len = int(len(data)/sample_rate) + reduced = [0 for i in range(song_len*1000)] + new = [0 for i in range(song_len*1000)] + + # build the reduced version + for i in range(len(data)): + x = int(i*1000/sample_rate) + if(x < len(reduced)): + reduced[x] = max(reduced[x], data[i]) + + + print("Build done") + # snap k = 0 current_t = first_offset - for i in range(len(data)): + while(current_t < 0): + k += 1 + current_t = first_offset + k*60/(bpm*div) - if(i/sample_rate > current_t): + for j in range(len(new)): + if(j/1000 > current_t): k += 1 current_t = first_offset + k*60/(bpm*div) - x = int(current_t*1000) - if(x < len(new)): - new[x] = max(new[x], data[i]) + y = int(current_t*1000) + if(y < len(new)): + new[y] = max(new[y], reduced[j]) + + print("Snap done") if(show): t = [j/1000+first_offset for j in range(len(new))] beats = [0 for j in range(len(new))] + k = 0 - while((first_offset + k*60/(bpm*div)*1000 < len(new))): - beats[int(first_offset + k*60/(bpm*div)*1000)] = 16384 + current_t = first_offset + + while(current_t < 0): k += 1 + current_t = first_offset + k*60/(bpm*div) + + while(1000*current_t < len(new)): + beats[int(1000*current_t)] = 16384 + + k += 1 + current_t = first_offset + k*60/(bpm*div) plt.plot(t, beats) plt.plot(t, new) @@ -532,7 +557,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh return convert_tuple(datares, frequencies) def main(): - data = process_song("tetris_4.wav", 160, n_iter_2 = 64) + data = process_song("tetris_4.wav", 160, n_iter_2 = 32) #print(data) print("Program finished with return 0") From ee04fd795874f2a87738e855e2621a0f7ef819a5 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Thu, 30 May 2024 20:31:49 +0200 Subject: [PATCH 3/3] Fix broken line in main.py --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index e795421..5a09042 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,6 @@ def main(): timings, amplitudes, freqs = [x[0] for x in data], [x[1] for x in data], [x[2] for x in data] beatmap._hit_objects = place.greedy(bpm, offset, timings, amplitudes) - beatmap.display_name = "TIPE's Extra" #beatmap._hit_objects = [sl.Slider(sl.Position(0, 0), timedelta(milliseconds=3), timedelta(milliseconds=130), 0, sl.curve.Linear([sl.Position(0, 0), sl.Position(100, 100)], 100), 100, 2, 1, 1, 1, timing.ms_per_beat, [], [],)] beatmap.write_path("rewrite.osu")