Compare commits

..

No commits in common. "cba14e4782ca5533cb0c90ed578607e24f49e3d5" and "9e7c18930f5b2f5afd5e5f461c667973bf4540ae" have entirely different histories.

6 changed files with 37 additions and 49 deletions

BIN
audio.wav

Binary file not shown.

View File

@ -27,11 +27,10 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize):
# remove high_pitched/low-pitched frequencies
minfreq = 110
maxfreq = 440*6
maxfreq = 440*8
# cutting the song to only keep the one we're interested in
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen), "-i", song_name, "crop.wav"], shell=False)
subprocess.run(["clear"])
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False)
# extracting data from cropped song
sample_rate, raw_song_data = wavfile.read("crop.wav")
@ -51,9 +50,6 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize):
else:
song_data = raw_song_data
print("\nSampleRate : ", sample_rate)
print("SegSize : ", blit)
# remove the copy of the song
subprocess.run(["rm", "crop.wav"], shell=False)
@ -81,7 +77,6 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize):
# calculate the fft, append it to fft_list
pff = scp.fft.rfft(song_data[int(current_time*sample_rate):int(sample_rate*(current_time+segsize))])
fft_list.append(pff)
#print("(k =", k, ") :", left_id, "to", right_id)
# just to avoid what causes 0.1 + 0.1 == 0.2 to be False
k += 1
@ -103,7 +98,7 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize):
current_fmax = 0
for j in range(len(fft_list[i])):
if(j < len(pfreq) and pfreq[j] < maxfreq and pfreq[j] >= minfreq and np.abs(fft_list[i][j]) > current_max):
if(pfreq[j] < maxfreq and pfreq[j] >= minfreq and np.abs(fft_list[i][j]) > current_max):
current_max = np.abs(fft_list[i][j])
current_fmax = pfreq[j]
@ -118,8 +113,15 @@ def void_freq_clean(song_name, offset, songlen, segsize, minfreq, maxfreq, ampth
# removes unnecessary frequencies/amps from a song
#ampthr is in [0, 1]
# remove high_pitched/low-pitched frequencies
minfreq = 110
maxfreq = 440*8
# cutting the song to only keep the one we're interested in
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False)
# extracting data from cropped song
sample_rate, raw_song_data = wavfile.read(song_name)
sample_rate, raw_song_data = wavfile.read("crop.wav")
blit = int(sample_rate*segsize) # Te
song_data = [0 for i in range(len(raw_song_data))]
@ -206,15 +208,13 @@ def void_freq_clean(song_name, offset, songlen, segsize, minfreq, maxfreq, ampth
res = np.array(res)
wavfile.write(output_name, sample_rate, res)
def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent, divlen):
def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent):
# returns a list with the percent% peak amplitudes alongside the sample rate
# /!\ song_name is specified to be a list, NOT a list of couples (aka song is mono)
# segsize is in seconds
# divlen is in seconds
# cutting the song to only keep the one we're interested in
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen), "-i", song_name, "crop.wav"], shell=False)
subprocess.run(["clear"])
subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen+offset), "-i", song_name, "crop.wav"], shell=False)
# extracting data from cropped song
sample_rate, raw_song_data = wavfile.read("crop.wav")
@ -239,24 +239,12 @@ def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent, divlen)
is_locked = [False for i in range(len(song_data))]
x = int((len(song_data)*percent)//100)
# length of segments
seglen = int(divlen*sample_rate)
# current offset
curptr = 0
print("Retreiving the", int(x), "/", len(song_data), "highest values")
while(curptr < len(song_data)):
left = curptr
right = min(len(song_data), curptr+seglen)
elements = heapq.nlargest(int(x), enumerate(song_data), key=lambda x: x[1])
#returns a list of couples [id, value]
elements = heapq.nlargest(int(x), enumerate(song_data[left:right]), key=lambda x: x[1])
for idx in range(len(elements)):
is_locked[elements[idx][0]+left] = True
curptr += seglen
is_locked[elements[idx][0]] = True
for r in range(len(song_data)):
if(is_locked[r] == False):
@ -303,7 +291,7 @@ def convert_to_wav(song_name:str, output_file="audio.wav") -> str:
return output_file
return song_name
def retrieve_all_from_song(filename, t0, t1, bpm, dta=0.001, dtf=0.01, threshold=0.06, show=True):
def retrieve_all_from_song(filename, t0, t1, dt=0.001, threshold=0.1):
# dt = sample interval
# threshold is in percent
@ -314,37 +302,37 @@ def retrieve_all_from_song(filename, t0, t1, bpm, dta=0.001, dtf=0.01, threshold
# converts format to .wav
new_fn = convert_to_wav(filename)
# crop the song to the part that will be mapped
subprocess.run(["ffmpeg", "-ss", str(t0), "-t", str(t1), "-i", new_fn, "crop0.wav"], shell=False)
subprocess.run(["clear"])
sample_rate, _ = wavfile.read("crop0.wav")
print("Filtering song...")
#void_freq_clean(new_fn, t0, t1-t0, dt, 200, 2500, 0.05, "crop1.wav")
void_freq_clean(new_fn, t0, t1-t0, dt, 200, 2500, 0.05, "crop1.wav")
print("Now retrieving the frequencies")
(maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1, dtf)
(maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1-t0, dt)
print("Now retrieving the amplitudes")
amps = retrieve_dominant_amps(new_fn, t0, t1, dta, threshold, 4/(bpm/60))
amps = retrieve_dominant_amps(new_fn, t0, t1-t0, dt, threshold)
print("Len of freqs : ", len(maxlist), "|", len(maxamps))
print("Len of amps : ", len(maxlist), "|", len(amps))
maxa = amps[0]
for jj in amps:
if(jj > maxa):
maxa = jj
timesF = [t0 + dt*k for k in range(len(maxlist))]
timesA = [t0 + dt*k for k in range(len(amps))]
for i in range(len(amps)):
amps[i] = (amps[i] * 2000) / maxa
if(show):
timesF = [t0 + dtf*k for k in range(len(maxlist))]
timesA = [t0 + dta*k for k in range(len(amps))]
plt.plot(timesA, amps)
plt.plot(timesF, maxlist)
plt.show()
# free()
plt.plot(timesA, amps)
plt.show()
retrieve_all_from_song("ctype.mp3", 0, 5, 149.3, dtf=1/(149.3/60)/8)
# free()
subprocess.run(["rm", "crop0.wav"], shell=False)
retrieve_all_from_song("tetris_4.wav", 0, 5)
print("yipee")

BIN
crop.wav

Binary file not shown.

BIN
crop1.wav

Binary file not shown.

BIN
ctype.mp3

Binary file not shown.

Binary file not shown.