changeset 56:3db99d3f2e30

Start Python module, add framer
author Chris Cannam
date Wed, 14 Jan 2015 09:52:33 +0000
parents 5e7bda1d5e9a
children a0243984805b
files test/test_frames.py vamp/__init__.py
diffstat 2 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test_frames.py	Wed Jan 14 09:52:33 2015 +0000
@@ -0,0 +1,22 @@
+
+import vamp
+import numpy as np
+
+def to_lists(arrs):
+    return [list([list(r) for r in f]) for f in arrs]
+
+def test_frames_from_1d_buffer():
+    buf = np.arange(6)
+    ff = to_lists(vamp.framesFromArray(buf, 2, 2))
+    assert(ff == [[[0,1]],[[2,3]],[[4,5]]])
+    ff = to_lists(vamp.framesFromArray(buf, 1, 2))
+    assert(ff == [[[0,1]],[[1,2]],[[2,3]],[[3,4]],[[4,5]],[[5,0]]])
+
+def test_frames_from_2d_buffer():
+    buf = np.array([np.arange(6),np.arange(6,12)])
+    ff = to_lists(vamp.framesFromArray(buf, 2, 2))
+    assert(ff == [[[0,1],[6,7]],[[2,3],[8,9]],[[4,5],[10,11]]])
+    ff = to_lists(vamp.framesFromArray(buf, 1, 2))
+    assert(ff == [[[0,1],[6,7]],[[1,2],[7,8]],[[2,3],[8,9]],[[3,4],[9,10]],[[4,5],[10,11]],[[5,0],[11,0]]])
+
+    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vamp/__init__.py	Wed Jan 14 09:52:33 2015 +0000
@@ -0,0 +1,32 @@
+'''A high-level interface to the vampyhost extension module, for quickly and easily running Vamp audio analysis plugins on audio files and buffers.'''
+
+import vampyhost
+import numpy
+
+def listPlugins():
+    return vampyhost.listPlugins()
+
+def framesFromArray(arr, stepSize, frameSize):
+    """Generate a list of frames of size frameSize, extracted from the input array arr at stepSize intervals"""
+    # presumably such a function exists in many places, but I need practice
+    assert(stepSize > 0)
+    if arr.ndim == 1: # turn 1d into 2d array with 1 channel
+        arr = numpy.reshape(arr, (1, arr.shape[0]))
+    assert(arr.ndim == 2)
+    n = arr.shape[1]
+    i = 0
+    while (i < n):
+        frame = arr[:, i : i + frameSize]
+        w = frame.shape[1]
+        if (w < frameSize):
+            pad = numpy.zeros((frame.shape[0], frameSize - w))
+            frame = numpy.concatenate((frame, pad), 1)
+        yield frame
+        i = i + stepSize
+
+def process(data, samplerate, key, parameters, outputs):
+    plug = vampyhost.loadPlugin(key, samplerate, vampyhost.AdaptNone) ##!!! input domain
+    stepSize = plug.getPreferredStepSize()
+    blockSize = plug.getPreferredBlockSize()
+    ff = framesFromArray(data, stepSize, blockSize)
+    return True