Changeset 34409


Ignore:
Timestamp:
2020-09-17T17:58:05+12:00 (4 years ago)
Author:
davidb
Message:

Code tidyup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/mars-src/trunk/bin/script/essentia-hpcp.py

    r34389 r34409  
    99
    1010import essentia
    11 import essentia.standard
    12 import essentia.streaming
    13 import essentia.streaming as ess
    14 
    15 
     11import essentia.standard  as esstandard
     12import essentia.streaming as esstreaming
    1613
    1714# 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'
    1817import 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
     19matplotlib.use('Agg')
     20import matplotlib.pyplot as pyplot
    2521
    2622argc = len(sys.argv)
    2723
    28 input_audio_filename     = None
     24input_audio_filename = None
    2925output_filename_root = None
    3026
    3127if argc <= 1:
    32 # Python3 syntax:
    3328    print("Usage: "+sys.argv[0] +" input_file [output_file]\n",file=sys.stderr)   
    34 #    print "Usage: "+sys.argv[0] +" input_file [output_file]\n"   
    3529    sys.exit(1)
    3630else:
    37     input_audio_filename    = sys.argv[1]
     31    input_audio_filename = sys.argv[1]
    3832    if argc == 2:
    3933        output_filename_root = os.path.splitext(input_audio_filename)[0]
    4034    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]
    4237
    4338
    4439
    4540# Initialize algorithms we will use
    46 loader = ess.MonoLoader(filename=input_audio_filename)
     41loader = esstreaming.MonoLoader(filename=input_audio_filename)
    4742
    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)
     43framecutter   = esstreaming.FrameCutter(frameSize=4096, hopSize=2048, silentFrames='noise')
     44windowing     = esstreaming.Windowing(type='blackmanharris62')
     45spectrum      = esstreaming.Spectrum()
     46spectralpeaks = esstreaming.SpectralPeaks(orderBy='magnitude',
     47                                          magnitudeThreshold=0.00001,
     48                                          minFrequency=20,
     49                                          maxFrequency=3500,
     50                                          maxPeaks=60)
     51
    5652
    5753# Use default HPCP parameters for plots, however we will need higher resolution
    5854# and custom parameters for better Key estimation
    5955
    60 hpcp = ess.HPCP()
    61 hpcp_key = ess.HPCP(size=36, # we will need higher resolution for Key estimation
    62                     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.)
     56hpcp     = esstreaming.HPCP()
     57hpcp_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.)
    6965
    70 key = ess.Key(profileType='edma', # Use profile for electronic music
    71               numHarmonics=4,
    72               pcpSize=36,
    73               slope=0.6,
    74               usePolyphony=True,
    75               useThreeChords=True)
     66key       = 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)
    7672
    7773# Use pool to store data
     
    7975
    8076# Connect streaming algorithms
    81 loader.audio >> framecutter.signal
    82 framecutter.frame >> windowing.frame >> spectrum.frame
    83 spectrum.spectrum >> spectralpeaks.spectrum
    84 spectralpeaks.magnitudes >> hpcp.magnitudes
     77loader.audio              >> framecutter.signal
     78framecutter.frame         >> windowing.frame        >> spectrum.frame
     79spectrum.spectrum         >> spectralpeaks.spectrum
     80spectralpeaks.magnitudes  >> hpcp.magnitudes
    8581spectralpeaks.frequencies >> hpcp.frequencies
    86 spectralpeaks.magnitudes >> hpcp_key.magnitudes
     82
     83spectralpeaks.magnitudes  >> hpcp_key.magnitudes
    8784spectralpeaks.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')
     85hpcp_key.hpcp             >> key.pcp
     86
     87hpcp.hpcp     >> (pool, 'tonal.hpcp')
     88
     89key.key       >> (pool, 'tonal.key_key')
     90key.scale     >> (pool, 'tonal.key_scale')
     91key.strength  >> (pool, 'tonal.key_strength')
    9392
    9493# Run streaming network
     
    9695
    9796
     97output_filename_hpcp = output_filename_root + ".png"
    9898
    9999# 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]
     101pyplot.imshow(pool['tonal.hpcp'].T, aspect='auto', origin='lower', interpolation='none')
     102
     103pyplot.axis('off')
     104pyplot.savefig(output_filename_hpcp , bbox_inches="tight", pad_inches=0)
    103105
    104106
    105 #plt.matshow(np.log(np.abs(constantq)),origin='lower', aspect='auto')
    106 #aspect='auto' fits axis with the dimensions of the matrix
     107output_filename_hpcp_json = output_filename_root + ".json"
     108output = esstandard.YamlOutput(filename=output_filename_hpcp_json, format='json')
     109output(pool)
    107110
    108 output_filename_hpcp = output_filename_root + "-hpcp.png"
     111# print("Estimated key and scale:", pool['tonal.key_key'] + " " + pool['tonal.key_scale'])
    109112
    110 # https://stackoverflow.com/questions/47147146/save-an-image-only-content-without-axes-or-anything-else-to-a-file-using-matl
    111 #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.