Chris@117
|
1 #!/usr/bin/env python
|
Chris@117
|
2
|
Chris@117
|
3 # Python Vamp Host
|
Chris@117
|
4 # Copyright (c) 2008-2015 Queen Mary, University of London
|
Chris@117
|
5 #
|
Chris@117
|
6 # Permission is hereby granted, free of charge, to any person
|
Chris@117
|
7 # obtaining a copy of this software and associated documentation
|
Chris@117
|
8 # files (the "Software"), to deal in the Software without
|
Chris@117
|
9 # restriction, including without limitation the rights to use, copy,
|
Chris@117
|
10 # modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@117
|
11 # of the Software, and to permit persons to whom the Software is
|
Chris@117
|
12 # furnished to do so, subject to the following conditions:
|
Chris@117
|
13 #
|
Chris@117
|
14 # The above copyright notice and this permission notice shall be
|
Chris@117
|
15 # included in all copies or substantial portions of the Software.
|
Chris@117
|
16 #
|
Chris@117
|
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@117
|
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@117
|
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@117
|
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
Chris@117
|
21 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@117
|
22 # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@117
|
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@117
|
24 #
|
Chris@117
|
25 # Except as contained in this notice, the names of the Centre for
|
Chris@117
|
26 # Digital Music and Queen Mary, University of London shall not be
|
Chris@117
|
27 # used in advertising or otherwise to promote the sale, use or other
|
Chris@117
|
28 # dealings in this Software without prior written authorization.
|
Chris@117
|
29
|
Chris@129
|
30 '''
|
Chris@129
|
31 Python Vamp plugin host
|
Chris@129
|
32 =======================
|
Chris@129
|
33
|
Chris@129
|
34 This module allows Python code to load and use native-code Vamp
|
Chris@129
|
35 plugins (http://vamp-plugins.org) for audio feature analysis.
|
Chris@129
|
36
|
Chris@129
|
37 The module consists of a native-code extension ("vampyhost") that
|
Chris@129
|
38 provides a low-level wrapper for the Vamp plugin SDK, along with a
|
Chris@129
|
39 Python wrapper ("vamp") that provides a higher-level abstraction.
|
Chris@129
|
40
|
Chris@129
|
41 No code for loading audio files is included; you'll need to use some
|
Chris@129
|
42 other module for that. This code expects to receive decoded audio data
|
Chris@129
|
43 of one or more channels, either as a series of frames or as a single
|
Chris@129
|
44 whole buffer.
|
Chris@129
|
45
|
Chris@129
|
46 Written by Chris Cannam and George Fazekas at the Centre for Digital
|
Chris@129
|
47 Music, Queen Mary University of London. Copyright 2008-2015 Queen
|
Chris@129
|
48 Mary, University of London. Refer to COPYING.rst for licence details.
|
Chris@129
|
49
|
Chris@129
|
50 See home page at https://code.soundsoftware.ac.uk/projects/vampy-host
|
Chris@129
|
51 for more details.
|
Chris@129
|
52
|
Chris@129
|
53
|
Chris@129
|
54 A simple example
|
Chris@129
|
55 ----------------
|
Chris@129
|
56
|
Chris@129
|
57 Using librosa (http://bmcfee.github.io/librosa/) to read an audio
|
Chris@129
|
58 file, and the NNLS Chroma Vamp plugin
|
Chris@129
|
59 (https://code.soundsoftware.ac.uk/projects/nnls-chroma/) for
|
Chris@129
|
60 analysis::
|
Chris@129
|
61
|
Chris@129
|
62 $ python
|
Chris@129
|
63 >>> import vamp
|
Chris@129
|
64 >>> import librosa
|
Chris@129
|
65 >>> data, rate = librosa.load("example.wav")
|
Chris@129
|
66 >>> chroma = vamp.collect(data, rate, "nnls-chroma:nnls-chroma")
|
Chris@129
|
67 >>> chroma
|
Chris@129
|
68 {'matrix': ( 0.092879819, array([[ 61.0532608 , 60.27478409, 59.3938446 , ..., 182.13394165,
|
Chris@129
|
69 42.40084457, 116.55457306],
|
Chris@129
|
70 [ 68.8901825 , 63.98115921, 60.77633667, ..., 245.88218689,
|
Chris@129
|
71 68.51251984, 164.70120239],
|
Chris@129
|
72 [ 58.59794617, 50.3429184 , 45.44804764, ..., 258.02362061,
|
Chris@129
|
73 83.95749664, 179.91200256],
|
Chris@129
|
74 ...,
|
Chris@129
|
75 [ 0. , 0. , 0. , ..., 0. ,
|
Chris@129
|
76 0. , 0. ],
|
Chris@129
|
77 [ 0. , 0. , 0. , ..., 0. ,
|
Chris@129
|
78 0. , 0. ],
|
Chris@129
|
79 [ 0. , 0. , 0. , ..., 0. ,
|
Chris@129
|
80 0. , 0. ]], dtype=float32))}
|
Chris@129
|
81 >>> stepsize, chromadata = chroma["matrix"]
|
Chris@129
|
82 >>> import matplotlib.pyplot as plt
|
Chris@129
|
83 >>> plt.imshow(chromadata)
|
Chris@129
|
84 <matplotlib.image.AxesImage object at 0x7fe9e0043fd0>
|
Chris@129
|
85 >>> plt.show()
|
Chris@129
|
86
|
Chris@129
|
87 And a pitch-chroma plot appears.
|
Chris@129
|
88
|
Chris@129
|
89
|
Chris@129
|
90 High-level interface (vamp)
|
Chris@129
|
91 ---------------------------
|
Chris@129
|
92
|
Chris@129
|
93 This module contains three sorts of function:
|
Chris@129
|
94
|
Chris@129
|
95 1. Basic info and lookup functions:
|
Chris@129
|
96
|
Chris@129
|
97 * ``vamp.list_plugins``
|
Chris@129
|
98 * ``vamp.get_outputs_of``
|
Chris@129
|
99 * ``vamp.get_category_of``
|
Chris@129
|
100
|
Chris@129
|
101 These retrieve the installed plugin keys and get basic information
|
Chris@129
|
102 about each plugin. (For more detailed information, load a plugin
|
Chris@129
|
103 and inspect it using the low-level interface described below.)
|
Chris@129
|
104
|
Chris@129
|
105 2. Process functions:
|
Chris@129
|
106
|
Chris@129
|
107 * ``vamp.process_audio``
|
Chris@129
|
108 * ``vamp.process_frames``
|
Chris@129
|
109 * ``vamp.process_audio_multiple_outputs``
|
Chris@129
|
110 * ``vamp.process_frames_multiple_outputs``
|
Chris@129
|
111
|
Chris@129
|
112 These accept audio input, and produce output in the form of a list
|
Chris@129
|
113 of feature sets structured similarly to those in the C++ Vamp
|
Chris@129
|
114 plugin SDK. The plugin to be used is specified by its key (the
|
Chris@129
|
115 identifier as returned by ``vamp.list_plugins``). A dictionary of
|
Chris@129
|
116 plugin parameter settings may optionally be supplied.
|
Chris@129
|
117
|
Chris@129
|
118 The ``_audio`` versions take a single (presumably long) array of
|
Chris@129
|
119 audio samples as input, and chop it into frames according to the
|
Chris@129
|
120 plugin's preferred step and block sizes. The ``_frames`` versions
|
Chris@129
|
121 instead accept an enumerable sequence of audio frame arrays.
|
Chris@129
|
122
|
Chris@129
|
123 3. The process-and-collect function:
|
Chris@129
|
124
|
Chris@129
|
125 * ``vamp.collect``
|
Chris@129
|
126
|
Chris@129
|
127 This accepts a single array of audio samples as input, and returns
|
Chris@129
|
128 an output structure that reflects the underlying structure of the
|
Chris@129
|
129 feature output (depending on whether it is a curve, grid, etc). The
|
Chris@129
|
130 plugin to be used is specified by its key. A dictionary of plugin
|
Chris@129
|
131 parameter settings may optionally be supplied.
|
Chris@129
|
132
|
Chris@129
|
133 The ``collect`` function processes the whole input before returning
|
Chris@129
|
134 anything; if you need to supply a streamed input, or retrieve
|
Chris@129
|
135 results as they are calculated, then you must use one of the
|
Chris@129
|
136 ``process`` functions (above) or else the low-level interface
|
Chris@129
|
137 (below).
|
Chris@129
|
138
|
Chris@129
|
139
|
Chris@129
|
140 Low-level interface (vampyhost)
|
Chris@129
|
141 -------------------------------
|
Chris@129
|
142
|
Chris@129
|
143 This extension contains facilities that operate on Vamp plugins in a
|
Chris@129
|
144 way analogous to the existing C++ Vamp Host SDK: ``list_plugins``,
|
Chris@129
|
145 ``get_plugin_path``, ``get_category_of``, ``get_library_for``,
|
Chris@129
|
146 ``get_outputs_of``, ``load_plugin``, and a utility function
|
Chris@129
|
147 ``frame_to_realtime``.
|
Chris@129
|
148
|
Chris@129
|
149 Calling ``load_plugin`` gets you a ``vampyhost.Plugin`` object, which
|
Chris@129
|
150 then exposes all of the methods found in the Vamp SDK Plugin class.
|
Chris@129
|
151
|
Chris@129
|
152 (Note that methods wrapped directly from the Vamp SDK are named using
|
Chris@129
|
153 camelCase, so as to match the names found in the C++ SDK. Elsewhere
|
Chris@129
|
154 this module follows Python PEP8 naming.)
|
Chris@129
|
155
|
Chris@129
|
156 See the individual module and function documentation for further
|
Chris@129
|
157 details.
|
Chris@129
|
158
|
Chris@124
|
159 '''
|
Chris@56
|
160
|
Chris@75
|
161 import vampyhost
|
Chris@68
|
162
|
Chris@112
|
163 from vamp.load import list_plugins, get_outputs_of, get_category_of
|
Chris@117
|
164 from vamp.process import process_audio, process_frames, process_audio_multiple_outputs, process_frames_multiple_outputs
|
Chris@112
|
165 from vamp.collect import collect
|
Chris@95
|
166
|