def RMSFlux(fileName): song = onset.Song(fileName) song.FindAlphaPeak(0, 0.8) rms = [] quiet = [] len_samp = 5 * song.sampfreq for i in range((len(song.data) // len_samp)): start = i * len_samp end = i * len_samp + len_samp power = onset.GetRMS(song.data[start:end]) rms.append([start/song.sampfreq, power]) for i in range(len(rms)-1): a = rms[i + 1][1] b = rms[i][1] if b - a > 10: quiet.append(rms[i + 1]) before = int(quiet[0][0]*song.sampfreq) - int(5*song.sampfreq) len_samp = int(0.5*song.sampfreq) zoomRMS = [] for i in range(11): start = before + len_samp * i end = before + len_samp * i + len_samp power = onset.GetRMS_100(song.data[start:end]) zoomRMS.append([start, power]) print(start, power)
def Tempo(fileName): song = onset.Song(fileName) print("RMS:", song.GetRMS(), "dB") tr = song.CalculateThreshold_RMS() song.FindAlphaPeak(0, tr) song.peakAlphaIndex = TruePeak(song) print("Peak: ", song.peakAlphaIndex, song.peakAlphaIndex / song.sampfreq, "seconds.") bpms = onset.GetBPMS_All(song, tr) print("BPMS:",bpms)
def SampleProfile(file, resolution): audio = onset.Song(wavfile.read("songs/samples/" + file)) limits = [240,1680] note = audio.data[0:resolution] onset.PlotNote(note, audio.sampfreq, limits[0], limits[len(limits)-1], file.split(".")[0] + ".png") freqs, fft = onset.CalculateFFT_dB(note, audio.sampfreq, limits[0], limits[len(limits)-1]) x, y = onset.GetSpectrumPeaks(freqs, fft) print(x,y) freq, amp = onset.GetTopFrequencies(x,y,8) plt.figure(figsize = (20,10)) plt.xticks(freq) plt.scatter(freq,amp) plt.xlim(limits[0],limits[1]) plt.ylim(10,30) plt.savefig("sp_" + file.split(".")[0] + ".png") plt.show()
# -*- coding: utf-8 -*- """ GET FREQUENCY AND ENERGY SEQUENCE AND PARSONS CODE @author: albertovaldez """ import onset import parsons import easygram as ez import getBPM_Bass as bpmb import mp3 songName = "spectre.mp3" plotPath, csvPath = "PCP/plots/", "PCP/csv/" wavFile = mp3.toWAV(songName, "mp3/") song = onset.Song(wavFile) tr = onset.CalculateThreshold_RMS(song.data) #bpms = bpmb.GetBPM(song, tr) bpm_user = int(input("Enter selected BPM: \n")) #SONG THRESHOLD FOR ALPHA PEAK, 0.0 if cut, 0.1 if instant, 0.5 if soft fade in, 0.8 if loud fade in songTR = 0.8 noteTR = 0.7 alphaPeak = song.FindAlphaPeak(0, songTR) bpm, measure, unitSize = bpm_user, 4, 0.25 #freqBands = [i*60 for i in range(5,26)] # Melody/Lead use continuous #freqBands = [i*60 for i in range(2,6)] # Body/Snare use step #freqBands = [9000, 16000] # High/Hats need custom de-peak process freqBands = [i * 60 for i in range(0, 3)] # Bass/Kick use continuous
def Song(file): p = Path(file) directory = str(p.parent) + "/" if ".mp3" in file: wavFile = toWAV(file) song = onset.Song(wavFile) else: song = onset.Song(file) songName = file.split("/")[-1:][0] print(songName) u1 = int(input(("Press 1 if your song has no fade-in, Press 2 if the fade-in is quiet. Press 3 if the fade-in is loud.\n"))) if u1 == 1: songTR = 0.1 elif u1 == 2: songTR = 0.5 elif u1 == 3: songTR = 0.8 else: songTR = 0.8 alphaPeak = song.FindAlphaPeak(0,songTR) u2 = str(input("Press 1 to get a list of suggested BPMs. Press any other key to skip:\n")) if u2 == "1": bpmb.GetBPM(song, onset.CalculateThreshold_RMS(song.data)) bpm_user = float(input("Enter selected BPM: \n")) measure_user = int(input("Enter how many beats are in a bar: \n")) bpm, measure = bpm_user, measure_user maxBars = int((song.length_seconds * (1/(60/bpm)) / measure )) print("Song duration in bars: " + str(maxBars)) def Melody(): noteThreshold = 0.6 unitSize = 0.25 print("\nObtaining Melody...") # Continuous, Peaks, 4 by 4, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [300,1800] barBlock = 4 for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksMelody" + "_" + songName.split(".")[0] + ".csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotMelody_" + songName, noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV3D(x, pc_e,pc_f, directory, "dataMelody_" + songName) print("Done.") def Snare(): noteThreshold = 0.6 unitSize = 0.5 print("\nObtaining Snare...") # Step, Peaks, 4 by 4, unit 0.5 x_all, y_energy, z_freq = [],[],[] freqBands = [120,300] barBlock = 4 for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Step(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksSnare" + "_" + songName.split(".")[0] + ".csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotSnare_" + songName, noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV3D(x, pc_e,pc_f, directory, "dataSnare_" + songName) print("Done.") def Bass(): noteThreshold = 0.7 unitSize = 0.25 print("\nObtaining Bass...") # Continuous, Peaks, all bars, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [0,120] barBlock = maxBars for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksBass" + "_" + songName.split(".")[0] + ".csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotBass_" + songName, noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV3D(x, pc_e,pc_f, directory, "dataBass_" + songName) print("Done.") def Hats(): noteThreshold = 0.7 unitSize = 0.25 print("\nObtaining HiHats...") # Continuous, Peaks, all bars, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [9000,16000] barBlock = maxBars for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous_Sum(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksHats" + "_" + songName.split(".")[0] + ".csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotHats_" + songName, noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV3D(x, pc_e,pc_f, directory, "dataHats_" + songName) print("Done.") Melody() Snare() Bass() Hats() os.remove(wavFile)
def Song(file): try: os.mkdir(file.split(".")[0]) except: pass p = Path(file) #directory = str(p.parent) + "/" directory = file.split(".")[0] + "/" song = onset.Song(file) songName = file.split("/")[-1:][0] print(songName) u1 = int(input(("Press 1 if your song has no fade-in, Press 2 if the fade-in is quiet. Press 3 if the fade-in is loud. Press 4 if the fade-in is very loud.\n"))) if u1 == 1: songTR = 0.1 elif u1 == 2: songTR = 0.5 elif u1 == 3: songTR = 0.8 elif u1 == 4: songTR = 0.96 else: songTR = 0.5 alphaPeak = song.FindAlphaPeak(0,songTR) print("Song starts at " + str(alphaPeak) + " seconds.") ''' u2 = str(input("Press 1 to get a list of suggested BPMs. Press any other key to skip:\n")) if u2 == "1": onset.GetBPMS(song, song.CalculateThreshold_RMS()) ''' bpm_user = float(input("Enter selected BPM: \n")) measure_user = int(input("Enter how many beats are in a bar: \n")) bpm, measure = bpm_user, measure_user maxBars = int((song.length_seconds * (1/(60/bpm)) / measure )) print("Song duration in bars: " + str(maxBars)) def Melody(): noteThreshold = 0.5 unitSize = 0.25 # Continuous, Peaks, 4 by 4, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [300,1800] barBlock = 4 for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [i * (60/bpm) * unitSize * song.sampfreq for i in x_all] onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksMelody.csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotMelody.png", noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV5D(x, pc_e,pc_f,y_energy,z_freq,directory, "dataMelody.csv") print("Done.") def Snare(): noteThreshold = 0.5 unitSize = 0.5 # Step, Peaks, 4 by 4, unit 0.5 x_all, y_energy, z_freq = [],[],[] freqBands = [120,300] barBlock = 4 for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Step(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksSnare.csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory,"plotSnare.png", noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV5D(x, pc_e,pc_f,y_energy,z_freq,directory, "dataSnare.csv") print("Done.") def Bass(): noteThreshold = 0.5 unitSize = 0.25 # Continuous, Peaks, all bars, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [0,120] barBlock = maxBars for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksBass.csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotBass.png", noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV5D(x, pc_e,pc_f,y_energy,z_freq,directory, "dataBass.csv") print("Done.") def Hats(): noteThreshold = 0.5 unitSize = 0.25 # Continuous, Peaks, all bars, unit 0.25 x_all, y_energy, z_freq = [],[],[] freqBands = [9000,16000] barBlock = maxBars for i in range(maxBars//barBlock): barNum = i*barBlock x,y,z = ez.GetNotesPeaks3D_Continuous_Sum(freqBands, song, bpm, barNum, barNum+barBlock, measure, unitSize, noteThreshold) offset = i*barBlock*measure*(1/unitSize) for j in range(len(y)): x_all.append(x[j] + offset) y_energy.append(y[j]) z_freq.append(z[j]) peaks = [] for i in x_all: peaks.append(i * (60/bpm)*unitSize * song.sampfreq) onset.SavePeaks(peaks, song.sampfreq, 1, song.peakAlphaIndex, directory + "peaksHats.csv") pc_e,pc_f = parsons.GetPCode(x_all, y_energy),parsons.GetPCode(x_all, z_freq) x = [i*unitSize for i in x_all] ez.PlotComplete(x, pc_e, pc_f, bpm, maxBars, measure, unitSize, freqBands, directory, "plotHats.png", noteThreshold, song.GetRMS(), alphaPeak) parsons.SaveCSV5D(x, pc_e,pc_f,y_energy,z_freq,directory, "dataHats.csv") print("Done.") print("\nObtaining Melody...") Melody() print("\nObtaining Snare...") Snare() print("\nObtaining Bass...") Bass() print("\nObtaining HiHats...") Hats() snaps.Density(directory) snaps.RateOfChange(directory)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ GET FREQUENCY AND ENERGY SEQUENCE AND PARSONS CODE @author: albertovaldez """ import onset import parsons import easygram as ez import getBPM_Bass as bpmb songName = "spectre.mp3" plotPath, csvPath = "PCP/plots/", "PCP/csv/" song = onset.Song("songs/" + songName) tr = onset.CalculateThreshold_RMS(song.data) #bpms = bpmb.GetBPM(song, tr) bpm_user = int(input("Enter selected BPM: \n")) #SONG THRESHOLD FOR ALPHA PEAK, 0.0 if cut, 0.1 if instant, 0.5 if soft fade in, 0.8 if loud fade in songTR = 0.8 noteTR = 0.7 alphaPeak = song.FindAlphaPeak(0,songTR) bpm, measure, unitSize = bpm_user, 4, 0.25 #freqBands = [i*60 for i in range(5,26)] # Melody/Lead use continuous #freqBands = [i*60 for i in range(2,6)] # Body/Snare use step #freqBands = [9000, 16000] # High/Hats need custom de-peak process freqBands = [i*60 for i in range(0,3)] # Bass/Kick use continuous
def RMSFluxStart(fileName): song = onset.Song(fileName) rms = song.GetRMS() print("RMS:", rms) x,y = [],[] on = [] len_samp = 5 * song.sampfreq total = 60 * song.sampfreq for i in range(total // len_samp): start = i * len_samp end = i * len_samp + len_samp power = onset.GetRMS(song.data[start:end]) x.append(start/song.sampfreq) y.append(power) # 5 SECOND RULE for i in range(len(y)-1): a = y[i] b = y[i + 1] if abs(a) - abs(b) > 9: on = [x[i+1],y[i+1]] break intro = on[0] * song.sampfreq // 2 unit = song.sampfreq length = 5 x, y = [],[] print(intro/song.sampfreq) for i in range(length): start = int(intro + (unit * i)) end = start + unit chunkRMS = onset.GetRMS(song.data[start:end]) x.append(start) y.append(chunkRMS) for i in range(len(y)-1): a = y[i] b = y[i + 1] if abs(a) - abs(b) > 9: on = [x[i+1], y[i+1]] break print(max(y), x[y.index(max(y))]) intro = on[0] unit = song.sampfreq // 20 length = 20 x, y = [],[] for i in range(length): start = int(intro + (unit * i)) end = start + unit chunkRMS = onset.GetRMS(song.data[start:end]) x.append(start) y.append(chunkRMS) print(max(y), x[y.index(max(y))]) intro = x[y.index(max(y))] - 8192 unit = 1024 length = 8 x, y = [],[] for i in range(length): start = int(intro + (unit * i)) end = start + unit chunkRMS = onset.GetRMS(song.data[start:end]) x.append(start) y.append(chunkRMS) print(max(y), x[y.index(max(y))]) plt.plot(x,y)
""" Created on Mon Jul 20 17:19:41 2020 @author: albertovaldezquinto """ import librosa import librosa.display import numpy as np import matplotlib.pyplot as plt import onset from scipy import stats audioPath = "drums.wav" song = onset.Song(audioPath) rms = onset.GetRMS(song.data) print("RMS:", rms, "dB") data = song.data sr = song.sampfreq tempo, beats = librosa.beat.beat_track(y=data, sr=sr, units="samples") print("BPM", tempo) def GetBeatFrequencySnapshot(x, chunkSize, filterLow, filterHigh): chunksInData = len(x) // chunkSize frequencies = np.empty(0) energies = np.empty(0)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: albertovaldez """ import onset from scipy.io import wavfile songName = "cut.wav" song = onset.Song(wavfile.read("songs/" + songName)) tr = onset.CalculateThreshold_RMS(song.data) song.FindAlphaPeak(0,tr) song.GetNoteOnset(unit = 4096, chunk_size = 4096, threshold_ratio = tr, HPF = 960, LPF = 1080, base = 10) song.GetPeaks(x = 4096) onset.SavePeaks(song.pks, song.sampfreq, 1, 0, "peaks.csv") song.PlotPeaks() def GetSimpleSpectrogram_FromNotes(limits): multiband = [] for i in range(len(song.notes)): start, end = song.notes[i][0], song.notes[i][0] + 4096 chunk = song.data[start:end] freqs, fft = onset.CalculateFFT_dB(chunk, song.sampfreq, limits[0], limits[len(limits)-1]) #x, y = onset.GetSpectrumPeaks(freqs, fft) x, y = freqs, fft freqBandsAmp = onset.FrequencyBands(x,y,limits)
files = [] for file in os.listdir("/Volumes/AV5 HD B/02Samples/Music/RGM/Funky Panda"): if ".mp3" in file: files.append("/Volumes/AV5 HD B/02Samples/Music/RGM/Funky Panda/" + file) data = [] csv = "Song, Low, Mid, High, Total, RMS" with open("data.csv", "w+") as file: file.write(csv) for file in files: print(file) try: song = onset.Song(file) tr = song.CalculateThreshold_RMS() song.FindAlphaPeak(0, tr) rms = song.GetRMS() print("RMS:", rms, "Threshold:", tr, "Alpha Peak:", song.peakAlphaIndex / song.sampfreq) FindEnding(song) sl = int((song.length / song.sampfreq) * 1000) / 1000 print("Song Length, seconds", sl) notes = EasyOnsets(song, [0, 120], 2048, tr) low = int((len(notes) * 4 / sl) * 1000) / 1000 notes = EasyOnsets(song, [300, 1800], 2048, tr) mid = int((len(notes) / sl) * 1000) / 1000
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jul 1 18:30:55 2020 @author: albertovaldez """ import onset song = onset.Song("songs/axtasia_closer.wav") song.FindAlphaPeak(0, 0.5) tr = onset.CalculateThreshold_RMS(song.data) tr = 0.8 song.GetNoteOnset(unit=2048, chunk_size=2048, threshold_ratio=tr, HPF=0, LPF=120, base=10) song.GetPeaks(x=0) bpm1, bpm2 = song.GetBPM(), song.GetBPM_PKS() print(bpm1, bpm2) onset.SavePeaks(song.pks, song.sampfreq, 1, -song.peakAlpha, "reaper/Files/peaks.csv")
bpm = 60 / (onset.mode(d) / song.sampfreq ) #Convert deltas from sample to seconds to bpm while bpm < bpmMin or bpm > bpmMax: #Reduce BPM to beats approximation if bpm < bpmMin: bpm = bpm * 2 elif bpm > bpmMax: bpm = bpm / 2 return int(bpm * 100) / 100 for i in os.listdir(outputFolder): if ".DS_Store" != i: for j in os.listdir(outputFolder / i): if stemName in j: song = onset.Song(str(outputFolder / i / stemName)) tr = song.CalculateThreshold_RMS( ) # Tries to compensate, as a manual limiter indexes = np.where( song.data > tr)[0] # Get samples higher than threshold hop = 2048 # Separate positions of samples. Using frame size for FFT, value can change, 2048 is 40 milliseconds peaks = indexes[np.where(np.diff(indexes) > hop)[0] + 1] if len(peaks) > 4: bpm = GetBPM(peaks) print(i, bpm) else: bpms, peaks = onset.GetBPMS(song, tr, 120) bpm = bpms[0][1] + 0.0069