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@27
|
39 PyTypeConversions: Type safe conversion utilities between Python
|
Chris@27
|
40 types and basic C/C++ types.
|
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 #ifdef HAVE_NUMPY
|
Chris@26
|
61 enum eArrayDataType {
|
Chris@27
|
62 dtype_float32 = (int) NPY_FLOAT,
|
Chris@27
|
63 dtype_complex64 = (int) NPY_CFLOAT
|
Chris@27
|
64 };
|
Chris@26
|
65 #endif
|
Chris@26
|
66
|
Chris@26
|
67 /* C++ mapping of PyNone Type */
|
Chris@26
|
68 struct NoneType {};
|
Chris@26
|
69
|
Chris@26
|
70 // Data
|
Chris@26
|
71 class ValueError
|
Chris@26
|
72 {
|
Chris@26
|
73 public:
|
Chris@27
|
74 ValueError() {}
|
Chris@27
|
75 ValueError(std::string m, bool s) : message(m),strict(s) {}
|
Chris@27
|
76 std::string location;
|
Chris@27
|
77 std::string message;
|
Chris@27
|
78 bool strict;
|
Chris@27
|
79 std::string str() const {
|
Chris@27
|
80 return (location.empty()) ? message : message + "\nLocation: " + location;}
|
Chris@27
|
81 template<typename V> ValueError &operator<< (const V& v)
|
Chris@27
|
82 {
|
Chris@27
|
83 std::ostringstream ss;
|
Chris@27
|
84 ss << v;
|
Chris@27
|
85 location += ss.str();
|
Chris@27
|
86 return *this;
|
Chris@27
|
87 }
|
Chris@26
|
88 };
|
Chris@26
|
89
|
Chris@26
|
90 class PyTypeConversions
|
Chris@26
|
91 {
|
Chris@26
|
92 public:
|
Chris@27
|
93 PyTypeConversions();
|
Chris@27
|
94 ~PyTypeConversions();
|
Chris@27
|
95
|
Chris@27
|
96 ValueError getError() const;
|
Chris@27
|
97 std::string PyValue_Get_TypeName(PyObject*) const;
|
Chris@27
|
98 float PyValue_To_Float(PyObject*) const;
|
Chris@27
|
99
|
Chris@27
|
100 // Sequence types
|
Chris@27
|
101 std::vector<std::string> PyValue_To_StringVector (PyObject*) const;
|
Chris@27
|
102 std::vector<float> PyValue_To_FloatVector (PyObject*) const;
|
Chris@27
|
103 std::vector<float> PyList_To_FloatVector (PyObject*) const;
|
Chris@27
|
104
|
Chris@27
|
105 PyObject *PyValue_From_StringVector(const std::vector<std::string> &) const;
|
Chris@26
|
106
|
Chris@27
|
107 // Numpy types
|
Chris@27
|
108 std::vector<float> PyArray_To_FloatVector (PyObject *pyValue) const;
|
Chris@27
|
109 PyObject *FloatVector_To_PyArray(const std::vector<float> &) const; // Copying the data
|
Chris@26
|
110
|
Chris@27
|
111 /// Convert DTYPE type 1D NumpyArray to std::vector<RET>
|
Chris@27
|
112 template<typename RET, typename DTYPE>
|
Chris@27
|
113 std::vector<RET> PyArray_Convert(void* raw_data_ptr, long length, size_t strides) const
|
Chris@27
|
114 {
|
Chris@27
|
115 std::vector<RET> rValue;
|
Chris@27
|
116
|
Chris@27
|
117 /// check if the array is continuous, if not use strides info
|
Chris@27
|
118 if (sizeof(DTYPE)!=strides) {
|
Chris@27
|
119 #ifdef _DEBUG_VALUES
|
Chris@27
|
120 cerr << "Warning: discontinuous numpy array. Strides: " << strides << " bytes. sizeof(dtype): " << sizeof(DTYPE) << endl;
|
Chris@27
|
121 #endif
|
Chris@27
|
122 char* data = (char*) raw_data_ptr;
|
Chris@27
|
123 for (long i = 0; i<length; ++i){
|
Chris@27
|
124 rValue.push_back((RET)(*((DTYPE*)data)));
|
Chris@27
|
125 #ifdef _DEBUG_VALUES
|
Chris@27
|
126 cerr << "value: " << (RET)(*((DTYPE*)data)) << endl;
|
Chris@27
|
127 #endif
|
Chris@27
|
128 data+=strides;
|
Chris@27
|
129 }
|
Chris@27
|
130 return rValue;
|
Chris@27
|
131 }
|
Chris@26
|
132
|
Chris@27
|
133 DTYPE* data = (DTYPE*) raw_data_ptr;
|
Chris@27
|
134 for (long i = 0; i<length; ++i){
|
Chris@27
|
135 #ifdef _DEBUG_VALUES
|
Chris@27
|
136 cerr << "value: " << (RET)data[i] << endl;
|
Chris@26
|
137 #endif
|
Chris@27
|
138 rValue.push_back((RET)data[i]);
|
Chris@27
|
139 }
|
Chris@27
|
140 return rValue;
|
Chris@27
|
141 }
|
Chris@26
|
142
|
Chris@27
|
143 /// this is a special case. numpy.float64 has an array conversions but no array descriptor
|
Chris@27
|
144 std::vector<float> PyArray0D_Convert(PyArrayInterface *ai) const
|
Chris@27
|
145 {
|
Chris@27
|
146 std::vector<float> rValue;
|
Chris@27
|
147 if ((ai->typekind) == *"f")
|
Chris@27
|
148 rValue.push_back((float)*(double*)(ai->data));
|
Chris@27
|
149 else {
|
Chris@27
|
150 setValueError("Unsupported NumPy data type.",m_strict);
|
Chris@27
|
151 return rValue;
|
Chris@27
|
152 }
|
Chris@26
|
153 #ifdef _DEBUG_VALUES
|
Chris@27
|
154 cerr << "value: " << rValue[0] << endl;
|
Chris@26
|
155 #endif
|
Chris@27
|
156 return rValue;
|
Chris@27
|
157 }
|
Chris@26
|
158
|
Chris@26
|
159 private:
|
Chris@27
|
160 bool m_strict;
|
Chris@27
|
161 mutable bool m_error;
|
Chris@27
|
162 mutable std::queue<ValueError> m_errorQueue;
|
Chris@26
|
163
|
Chris@27
|
164 void setValueError(std::string,bool) const;
|
Chris@27
|
165 ValueError& lastError() const;
|
Chris@26
|
166 public:
|
Chris@27
|
167 const bool& error;
|
Chris@26
|
168
|
Chris@26
|
169 };
|
Chris@26
|
170
|
Chris@26
|
171 #endif
|