Fixed snap error

This commit is contained in:
Alexandre 2024-05-29 21:32:38 +02:00
parent d2af367f2d
commit 0fcad4174a
2 changed files with 86 additions and 40 deletions

View File

@ -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)
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
timings, amplitudes, freqs = [x[0] for x in data], [x[1] for x in data], [x[2] for x in data]

View File

@ -195,44 +195,6 @@ def round_t(id, sample_rate, bpm, div, offset, k0):
return t
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):
res = []
@ -328,11 +290,17 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr,
subprocess.run(["clear"])
subprocess.run(["rm", "crop.wav"])
a = 0
if(is_data_stereo(raw_global_data)):
print("Converting to mono...")
for x in range(len(raw_global_data)):
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:
global_data = raw_global_data
@ -438,6 +406,83 @@ def get_songlen(filename):
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
"""
new = [0 for i in range(int(1000*len(data)/sample_rate))]
k = 0
current_t = first_offset
for i in range(len(data)):
if(i/sample_rate > current_t):
k += 1
current_t = first_offset + k*60/(bpm*div)
x = int(current_t*1000)
if(x < len(new)):
new[x] = max(new[x], data[i])
if(show):
t = [j/1000+first_offset for j in range(len(new))]
beats = [0 for j in range(len(new))]
k = 0
while((first_offset + k*60/(bpm*div)*1000 < len(new))):
beats[int(first_offset + k*60/(bpm*div)*1000)] = 16384
k += 1
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:
"""
Converts the song to .wav, only if it's not already in wave format.
@ -480,7 +525,8 @@ 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, 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 = snap(datares, 44100, bpm, 4, True)
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()
return convert_tuple(datares, frequencies)