def test_get_set_parameter(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) value = plug.get_parameter_value("produce_output") assert(value == 1.0) plug.set_parameter_value("produce_output", 0.0) value = plug.get_parameter_value("produce_output") assert(value == 0.0)
def test_process_without_initialise(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) try: plug.process_block([[1,2,3,4]], vh.RealTime(0, 0)) assert False except Exception: pass
def test_plugin_exists(): assert plugin_key in vh.list_plugins() plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert "pluginVersion" in plug.info if plug.info["pluginVersion"] != expectedVersion: print("Test plugin version " + str(plug.info["pluginVersion"]) + " does not match expected version " + str(expectedVersion)) assert plug.info["pluginVersion"] == expectedVersion
def test_setparameter(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert plug.parameters[0]["identifier"] == "produce_output" assert plug.parameters[0]["defaultValue"] == 1 assert plug.get_parameter_value("produce_output") == plug.parameters[0]["defaultValue"] assert plug.set_parameter_value("produce_output", 0) == True assert plug.get_parameter_value("produce_output") == 0 assert plug.set_parameter_values({ "produce_output": 1 }) == True assert plug.get_parameter_value("produce_output") == 1 try: plug.set_parameter_value("produce_output", "fish") assert False except TypeError: pass try: plug.set_parameter_value(4, 0) assert False except TypeError: pass try: plug.set_parameter_value("steak", 0) assert False except Exception: pass try: plug.get_parameter_value(4) assert False except TypeError: pass try: plug.get_parameter_value("steak") assert False except Exception: pass
def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(VampAnalyzer, self).setup(channels, samplerate, blocksize, totalframes) self.plugin = vampyhost.load_plugin( self.plugin_key, float(self.input_samplerate), vampyhost.ADAPT_INPUT_DOMAIN + vampyhost.ADAPT_BUFFER_SIZE + vampyhost.ADAPT_CHANNEL_COUNT) if not self.plugin.initialise(self.input_channels, self.input_stepsize, self.input_blocksize): raise RuntimeError("Vampy-Host failed to initialise plugin %d" % self.plugin_key) self.out_index = self.plugin.get_output( self.plugin_output)["output_index"] if not self.plugin_output: self.output_desc = self.plugin.get_output(0) self.plugin_output = self.output_desc["identifier"] else: self.output_desc = self.plugin.get_output(self.plugin_output)
def test_setparameter(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert plug.parameters[0]["identifier"] == "produce_output" assert plug.parameters[0]["defaultValue"] == 1 assert plug.get_parameter_value( "produce_output") == plug.parameters[0]["defaultValue"] assert plug.set_parameter_value("produce_output", 0) == True assert plug.get_parameter_value("produce_output") == 0 assert plug.set_parameter_values({"produce_output": 1}) == True assert plug.get_parameter_value("produce_output") == 1 try: plug.set_parameter_value("produce_output", "fish") assert False except TypeError: pass try: plug.set_parameter_value(4, 0) assert False except TypeError: pass try: plug.set_parameter_value("steak", 0) assert False except Exception: pass try: plug.get_parameter_value(4) assert False except TypeError: pass try: plug.get_parameter_value("steak") assert False except Exception: pass
def test_process_input_format(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(2, 4, 4) # channels, stepsize, blocksize result = plug.process_block([[1, 2, 3, 4], [5, 6, 7, 8]], vh.RealTime(0, 0)) result = plug.process_block( [np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])], vh.RealTime(0, 0)) result = plug.process_block(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), vh.RealTime(0, 0)) try: # Wrong number of channels result = plug.process_block(np.array([[1, 2, 3, 4]]), vh.RealTime(0, 0)) assert False except TypeError: pass try: # Wrong number of samples per channel result = plug.process_block(np.array([[1, 2, 3], [4, 5, 6]]), vh.RealTime(0, 0)) assert False except TypeError: pass try: # Differing numbers of samples per channel result = plug.process_block(np.array([[1, 2, 3, 4], [5, 6, 7]]), vh.RealTime(0, 0)) assert False except TypeError: pass
def test_get_set_parameter(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) value = plug.get_parameter_value("produce_output") assert (value == 1.0) plug.set_parameter_value("produce_output", 0.0) value = plug.get_parameter_value("produce_output") assert (value == 0.0)
def test_process_without_initialise(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) try: plug.process_block([[1, 2, 3, 4]], vh.RealTime(0, 0)) assert False except Exception: pass
def get_parameters_of(plugin_key): """Obtain the parameter descriptors, if any, for the given plugin key. """ plug = vampyhost.load_plugin(plugin_key, 44100, vampyhost.ADAPT_NONE) params = plug.parameters plug.unload() return params
def test_timestamp_method_fail(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) try: plug.set_process_timestamp_method(vh.SHIFT_DATA) assert False except Exception: pass
def test_load_unload(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.unload() try: plug.unload() # should throw but not crash assert(False) except AttributeError: pass
def test_load_unload(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.unload() try: plug.unload() # should throw but not crash assert (False) except AttributeError: pass
def test_process_output_1ch(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(1, 2, 2) try: # Too many channels result = plug.process_block([[3,4],[5,6]], vh.RealTime(0, 0)) assert False except TypeError: pass result = plug.process_block([[3,3]], vh.RealTime(0, 0)) assert result[9] == [ { "label" : "", "values" : np.array([5.0]) } ] result = plug.process_block([[3,0]], vh.RealTime(0, 0)) assert result[9] == [ { "label" : "", "values" : np.array([4.0]) } ]
def test_process_output_1ch(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(1, 2, 2) try: # Too many channels result = plug.process_block([[3, 4], [5, 6]], vh.RealTime(0, 0)) assert False except TypeError: pass result = plug.process_block([[3, 3]], vh.RealTime(0, 0)) assert result[9] == [{"label": "", "values": np.array([5.0])}] result = plug.process_block([[3, 0]], vh.RealTime(0, 0)) assert result[9] == [{"label": "", "values": np.array([4.0])}]
def load_and_configure(data, sample_rate, plugin_key, parameters, **kwargs): """Load the plugin with the given plugin key, at a given sample rate, configure it with the parameter keys and values in the given parameter dictionary, and initialise it with its preferred step and block size (or others as specified, see below). The channel count is taken from the shape of the data array provided. If you wish to override the step size, block size, or process timestamp method to be used, you may supply them as keyword arguments with keywords step_size (int), block_size (int), and process_timestamp_method (choose from vamp.vampyhost.SHIFT_DATA, vamp.vampyhost.SHIFT_TIMESTAMP, or vamp.vampyhost.NO_SHIFT). """ plug = vampyhost.load_plugin(plugin_key, sample_rate, vampyhost.ADAPT_INPUT_DOMAIN + vampyhost.ADAPT_CHANNEL_COUNT) if "process_timestamp_method" in kwargs: plug.set_process_timestamp_method(kwargs.pop("process_timestamp_method")) plug.set_parameter_values(parameters) block_size = 0 if "block_size" in kwargs: block_size = kwargs.pop("block_size") if block_size == 0: block_size = plug.get_preferred_block_size() if block_size == 0: block_size = 1024 step_size = 0 if "step_size" in kwargs: step_size = kwargs.pop("step_size") if step_size == 0: step_size = plug.get_preferred_step_size() if step_size == 0: step_size = block_size if kwargs != {}: raise Exception("Unexpected arguments in kwargs: " + str(kwargs.keys())) channels = 1 if data.ndim > 1: channels = data.shape[0] if plug.initialise(channels, step_size, block_size): return (plug, step_size, block_size) else: raise Exception("Failed to initialise plugin")
def test_get_output_by_id(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) out = plug.get_output("input-summary") assert "sampleType" in out assert out["output_index"] == 9 try: out = plug.get_output("chops") assert False except Exception: pass try: out = plug.get_output("") assert False except Exception: pass
def test_process_output_3ch(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(3, 2, 2) try: # Too few channels result = plug.process_block([[3,4],[5,6]], vh.RealTime(0, 0)) assert False except TypeError: pass try: # Too many channels result = plug.process_block([[3,4],[5,6],[7,8],[9,10]], vh.RealTime(0, 0)) assert False except TypeError: pass result = plug.process_block([[3,3],[4,4],[5,5]], vh.RealTime(0, 0)) assert (result[9][0]["values"] == np.array([5.0,6.0,7.0])).all() result = plug.process_block([[3,0],[4,0],[5,0]], vh.RealTime(0, 0)) assert (result[9][0]["values"] == np.array([4.0,5.0,6.0])).all()
def test_process_output_2ch(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(2, 2, 2) try: # Too few channels result = plug.process_block([[3, 4]], vh.RealTime(0, 0)) assert False except TypeError: pass try: # Too many channels result = plug.process_block([[3, 4], [5, 6], [7, 8]], vh.RealTime(0, 0)) assert False except TypeError: pass result = plug.process_block([[3, 3], [4, 4]], vh.RealTime(0, 0)) assert (result[9][0]["values"] == np.array([5.0, 6.0])).all() result = plug.process_block([[3, 0], [4, 0]], vh.RealTime(0, 0)) assert (result[9][0]["values"] == np.array([4.0, 5.0])).all()
def test_get_output_by_index(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) out = plug.get_output(0) assert "sampleType" in out assert out["identifier"] == "instants" assert out["output_index"] == 0 try: out = plug.get_output(20) assert False except Exception: pass try: out = plug.get_output(-1) assert False except Exception: pass try: out = plug.get_output(plug) assert False except Exception: pass
def test_process_input_format(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) plug.initialise(2, 4, 4) # channels, stepsize, blocksize result = plug.process_block([[1,2,3,4],[5,6,7,8]], vh.RealTime(0, 0)) result = plug.process_block([np.array([1,2,3,4]),np.array([5,6,7,8])], vh.RealTime(0, 0)) result = plug.process_block(np.array([[1,2,3,4],[5,6,7,8]]), vh.RealTime(0, 0)) try: # Wrong number of channels result = plug.process_block(np.array([[1,2,3,4]]), vh.RealTime(0, 0)) assert False except TypeError: pass try: # Wrong number of samples per channel result = plug.process_block(np.array([[1,2,3],[4,5,6]]), vh.RealTime(0, 0)) assert False except TypeError: pass try: # Differing numbers of samples per channel result = plug.process_block(np.array([[1,2,3,4],[5,6,7]]), vh.RealTime(0, 0)) assert False except TypeError: pass
def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(VampAnalyzer, self).setup( channels, samplerate, blocksize, totalframes) self.plugin = vampyhost.load_plugin(self.plugin_key, float(self.input_samplerate), vampyhost.ADAPT_INPUT_DOMAIN + vampyhost.ADAPT_BUFFER_SIZE + vampyhost.ADAPT_CHANNEL_COUNT) if not self.plugin.initialise(self.input_channels, self.input_stepsize, self.input_blocksize): raise RuntimeError("Vampy-Host failed to initialise plugin %d" % self.plugin_key) self.out_index = self.plugin.get_output( self.plugin_output)["output_index"] if not self.plugin_output: self.output_desc = self.plugin.get_output(0) self.plugin_output = self.output_desc["identifier"] else: self.output_desc = self.plugin.get_output(self.plugin_output)
def test_inputdomain(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert plug.inputDomain == vh.TIME_DOMAIN
def test_getoutputlist_2(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) outputs = plug.get_outputs() assert len(outputs) == 11
def test_info(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert plug.info["identifier"] == "vamp-test-plugin"
def test_parameterdescriptors(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_NONE) assert plug.parameters[0]["identifier"] == "produce_output"
def test_timestamp_method_succeed(): plug = vh.load_plugin(plugin_key_freq, rate, vh.ADAPT_INPUT_DOMAIN) assert plug.set_process_timestamp_method(vh.SHIFT_DATA)
def test_timestamp_method_fail2(): plug = vh.load_plugin(plugin_key, rate, vh.ADAPT_INPUT_DOMAIN) # Not a freq-domain plugin: shouldn't throw, but should return false assert (not plug.set_process_timestamp_method(vh.SHIFT_DATA))
def process_frames_multiple_outputs(ff, sample_rate, step_size, plugin_key, outputs, parameters = {}): """Process audio data with a Vamp plugin, and make the results from a set of plugin outputs available as a generator. The provided data should be an enumerable sequence of time-domain audio frames, of which each frame is 2-dimensional list or NumPy array of floats. The first dimension is taken to be the channel count, and the second dimension the frame or block size. The step_size argument gives the increment in audio samples from one frame to the next. Each frame must have the same size. The returned results will be those calculated by the plugin with the given key and returned through its outputs whose identifiers are given in the outputs argument. If the parameters dict is non-empty, the plugin will be configured by setting its parameters according to the (string) key and (float) value data found in the dict. This function acts as a generator, yielding a sequence of result feature sets as it obtains them. Each feature set is a dictionary mapping from output identifier to a list of features, each represented as a dictionary containing, optionally, timestamp and duration (RealTime objects), label (string), and a 1-dimensional array of float values. """ plugin = vampyhost.load_plugin(plugin_key, sample_rate, vampyhost.ADAPT_INPUT_DOMAIN + vampyhost.ADAPT_BUFFER_SIZE + vampyhost.ADAPT_CHANNEL_COUNT) out_indices = dict([(id, plugin.get_output(id)["output_index"]) for id in outputs]) fi = 0 channels = 0 block_size = 0 for f in ff: if fi == 0: channels = f.shape[0] block_size = f.shape[1] plugin.set_parameter_values(parameters) if not plugin.initialise(channels, step_size, block_size): raise "Failed to initialise plugin" timestamp = vampyhost.frame_to_realtime(fi, sample_rate) results = plugin.process_block(f, timestamp) # results is a dict mapping output number -> list of feature dicts for o in outputs: ix = out_indices[o] if ix in results: for r in results[ix]: yield { o: r } fi = fi + step_size if fi > 0: results = plugin.get_remaining_features() for o in outputs: ix = out_indices[o] if ix in results: for r in results[ix]: yield { o: r } plugin.unload()
def process_frames_multiple_outputs(ff, sample_rate, step_size, plugin_key, outputs, parameters={}): """Process audio data with a Vamp plugin, and make the results from a set of plugin outputs available as a generator. The provided data should be an enumerable sequence of time-domain audio frames, of which each frame is 2-dimensional list or NumPy array of floats. The first dimension is taken to be the channel count, and the second dimension the frame or block size. The step_size argument gives the increment in audio samples from one frame to the next. Each frame must have the same size. The returned results will be those calculated by the plugin with the given key and returned through its outputs whose identifiers are given in the outputs argument. If the parameters dict is non-empty, the plugin will be configured by setting its parameters according to the (string) key and (float) value data found in the dict. This function acts as a generator, yielding a sequence of result feature sets as it obtains them. Each feature set is a dictionary mapping from output identifier to a list of features, each represented as a dictionary containing, optionally, timestamp and duration (RealTime objects), label (string), and a 1-dimensional array of float values. """ plugin = vampyhost.load_plugin( plugin_key, sample_rate, vampyhost.ADAPT_INPUT_DOMAIN + vampyhost.ADAPT_BUFFER_SIZE + vampyhost.ADAPT_CHANNEL_COUNT) out_indices = dict([(id, plugin.get_output(id)["output_index"]) for id in outputs]) fi = 0 channels = 0 block_size = 0 for f in ff: if fi == 0: channels = f.shape[0] block_size = f.shape[1] plugin.set_parameter_values(parameters) if not plugin.initialise(channels, step_size, block_size): raise "Failed to initialise plugin" timestamp = vampyhost.frame_to_realtime(fi, sample_rate) results = plugin.process_block(f, timestamp) # results is a dict mapping output number -> list of feature dicts for o in outputs: ix = out_indices[o] if ix in results: for r in results[ix]: yield {o: r} fi = fi + step_size if fi > 0: results = plugin.get_remaining_features() for o in outputs: ix = out_indices[o] if ix in results: for r in results[ix]: yield {o: r} plugin.unload()