def chordBeats(infile, outfile): mywindow = np.array([ 0.001769, 0.015848, 0.043608, 0.084265, 0.136670, 0.199341, 0.270509, 0.348162, 0.430105, 0.514023, 0.597545, 0.678311, 0.754038, 0.822586, 0.882019, 0.930656, 0.967124, 0.990393, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999650, 0.996856, 0.991283, 0.982963, 0.971942, 0.958281, 0.942058, 0.923362, 0.902299, 0.878986, 0.853553, 0.826144, 0.796910, 0.766016, 0.733634, 0.699946, 0.665140, 0.629410, 0.592956, 0.555982, 0.518696, 0.481304, 0.444018, 0.407044, 0.370590, 0.334860, 0.300054, 0.266366, 0.233984, 0.203090, 0.173856, 0.146447, 0.121014, 0.097701, 0.076638, 0.057942, 0.041719, 0.028058, 0.017037, 0.008717, 0.003144, 0.000350 ]) l, beats, semitones = essentia_chord_utils.loadBeatsAndChroma(infile) chroma = np.zeros((semitones.shape[0], 12)) for i in range(semitones.shape[0]): tones = semitones[i] * mywindow cc = chroma[i] for j in range(tones.size): cc[j % 12] = cc[j % 12] + tones[j] chroma = essentia_chord_utils.smooth(chroma, window_len=int(1.5 * 44100 / 2048), window='hanning').astype('float32') chords = ChordsDetectionBeats(hopSize=2048) syms, strengths = chords(chroma, beats) segments = essentia_chord_utils.toMirexLab(0.0, l / 44100.0, beats, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chordBeats(infile, outfile): print 'Loading audio file...', infile audio = essentia.standard.MonoLoader(filename=infile)() bt = BeatTrackerMultiFeature() beats, _ = bt(audio) #beats = beats[::4] parameters = {} stepsize, chroma = vamp.collect(audio, 44100, "nnls-chroma:nnls-chroma", output="chroma", step_size=2048)["matrix"] # to essentia convention #chroma = np.roll(chroma, 3, 1) chords = ChordsDetectionBeats(hopSize=2048) syms, strengths = chords(chroma, beats) segments = essentia_chord_utils.toMirexLab(0.0, len(audio) / 44100.0, beats, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chordBeats(infile, outfile): # initialize algorithms we will use tuningFreq = essentia_chord_utils.tuning(infile) chordHopSize = 2048 frameSize = 8192 loader = MonoLoader(filename=infile) framecutter = FrameCutter(hopSize=chordHopSize, frameSize=frameSize) windowing = Windowing(type="blackmanharris62") spectrum = Spectrum() spectralpeaks = SpectralPeaks(orderBy="magnitude", magnitudeThreshold=1e-05, minFrequency=40, maxFrequency=5000, maxPeaks=10000) hpcp = HPCP(size=12, referenceFrequency=tuningFreq, harmonics=8, bandPreset=True, minFrequency=40.0, maxFrequency=5000.0, bandSplitFrequency=500.0, weightType="cosine", nonLinear=True, windowSize=1.0) # use pool to store data pool = essentia.Pool() # connect algorithms together loader.audio >> framecutter.signal framecutter.frame >> windowing.frame >> spectrum.frame spectrum.spectrum >> spectralpeaks.spectrum spectralpeaks.magnitudes >> hpcp.magnitudes spectralpeaks.frequencies >> hpcp.frequencies hpcp.hpcp >> (pool, 'chroma.hpcp') # network is ready, run it essentia.run(loader) # don't forget, we can actually instantiate and call an algorithm on the same line! print 'Loading audio file...', infile audio = essentia.standard.MonoLoader(filename=infile)() bt = BeatTrackerMultiFeature() beats, _ = bt(audio) beats = beats[::4] chords = ChordsDetectionBeats() syms, strengths = chords(pool['chroma.hpcp'], beats) segments = essentia_chord_utils.toMirexLab(0.0, len(audio) / 44100.0, beats, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chordBeats(infile, chromafile, outfile): print 'Loading audio file...', infile audio = essentia.standard.MonoLoader(filename=infile)() bt = BeatTrackerMultiFeature() beats, _ = bt(audio) beats = beats[::1] chroma = np.loadtxt(chromafile, dtype='float32') # to essentia convention chroma = np.roll(chroma, 3, 1) chords = ChordsDetectionBeats(hopSize=2048) syms, strengths = chords(chroma, beats) segments = essentia_chord_utils.toMirexLab(0.0, len(audio) / 44100.0, beats, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chordBeats(infile, outfile): # to essentia convention #chroma = np.roll(chroma, 3, 1) l, beats, semitones = essentia_chord_utils.loadBeatsAndChroma(infile) treblewindow = np.array([ 0.000350, 0.003144, 0.008717, 0.017037, 0.028058, 0.041719, 0.057942, 0.076638, 0.097701, 0.121014, 0.146447, 0.173856, 0.203090, 0.233984, 0.266366, 0.300054, 0.334860, 0.370590, 0.407044, 0.444018, 0.481304, 0.518696, 0.555982, 0.592956, 0.629410, 0.665140, 0.699946, 0.733634, 0.766016, 0.796910, 0.826144, 0.853553, 0.878986, 0.902299, 0.923362, 0.942058, 0.958281, 0.971942, 0.982963, 0.991283, 0.996856, 0.999650, 0.999650, 0.996856, 0.991283, 0.982963, 0.971942, 0.958281, 0.942058, 0.923362, 0.902299, 0.878986, 0.853553, 0.826144, 0.796910, 0.766016, 0.733634, 0.699946, 0.665140, 0.629410, 0.592956, 0.555982, 0.518696, 0.481304, 0.444018, 0.407044, 0.370590, 0.334860, 0.300054, 0.266366, 0.233984, 0.203090, 0.173856, 0.146447, 0.121014, 0.097701, 0.076638, 0.057942, 0.041719, 0.028058, 0.017037, 0.008717, 0.003144, 0.000350 ]) basswindow = np.array([ 0.001769, 0.015848, 0.043608, 0.084265, 0.136670, 0.199341, 0.270509, 0.348162, 0.430105, 0.514023, 0.597545, 0.678311, 0.754038, 0.822586, 0.882019, 0.930656, 0.967124, 0.990393, 0.999803, 0.995091, 0.976388, 0.944223, 0.899505, 0.843498, 0.777785, 0.704222, 0.624888, 0.542025, 0.457975, 0.375112, 0.295778, 0.222215, 0.156502, 0.100495, 0.055777, 0.023612, 0.004909, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000 ]) mywindow = np.array([ 0.001769, 0.015848, 0.043608, 0.084265, 0.136670, 0.199341, 0.270509, 0.348162, 0.430105, 0.514023, 0.597545, 0.678311, 0.754038, 0.822586, 0.882019, 0.930656, 0.967124, 0.990393, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999803, 0.999650, 0.996856, 0.991283, 0.982963, 0.971942, 0.958281, 0.942058, 0.923362, 0.902299, 0.878986, 0.853553, 0.826144, 0.796910, 0.766016, 0.733634, 0.699946, 0.665140, 0.629410, 0.592956, 0.555982, 0.518696, 0.481304, 0.444018, 0.407044, 0.370590, 0.334860, 0.300054, 0.266366, 0.233984, 0.203090, 0.173856, 0.146447, 0.121014, 0.097701, 0.076638, 0.057942, 0.041719, 0.028058, 0.017037, 0.008717, 0.003144, 0.000350 ]) chroma = np.zeros((semitones.shape[0], 12)) for i in range(semitones.shape[0]): tones = semitones[i] * mywindow cc = chroma[i] for j in range(tones.size): cc[j % 12] = cc[j % 12] + tones[j] chroma = essentia_chord_utils.smooth(chroma, window_len=int(1.3 * 44100 / 2048), window='hanning').astype('float32') #chroma = preprocessing.normalize(chroma, norm='max') #beats = beats[::3] #chords = ChordsDetectionBeats(hopSize=2048) #syms, strengths = chords(chroma, beats) noteNames = [ "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" ] chordNames = np.empty(24, dtype='object') for i in xrange(12): p = i * 2 chordNames[p] = noteNames[i] chordNames[p + 1] = noteNames[i] + ":min" probs = chordPseudoProbabilities(2048, 44100, chroma, beats) #indices = np.argmax(probs, axis=1) # #transitionMatrix = np.ones((24, 24)) * 1.0/24 #logMatrix = np.log(transitionMatrix) #penalty = np.ones((24, 24)) * 0.15 #penalty[xrange(24), xrange(24)] = 0 #logMatrix = logMatrix - penalty # # temperley pOthers = np.array([ 113, 113, 1384, 1384, 410, 410, 326, 326, 2266, 2266, 20, 20, 2220, 2220, 412, 412, 454, 454, 1386, 1386, 162, 162 ], dtype='float') pOthers = pOthers / sum(pOthers) pSelftransition = 5.0 / 8.0 pModeChange = 1.0 / 24.0 pMaj = np.concatenate((np.array([pSelftransition, pModeChange]), pOthers * (1.0 - pSelftransition - pModeChange))) pMin = np.concatenate((np.array([pModeChange, pSelftransition]), pOthers * (1.0 - pSelftransition - pModeChange))) pMaj = smoothProbabilities(pMaj, 1.055) pMin = smoothProbabilities(pMin, 1.055) transitionMatrix = np.zeros((24, 24)) for i in xrange(12): transitionMatrix[2 * i, :] = np.roll(pMaj, 2 * i) transitionMatrix[2 * i + 1, :] = np.roll(pMin, 2 * i) # # #transitionMatrix = np.ones((24, 24), dtype='float') * 1.0/24 # simplest one. #p = 0.046 #transitionMatrix = np.ones((24, 24)) * (1 - p) / 24 #transitionMatrix[xrange(24), xrange(24)]=p logMatrix = np.log(transitionMatrix) indices = viterbi(probs, logMatrix) syms = map(lambda x: chordNames[x], indices) strengths = probs[xrange(len(indices)), indices] segments = essentia_chord_utils.toMirexLab(0.0, l / 44100.0, beats, syms, strengths) #chords = ChordsDetection(windowSize=0.2) #syms, strengths = chords(chroma) #endTime = l / 44100.0 #stamps = np.arange(0, endTime, float(2048/44100.0)) #segments = essentia_chord_utils.toMirexLab(0.0, endTime, stamps, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chordBeats(infile, outfile): # chroma_cq = librosa.feature.chroma_cqt(y=y, sr=44100) chordHopSize = 4096 frameSize = 8192 tuningFreq = essentia_chord_utils.tuning(infile) # initialize algorithms we will use loader = MonoLoader(filename=infile) framecutter = FrameCutter(hopSize=chordHopSize, frameSize=frameSize) windowing = Windowing(type="blackmanharris62") spectrum = Spectrum() spectralpeaks = SpectralPeaks(orderBy="magnitude", magnitudeThreshold=1e-05, minFrequency=40, maxFrequency=5000, maxPeaks=10000) hpcp = HPCP(size=12, referenceFrequency=tuningFreq, harmonics=8, bandPreset=True, minFrequency=40.0, maxFrequency=5000.0, bandSplitFrequency=500.0, weightType="cosine", nonLinear=True, windowSize=1.0) # use pool to store data pool = essentia.Pool() # connect algorithms together loader.audio >> framecutter.signal framecutter.frame >> windowing.frame >> spectrum.frame spectrum.spectrum >> spectralpeaks.spectrum spectralpeaks.magnitudes >> hpcp.magnitudes spectralpeaks.frequencies >> hpcp.frequencies hpcp.hpcp >> (pool, 'chroma.hpcp') ## network is ready, run it #essentia.run(loader) ## don't forget, we can actually instantiate and call an algorithm on the same line! print 'Loading audio file...', infile audio = essentia.standard.MonoLoader(filename=infile)() bt = BeatTrackerMultiFeature() beats, _ = bt(audio) beats = beats[::4] #y, sr = librosa.load(infile, sr=44100) #chroma_stft = librosa.feature.chroma_stft(y=y, sr=44100, n_chroma = 12, n_fft = 8192, hop_length=2048) #chroma_stft = chroma_stft.transpose() #chroma_stft = chroma_stft.astype('float32') ## to essentia convention #chroma_stft = np.roll(chroma_stft, 3, 1) # #chords = ChordsDetectionBeats() #syms, strengths = chords(chroma_stft, beats) #y, sr = librosa.load(infile, sr=44100) #C = np.log(np.abs(librosa.cqt(y, sr=sr, # hop_length=2048, # fmin=None, # n_bins=12 * 7, # bins_per_octave=12, # tuning=None, # norm=None, # scale=False)) * 100 + 1) #chroma_cqt = librosa.feature.chroma_cqt(y, sr, norm=2, C=C) #chroma_cqt = librosa.feature.chroma_cqt(y, sr, hop_length=2048, norm=2, threshold=0.0) #chroma_cqt = chroma_cqt.transpose() #chroma_cqt = chroma_cqt.astype('float32') ## to essentia convention #chroma_cqt = np.roll(chroma_cqt, 3, 1) # #chords = ChordsDetectionBeats() #syms, strengths = chords(chroma_cqt, beats) chords = ChordsDetectionBeats() syms, strengths = chords(pool['chroma.hpcp'], beats) print 'size: ', len(pool['chroma.hpcp']) #name, ext = os.path.splitext(outfile) #YamlOutput(filename=name+'.chroma.json')(pool) segments = essentia_chord_utils.toMirexLab(0.0, len(audio) / 44100.0, beats, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')
def chords(infile, outfile): """ Algorithm* peaks = factory.create("SpectralPeaks", "maxPeaks", 10000, "magnitudeThreshold", 0.00001, "minFrequency", 40, "maxFrequency", 5000, "orderBy", "magnitude"); Algorithm* skey = factory.create("Key", "numHarmonics", 4, "pcpSize", 36, "profileType", "temperley", "slope", 0.6, "usePolyphony", true, "useThreeChords", true); Algorithm* hpcp_chord = factory.create("HPCP", "size", 36, "referenceFrequency", tuningFreq, "harmonics", 8, "bandPreset", true, "minFrequency", 40.0, "maxFrequency", 5000.0, "bandSplitFrequency", 500.0, "weightType", "cosine", "nonLinear", true, "windowSize", 0.5); """ # initialize algorithms we will use tuningFreq = essentia_chord_utils.tuning(infile) print "tg = ", tuningFreq chordHopSize = 2048 frameSize = 8192 loader = MonoLoader(filename=infile) framecutter = FrameCutter(hopSize=chordHopSize, frameSize=frameSize) windowing = Windowing(type="blackmanharris62") spectrum = Spectrum() spectralpeaks = SpectralPeaks(orderBy="magnitude", magnitudeThreshold=1e-05, minFrequency=40, maxFrequency=5000, maxPeaks=10000) hpcp = HPCP(size=12, referenceFrequency=tuningFreq, harmonics=8, bandPreset=True, minFrequency=40.0, maxFrequency=5000.0, bandSplitFrequency=500.0, weightType="cosine", nonLinear=True, windowSize=1.0) chords = ChordsDetection() # use pool to store data pool = essentia.Pool() # connect algorithms together loader.audio >> framecutter.signal framecutter.frame >> windowing.frame >> spectrum.frame spectrum.spectrum >> spectralpeaks.spectrum spectralpeaks.magnitudes >> hpcp.magnitudes spectralpeaks.frequencies >> hpcp.frequencies hpcp.hpcp >> chords.pcp hpcp.hpcp >> (pool, 'chroma.hpcp') chords.chords >> (pool, 'tonal.key_chords') chords.strength >> (pool, 'tonal.key_strength') # network is ready, run it print 'Processing audio file...', infile essentia.run(loader) audio = essentia.standard.MonoLoader(filename=infile)() endTime = len(audio) / 44100.0 stamps = np.arange(0, endTime, float(chordHopSize / 44100.0)) # workaround for Essentia behaviour I don't quite undestand syms = list(pool['tonal.key_chords'][:-1]) strengths = list(pool['tonal.key_strength'][:-1]) segments = essentia_chord_utils.toMirexLab(0.0, endTime, stamps, syms, strengths) with open(outfile, 'w') as content_file: for s in segments: content_file.write(str(s) + '\n')