#!/usr/bin/env python from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import os import essentia import essentia.standard import essentia.streaming import essentia.streaming as ess # https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plot #from pylab import show, figure, imshow import pylab argc = len(sys.argv) input_audio_filename = None output_filename_root = None if argc <= 1: # Python3 syntax: print("Usage: "+sys.argv[0] +" input_file [output_file]\n",file=sys.stderr) # print "Usage: "+sys.argv[0] +" input_file [output_file]\n" sys.exit(1) else: input_audio_filename = sys.argv[1] if argc == 2: output_filename_root = os.path.splitext(input_audio_filename)[0] else: output_filename_root = sys.argv[2] # Initialize algorithms we will use loader = ess.MonoLoader(filename=input_audio_filename) framecutter = ess.FrameCutter(frameSize=4096, hopSize=2048, silentFrames='noise') windowing = ess.Windowing(type='blackmanharris62') spectrum = ess.Spectrum() spectralpeaks = ess.SpectralPeaks(orderBy='magnitude', magnitudeThreshold=0.00001, minFrequency=20, maxFrequency=3500, maxPeaks=60) # Use default HPCP parameters for plots, however we will need higher resolution # and custom parameters for better Key estimation hpcp = ess.HPCP() hpcp_key = ess.HPCP(size=36, # we will need higher resolution for Key estimation referenceFrequency=440, # assume tuning frequency is 44100. bandPreset=False, minFrequency=20, maxFrequency=3500, weightType='cosine', nonLinear=False, windowSize=1.) key = ess.Key(profileType='edma', # Use profile for electronic music numHarmonics=4, pcpSize=36, slope=0.6, usePolyphony=True, useThreeChords=True) # Use pool to store data pool = essentia.Pool() # Connect streaming algorithms loader.audio >> framecutter.signal framecutter.frame >> windowing.frame >> spectrum.frame spectrum.spectrum >> spectralpeaks.spectrum spectralpeaks.magnitudes >> hpcp.magnitudes spectralpeaks.frequencies >> hpcp.frequencies spectralpeaks.magnitudes >> hpcp_key.magnitudes spectralpeaks.frequencies >> hpcp_key.frequencies hpcp_key.hpcp >> key.pcp hpcp.hpcp >> (pool, 'tonal.hpcp') key.key >> (pool, 'tonal.key_key') key.scale >> (pool, 'tonal.key_scale') key.strength >> (pool, 'tonal.key_strength') # Run streaming network essentia.run(loader) # Plot HPCP pylab.imshow(pool['tonal.hpcp'].T, aspect='auto', origin='lower', interpolation='none') #plot.title("HPCPs in frames (the 0-th HPCP coefficient corresponds to A)") #show() #plt.matshow(np.log(np.abs(constantq)),origin='lower', aspect='auto') #aspect='auto' fits axis with the dimensions of the matrix output_filename_hpcp = output_filename_root + "-hpcp.png" # https://stackoverflow.com/questions/47147146/save-an-image-only-content-without-axes-or-anything-else-to-a-file-using-matl #fig,ax = plot.subplots(1) #fig.subplots_adjust(left=0,right=1,bottom=0,top=1) #ax.axis('tight') #ax.axis('off') plot.axis('off') plot.savefig(output_filename_hpcp , bbox_inches="tight", pad_inches=0) # , transparent=True) # plot.savefig(output_filename_hpcp, bbox_inches="tight", pad_inches=0, transparent=True) # fig.savefig('sp_xyz.png', dpi=300, frameon='false') print("Estimated key and scale:", pool['tonal.key_key'] + " " + pool['tonal.key_scale'])