Merge snapping fixes from main

This commit is contained in:
Thibaud 2024-05-30 20:35:07 +02:00
commit acd0f68e4c
2 changed files with 113 additions and 43 deletions

View File

@ -13,12 +13,11 @@ 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) 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 # 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._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

@ -195,44 +195,6 @@ def round_t(id, sample_rate, bpm, div, offset, k0):
return t return t
return (t - 1/(bpm*div), 0) 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): def compress(Zxx):
res = [] res = []
@ -328,11 +290,17 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr,
subprocess.run(["clear"]) subprocess.run(["clear"])
subprocess.run(["rm", "crop.wav"]) subprocess.run(["rm", "crop.wav"])
a = 0
if(is_data_stereo(raw_global_data)): if(is_data_stereo(raw_global_data)):
print("Converting to mono...") print("Converting to mono...")
for x in range(len(raw_global_data)): for x in range(len(raw_global_data)):
global_data[x] = raw_global_data[x][0]/2 + raw_global_data[x][1]/2 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: else:
global_data = raw_global_data global_data = raw_global_data
@ -438,6 +406,108 @@ def get_songlen(filename):
return (len(global_data)/sample_rate) 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
"""
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
while(current_t < 0):
k += 1
current_t = first_offset + k*60/(bpm*div)
for j in range(len(new)):
if(j/1000 > current_t):
k += 1
current_t = first_offset + k*60/(bpm*div)
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
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)
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: 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. Converts the song to .wav, only if it's not already in wave format.
@ -480,13 +550,14 @@ 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, 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) #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 = filter_n_percent_serial(filtered_name, offset, n_iter, div_len, threshold)
datares = snap(datares, 44100, bpm, 4, False) #datares = snap(datares, 44100, bpm, 4, True)
frequencies = get_freq(filtered_name, offset, div_len, div_len*n_iter, datares, False) 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() Path(f"{filename}_trimmed.wav").unlink()
return convert_tuple(datares, frequencies) return convert_tuple(datares, frequencies)
def main(): def main():
data = process_song("audio.mp3", 160, n_iter_2 = 64) data = process_song("tetris_4.wav", 160, n_iter_2 = 32)
#print(data) #print(data)
print("Program finished with return 0") print("Program finished with return 0")