Prepare friendly song_process function
This commit is contained in:
parent
d0f20b74e8
commit
3ed980795b
|
@ -11,6 +11,7 @@ import heapq
|
||||||
import scipy
|
import scipy
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
from pathlib import Path
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
print("Starting...\n")
|
print("Starting...\n")
|
||||||
|
@ -184,7 +185,7 @@ 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):
|
def snap(data, sample_rate, bpm, divisor, show=False):
|
||||||
# adjust time amplitudes to match the given BPM
|
# adjust time amplitudes to match the given BPM
|
||||||
new = [0 for x in range(int(1000*len(data)/sample_rate))] # 1pt per millisecond
|
new = [0 for x in range(int(1000*len(data)/sample_rate))] # 1pt per millisecond
|
||||||
print("old =", len(data))
|
print("old =", len(data))
|
||||||
|
@ -225,7 +226,7 @@ def snap(data, sample_rate, bpm, divisor, show):
|
||||||
def compress(Zxx):
|
def compress(Zxx):
|
||||||
res = []
|
res = []
|
||||||
|
|
||||||
def get_freq(song_name, offset, step, songlen, data):
|
def get_freq(song_name, offset, step, songlen, data, display=False):
|
||||||
fft_list = []
|
fft_list = []
|
||||||
times = []
|
times = []
|
||||||
current_time = offset
|
current_time = offset
|
||||||
|
@ -260,7 +261,7 @@ def get_freq(song_name, offset, step, songlen, data):
|
||||||
elif s != 0:
|
elif s != 0:
|
||||||
frequencies[s] = 0
|
frequencies[s] = 0
|
||||||
|
|
||||||
if(True):
|
if(display):
|
||||||
plt.plot([t/1000 for t in range(len(data))], frequencies)
|
plt.plot([t/1000 for t in range(len(data))], frequencies)
|
||||||
plt.grid()
|
plt.grid()
|
||||||
plt.xlabel("Time (s)")
|
plt.xlabel("Time (s)")
|
||||||
|
@ -271,7 +272,7 @@ def get_freq(song_name, offset, step, songlen, data):
|
||||||
return frequencies
|
return frequencies
|
||||||
|
|
||||||
|
|
||||||
def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, ampthr, ampfreq, ampval, leniency, write):
|
def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr, ampthr, ampfreq, ampval, leniency, write, output_file="trimmed.wav"):
|
||||||
fft_list = []
|
fft_list = []
|
||||||
times = []
|
times = []
|
||||||
current_time = offset
|
current_time = offset
|
||||||
|
@ -341,7 +342,7 @@ def void_freq(song_name, offset, songlen, increment, minfreq, maxfreq, upperthr,
|
||||||
res[i] = np.int16(32767*res[i]/mx)
|
res[i] = np.int16(32767*res[i]/mx)
|
||||||
|
|
||||||
res = np.array(res)
|
res = np.array(res)
|
||||||
wavfile.write("trimmed.wav", 44100, res)
|
wavfile.write(output_file, 44100, res)
|
||||||
|
|
||||||
#plt.plot(np.abs(pfreq[:len(fft_list[0])]), np.abs(fft_list[0]))
|
#plt.plot(np.abs(pfreq[:len(fft_list[0])]), np.abs(fft_list[0]))
|
||||||
#plt.grid()
|
#plt.grid()
|
||||||
|
@ -379,38 +380,31 @@ def test_sample(timelist):
|
||||||
#BPM = 140
|
#BPM = 140
|
||||||
#Length = 32*60/BPM-0.01
|
#Length = 32*60/BPM-0.01
|
||||||
|
|
||||||
Offset = 0
|
def convert_tuple(datares):
|
||||||
BPM = 160
|
"""
|
||||||
Length = 30*60/BPM-0.01
|
Takes datares and converts it to a list of tuples (amplitude, time in ms)
|
||||||
DivLen = 60/BPM-0.01
|
"""
|
||||||
N_iter = 48
|
return [(i, datares[i]) for i in range(len(datares)) if datares[i] > 0]
|
||||||
|
|
||||||
print("Total length :", np.round(DivLen*N_iter, 3), "s")
|
|
||||||
|
|
||||||
subprocess.run(["sleep", "1"])
|
|
||||||
|
|
||||||
if(True):
|
|
||||||
#void_freq("tetris_4.wav", Offset, Offset+Length, 4*60/BPM, minfreq=0, maxfreq=330, upperthr=5000, ampthr=60, ampfreq = 1200, ampval = 7.27, leniency = 0.005, write=True)
|
|
||||||
void_freq("tetris_4.wav", Offset, Offset+DivLen*(N_iter+1)+0.01, 4*60/BPM, minfreq=0, maxfreq=330, upperthr=5000, ampthr=60, ampfreq = 1200, ampval = 7.27, leniency = 0.005, write=True)
|
|
||||||
|
|
||||||
if(False):
|
|
||||||
#data2 = filter_n_percent("worlds_end_3.wav", 74.582, 15, 0.2, reduce=False, show=True)
|
|
||||||
data2 = filter_n_percent("trimmed.wav", 0, Length, 0.5, reduce=False, show=False)
|
|
||||||
#data2 = filter_n_percent("tetris_3.wav", 7, Length, 0.01, reduce=False, show=False)
|
|
||||||
data2 = snap(data2, 44100, BPM, 4, show=True)
|
|
||||||
|
|
||||||
#timing_pts = get_tpts(data2, 1000, 1000)
|
|
||||||
#test_sample(timing_pts)
|
|
||||||
|
|
||||||
write_to_file_thr(1000, data2, Offset, 0.02, "timing_points.csv")
|
|
||||||
|
|
||||||
if(True):
|
|
||||||
datares = filter_n_percent_serial("trimmed.wav", Offset, N_iter, DivLen, 0.5)
|
|
||||||
datares = snap(datares, 44100, BPM, 4, show=True)
|
|
||||||
freqs = get_freq("trimmed.wav", Offset, DivLen, DivLen*N_iter, datares)
|
|
||||||
|
|
||||||
|
|
||||||
print("Program finished with return 0")
|
def process_song(filename, offset, bpm, div_len_factor=60, n_iter=48, threshold=0.5, divisor=4):
|
||||||
|
div_len = div_len_factor/bpm-0.01
|
||||||
|
filtered_name = f"{filename}_trimmed.wav"
|
||||||
|
void_freq(filename, offset, offset+div_len*(n_iter+1)+0.01, 4*60/bpm, minfreq=0, maxfreq=330, upperthr=5000, ampthr=60, ampfreq = 1200, ampval = 7.27, leniency = 0.005, write=True, output_file=filtered_name)
|
||||||
|
datares = filter_n_percent_serial(filtered_name, offset, n_iter, div_len, threshold)
|
||||||
|
datares = snap(datares, 44100, bpm, 4)
|
||||||
|
frequencies = get_freq(filtered_name, offset, div_len, div_len*n_iter, datares)
|
||||||
|
Path(f"{filename}_trimmed.wav").unlink()
|
||||||
|
return convert_tuple(datares), frequencies
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
data, freq = process_song("tetris_4.wav", 0, 160)
|
||||||
|
print(data)
|
||||||
|
print("Program finished with return 0")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -813,4 +807,4 @@ def void_freq(song_name, offset, songlen, increment, lthr, gthr):
|
||||||
wavfile.write('test.wav', sample_rate, np.array(audio_signal, dtype=np.int16))
|
wavfile.write('test.wav', sample_rate, np.array(audio_signal, dtype=np.int16))
|
||||||
|
|
||||||
print("Done")
|
print("Done")
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Reference in New Issue