-
Notifications
You must be signed in to change notification settings - Fork 554
/
Copy pathessentia.cpp
128 lines (105 loc) · 4.26 KB
/
essentia.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
*
* This file is part of Essentia
*
* Essentia is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation (FSF), either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the Affero GNU General Public License
* version 3 along with this program. If not, see http://www.gnu.org/licenses/
*/
#include <Python.h>
// defined only in python 2.4, make it available for 2.3 as well
#ifndef Py_RETURN_TRUE
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
#endif
#ifndef Py_RETURN_FALSE
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
#endif
// numpy import
#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
#include "numpy/arrayobject.h"
// essentia import
#include "essentia.h"
#include "algorithmfactory.h"
using namespace std;
using namespace essentia;
#include "typedefs.h"
#include "pyalgorithm.cpp"
#include "pystreamingalgorithm.cpp"
#include "pyvectorinput.cpp"
// global functions available in the _essentia module, such as version, keys, etc...
#include "globalfuncs.cpp"
static PyObject* Essentia__Module = NULL;
//extern "C" ESSENTIA_API void
PyMODINIT_FUNC
init_essentia() {
// import our wrapper types
if (PyType_Ready(&PyAlgorithmType) < 0 ||
PyType_Ready(&PyStreamingAlgorithmType) < 0 ||
PyType_Ready(&PyVectorInputType) < 0 ||
PyType_Ready(&StringType) < 0 ||
PyType_Ready(&BooleanType) < 0 ||
PyType_Ready(&IntegerType) < 0 ||
PyType_Ready(&PyRealType) < 0 ||
PyType_Ready(&VectorRealType) < 0 ||
PyType_Ready(&VectorComplexType) < 0 ||
PyType_Ready(&VectorStringType) < 0 ||
PyType_Ready(&VectorIntegerType) < 0 ||
PyType_Ready(&VectorVectorRealType) < 0 ||
PyType_Ready(&VectorVectorStringType) < 0 ||
PyType_Ready(&MatrixRealType) < 0 ||
PyType_Ready(&PyPoolType) < 0 ||
PyType_Ready(&PyStereoSampleType) < 0 ||
PyType_Ready(&VectorStereoSampleType) < 0 ||
PyType_Ready(&VectorMatrixRealType) < 0 ||
PyType_Ready(&TensorRealType) < 0 ||
PyType_Ready(&VectorTensorRealType) < 0 ||
PyType_Ready(&VectorVectorStereoSampleType) < 0) {
cerr << "Unable to instantiate Essentia's wrapper types." << endl;
return NULL;
}
// import the NumPy C api
int numpy_error = _import_array();
if (numpy_error) {
cerr << "Unable to import NumPy C API from Essentia module. Error code = " << numpy_error << endl;
return NULL;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_essentia", /* m_name */
"Module that allows access to essentia plugins and algorithms.", /* m_doc */
-1, /* m_size */
Essentia__Methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
Essentia__Module = PyModule_Create(&moduledef);
if (Essentia__Module == NULL) {
cerr << "Error loading _essentia python/C module" << endl;
return NULL;
}
// insert the Algorithm class
Py_INCREF(&PyAlgorithmType);
PyModule_AddObject(Essentia__Module, (char*)"Algorithm", (PyObject*)&PyAlgorithmType);
Py_INCREF(&PyStreamingAlgorithmType);
PyModule_AddObject(Essentia__Module, (char*)"StreamingAlgorithm", (PyObject*)&PyStreamingAlgorithmType);
Py_INCREF(&PyVectorInputType);
PyModule_AddObject(Essentia__Module, (char*)"VectorInput", (PyObject*)&PyVectorInputType);
Py_INCREF(&PyPoolType);
PyModule_AddObject(Essentia__Module, (char*)"Pool", (PyObject*)&PyPoolType);
// register algorithms in the factory
essentia::init();
E_DEBUG(EPyBindings, "Successfully initialized _essentia python/C module");
return Essentia__Module;
}