Chris@112: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@112: Chris@112: /* Chris@112: VampyHost Chris@112: Chris@112: Use Vamp audio analysis plugins in Python Chris@112: Chris@112: Gyorgy Fazekas and Chris Cannam Chris@112: Centre for Digital Music, Queen Mary, University of London Chris@117: Copyright 2008-2015 Queen Mary, University of London Chris@112: Chris@112: Permission is hereby granted, free of charge, to any person Chris@112: obtaining a copy of this software and associated documentation Chris@112: files (the "Software"), to deal in the Software without Chris@112: restriction, including without limitation the rights to use, copy, Chris@112: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@112: of the Software, and to permit persons to whom the Software is Chris@112: furnished to do so, subject to the following conditions: Chris@112: Chris@112: The above copyright notice and this permission notice shall be Chris@112: included in all copies or substantial portions of the Software. Chris@112: Chris@112: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@112: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@112: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@112: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@112: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@112: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@112: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@112: Chris@112: Except as contained in this notice, the names of the Centre for Chris@112: Digital Music; Queen Mary, University of London; and the authors Chris@112: shall not be used in advertising or otherwise to promote the sale, Chris@112: use or other dealings in this Software without prior written Chris@112: authorization. Chris@112: */ Chris@112: Chris@112: /* Chris@112: StringConversion: A couple of type safe conversion utilities between Chris@112: Python types and C++ strings. Chris@112: */ Chris@112: Chris@112: #ifndef VAMPYHOST_STRING_CONVERSION_H Chris@112: #define VAMPYHOST_STRING_CONVERSION_H Chris@112: Chris@112: #include Chris@112: #include Chris@112: Chris@112: class StringConversion Chris@112: { Chris@112: public: Chris@112: StringConversion() {} Chris@112: ~StringConversion() {} Chris@112: Chris@112: PyObject *string2py(const std::string &s) { Chris@112: #if PY_MAJOR_VERSION < 3 Chris@112: return PyString_FromString(s.c_str()); Chris@112: #else Chris@112: return PyUnicode_FromString(s.c_str()); Chris@112: #endif Chris@112: } Chris@112: Chris@112: std::string py2string(PyObject *obj) { Chris@112: #if PY_MAJOR_VERSION < 3 Chris@112: char *cstr = PyString_AsString(obj); Chris@112: if (!cstr) return std::string(); Chris@112: else return cstr; Chris@112: #else Chris@112: PyObject *uobj = PyUnicode_AsUTF8String(obj); Chris@112: if (!uobj) return std::string(); Chris@112: char *cstr = PyBytes_AsString(uobj); Chris@112: if (!cstr) return std::string(); Chris@112: else return cstr; Chris@112: #endif Chris@112: } Chris@112: }; Chris@112: Chris@112: #endif Chris@112: Chris@112: