1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | from __future__ import absolute_import
|
---|
4 | from __future__ import division
|
---|
5 | from __future__ import print_function
|
---|
6 |
|
---|
7 | import sys
|
---|
8 | import os
|
---|
9 |
|
---|
10 | import essentia
|
---|
11 | import essentia.standard
|
---|
12 | import essentia.streaming
|
---|
13 | import essentia.streaming as ess
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | # https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib
|
---|
18 | import matplotlib
|
---|
19 | matplotlib.use('Agg')
|
---|
20 | import matplotlib.pyplot as plot
|
---|
21 |
|
---|
22 | #from pylab import show, figure, imshow
|
---|
23 |
|
---|
24 | import pylab
|
---|
25 |
|
---|
26 | argc = len(sys.argv)
|
---|
27 |
|
---|
28 | input_audio_filename = None
|
---|
29 | output_filename_root = None
|
---|
30 |
|
---|
31 | if 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)
|
---|
36 | else:
|
---|
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
|
---|
46 | loader = ess.MonoLoader(filename=input_audio_filename)
|
---|
47 |
|
---|
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)
|
---|
56 |
|
---|
57 | # Use default HPCP parameters for plots, however we will need higher resolution
|
---|
58 | # and custom parameters for better Key estimation
|
---|
59 |
|
---|
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.)
|
---|
69 |
|
---|
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)
|
---|
76 |
|
---|
77 | # Use pool to store data
|
---|
78 | pool = essentia.Pool()
|
---|
79 |
|
---|
80 | # 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
|
---|
85 | spectralpeaks.frequencies >> hpcp.frequencies
|
---|
86 | spectralpeaks.magnitudes >> hpcp_key.magnitudes
|
---|
87 | 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')
|
---|
93 |
|
---|
94 | # Run streaming network
|
---|
95 | essentia.run(loader)
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
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()
|
---|
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 |
|
---|
108 | output_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 |
|
---|
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'])
|
---|