annotate VectorConversion.h @ 29:7e7f2f7d9542

PyTypeConversions -> VectorConversion
author Chris Cannam
date Wed, 26 Nov 2014 10:56:23 +0000
parents PyTypeConversions.h@1175e814954e
children fa3f80d4e340
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@27 10 Copyright 2008-2014 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@26 43 #ifndef PY_TYPE_CONVERSIONS_H
Chris@26 44 #define PY_TYPE_CONVERSIONS_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@27 90
Chris@27 91 PyObject *PyValue_From_StringVector(const std::vector<std::string> &) const;
Chris@28 92 PyObject *PyArray_From_FloatVector(const std::vector<float> &) const;
Chris@28 93
Chris@28 94 private:
Chris@28 95 std::string PyValue_Get_TypeName(PyObject*) const;
Chris@28 96 float PyValue_To_Float(PyObject*) const;
Chris@26 97
Chris@27 98 /// Convert DTYPE type 1D NumpyArray to std::vector<RET>
Chris@27 99 template<typename RET, typename DTYPE>
Chris@28 100 std::vector<RET> PyArray_Convert(void* raw_data_ptr,
Chris@28 101 int length,
Chris@28 102 size_t strides) const {
Chris@28 103
Chris@28 104 std::vector<RET> v(length);
Chris@27 105
Chris@27 106 /// check if the array is continuous, if not use strides info
Chris@28 107 if (sizeof(DTYPE) != strides) {
Chris@27 108 #ifdef _DEBUG_VALUES
Chris@27 109 cerr << "Warning: discontinuous numpy array. Strides: " << strides << " bytes. sizeof(dtype): " << sizeof(DTYPE) << endl;
Chris@27 110 #endif
Chris@27 111 char* data = (char*) raw_data_ptr;
Chris@28 112 for (int i = 0; i < length; ++i){
Chris@28 113 v[i] = (RET)(*((DTYPE*)data));
Chris@28 114 data += strides;
Chris@27 115 }
Chris@28 116 return v;
Chris@27 117 }
Chris@26 118
Chris@27 119 DTYPE* data = (DTYPE*) raw_data_ptr;
Chris@28 120 for (int i = 0; i < length; ++i){
Chris@28 121 v[i] = (RET)data[i];
Chris@27 122 }
Chris@26 123
Chris@28 124 return v;
Chris@27 125 }
Chris@26 126
Chris@26 127 private:
Chris@27 128 mutable bool m_error;
Chris@27 129 mutable std::queue<ValueError> m_errorQueue;
Chris@26 130
Chris@28 131 void setValueError(std::string) const;
Chris@27 132 ValueError& lastError() const;
Chris@28 133
Chris@26 134 public:
Chris@27 135 const bool& error;
Chris@26 136 };
Chris@26 137
Chris@26 138 #endif