Chris@112
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@112
|
2
|
Chris@112
|
3 /*
|
Chris@112
|
4 VampyHost
|
Chris@112
|
5
|
Chris@112
|
6 Use Vamp audio analysis plugins in Python
|
Chris@112
|
7
|
Chris@112
|
8 Gyorgy Fazekas and Chris Cannam
|
Chris@112
|
9 Centre for Digital Music, Queen Mary, University of London
|
Chris@117
|
10 Copyright 2008-2015 Queen Mary, University of London
|
Chris@112
|
11
|
Chris@112
|
12 Permission is hereby granted, free of charge, to any person
|
Chris@112
|
13 obtaining a copy of this software and associated documentation
|
Chris@112
|
14 files (the "Software"), to deal in the Software without
|
Chris@112
|
15 restriction, including without limitation the rights to use, copy,
|
Chris@112
|
16 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@112
|
17 of the Software, and to permit persons to whom the Software is
|
Chris@112
|
18 furnished to do so, subject to the following conditions:
|
Chris@112
|
19
|
Chris@112
|
20 The above copyright notice and this permission notice shall be
|
Chris@112
|
21 included in all copies or substantial portions of the Software.
|
Chris@112
|
22
|
Chris@112
|
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@112
|
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@112
|
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@112
|
26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@112
|
27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@112
|
28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@112
|
29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@112
|
30
|
Chris@112
|
31 Except as contained in this notice, the names of the Centre for
|
Chris@112
|
32 Digital Music; Queen Mary, University of London; and the authors
|
Chris@112
|
33 shall not be used in advertising or otherwise to promote the sale,
|
Chris@112
|
34 use or other dealings in this Software without prior written
|
Chris@112
|
35 authorization.
|
Chris@112
|
36 */
|
Chris@112
|
37
|
Chris@112
|
38 /*
|
Chris@112
|
39 StringConversion: A couple of type safe conversion utilities between
|
Chris@112
|
40 Python types and C++ strings.
|
Chris@112
|
41 */
|
Chris@112
|
42
|
Chris@112
|
43 #ifndef VAMPYHOST_STRING_CONVERSION_H
|
Chris@112
|
44 #define VAMPYHOST_STRING_CONVERSION_H
|
Chris@112
|
45
|
Chris@112
|
46 #include <Python.h>
|
Chris@112
|
47 #include <string>
|
Chris@112
|
48
|
Chris@112
|
49 class StringConversion
|
Chris@112
|
50 {
|
Chris@112
|
51 public:
|
Chris@112
|
52 StringConversion() {}
|
Chris@112
|
53 ~StringConversion() {}
|
Chris@112
|
54
|
Chris@112
|
55 PyObject *string2py(const std::string &s) {
|
Chris@112
|
56 #if PY_MAJOR_VERSION < 3
|
Chris@112
|
57 return PyString_FromString(s.c_str());
|
Chris@112
|
58 #else
|
Chris@112
|
59 return PyUnicode_FromString(s.c_str());
|
Chris@112
|
60 #endif
|
Chris@112
|
61 }
|
Chris@112
|
62
|
Chris@112
|
63 std::string py2string(PyObject *obj) {
|
Chris@112
|
64 #if PY_MAJOR_VERSION < 3
|
Chris@112
|
65 char *cstr = PyString_AsString(obj);
|
Chris@112
|
66 if (!cstr) return std::string();
|
Chris@112
|
67 else return cstr;
|
Chris@112
|
68 #else
|
Chris@112
|
69 PyObject *uobj = PyUnicode_AsUTF8String(obj);
|
Chris@112
|
70 if (!uobj) return std::string();
|
Chris@112
|
71 char *cstr = PyBytes_AsString(uobj);
|
Chris@112
|
72 if (!cstr) return std::string();
|
Chris@112
|
73 else return cstr;
|
Chris@112
|
74 #endif
|
Chris@112
|
75 }
|
Chris@112
|
76 };
|
Chris@112
|
77
|
Chris@112
|
78 #endif
|
Chris@112
|
79
|
Chris@112
|
80
|