diff --git a/main.py b/main.py index 295a573..0143e7e 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,8 @@ def main(): offset = timing.offset.total_seconds() * 10e3 print(beatmap.audio_filename) - timings, amplitudes, freqs = sound_process.process_song(beatmap.audio_filename, bpm, offset=offset) + timings, amplitudes, freqs = 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 beatmap._hit_objects = place.greedy(bpm, offset, timings, amplitudes) #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, [], [],)] diff --git a/sound_process.py b/sound_process.py index dabcc3d..5427eb2 100755 --- a/sound_process.py +++ b/sound_process.py @@ -144,24 +144,26 @@ def filter_n_percent_serial(song_name, offset, n_iter, step, threshold): for i in range(n_iter): print(i, "/", n_iter) + print(i * step) song_data = global_data[int(i*step*sample_rate):int((i+1)*step*sample_rate)] - mx = max(song_data) - - is_locked = [False for i in range(len(song_data))] - x = int((len(song_data)*threshold)//100) - #print("X = ", x) + if(len(song_data) != 0): + mx = max(song_data) + + is_locked = [False for i in range(len(song_data))] + x = int((len(song_data)*threshold)//100) + #print("X = ", x) - #print("Retreiving the", int(x), "/", len(song_data), "highest values") - elements = heapq.nlargest(int(x), enumerate(song_data), key=lambda x: x[1]) - #print("Done") + #print("Retreiving the", int(x), "/", len(song_data), "highest values") + elements = heapq.nlargest(int(x), enumerate(song_data), key=lambda x: x[1]) + #print("Done") - for idx in range(len(elements)): - is_locked[elements[idx][0]] = True + for idx in range(len(elements)): + is_locked[elements[idx][0]] = True - for r in range(len(song_data)): - if(is_locked[r] == False): - global_data[r+int(i*step*sample_rate)] = 0 + for r in range(len(song_data)): + if(is_locked[r] == False): + global_data[r+int(i*step*sample_rate)] = 0 return global_data @@ -234,6 +236,9 @@ def compress(Zxx): res = [] def get_freq(song_name, offset, step, songlen, data, display=False): + """ + for a given list of amplitudes, returns the corresponding peak frequencies + """ fft_list = [] times = [] current_time = offset @@ -269,7 +274,7 @@ def get_freq(song_name, offset, step, songlen, data, display=False): frequencies[s] = 0 if(display): - plt.plot([t/1000 for t in range(len(data))], frequencies) + plt.plot([offset+t/1000 for t in range(len(data))], frequencies) plt.grid() plt.xlabel("Time (s)") plt.ylabel("Dominant frequency (Hz)") @@ -279,7 +284,7 @@ def get_freq(song_name, offset, step, songlen, data, display=False): return frequencies -def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, ampthr, ampfreq, ampval, leniency, write, linear, output_file="trimmed.wav"): +def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, ampthr, ampfreq, ampval, leniency, write, linear, is_stereo, output_file="trimmed.wav"): """ song_name : string offset : int @@ -302,12 +307,22 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"]) - sample_rate, global_data = wavfile.read("crop.wav") + sample_rate, raw_global_data = wavfile.read("crop.wav") blit = int(sample_rate*increment) + global_data = [0 for i in range(len(raw_global_data))] + subprocess.run(["clear"]) subprocess.run(["rm", "crop.wav"]) + if(is_stereo): + print("Converting to mono...") + for x in range(len(raw_global_data)): + global_data[x] = raw_global_data[x][0] + raw_global_data[x][1] + + else: + global_data = raw_global_data + #print("Blit :", blit) pfreq = scipy.fft.rfftfreq(blit, 1/sample_rate) @@ -406,10 +421,11 @@ def get_songlen(filename): retrieves the length of the song in seconds """ sample_rate, global_data = wavfile.read(filename) + print("LEN :", len(global_data)/sample_rate) return (len(global_data)/sample_rate) -def process_song(filename, bpm, offset=0, div_len_factor=1, n_iter_2=-1, threshold=0.5, divisor=4): +def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, threshold=0.5, divisor=4): """ filename : string (name of the song) offset : int [+] (song mapping will start from this time in seconds, default is 0) @@ -420,21 +436,24 @@ def process_song(filename, bpm, offset=0, div_len_factor=1, n_iter_2=-1, thresho divisor : int [+] (beat divisor used to snap the notes, default is 4) """ + offset = offset0/1000 + div_len = div_len_factor*60/bpm-0.01 n_iter = n_iter_2 + song_len = get_songlen(filename) + if(n_iter == -1): - song_len = get_songlen(filename) - n_iter = int(song_len/div_len)-1/3 + n_iter = int((song_len-offset/1000)/div_len)-4 filtered_name = f"{filename}_trimmed.wav" - void_freq(filename, offset, offset+div_len*(n_iter+1)+0.01, 4*60/bpm, minfreq=0, maxfreq=330, upperthr=5000, ampthr=60, ampfreq = 1200, ampval = 5.0, leniency = 0.005, write=True, linear=False, output_file=filtered_name) + 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, is_stereo=True, 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) frequencies = get_freq(filtered_name, offset, div_len, div_len*n_iter, datares, True) - #Path(f"{filename}_trimmed.wav").unlink() + Path(f"{filename}_trimmed.wav").unlink() return convert_tuple(datares, frequencies)