-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathGistPythonModule.cpp
333 lines (254 loc) · 12.6 KB
/
GistPythonModule.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
#include <Python.h>
#include <assert.h>
#include <numpy/arrayobject.h>
#include "../src/Gist.h"
//=======================================================================
/** The Gist module */
Gist<double> gist (512, 44100);
//=======================================================================
static PyObject* setAudioFrameSize (PyObject *dummy, PyObject *args)
{
int audioFrameSize;
if (!PyArg_ParseTuple(args, "i", &audioFrameSize))
{
return NULL;
}
gist.setAudioFrameSize (audioFrameSize);
return Py_BuildValue("");
}
//=======================================================================
static PyObject* setSamplingFrequency (PyObject *dummy, PyObject *args)
{
int samplingFrequency;
if (!PyArg_ParseTuple(args, "i", &samplingFrequency))
{
return NULL;
}
gist.setSamplingFrequency (samplingFrequency);
return Py_BuildValue("");
}
//=======================================================================
static PyObject* getAudioFrameSize (PyObject *dummy, PyObject *args)
{
return PyLong_FromLong((long) gist.getAudioFrameSize());
}
//=======================================================================
static PyObject* getSamplingFrequency (PyObject *dummy, PyObject *args)
{
return PyLong_FromLong((long) gist.getSamplingFrequency());
}
//=======================================================================
static PyObject * processFrame (PyObject *dummy, PyObject *args)
{
PyObject *arg1 = NULL;
PyObject *arr1 = NULL;
if (!PyArg_ParseTuple (args, "O", &arg1))
{
return NULL;
}
arr1 = PyArray_FROM_OTF (arg1, NPY_DOUBLE, NPY_IN_ARRAY);
if (arr1 == NULL)
{
return NULL;
}
double* audioFrame = (double*) PyArray_DATA(arr1);
long audioFrameSize = (int) PyArray_Size ((PyObject*)arr1);
if (audioFrameSize != gist.getAudioFrameSize())
{
PyErr_SetString (PyExc_ValueError, "You are passing an audio frame with a different size to the frame size set in Gist. Use gist.getAudioFrameSize() to find out what is being used and change it with gist.setAudioFrameSize(frameSize)");
return NULL;
}
gist.processAudioFrame (audioFrame, (unsigned long) audioFrameSize);
Py_DECREF (arr1);
return Py_BuildValue("");
}
//=======================================================================//
//=================== CORE TIME DOMAIN FEATURES ========================//
//=======================================================================//
//=======================================================================
static PyObject * rootMeanSquare (PyObject *dummy, PyObject *args)
{
double rms = gist.rootMeanSquare();
return PyFloat_FromDouble (rms);
}
//=======================================================================
static PyObject * peakEnergy (PyObject *dummy, PyObject *args)
{
double p = gist.peakEnergy();
return PyFloat_FromDouble (p);
}
//=======================================================================
static PyObject * zeroCrossingRate (PyObject *dummy, PyObject *args)
{
double zcr = gist.zeroCrossingRate();
return PyFloat_FromDouble (zcr);
}
//=======================================================================//
//================= CORE FREQUENCY DOMAIN FEATURES ======================//
//=======================================================================//
//=======================================================================
static PyObject * spectralCentroid (PyObject *dummy, PyObject *args)
{
double sc = gist.spectralCentroid();
return PyFloat_FromDouble (sc);
}
//=======================================================================
static PyObject * spectralCrest (PyObject *dummy, PyObject *args)
{
double sc = gist.spectralCrest();
return PyFloat_FromDouble (sc);
}
//=======================================================================
static PyObject * spectralFlatness (PyObject *dummy, PyObject *args)
{
double sf = gist.spectralFlatness();
return PyFloat_FromDouble (sf);
}
//=======================================================================
static PyObject * spectralRolloff (PyObject *dummy, PyObject *args)
{
double sr = gist.spectralRolloff();
return PyFloat_FromDouble (sr);
}
//=======================================================================
static PyObject * spectralKurtosis (PyObject *dummy, PyObject *args)
{
double sk = gist.spectralKurtosis();
return PyFloat_FromDouble (sk);
}
//=======================================================================//
//==================== ONSET DETECTION FUNCTIONS ========================//
//=======================================================================//
//=======================================================================
static PyObject * energyDifference (PyObject *dummy, PyObject *args)
{
double ed = gist.energyDifference();
return PyFloat_FromDouble (ed);
}
//=======================================================================
static PyObject * spectralDifference (PyObject *dummy, PyObject *args)
{
double sd = gist.spectralDifference();
return PyFloat_FromDouble (sd);
}
//=======================================================================
static PyObject * spectralDifferenceHWR (PyObject *dummy, PyObject *args)
{
double sdhwr = gist.spectralDifferenceHWR();
return PyFloat_FromDouble (sdhwr);
}
//=======================================================================
static PyObject * complexSpectralDifference (PyObject *dummy, PyObject *args)
{
double csd = gist.complexSpectralDifference();
return PyFloat_FromDouble (csd);
}
//=======================================================================
static PyObject * highFrequencyContent (PyObject *dummy, PyObject *args)
{
double hfc = gist.highFrequencyContent();
return PyFloat_FromDouble (hfc);
}
//=======================================================================//
//============================== PITCH ==================================//
//=======================================================================//
//=======================================================================
static PyObject * pitch (PyObject *dummy, PyObject *args)
{
double p = gist.pitch();
return PyFloat_FromDouble (p);
}
//=======================================================================//
//============================= SPECTRA =================================//
//=======================================================================//
//=======================================================================
static PyObject * magnitudeSpectrum (PyObject *dummy, PyObject *args)
{
std::vector<double> magnitudeSpectrum = gist.getMagnitudeSpectrum();
int numDimensions = 1;
npy_intp numElements = magnitudeSpectrum.size();
PyObject* c = PyArray_SimpleNew (numDimensions, &numElements, NPY_DOUBLE);
void* arrayData = PyArray_DATA ( (PyArrayObject*)c);
memcpy (arrayData, &magnitudeSpectrum[0], PyArray_ITEMSIZE((PyArrayObject*) c) * numElements);
return c;
}
//=======================================================================
static PyObject * melFrequencySpectrum (PyObject *dummy, PyObject *args)
{
const std::vector<double>& melFrequencySpectrum = gist.getMelFrequencySpectrum();
int numDimensions = 1;
npy_intp numElements = melFrequencySpectrum.size();
PyObject* c = PyArray_SimpleNew (numDimensions, &numElements, NPY_DOUBLE);
void* arrayData = PyArray_DATA ( (PyArrayObject*)c);
memcpy (arrayData, &melFrequencySpectrum[0], PyArray_ITEMSIZE((PyArrayObject*) c) * numElements);
return c;
}
//=======================================================================
static PyObject * mfccs (PyObject *dummy, PyObject *args)
{
const std::vector<double>& mfccs = gist.getMelFrequencyCepstralCoefficients();
int numDimensions = 1;
npy_intp numElements = mfccs.size();
PyObject* c = PyArray_SimpleNew (numDimensions, &numElements, NPY_DOUBLE);
void* arrayData = PyArray_DATA ( (PyArrayObject*)c);
memcpy (arrayData, &mfccs[0], PyArray_ITEMSIZE((PyArrayObject*) c) * numElements);
return c;
}
//=======================================================================
static PyMethodDef gist_methods[] = {
/** Configuration methods */
{"setAudioFrameSize", setAudioFrameSize, METH_VARARGS, "Set the audio frame size to be used"},
{"getAudioFrameSize", getAudioFrameSize, METH_VARARGS, "Get the audio frame size currently being used"},
{"setSamplingFrequency", setSamplingFrequency, METH_VARARGS, "Set the audio sampling frequency to be used"},
{"getSamplingFrequency", getSamplingFrequency, METH_VARARGS, "Get the audio sampling frequency currently being used"},
{"processFrame", processFrame, METH_VARARGS, "Process a single audio frame"},
/** Core Time Domain Features */
{"rms", rootMeanSquare, METH_VARARGS, "Return the RMS of the most recent audio frame"},
{"peakEnergy", peakEnergy, METH_VARARGS, "Return the peak energy of the most recent audio frame"},
{"zeroCrossingRate", zeroCrossingRate, METH_VARARGS, "Return the zero crossing rate of the most recent audio frame"},
/** Core Frequency Domain Features */
{"spectralCentroid", spectralCentroid, METH_VARARGS, "Return the spectral centroid of the most recent audio frame"},
{"spectralCrest", spectralCrest, METH_VARARGS, "Return the spectral crest of the most recent audio frame"},
{"spectralFlatness", spectralFlatness, METH_VARARGS, "Return the spectral flatness of the most recent audio frame"},
{"spectralRolloff", spectralRolloff, METH_VARARGS, "Return the spectral rolloff of the most recent audio frame"},
{"spectralKurtosis", spectralKurtosis, METH_VARARGS, "Return the spectral kurtosis of the most recent audio frame"},
/** Onset Detection Functions */
{"energyDifference", energyDifference, METH_VARARGS, "Return the energy difference onset detection function of the most recent audio frame"},
{"spectralDifference", spectralDifference, METH_VARARGS, "Return the spectral difference onset detection function of the most recent audio frame"},
{"spectralDifferenceHWR", spectralDifferenceHWR, METH_VARARGS, "Return the spectral difference (half-wave rectified) onset detection function of the most recent audio frame"},
{"complexSpectralDifference", complexSpectralDifference, METH_VARARGS, "Return the complex spectral difference onset detection function of the most recent audio frame"},
{"highFrequencyContent", highFrequencyContent, METH_VARARGS, "Return the high frequency content onset detection function of the most recent audio frame"},
/** Pitch */
{"pitch", pitch, METH_VARARGS, "Return the monophonic pitch estimate the most recent audio frame"},
/** Spectra */
{"magnitudeSpectrum", magnitudeSpectrum, METH_VARARGS, "Return the magnitude spectrum for the most recent audio frame"},
{"melFrequencySpectrum", melFrequencySpectrum, METH_VARARGS, "Return the mel-frequency spectrum for the most recent audio frame"},
{"mfccs", mfccs, METH_VARARGS, "Return the mel-frequency cepstral coefficients for the most recent audio frame"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef gist_definition = {
PyModuleDef_HEAD_INIT,
"gist",
"Python bindings for the Gist C++ based audio analysis library.",
-1,
gist_methods
};
//=======================================================================
PyMODINIT_FUNC PyInit_gist (void)
{
import_array();
return PyModule_Create(&gist_definition);
}
//=======================================================================
int main (int argc, char *argv[])
{
//Convert char* to const wchar_t
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(program);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
PyInit_gist();
}