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@56
|
30 '''A high-level interface to the vampyhost extension module, for quickly and easily running Vamp audio analysis plugins on audio files and buffers.'''
|
Chris@56
|
31
|
Chris@56
|
32 import vampyhost
|
Chris@56
|
33
|
Chris@79
|
34 def list_plugins():
|
Chris@98
|
35 """Obtain a list of plugin keys for all currently installed Vamp plugins.
|
Chris@98
|
36
|
Chris@98
|
37 The returned value is a list of strings, each of which is the key
|
Chris@98
|
38 for one plugin. (Note that a plugin may have multiple outputs, if
|
Chris@98
|
39 it computes more than one type of feature.)
|
Chris@98
|
40
|
Chris@98
|
41 To query the available outputs and category of a plugin, you may
|
Chris@98
|
42 use vamp.get_outputs_of() and vamp.get_category_of(). Further
|
Chris@98
|
43 information may be retrieved by loading the plugin and querying
|
Chris@98
|
44 its info dictionary using the low-level functions in the
|
Chris@98
|
45 vamp.vampyhost extension module.
|
Chris@98
|
46
|
Chris@98
|
47 To make use of a plugin to extract features from audio data, pass
|
Chris@98
|
48 the plugin key and optionally an output identifier to
|
Chris@98
|
49 vamp.process() or vamp.collect().
|
Chris@98
|
50 """
|
Chris@79
|
51 return vampyhost.list_plugins()
|
Chris@56
|
52
|
Chris@129
|
53 def get_outputs_of(plugin_key):
|
Chris@98
|
54 """Obtain a list of the output identifiers for the given plugin key.
|
Chris@98
|
55 """
|
Chris@129
|
56 return vampyhost.get_outputs_of(plugin_key)
|
Chris@98
|
57
|
Chris@129
|
58 def get_category_of(plugin_key):
|
Chris@98
|
59 """Obtain the category descriptor, if any, for the given plugin key.
|
Chris@98
|
60
|
Chris@98
|
61 The returned value is a list of descriptor terms, from least
|
Chris@98
|
62 specific to most specific. The list may be empty if no category
|
Chris@98
|
63 information is found for the plugin.
|
Chris@98
|
64 """
|
Chris@129
|
65 return vampyhost.get_category_of(plugin_key)
|
Chris@98
|
66
|
Chris@144
|
67 def get_parameters_of(plugin_key):
|
Chris@144
|
68 """Obtain the parameter descriptors, if any, for the given plugin key.
|
Chris@144
|
69 """
|
Chris@144
|
70 plug = vampyhost.load_plugin(plugin_key, 44100, vampyhost.ADAPT_NONE)
|
Chris@144
|
71 params = plug.parameters
|
Chris@144
|
72 plug.unload()
|
Chris@144
|
73 return params
|
Chris@144
|
74
|
Chris@140
|
75 def load_and_configure(data, sample_rate, plugin_key, parameters, **kwargs):
|
Chris@140
|
76 """Load the plugin with the given plugin key, at a given sample
|
Chris@140
|
77 rate, configure it with the parameter keys and values in the given
|
Chris@140
|
78 parameter dictionary, and initialise it with its preferred step and
|
Chris@140
|
79 block size (or others as specified, see below). The channel count is
|
Chris@140
|
80 taken from the shape of the data array provided.
|
Chris@140
|
81
|
Chris@143
|
82 If you wish to override the step size, block size, or process
|
Chris@143
|
83 timestamp method to be used, you may supply them as keyword
|
Chris@143
|
84 arguments with keywords step_size (int), block_size (int), and
|
Chris@143
|
85 process_timestamp_method (choose from vamp.vampyhost.SHIFT_DATA,
|
Chris@143
|
86 vamp.vampyhost.SHIFT_TIMESTAMP, or vamp.vampyhost.NO_SHIFT).
|
Chris@98
|
87 """
|
Chris@82
|
88
|
Chris@129
|
89 plug = vampyhost.load_plugin(plugin_key, sample_rate,
|
Chris@82
|
90 vampyhost.ADAPT_INPUT_DOMAIN +
|
Chris@82
|
91 vampyhost.ADAPT_CHANNEL_COUNT)
|
Chris@64
|
92
|
Chris@140
|
93 if "process_timestamp_method" in kwargs:
|
Chris@140
|
94 plug.set_process_timestamp_method(kwargs.pop("process_timestamp_method"))
|
Chris@140
|
95
|
Chris@80
|
96 plug.set_parameter_values(parameters)
|
Chris@68
|
97
|
Chris@140
|
98 block_size = 0
|
Chris@140
|
99 if "block_size" in kwargs:
|
Chris@140
|
100 block_size = kwargs.pop("block_size")
|
Chris@140
|
101 if block_size == 0:
|
Chris@140
|
102 block_size = plug.get_preferred_block_size()
|
Chris@84
|
103 if block_size == 0:
|
Chris@84
|
104 block_size = 1024
|
Chris@140
|
105
|
Chris@140
|
106 step_size = 0
|
Chris@140
|
107 if "step_size" in kwargs:
|
Chris@140
|
108 step_size = kwargs.pop("step_size")
|
Chris@84
|
109 if step_size == 0:
|
Chris@140
|
110 step_size = plug.get_preferred_step_size()
|
Chris@140
|
111 if step_size == 0:
|
Chris@140
|
112 step_size = block_size
|
Chris@140
|
113
|
Chris@140
|
114 if kwargs != {}:
|
Chris@140
|
115 raise Exception("Unexpected arguments in kwargs: " + str(kwargs.keys()))
|
Chris@68
|
116
|
Chris@68
|
117 channels = 1
|
Chris@68
|
118 if data.ndim > 1:
|
Chris@68
|
119 channels = data.shape[0]
|
Chris@68
|
120
|
Chris@95
|
121 if plug.initialise(channels, step_size, block_size):
|
Chris@95
|
122 return (plug, step_size, block_size)
|
Chris@95
|
123 else:
|
Chris@140
|
124 raise Exception("Failed to initialise plugin")
|