Chris@117: #!/usr/bin/env python Chris@117: Chris@117: # Python Vamp Host Chris@117: # Copyright (c) 2008-2015 Queen Mary, University of London Chris@117: # Chris@117: # Permission is hereby granted, free of charge, to any person Chris@117: # obtaining a copy of this software and associated documentation Chris@117: # files (the "Software"), to deal in the Software without Chris@117: # restriction, including without limitation the rights to use, copy, Chris@117: # modify, merge, publish, distribute, sublicense, and/or sell copies Chris@117: # of the Software, and to permit persons to whom the Software is Chris@117: # furnished to do so, subject to the following conditions: Chris@117: # Chris@117: # The above copyright notice and this permission notice shall be Chris@117: # included in all copies or substantial portions of the Software. Chris@117: # Chris@117: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@117: # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@117: # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@117: # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY Chris@117: # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@117: # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@117: # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@117: # Chris@117: # Except as contained in this notice, the names of the Centre for Chris@117: # Digital Music and Queen Mary, University of London shall not be Chris@117: # used in advertising or otherwise to promote the sale, use or other Chris@117: # dealings in this Software without prior written authorization. Chris@117: 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 numpy Chris@56: Chris@95: def frames_from_array(arr, step_size, frame_size): Chris@95: """Generate a list of frames of size frame_size, extracted from the input array arr at step_size intervals""" Chris@56: # presumably such a function exists in many places, but I need practice Chris@84: assert(step_size > 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@95: frame = arr[:, i : i + frame_size] Chris@56: w = frame.shape[1] Chris@95: if (w < frame_size): Chris@95: pad = numpy.zeros((frame.shape[0], frame_size - w)) Chris@56: frame = numpy.concatenate((frame, pad), 1) Chris@56: yield frame Chris@84: i = i + step_size Chris@56: