Chris@56: '''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: Chris@56: import vampyhost Chris@56: import numpy Chris@56: Chris@56: def listPlugins(): Chris@56: return vampyhost.listPlugins() Chris@56: Chris@56: def framesFromArray(arr, stepSize, frameSize): Chris@56: """Generate a list of frames of size frameSize, extracted from the input array arr at stepSize intervals""" Chris@56: # presumably such a function exists in many places, but I need practice Chris@56: assert(stepSize > 0) Chris@56: if arr.ndim == 1: # turn 1d into 2d array with 1 channel Chris@56: arr = numpy.reshape(arr, (1, arr.shape[0])) Chris@56: assert(arr.ndim == 2) Chris@56: n = arr.shape[1] Chris@56: i = 0 Chris@56: while (i < n): Chris@56: frame = arr[:, i : i + frameSize] Chris@56: w = frame.shape[1] Chris@56: if (w < frameSize): Chris@56: pad = numpy.zeros((frame.shape[0], frameSize - w)) Chris@56: frame = numpy.concatenate((frame, pad), 1) Chris@56: yield frame Chris@56: i = i + stepSize Chris@56: Chris@61: def selectOutputs(result, outputs): Chris@61: return result ##!!! for now Chris@61: Chris@56: def process(data, samplerate, key, parameters, outputs): Chris@58: plug = vampyhost.loadPlugin(key, samplerate, Chris@58: vampyhost.AdaptInputDomain + Chris@58: vampyhost.AdaptChannelCount) Chris@56: stepSize = plug.getPreferredStepSize() Chris@56: blockSize = plug.getPreferredBlockSize() Chris@61: if blockSize == 0: Chris@61: blockSize = 1024 Chris@61: if stepSize == 0: Chris@61: stepSize = blockSize ##!!! or blockSize/2, but check this with input domain adapter Chris@61: channels = 1 Chris@61: if data.ndim > 1: Chris@61: channels = data.shape[0] Chris@61: plug.initialise(channels, stepSize, blockSize) Chris@56: ff = framesFromArray(data, stepSize, blockSize) Chris@61: fi = 0 Chris@61: for f in ff: Chris@61: result = plug.processBlock(f, vampyhost.frame2RealTime(fi, samplerate)) Chris@61: yield selectOutputs(result, outputs) Chris@61: fi = fi + stepSize Chris@61: