From f28e50e6432ad74e49d6fa23881fe33190450c7a Mon Sep 17 00:00:00 2001 From: Alexandre Date: Fri, 31 May 2024 20:37:02 +0200 Subject: [PATCH] Added resnap function --- main.py | 2 +- sound_process.py | 53 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 240726d..6d83827 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, n_iter_2=32) + data = sound_process.process_song(beatmap.audio_filename, int(bpm), offset0=offset, n_iter_2=-1) # 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 54582ea..d6e0a00 100755 --- a/sound_process.py +++ b/sound_process.py @@ -335,7 +335,36 @@ def snap(data, sample_rate, bpm, divisor, show=False): return new -def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): +def adjust_timings(raw_data, snapped_data, indexes, thr=100): + """ + adjusts weirdly snapped notes + """ + current = 0 + + while(current < len(indexes)): + + if(current < len(indexes) - 3 and current % 2 == 1): # on a 1/4 beat + if(snapped_data[indexes[current]] > thr and snapped_data[indexes[current+1]] > thr and snapped_data[indexes[current+2]] > thr and snapped_data[indexes[current+3]] <= thr): + # -XXX_ + snapped_data[indexes[current+3]] = snapped_data[indexes[current+2]] + snapped_data[indexes[current+2]] = 0 + + if(current > 0 and current < len(indexes) - 1 and current % 2 == 1): + if(snapped_data[indexes[current]] > thr and (snapped_data[indexes[current+1]] < thr or snapped_data[indexes[current-1]] < thr)): + #_X_ + '''if(snapped_data[indexes[current-1]] < thr and raw_data[indexes[current-1]] > raw_data[indexes[current+1]]): + snapped_data[indexes[current-1]] = snapped_data[indexes[current]] + else: + snapped_data[indexes[current+1]] = snapped_data[indexes[current]]''' + + snapped_data[indexes[current]] = 0 + + current += 1 + + print("Resnap done") + return snapped_data + +def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False, adjust=False): """ data : list(int) sample_rate : int @@ -344,6 +373,9 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): song_len = int(len(data)/sample_rate) + indexes = [] + app = True + reduced = [0 for i in range(song_len*1000)] new = [0 for i in range(song_len*1000)] @@ -367,13 +399,24 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): if(j/1000 > current_t): k += 1 current_t = first_offset + k*60/(bpm*div) + app = True y = int(current_t*1000) if(y < len(new)): new[y] = max(new[y], reduced[j]) - print("Snap done") + if(app): + indexes.append(y) + app = False + print("Snap done") + + if(adjust): + print("Len :", len(indexes)) + + new = adjust_timings(reduced, new, indexes) + + if(show): new2 = [0.9 if new[i] != 0 else 0 for i in range(len(new))] @@ -409,7 +452,7 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False): plt.ylabel("Amplitude") plt.grid() plt.show() - + return new def convert_to_wav(song_name:str, output_file="audio.wav") -> str: @@ -447,7 +490,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh song_len = get_songlen(filename) if(n_iter == -1): - n_iter = int((song_len-offset/1000)/div_len)-4 + n_iter = int((song_len-offset/1000)/div_len)-1 filtered_name = f"{filename}_trimmed.wav" @@ -455,7 +498,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh #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 = snap2(datares, 44100, bpm, first_offset=offset, div=4, show=True) + datares = snap2(datares, 44100, bpm, first_offset=offset, div=4, show=True, adjust=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)