annotate native/VectorConversion.h @ 151:5a6b8f4be9b9 tracks tip

Docs
author Chris Cannam
date Fri, 21 Apr 2017 14:33:57 +0100
parents 2370b942cd32
children
rev   line source
Chris@26 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@26 2
Chris@26 3 /*
Chris@27 4 VampyHost
Chris@26 5
Chris@27 6 Use Vamp audio analysis plugins in Python
Chris@26 7
Chris@27 8 Gyorgy Fazekas and Chris Cannam
Chris@27 9 Centre for Digital Music, Queen Mary, University of London
Chris@117 10 Copyright 2008-2015 Queen Mary, University of London
Chris@26 11
Chris@27 12 Permission is hereby granted, free of charge, to any person
Chris@27 13 obtaining a copy of this software and associated documentation
Chris@27 14 files (the "Software"), to deal in the Software without
Chris@27 15 restriction, including without limitation the rights to use, copy,
Chris@27 16 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@27 17 of the Software, and to permit persons to whom the Software is
Chris@27 18 furnished to do so, subject to the following conditions:
Chris@26 19
Chris@27 20 The above copyright notice and this permission notice shall be
Chris@27 21 included in all copies or substantial portions of the Software.
Chris@26 22
Chris@27 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@27 24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@27 25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@27 26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@27 27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@27 28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@27 29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@26 30
Chris@27 31 Except as contained in this notice, the names of the Centre for
Chris@27 32 Digital Music; Queen Mary, University of London; and the authors
Chris@27 33 shall not be used in advertising or otherwise to promote the sale,
Chris@27 34 use or other dealings in this Software without prior written
Chris@27 35 authorization.
Chris@26 36 */
Chris@26 37
Chris@26 38 /*
Chris@29 39 VectorConversion: A handful of type safe conversion utilities
Chris@29 40 between Python types and C++ vectors.
Chris@26 41 */
Chris@26 42
Chris@48 43 #ifndef VAMPYHOST_VECTOR_CONVERSION_H
Chris@48 44 #define VAMPYHOST_VECTOR_CONVERSION_H
Chris@26 45
Chris@26 46 #include <Python.h>
Chris@26 47
Chris@27 48 // NumPy is required here
Chris@27 49 #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API
Chris@26 50 #define NO_IMPORT_ARRAY
Chris@26 51 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
Chris@26 52 #include "numpy/arrayobject.h"
Chris@26 53
Chris@26 54 #include <vector>
Chris@26 55 #include <queue>
Chris@26 56 #include <string>
Chris@26 57 #include <sstream>
Chris@26 58 #include <iostream>
Chris@26 59
Chris@26 60 // Data
Chris@26 61 class ValueError
Chris@26 62 {
Chris@26 63 public:
Chris@27 64 ValueError() {}
Chris@28 65 ValueError(std::string m) : message(m) {}
Chris@27 66 std::string location;
Chris@27 67 std::string message;
Chris@27 68 std::string str() const {
Chris@27 69 return (location.empty()) ? message : message + "\nLocation: " + location;}
Chris@27 70 template<typename V> ValueError &operator<< (const V& v)
Chris@27 71 {
Chris@27 72 std::ostringstream ss;
Chris@27 73 ss << v;
Chris@27 74 location += ss.str();
Chris@27 75 return *this;
Chris@27 76 }
Chris@26 77 };
Chris@26 78
Chris@29 79 class VectorConversion
Chris@26 80 {
Chris@26 81 public:
Chris@29 82 VectorConversion();
Chris@29 83 ~VectorConversion();
Chris@27 84
Chris@28 85 ValueError getError() const;
Chris@27 86
Chris@27 87 std::vector<float> PyValue_To_FloatVector (PyObject*) const;
Chris@28 88 std::vector<float> PyArray_To_FloatVector (PyObject *) const;
Chris@27 89 std::vector<float> PyList_To_FloatVector (PyObject*) const;
Chris@40 90 std::vector<std::vector<float> > Py2DArray_To_FloatVector (PyObject *) const;
Chris@40 91
Chris@27 92 PyObject *PyValue_From_StringVector(const std::vector<std::string> &) const;
Chris@28 93 PyObject *PyArray_From_FloatVector(const std::vector<float> &) const;
Chris@28 94
Chris@28 95 private:
Chris@28 96 std::string PyValue_Get_TypeName(PyObject*) const;
Chris@28 97 float PyValue_To_Float(PyObject*) const;
Chris@26 98
Chris@27 99 /// Convert DTYPE type 1D NumpyArray to std::vector<RET>
Chris@27 100 template<typename RET, typename DTYPE>
Chris@28 101 std::vector<RET> PyArray_Convert(void* raw_data_ptr,
Chris@28 102 int length,
Chris@28 103 size_t strides) const {
Chris@40 104
Chris@28 105 std::vector<RET> v(length);
Chris@27 106
Chris@27 107 /// check if the array is continuous, if not use strides info
Chris@28 108 if (sizeof(DTYPE) != strides) {
Chris@27 109 #ifdef _DEBUG_VALUES
Chris@27 110 cerr << "Warning: discontinuous numpy array. Strides: " << strides << " bytes. sizeof(dtype): " << sizeof(DTYPE) << endl;
Chris@27 111 #endif
Chris@27 112 char* data = (char*) raw_data_ptr;
Chris@28 113 for (int i = 0; i < length; ++i){
Chris@28 114 v[i] = (RET)(*((DTYPE*)data));
Chris@28 115 data += strides;
Chris@27 116 }
Chris@28 117 return v;
Chris@27 118 }
Chris@26 119
Chris@27 120 DTYPE* data = (DTYPE*) raw_data_ptr;
Chris@28 121 for (int i = 0; i < length; ++i){
Chris@28 122 v[i] = (RET)data[i];
Chris@27 123 }
Chris@26 124
Chris@28 125 return v;
Chris@27 126 }
Chris@26 127
Chris@26 128 private:
Chris@27 129 mutable bool m_error;
Chris@27 130 mutable std::queue<ValueError> m_errorQueue;
Chris@26 131
Chris@28 132 void setValueError(std::string) const;
Chris@27 133 ValueError& lastError() const;
Chris@28 134
Chris@26 135 public:
Chris@27 136 const bool& error;
Chris@26 137 };
Chris@26 138
Chris@26 139 #endif