source: gs3-extensions/mars-src/trunk/bin/script/essentia-hpcp.py@ 34389

Last change on this file since 34389 was 34389, checked in by davidb, 4 years ago

First cut at script to produce a borderless HPCP images of audio file

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1#!/usr/bin/env python
2
3from __future__ import absolute_import
4from __future__ import division
5from __future__ import print_function
6
7import sys
8import os
9
10import essentia
11import essentia.standard
12import essentia.streaming
13import essentia.streaming as ess
14
15
16
17# https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib
18import matplotlib
19matplotlib.use('Agg')
20import matplotlib.pyplot as plot
21
22#from pylab import show, figure, imshow
23
24import pylab
25
26argc = len(sys.argv)
27
28input_audio_filename = None
29output_filename_root = None
30
31if argc <= 1:
32# Python3 syntax:
33 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 sys.exit(1)
36else:
37 input_audio_filename = sys.argv[1]
38 if argc == 2:
39 output_filename_root = os.path.splitext(input_audio_filename)[0]
40 else:
41 output_filename_root = sys.argv[2]
42
43
44
45# Initialize algorithms we will use
46loader = ess.MonoLoader(filename=input_audio_filename)
47
48framecutter = ess.FrameCutter(frameSize=4096, hopSize=2048, silentFrames='noise')
49windowing = ess.Windowing(type='blackmanharris62')
50spectrum = ess.Spectrum()
51spectralpeaks = ess.SpectralPeaks(orderBy='magnitude',
52 magnitudeThreshold=0.00001,
53 minFrequency=20,
54 maxFrequency=3500,
55 maxPeaks=60)
56
57# Use default HPCP parameters for plots, however we will need higher resolution
58# and custom parameters for better Key estimation
59
60hpcp = ess.HPCP()
61hpcp_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.)
69
70key = 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)
76
77# Use pool to store data
78pool = essentia.Pool()
79
80# Connect streaming algorithms
81loader.audio >> framecutter.signal
82framecutter.frame >> windowing.frame >> spectrum.frame
83spectrum.spectrum >> spectralpeaks.spectrum
84spectralpeaks.magnitudes >> hpcp.magnitudes
85spectralpeaks.frequencies >> hpcp.frequencies
86spectralpeaks.magnitudes >> hpcp_key.magnitudes
87spectralpeaks.frequencies >> hpcp_key.frequencies
88hpcp_key.hpcp >> key.pcp
89hpcp.hpcp >> (pool, 'tonal.hpcp')
90key.key >> (pool, 'tonal.key_key')
91key.scale >> (pool, 'tonal.key_scale')
92key.strength >> (pool, 'tonal.key_strength')
93
94# Run streaming network
95essentia.run(loader)
96
97
98
99# Plot HPCP
100pylab.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()
103
104
105#plt.matshow(np.log(np.abs(constantq)),origin='lower', aspect='auto')
106#aspect='auto' fits axis with the dimensions of the matrix
107
108output_filename_hpcp = output_filename_root + "-hpcp.png"
109
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
116plot.axis('off')
117plot.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
123print("Estimated key and scale:", pool['tonal.key_key'] + " " + pool['tonal.key_scale'])
Note: See TracBrowser for help on using the repository browser.