big change to amplitude fct

This commit is contained in:
Alexandre 2024-10-07 17:52:03 +02:00
parent 542b6e9996
commit cba14e4782
5 changed files with 25 additions and 20 deletions

BIN
audio.wav

Binary file not shown.

View File

@ -30,7 +30,7 @@ def retrieve_dominant_freqs(song_name, offset, songlen, segsize):
maxfreq = 440*6 maxfreq = 440*6
# cutting the song to only keep the one we're interested in # 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) subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen), "-i", song_name, "crop.wav"], shell=False)
subprocess.run(["clear"]) subprocess.run(["clear"])
# extracting data from cropped song # extracting data from cropped song
@ -206,13 +206,14 @@ def void_freq_clean(song_name, offset, songlen, segsize, minfreq, maxfreq, ampth
res = np.array(res) res = np.array(res)
wavfile.write(output_name, sample_rate, res) wavfile.write(output_name, sample_rate, res)
def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent): def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent, divlen):
# returns a list with the percent% peak amplitudes alongside the sample rate # 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) # /!\ song_name is specified to be a list, NOT a list of couples (aka song is mono)
# segsize is in seconds # segsize is in seconds
# divlen is in seconds
# cutting the song to only keep the one we're interested in # 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) subprocess.run(["ffmpeg", "-ss", str(offset), "-t", str(songlen), "-i", song_name, "crop.wav"], shell=False)
subprocess.run(["clear"]) subprocess.run(["clear"])
# extracting data from cropped song # extracting data from cropped song
@ -238,12 +239,24 @@ def retrieve_dominant_amps(song_name, offset, songlen, segsize, percent):
is_locked = [False for i in range(len(song_data))] is_locked = [False for i in range(len(song_data))]
x = int((len(song_data)*percent)//100) x = int((len(song_data)*percent)//100)
print("Retreiving the", int(x), "/", len(song_data), "highest values") # length of segments
elements = heapq.nlargest(int(x), enumerate(song_data), key=lambda x: x[1]) seglen = int(divlen*sample_rate)
#returns a list of couples [id, value]
for idx in range(len(elements)): # current offset
is_locked[elements[idx][0]] = True 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)
#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
for r in range(len(song_data)): for r in range(len(song_data)):
if(is_locked[r] == False): if(is_locked[r] == False):
@ -290,7 +303,7 @@ def convert_to_wav(song_name:str, output_file="audio.wav") -> str:
return output_file return output_file
return song_name return song_name
def retrieve_all_from_song(filename, t0, t1, dta=0.001, dtf=0.01, threshold=0.1, show=True): def retrieve_all_from_song(filename, t0, t1, bpm, dta=0.001, dtf=0.01, threshold=0.06, show=True):
# dt = sample interval # dt = sample interval
# threshold is in percent # threshold is in percent
@ -301,20 +314,14 @@ def retrieve_all_from_song(filename, t0, t1, dta=0.001, dtf=0.01, threshold=0.1,
# converts format to .wav # converts format to .wav
new_fn = convert_to_wav(filename) 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...") 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") print("Now retrieving the frequencies")
(maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1-t0, dtf) (maxlist, maxamps) = retrieve_dominant_freqs(new_fn, t0, t1, dtf)
print("Now retrieving the amplitudes") print("Now retrieving the amplitudes")
amps = retrieve_dominant_amps(new_fn, t0, t1-t0, dta, threshold) amps = retrieve_dominant_amps(new_fn, t0, t1, dta, threshold, 4/(bpm/60))
print("Len of freqs : ", len(maxlist), "|", len(maxamps)) print("Len of freqs : ", len(maxlist), "|", len(maxamps))
print("Len of amps : ", len(maxlist), "|", len(amps)) print("Len of amps : ", len(maxlist), "|", len(amps))
@ -336,11 +343,9 @@ def retrieve_all_from_song(filename, t0, t1, dta=0.001, dtf=0.01, threshold=0.1,
plt.show() plt.show()
# free() # free()
subprocess.run(["rm", "crop0.wav"], shell=False)
retrieve_all_from_song("tetris_4.wav", 0, 5, dtf=0.375/2) retrieve_all_from_song("ctype.mp3", 0, 5, 149.3, dtf=1/(149.3/60)/8)
print("yipee") print("yipee")
print(1/(160/60));

BIN
crop.wav

Binary file not shown.

BIN
ctype.mp3 Normal file

Binary file not shown.

BIN
tetris_2.wav Executable file

Binary file not shown.