Tried fixing snap issues (although it's snapped, but not in the right way I guess)

This commit is contained in:
Alexandre 2024-05-30 21:47:14 +02:00
parent e2e207c294
commit b957068977
2 changed files with 129 additions and 117 deletions

View File

@ -13,12 +13,12 @@ def main():
offset = timing.offset.total_seconds() * 1000 offset = timing.offset.total_seconds() * 1000
print(beatmap.audio_filename) print(beatmap.audio_filename)
data = sound_process.process_song(beatmap.audio_filename, int(bpm), offset0=offset, n_iter_2=48) data = sound_process.process_song(beatmap.audio_filename, int(bpm), offset0=offset, n_iter_2=32)
# NOTE : remove n_iter_2 to map the whole music # 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] 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._hit_objects = place.greedy(bpm, offset, timings, amplitudes)
beatmap.display_name = "TIPE's Extra" #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._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") beatmap.write_path("rewrite.osu")

View File

@ -17,114 +17,6 @@ from datetime import timedelta
print("Starting...\n") 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):
# converts audio data to have exactly 1 sample per millisecond (aka set sample_rate to 1000)
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]
# filter data associated with song_name to keep only the highest threshold% values
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 filter_n_percent_serial(song_name, offset, n_iter, step, threshold): def filter_n_percent_serial(song_name, offset, n_iter, step, threshold):
""" """
song_name : string song_name : string
@ -145,7 +37,7 @@ def filter_n_percent_serial(song_name, offset, n_iter, step, threshold):
for i in range(n_iter): for i in range(n_iter):
print(i, "/", n_iter) print(i, "/", n_iter)
print(i * step) #print(i * step)
song_data = global_data[int(i*step*sample_rate):int((i+1)*step*sample_rate)] song_data = global_data[int(i*step*sample_rate):int((i+1)*step*sample_rate)]
if(len(song_data) != 0): if(len(song_data) != 0):
@ -483,8 +375,12 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False):
print("Snap done") print("Snap done")
if(show): if(show):
new2 = [0.9 if new[i] != 0 else 0 for i in range(len(new))]
t = [j/1000+first_offset for j in range(len(new))] t = [j/1000+first_offset for j in range(len(new))]
beats = [0 for j in range(len(new))] beats_1 = [0 for j in range(len(new))]
beats_2 = [0 for k in range(len(new))]
beats_4 = [0 for l in range(len(new))]
k = 0 k = 0
current_t = first_offset current_t = first_offset
@ -494,13 +390,21 @@ def snap2(data, sample_rate, bpm, first_offset=0, div=4, show=False):
current_t = first_offset + k*60/(bpm*div) current_t = first_offset + k*60/(bpm*div)
while(1000*current_t < len(new)): while(1000*current_t < len(new)):
beats[int(1000*current_t)] = 16384 beats_4[int(1000*current_t)] = 0.9
if(k % 2 == 0):
beats_2[int(1000*current_t)] = 0.92
if(k % 4 == 0):
beats_1[int(1000*current_t)] = 0.94
k += 1 k += 1
current_t = first_offset + k*60/(bpm*div) current_t = first_offset + k*60/(bpm*div)
plt.plot(t, beats) plt.plot(t, new2, "bo")
plt.plot(t, new) plt.plot(t, beats_4, "r-")
plt.plot(t, beats_2, "y-")
plt.plot(t, beats_1, "g-")
plt.xlabel("Time (s)") plt.xlabel("Time (s)")
plt.ylabel("Amplitude") plt.ylabel("Amplitude")
plt.grid() plt.grid()
@ -557,7 +461,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh
return convert_tuple(datares, frequencies) return convert_tuple(datares, frequencies)
def main(): def main():
data = process_song("tetris_4.wav", 160, n_iter_2 = 32) data = process_song("tetris_4.wav", 160, n_iter_2=48, threshold=100)
#print(data) #print(data)
print("Program finished with return 0") print("Program finished with return 0")
@ -965,4 +869,112 @@ def void_freq(song_name, offset, songlen, increment, lthr, gthr):
wavfile.write('test.wav', sample_rate, np.array(audio_signal, dtype=np.int16)) wavfile.write('test.wav', sample_rate, np.array(audio_signal, dtype=np.int16))
print("Done") print("Done")
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):
# converts audio data to have exactly 1 sample per millisecond (aka set sample_rate to 1000)
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]
# filter data associated with song_name to keep only the highest threshold% values
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
''' '''