rewrite of get_frequencies to match new data structure

This commit is contained in:
Alexandre 2024-06-03 22:28:41 +02:00
parent a567d0ef0d
commit 3587adbe2f
3 changed files with 49 additions and 48 deletions

View File

@ -213,6 +213,12 @@ def get_spacing(data, sample_rate=44100, show=False, retrieve=False):
- dt is in ms - dt is in ms
''' '''
def avg(data, i, j):
res = 0
for e in range(i, min(len(data), j)):
res += data[e]
return (res/(min(len(data), j) - i))
def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=False): def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=False):
''' '''
explaination : explaination :
@ -241,7 +247,7 @@ def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=Fa
if(time_diff[i] > mintime): if(time_diff[i] > mintime):
segments.append([current_left, i]) segments.append([current_left, i])
seglen.append(peak_times[i]-peak_times[current_left]) seglen.append(peak_times[i]-peak_times[current_left])
res_peaks.append(500) res_peaks.append(avg(data_peaks, current_left, i))
res_times.append(peak_times[i]) res_times.append(peak_times[i])
current_left = i current_left = i
@ -268,5 +274,6 @@ def snap3(data, sample_rate=44100, mintime=10, initial_plot=False, after_plot=Fa
plt.show() plt.show()
return (res_peaks, res_times) return (res_peaks, res_times)
""" res_times is in seconds """

View File

@ -13,7 +13,7 @@ def main():
offset = timing.offset.total_seconds() * 1000 offset = timing.offset.total_seconds() * 1000
print(beatmap.audio_filename) print(beatmap.audio_filename)
amplitudes, timings = sound_process.process_song(beatmap.audio_filename, bpm, offset0=offset, n_iter_2=-1) amplitudes, timings, frequencies = sound_process.process_song(beatmap.audio_filename, bpm, offset0=offset, n_iter_2=-1)
# NOTE : remove n_iter_2 to map the whole music # NOTE : remove n_iter_2 to map the whole music
timings = [timedelta(seconds=x) for x in timings] timings = [timedelta(seconds=x) for x in timings]

View File

@ -92,46 +92,38 @@ def round_t(id, sample_rate, bpm, div, offset, k0):
def compress(Zxx): def compress(Zxx):
res = [] res = []
def get_freq(song_name, offset, step, songlen, data, display=False): def get_freq(song_name, times, width=1000, display=False):
""" """
for a given list of amplitudes, returns the corresponding peak frequencies for a given list of times (in seconds), returns the corresponding peak frequencies
""" """
fft_list = []
times = []
current_time = offset
k = 0
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(offset+songlen), "-i", song_name, "crop.wav"]) subprocess.run(["ffmpeg", "-ss", str(0), "-t", str(max(np.array(times))), "-i", song_name, "crop.wav"])
sample_rate, global_data = wavfile.read("crop.wav") sample_rate, global_data = wavfile.read(song_name)
#blit = int(len(global_data) / len(data)) #blit = int(sample_rate*step)
blit = int(sample_rate*step)
subprocess.run(["clear"]) subprocess.run(["clear"])
subprocess.run(["rm", "crop.wav"]) subprocess.run(["rm", "crop.wav"])
pfreq = scipy.fft.rfftfreq(blit, 1/sample_rate) pfreq = scipy.fft.rfftfreq(2*width, 1/sample_rate)
print("len : ", len(global_data)) frequencies = [0 for s in range(len(times))]
print("len : ", len(data))
frequencies = [0 for s in range(len(data))]
print(len(pfreq)) print(len(pfreq))
for s in range(len(data)): for s in range(len(times)):
if(data[s] != 0): left = max(0, int(times[s]*44100)-width)
pff = scipy.fft.rfft(global_data[int(s*len(global_data)/len(data)):int(44100*step+int(s*len(global_data)/len(data)))]) right = min(len(global_data), int(times[s]*44100)+width)
pff = scipy.fft.rfft(global_data[left:right])
mx = max(np.abs(pff)) #print(len(pff), len(pfreq))
for id in range(len(pff)):
if frequencies[s] == 0 and np.abs(pff[id]) == mx:
frequencies[s] = pfreq[id]
elif s != 0: mx = max(np.abs(pff))
frequencies[s] = 0 for id in range(len(pff)):
if frequencies[s] == 0 and np.abs(pff[id]) == mx:
frequencies[s] = pfreq[id]
if(display): if(display):
plt.plot([offset+t/1000 for t in range(len(data))], frequencies) plt.plot(times, frequencies)
plt.grid() plt.grid()
plt.xlabel("Time (s)") plt.xlabel("Time (s)")
plt.ylabel("Dominant frequency (Hz)") plt.ylabel("Dominant frequency (Hz)")
@ -267,24 +259,6 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr,
print("Done") print("Done")
def get_tpts(data, sample_rate, thr):
res = []
for i in range(len(data)):
if(data[i] > thr):
res.append(i/sample_rate)
for i in res:
print(i)
return res
def test_sample(timelist):
for i in range(1,len(timelist)):
#os.system('play -n synth %s sin %s' % (0.05, 440))
for k in range(random.randint(1, 10)):
print("E", end="")
print("F")
sleep(timelist[i]-timelist[i-1])
def convert_tuple(data, times): def convert_tuple(data, times):
""" """
Takes data and converts it to a list of tuples (amplitude, datetimes) Takes data and converts it to a list of tuples (amplitude, datetimes)
@ -348,8 +322,10 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh
(snapped_data, times) = debug.snap3(datares, mintime=50, initial_plot=True, after_plot=True) (snapped_data, times) = debug.snap3(datares, mintime=50, initial_plot=True, after_plot=True)
#frequencies=get_freq(filtered_name, offset, div_len, div_len*n_iter, snapped_data, True) #frequencies=get_freq(filtered_name, offset, div_len, div_len*n_iter, snapped_data, True)
frequencies = get_freq(filtered_name, times, display=True)
Path(f"{filename}_trimmed.wav").unlink() Path(f"{filename}_trimmed.wav").unlink()
return snapped_data, times return snapped_data, times, frequencies
''' '''
datares = debug.snap2(datares, 44100, bpm, first_offset=offset, div=divisor, show=True, adjust=True) datares = debug.snap2(datares, 44100, bpm, first_offset=offset, div=divisor, show=True, adjust=True)
@ -359,7 +335,7 @@ def process_song(filename, bpm, offset0=0, div_len_factor=1, n_iter_2=-1, thresh
''' '''
def main(): def main():
data = process_song("tetris_4.wav", 160, n_iter_2=48) aa, bb, cc = process_song("tetris_4.wav", 160, n_iter_2=48)
#print(data) #print(data)
print("Program finished with return 0") print("Program finished with return 0")
@ -875,4 +851,22 @@ def filter_n_percent(song_name, offset, length, threshold, reduce, show):
plt.show() plt.show()
return song_data return song_data
def get_tpts(data, sample_rate, thr):
res = []
for i in range(len(data)):
if(data[i] > thr):
res.append(i/sample_rate)
for i in res:
print(i)
return res
def test_sample(timelist):
for i in range(1,len(timelist)):
#os.system('play -n synth %s sin %s' % (0.05, 440))
for k in range(random.randint(1, 10)):
print("E", end="")
print("F")
sleep(timelist[i]-timelist[i-1])
''' '''