Changeset 34409
- Timestamp:
- 2020-09-17T17:58:05+12:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-extensions/mars-src/trunk/bin/script/essentia-hpcp.py
r34389 r34409 9 9 10 10 import essentia 11 import essentia.standard 12 import essentia.streaming 13 import essentia.streaming as ess 14 15 11 import essentia.standard as esstandard 12 import essentia.streaming as esstreaming 16 13 17 14 # https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib 15 # Statements need to be in this order for MacOS operating a venv python2, otherwise an error message results 16 # related to the version being used not being a system OS 'Framework' 18 17 import matplotlib 19 matplotlib.use('Agg') 20 import matplotlib.pyplot as plot 21 22 #from pylab import show, figure, imshow 23 24 import pylab 18 # Select the Anti-Grain Geometry C++ backend for rendering to a file 19 matplotlib.use('Agg') 20 import matplotlib.pyplot as pyplot 25 21 26 22 argc = len(sys.argv) 27 23 28 input_audio_filename 24 input_audio_filename = None 29 25 output_filename_root = None 30 26 31 27 if argc <= 1: 32 # Python3 syntax:33 28 print("Usage: "+sys.argv[0] +" input_file [output_file]\n",file=sys.stderr) 34 # print "Usage: "+sys.argv[0] +" input_file [output_file]\n"35 29 sys.exit(1) 36 30 else: 37 input_audio_filename 31 input_audio_filename = sys.argv[1] 38 32 if argc == 2: 39 33 output_filename_root = os.path.splitext(input_audio_filename)[0] 40 34 else: 41 output_filename_root = sys.argv[2] 35 # output_filename_root = sys.argv[2] 36 output_filename_root = os.path.splitext(sys.argv[2])[0] 42 37 43 38 44 39 45 40 # Initialize algorithms we will use 46 loader = ess .MonoLoader(filename=input_audio_filename)41 loader = esstreaming.MonoLoader(filename=input_audio_filename) 47 42 48 framecutter = ess.FrameCutter(frameSize=4096, hopSize=2048, silentFrames='noise') 49 windowing = ess.Windowing(type='blackmanharris62') 50 spectrum = ess.Spectrum() 51 spectralpeaks = ess.SpectralPeaks(orderBy='magnitude', 52 magnitudeThreshold=0.00001, 53 minFrequency=20, 54 maxFrequency=3500, 55 maxPeaks=60) 43 framecutter = esstreaming.FrameCutter(frameSize=4096, hopSize=2048, silentFrames='noise') 44 windowing = esstreaming.Windowing(type='blackmanharris62') 45 spectrum = esstreaming.Spectrum() 46 spectralpeaks = esstreaming.SpectralPeaks(orderBy='magnitude', 47 magnitudeThreshold=0.00001, 48 minFrequency=20, 49 maxFrequency=3500, 50 maxPeaks=60) 51 56 52 57 53 # Use default HPCP parameters for plots, however we will need higher resolution 58 54 # and custom parameters for better Key estimation 59 55 60 hpcp = ess.HPCP()61 hpcp_key = ess .HPCP(size=36,# we will need higher resolution for Key estimation62 referenceFrequency=440, # assume tuning frequency is 44100.63 bandPreset=False,64 minFrequency=20,65 maxFrequency=3500,66 weightType='cosine',67 nonLinear=False,68 windowSize=1.)56 hpcp = esstreaming.HPCP() 57 hpcp_key = esstreaming.HPCP(size=36, # we will need higher resolution for Key estimation 58 referenceFrequency=440, # assume tuning frequency is 44100. 59 bandPreset=False, 60 minFrequency=20, 61 maxFrequency=3500, 62 weightType='cosine', 63 nonLinear=False, 64 windowSize=1.) 69 65 70 key = ess.Key(profileType='edma',# Use profile for electronic music71 numHarmonics=4,72 pcpSize=36,73 slope=0.6,74 usePolyphony=True,75 useThreeChords=True)66 key = esstreaming.Key(profileType='edma', # Use profile for electronic music 67 numHarmonics=4, 68 pcpSize=36, 69 slope=0.6, 70 usePolyphony=True, 71 useThreeChords=True) 76 72 77 73 # Use pool to store data … … 79 75 80 76 # Connect streaming algorithms 81 loader.audio >> framecutter.signal82 framecutter.frame >> windowing.frame>> spectrum.frame83 spectrum.spectrum >> spectralpeaks.spectrum84 spectralpeaks.magnitudes >> hpcp.magnitudes77 loader.audio >> framecutter.signal 78 framecutter.frame >> windowing.frame >> spectrum.frame 79 spectrum.spectrum >> spectralpeaks.spectrum 80 spectralpeaks.magnitudes >> hpcp.magnitudes 85 81 spectralpeaks.frequencies >> hpcp.frequencies 86 spectralpeaks.magnitudes >> hpcp_key.magnitudes 82 83 spectralpeaks.magnitudes >> hpcp_key.magnitudes 87 84 spectralpeaks.frequencies >> hpcp_key.frequencies 88 hpcp_key.hpcp >> key.pcp 89 hpcp.hpcp >> (pool, 'tonal.hpcp') 90 key.key >> (pool, 'tonal.key_key') 91 key.scale >> (pool, 'tonal.key_scale') 92 key.strength >> (pool, 'tonal.key_strength') 85 hpcp_key.hpcp >> key.pcp 86 87 hpcp.hpcp >> (pool, 'tonal.hpcp') 88 89 key.key >> (pool, 'tonal.key_key') 90 key.scale >> (pool, 'tonal.key_scale') 91 key.strength >> (pool, 'tonal.key_strength') 93 92 94 93 # Run streaming network … … 96 95 97 96 97 output_filename_hpcp = output_filename_root + ".png" 98 98 99 99 # Plot HPCP 100 pylab.imshow(pool['tonal.hpcp'].T, aspect='auto', origin='lower', interpolation='none') 101 #plot.title("HPCPs in frames (the 0-th HPCP coefficient corresponds to A)") 102 #show() 100 # data vals get normalized to be [0..1] 101 pyplot.imshow(pool['tonal.hpcp'].T, aspect='auto', origin='lower', interpolation='none') 102 103 pyplot.axis('off') 104 pyplot.savefig(output_filename_hpcp , bbox_inches="tight", pad_inches=0) 103 105 104 106 105 #plt.matshow(np.log(np.abs(constantq)),origin='lower', aspect='auto') 106 #aspect='auto' fits axis with the dimensions of the matrix 107 output_filename_hpcp_json = output_filename_root + ".json" 108 output = esstandard.YamlOutput(filename=output_filename_hpcp_json, format='json') 109 output(pool) 107 110 108 output_filename_hpcp = output_filename_root + "-hpcp.png" 111 # print("Estimated key and scale:", pool['tonal.key_key'] + " " + pool['tonal.key_scale']) 109 112 110 # https://stackoverflow.com/questions/47147146/save-an-image-only-content-without-axes-or-anything-else-to-a-file-using-matl111 #fig,ax = plot.subplots(1)112 #fig.subplots_adjust(left=0,right=1,bottom=0,top=1)113 #ax.axis('tight')114 #ax.axis('off')115 116 plot.axis('off')117 plot.savefig(output_filename_hpcp , bbox_inches="tight", pad_inches=0) # , transparent=True)118 119 # plot.savefig(output_filename_hpcp, bbox_inches="tight", pad_inches=0, transparent=True)120 121 # fig.savefig('sp_xyz.png', dpi=300, frameon='false')122 123 print("Estimated key and scale:", pool['tonal.key_key'] + " " + pool['tonal.key_scale'])
Note:
See TracChangeset
for help on using the changeset viewer.