From d899f7260233487a2df71030c64e82c6e2e0417b Mon Sep 17 00:00:00 2001 From: alexandre Date: Fri, 3 May 2024 22:23:50 +0200 Subject: [PATCH] adding more stuff --- new-process.py | 433 +++++++++++++++++++++++++++++----------------- timing_points.csv | 99 +++++++++++ 2 files changed, 377 insertions(+), 155 deletions(-) create mode 100644 timing_points.csv diff --git a/new-process.py b/new-process.py index 0565b02..ed5aed8 100644 --- a/new-process.py +++ b/new-process.py @@ -11,6 +11,278 @@ import heapq print("Starting...\n") +def find_bpm_2(sample_rate, data, threshold, maxbpm, show): + mx = np.max(data) + min_spacing = (60*sample_rate)/maxbpm + k = 0 + while(k < len(data) and data[k]/mx < threshold): + k += 1 + + k += 1 + spacing = [] + current = 1 + progress = 0 + + while(k < len(data)): + if(k%(len(data)/100) == 0): + print(progress, "%") + progress += 1 + if(data[k]/mx >= threshold and current > min_spacing): + spacing.append(current) + current = 0 + else: + current += 1 + k += 1 + + + for x in range(len(spacing)): + spacing[x] = 60/(spacing[x]/sample_rate) + + digits = [i for i in range(len(spacing))] + if(show): + plt.plot(digits, spacing) + plt.xlabel("N") + plt.ylabel("BPM") + plt.grid() + plt.show() + + beat = np.mean(spacing) + error = np.std(spacing) + + return (np.round(beat, 3), np.round(error, 3)) + +def to_ms(song_data, sample_rate, offset): + new_data = [] + spacing = int(sample_rate * 0.001) + mx = max(song_data) + i = 0 + while(i < len(song_data)): + avg = 0 + for k in range(spacing): + if(i+spacing < len(song_data)): + avg += song_data[i+spacing] + avg = avg / spacing + new_data.append(avg) + i += spacing + + if(False): # pls dont kill me thx + t = [offset + j/1000 for j in range(len(new_data))] + plt.plot(t, new_data) + plt.xlabel("Time") + plt.ylabel("Amplitude") + plt.grid() + plt.show() + + return (new_data, len(new_data)) + +def filter_n_percent(song_name, offset, length, threshold, reduce, show): + # threshold is in ]0, 100] + + subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(length), "-i", song_name, "crop.wav"]) + + sample_rate, song_data = wavfile.read('crop.wav') + + subprocess.run(["clear"]) + subprocess.run(["rm", "crop.wav"]) + + if(reduce): + (song_data,e) = to_ms(song_data, 44100, 1) + sample_rate = 1000 + + 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") + + for idx in range(len(elements)): + is_locked[elements[idx][0]] = True + + for r in range(len(song_data)): + if(is_locked[r] == False): + song_data[r] = 0 + + if(show): + #print("EEEEE") + t = [offset + j/sample_rate for j in range(len(song_data))] + plt.plot(t, song_data) + plt.xlabel("Time") + plt.ylabel("Amplitude") + plt.grid() + plt.show() + + return song_data + + +def write_to_file_thr(sample_rate, song_data, offset, threshold, filename): + file = open(filename, 'w') + file.writelines('time,amplitude\n') + mx = max(song_data) + print("writing to output...") + for i in range(len(song_data)): + if(i%(len(song_data)//50) == 0): + print(i, "/", len(song_data)) + if(song_data[i]/mx > threshold): + file.writelines(str(np.round(offset + i/sample_rate, 3))) + file.writelines(',') + file.writelines(str(np.round(song_data[i], 0))) + file.writelines('\n') + +def smooth(data, thr, mergeThr, show): + mx = max(data) + for i in range(len(data)-mergeThr): + if(data[i]/mx > thr): + for k in range(1, mergeThr): + data[i+k] = 0 + if(show): + t = [j/1000 for j in range(len(data))] + plt.plot(t, data) + plt.xlabel("Time (not scaled to origin)") + plt.ylabel("Amplitude") + plt.grid() + plt.show() + + return data + +def round_t(id, sample_rate, bpm, div, offset, k0): + k = k0 + t = offset + k/(bpm*div) + while(t < id/sample_rate): + t = offset + k/(bpm*div) + k += 1 + + if(np.abs(t - id/sample_rate) < np.abs((t - 1/(bpm*div)) - id/sample_rate)): + return t + return (t - 1/(bpm*div), 0) + +def snap(data, sample_rate, bpm, offset, divisor, show): + 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(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 +if(True): + #data = filter_n_percent("worlds_end_3.wav", 74.582, 30, 0.3, reduce=False, show=False) + #data = filter_n_percent("no.wav", 1, 15, 0.3) + #da = find_bpm(44100, data, 100, 200, 1, 0) + + # def find_bpm_2(sample_rate, data, threshold, maxbpm): + #da = find_bpm_2(44100, data, 0.92, 240, show=False) + #print("BPM is", da[0], "with std of", da[1]) + + + data2 = filter_n_percent("worlds_end_3.wav", 74.582, 15, 0.2, reduce=False, show=True) + data2 = snap(data2, 44100, 178, 74.582, 4, show=True) + write_to_file_thr(1000, data2, 74.582, 0.02, "timing_points.csv") + ''' + data2 = filter_n_percent("no.wav", 1, 30, 0.8, reduce=True, show=True) + write_to_file_thr(1000, smooth(data2, 0.5, 50, show=True), 1, 0.02, "timing_points.csv") + ''' + + #data = to_ms(data, 44100, 1) + +print("Program finished with return 0") + + + +''' -------------------------------------------------------------------- ''' +''' -----------------------| Feuilles mortes |-------------------------- ''' +''' -------------------------------------------------------------------- ''' + + +''' +if(False): + #t, f, Zxx = fct("no.wav", 0, 0.032, 10, 5000, False) + #t, f, Zxx = fct("worlds_end_3.wav", 150.889, 0.032, 170.889, 3000, False) + #t, f, Zxx = fct("deltamax.wav", 9.992, 0.032, 114.318, 3000, False) + #t, f, Zxx = fct("deltamax.wav", 9.992, 0.032, 20, 3000, False) + #t, f, Zxx = fct("da^9.wav", 8.463, 0.032, 20, 5000, False) + t, f, Zxx = fct("13. Cosmic Mind.wav", 0, 0.032, 20, 5000, False) + #t, f, Zxx = fct("Furioso Melodia 44100.wav", 4, 0.032, 8, 3000, False) + #t, f, Zxx = fct("changing.wav", 0, 0.05, 3.9, 5000, False) + #fct("worlds_end_3.wav", 75, (60/178)/4, 75+2, 2500) + + plot_max(t, f, Zxx, True) + +if(False): + #(t, data) = peaks("worlds_end_3.wav", 0, 300, False, 0.92) + (t, data) = peaks("worlds_end_3.wav", 74.582, 6, False, 0.9) + #(t, data) = peaks("da^9.wav", 8.463, 301.924 - 8.463, False, 0.95) + #(t, data) = peaks("deltamax.wav", 8.463, 30101.924 - 8.463, False, 0.92) + da = find_bpm(t, 44100, data, 100, 200, 1, 10) + print("BPM data is", da)''' + + #data = [-1 for i in range(int(x))] + #ids = [-1 for i in range(int(x))] +''' + data = [] + ids = [] + for k in range(int(x)): + data.append(int(7*mx/10)) + ids.append(-1) + # structure there is [[index, value]...] + + i = 0 + calc = 0 + while(i < len(song_data)): + if(i%10 == 0): + print(i, "/", len(song_data)) + if(data[int(x)-1] < song_data[i]): + calc += 1 + #print("\n \n \n \n \n") + data[int(x)-1] = song_data[i] + ids[int(x)-1] = i + + k = int(x)-1 + #while(k < int(x) & data[0] > data[k]): + while(k > 0 and data[k-1] <= data[k]): + data[k], data[k-1] = data[k-1], data[k] + ids[k], ids[k-1] = ids[k-1], ids[k] + k -= 1 + + #print(data[int(x)-1], calc, "/", x) + + i += skip + i += 1 + + + for s in range(int(x)-1): + if(data[s] < data[s+1]): + print("Nope", s) + assert(0) +''' + + +''' def fct(song_name, offset, increment, songlen, maxfreq, display): to_cut = 20000//maxfreq global_Zxx = np.array([]) @@ -37,11 +309,11 @@ def fct(song_name, offset, increment, songlen, maxfreq, display): #print(len(Zxx)) #print(len(Zxx[0])) - #convert to mel - ''' + for i in range(len(Zxx)): for j in range(len(Zxx[i])): - Zxx[i][j] *= 1127*np.log(1+f[i]/700)''' + Zxx[i][j] *= 1127*np.log(1+f[i]/700) + t = np.array([current_time + x for x in t]) @@ -98,7 +370,7 @@ def plot_max(time, freq, Zxx, save): if(save): write_to_file(time, fres, maxres, 'output.csv') - ''' + '''''' plt.plot(time, fres, 'r') plt.grid() plt.xlabel("Time") @@ -109,7 +381,7 @@ def plot_max(time, freq, Zxx, save): plt.xlabel("Time") plt.ylabel("Maximun values") - plt.show()''' + plt.show()'''''' fig, (ax1, ax2) = plt.subplots(2) fig.suptitle('Top : time and frequencies\nBottom : time and max values') @@ -208,7 +480,7 @@ def find_bpm(sample_rate, data, minbpm, maxbpm, step, width): bpmlst.append(beat) scores.append(accuracy) - if(True): + if(False): plt.plot(bpmlst, scores) plt.xlabel("BPM") plt.ylabel("Score") @@ -216,153 +488,4 @@ def find_bpm(sample_rate, data, minbpm, maxbpm, step, width): plt.show() return (optimal, optimal_acc) - -def find_bpm_2(sample_rate, data, threshold, maxbpm): - mx = np.max(data) - min_spacing = (60*sample_rate)/maxbpm - k = 0 - while(k < len(data) and data[k]/mx < threshold): - k += 1 - - k += 1 - spacing = [] - current = 1 - progress = 0 - - while(k < len(data)): - if(k%(len(data)/100) == 0): - print(progress, "%") - progress += 1 - if(data[k]/mx >= threshold and current > min_spacing): - spacing.append(current) - current = 0 - else: - current += 1 - k += 1 - - - for x in range(len(spacing)): - spacing[x] = 60/(spacing[x]/sample_rate) - - digits = [i for i in range(len(spacing))] - if(True): - plt.plot(digits, spacing) - plt.xlabel("N") - plt.ylabel("BPM") - plt.grid() - plt.show() - - beat = np.mean(spacing) - error = np.std(spacing) - - return (np.round(beat, 3), np.round(error, 3)) - -def filter_n_percent(song_name, offset, length, threshold): - # threshold is in ]0, 100] - - subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(length), "-i", song_name, "crop.wav"]) - - sample_rate, song_data = wavfile.read('crop.wav') - - subprocess.run(["clear"]) - subprocess.run(["rm", "crop.wav"]) - - is_locked = [False for i in range(len(song_data))] - x = int((len(song_data)*threshold)//100) - print("X = ", x) - - mx = max(song_data) - - 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 r in range(len(song_data)): - if(is_locked[r] == False): - song_data[r] = 0 - - if(True): - t = [offset + j/sample_rate for j in range(len(song_data))] - plt.plot(t, song_data) - plt.xlabel("Time") - plt.ylabel("Amplitude") - plt.grid() - plt.show() - - return song_data - -if(False): - #t, f, Zxx = fct("no.wav", 0, 0.032, 10, 5000, False) - #t, f, Zxx = fct("worlds_end_3.wav", 150.889, 0.032, 170.889, 3000, False) - #t, f, Zxx = fct("deltamax.wav", 9.992, 0.032, 114.318, 3000, False) - #t, f, Zxx = fct("deltamax.wav", 9.992, 0.032, 20, 3000, False) - #t, f, Zxx = fct("da^9.wav", 8.463, 0.032, 20, 5000, False) - t, f, Zxx = fct("13. Cosmic Mind.wav", 0, 0.032, 20, 5000, False) - #t, f, Zxx = fct("Furioso Melodia 44100.wav", 4, 0.032, 8, 3000, False) - #t, f, Zxx = fct("changing.wav", 0, 0.05, 3.9, 5000, False) - #fct("worlds_end_3.wav", 75, (60/178)/4, 75+2, 2500) - - plot_max(t, f, Zxx, True) - -if(False): - #(t, data) = peaks("worlds_end_3.wav", 0, 300, False, 0.92) - (t, data) = peaks("worlds_end_3.wav", 74.582, 6, False, 0.9) - #(t, data) = peaks("da^9.wav", 8.463, 301.924 - 8.463, False, 0.95) - #(t, data) = peaks("deltamax.wav", 8.463, 30101.924 - 8.463, False, 0.92) - da = find_bpm(t, 44100, data, 100, 200, 1, 10) - print("BPM data is", da) - -if(True): - #data = filter_n_percent("worlds_end_3.wav", 74.582, 20, 0.1) - data = filter_n_percent("no.wav", 1, 10, 0.1) - #da = find_bpm(44100, data, 100, 200, 1, 0) - da = find_bpm_2(44100, data, 0.97, 240) - print("BPM is", da[0], "with std of", da[1]) - -print("Program finished with return 0") - - - - - #data = [-1 for i in range(int(x))] - #ids = [-1 for i in range(int(x))] -''' - data = [] - ids = [] - for k in range(int(x)): - data.append(int(7*mx/10)) - ids.append(-1) - # structure there is [[index, value]...] - - i = 0 - calc = 0 - while(i < len(song_data)): - if(i%10 == 0): - print(i, "/", len(song_data)) - if(data[int(x)-1] < song_data[i]): - calc += 1 - #print("\n \n \n \n \n") - data[int(x)-1] = song_data[i] - ids[int(x)-1] = i - - k = int(x)-1 - #while(k < int(x) & data[0] > data[k]): - while(k > 0 and data[k-1] <= data[k]): - data[k], data[k-1] = data[k-1], data[k] - ids[k], ids[k-1] = ids[k-1], ids[k] - k -= 1 - - #print(data[int(x)-1], calc, "/", x) - - i += skip - i += 1 - - - for s in range(int(x)-1): - if(data[s] < data[s+1]): - print("Nope", s) - assert(0) ''' \ No newline at end of file diff --git a/timing_points.csv b/timing_points.csv new file mode 100644 index 0000000..5e33d5e --- /dev/null +++ b/timing_points.csv @@ -0,0 +1,99 @@ +time,amplitude +74.834,27830 +75.003,32767 +75.087,32767 +75.508,32767 +75.593,25442 +75.677,29518 +75.846,32767 +76.014,26270 +76.183,32767 +76.267,27283 +76.52,32767 +76.857,32767 +77.194,32767 +77.278,26713 +77.531,32767 +77.615,27054 +77.699,29431 +77.868,32767 +77.952,27120 +78.037,31654 +78.205,32767 +78.542,32767 +78.626,24911 +78.711,25315 +78.879,32767 +78.964,29835 +79.216,32767 +79.301,27455 +79.469,27799 +79.553,32767 +79.722,25509 +79.89,32767 +79.975,25061 +80.059,25816 +80.228,32767 +80.312,28958 +80.48,29818 +80.565,32767 +80.902,32767 +81.239,32767 +81.323,26323 +81.407,26077 +81.576,32767 +81.829,30393 +81.913,32767 +82.166,27691 +82.25,32767 +82.587,32767 +82.671,27114 +82.924,32767 +83.008,28674 +83.093,27410 +83.261,32767 +83.346,25455 +83.43,27424 +83.598,32767 +83.851,25760 +83.935,32767 +84.02,27722 +84.104,32379 +84.273,32767 +84.525,24740 +84.61,32767 +84.694,27458 +84.778,27806 +84.947,32767 +85.115,31308 +85.284,32767 +85.368,26439 +85.621,32492 +85.705,29965 +85.958,32767 +86.126,32767 +86.211,29632 +86.295,32767 +86.632,32767 +86.716,26059 +86.801,25544 +86.969,32767 +87.306,32767 +87.39,25362 +87.475,30738 +87.643,32767 +87.812,28238 +87.896,25271 +87.98,32767 +88.233,26175 +88.317,32767 +88.486,25579 +88.655,32767 +88.739,27065 +88.823,29194 +88.992,32767 +89.076,26450 +89.16,25234 +89.329,32767 +89.413,29012 +89.497,28347