changeset 140:1a494598ee2b

Support step size, block size, process timestamp method kwargs
author Chris Cannam
date Wed, 08 Jul 2015 12:46:04 +0100
parents aa96f69e2f14
children 72d6b86f8ce0
files test/test_collect.py test/test_process.py vamp/collect.py vamp/load.py vamp/process.py
diffstat 5 files changed, 73 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/test/test_collect.py	Wed Jul 08 11:37:06 2015 +0100
+++ b/test/test_collect.py	Wed Jul 08 12:46:04 2015 +0100
@@ -53,16 +53,33 @@
     step, results = rdict["vector"]
     assert len(results) > 0
 
-def test_process_summary_param_kwargs():
+def test_process_summary_param_kwargs_1():
     buf = input_data(blocksize * 10)
     rdict = vamp.collect(plugin_key = plugin_key, output = "input-summary", parameters = { "produce_output": False }, data = buf, sample_rate = rate)
     assert ("vector" in rdict)
     step, results = rdict["vector"]
     assert len(results) == 0
+
+def test_process_summary_param_kwargs_2():
+    buf = input_data(blocksize * 10)
     rdict = vamp.collect(plugin_key = plugin_key, output = "input-summary", data = buf, sample_rate = rate)
     assert ("vector" in rdict)
     step, results = rdict["vector"]
     assert len(results) > 0
+    
+def test_process_summary_param_kwargs_3():
+    buf = input_data(blocksize * 10)
+    rdict = vamp.collect(plugin_key = plugin_key, output = "input-summary", data = buf, sample_rate = rate, process_timestamp_method = vamp.vampyhost.SHIFT_DATA)
+    assert ("vector" in rdict)
+    step, results = rdict["vector"]
+    assert len(results) > 0
+
+def test_process_summary_param_kwargs_fail():
+    buf = input_data(blocksize * 10)
+    try:
+        rdict = vamp.collect(plugin_key = plugin_key, output = "input-summary", data = buf, sample_rate = rate, process_timestamp_method = vamp.vampyhost.SHIFT_DATA, unknown_argument = 1)
+    except Exception: # unknown kwarg
+        pass
 
 def test_collect_fixed_sample_rate():
     buf = input_data(blocksize * 10)
--- a/test/test_process.py	Wed Jul 08 11:37:06 2015 +0100
+++ b/test/test_process.py	Wed Jul 08 12:46:04 2015 +0100
@@ -156,6 +156,26 @@
         eps = 1e-6
         assert abs(actual - expected) < eps
 
+def test_process_freq_summary_shift():
+    buf = input_data(blocksize * 10)
+    results = list(vamp.process_audio(buf, rate, plugin_key_freq, "input-summary", {}, process_timestamp_method = vamp.vampyhost.SHIFT_DATA))
+    assert len(results) == 20
+    for i in range(len(results)):
+        # as test_process_freq_summary, except that the input is effectively
+        # padded by the adapter with an additional half-blocksize of zeros
+        # before conversion
+        if i == 0:
+            # this block doesn't interact at all well with our test, we get
+            # spurious low values in the block converted back within the plugin
+            # because of the big discontinuity & window ripple after fftshift
+            pass
+        else:
+            expected = (i-1) * (blocksize/2) + blocksize/2 + 1 # for "first" elt
+            expected = expected + blocksize - 1 # non-zero elts
+            actual = results[i]["values"][0]
+            eps = 1e-6
+            assert abs(actual - expected) < eps
+
 def test_process_multi_freq_summary():
     buf = input_data(blocksize * 10)
     results = list(vamp.process_audio_multiple_outputs(buf, rate, plugin_key_freq, [ "input-summary" ], {}))
--- a/vamp/collect.py	Wed Jul 08 11:37:06 2015 +0100
+++ b/vamp/collect.py	Wed Jul 08 12:46:04 2015 +0100
@@ -108,7 +108,7 @@
     return rv
 
         
-def collect(data, sample_rate, plugin_key, output = "", parameters = {}):
+def collect(data, sample_rate, plugin_key, output = "", parameters = {}, **kwargs):
     """Process audio data with a Vamp plugin, and make the results from a
     single plugin output available as a single structure.
 
@@ -158,7 +158,7 @@
 
     """
 
-    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters)
+    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters, **kwargs)
 
     if output == "":
         output_desc = plugin.get_output(0)
--- a/vamp/load.py	Wed Jul 08 11:37:06 2015 +0100
+++ b/vamp/load.py	Wed Jul 08 12:46:04 2015 +0100
@@ -64,27 +64,45 @@
     """
     return vampyhost.get_category_of(plugin_key)
 
-def load_and_configure(data, sample_rate, plugin_key, parameters):
-    """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. The channel count is taken from the shape of the
-    data array provided.
+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.
+
+    Optionally the step size, block size, and process timestamp method
+    may be provided through step_size, block_size, and
+    process_timestamp_method keyword arguments.
     """
 
     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)
 
-    step_size = plug.get_preferred_step_size()
-    block_size = plug.get_preferred_block_size()
-
+    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 = block_size ##!!! or block_size/2, but check this with input domain adapter
+        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:
@@ -93,4 +111,4 @@
     if plug.initialise(channels, step_size, block_size):
         return (plug, step_size, block_size)
     else:
-        raise "Failed to initialise plugin"
+        raise Exception("Failed to initialise plugin")
--- a/vamp/process.py	Wed Jul 08 11:37:06 2015 +0100
+++ b/vamp/process.py	Wed Jul 08 12:46:04 2015 +0100
@@ -59,7 +59,7 @@
                 yield { o: r }
 
 
-def process_audio(data, sample_rate, plugin_key, output = "", parameters = {}):
+def process_audio(data, sample_rate, plugin_key, output = "", parameters = {}, **kwargs):
     """Process audio data with a Vamp plugin, and make the results from a
     single plugin output available as a generator.
 
@@ -86,7 +86,7 @@
     structure, consider using vamp.collect() instead.
     """
 
-    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters)
+    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters, **kwargs)
 
     if output == "":
         output = plugin.get_output(0)["identifier"]
@@ -169,7 +169,7 @@
     plugin.unload()
     
 
-def process_audio_multiple_outputs(data, sample_rate, plugin_key, outputs, parameters = {}):
+def process_audio_multiple_outputs(data, sample_rate, plugin_key, outputs, parameters = {}, **kwargs):
     """Process audio data with a Vamp plugin, and make the results from a
     set of plugin outputs available as a generator.
 
@@ -193,7 +193,7 @@
     array of float values.
     """
 
-    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters)
+    plugin, step_size, block_size = vamp.load.load_and_configure(data, sample_rate, plugin_key, parameters, **kwargs)
 
     ff = vamp.frames.frames_from_array(data, step_size, block_size)