Skip to content

Commit e788415

Browse files
authored
Merge pull request #18 from TheOnlyJoey/master
Updated Python bindings and example to Python 3
2 parents f31dddf + b29e6c3 commit e788415

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

python-module/GistPythonModule.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ static PyObject* setSamplingFrequency (PyObject *dummy, PyObject *args)
4141
//=======================================================================
4242
static PyObject* getAudioFrameSize (PyObject *dummy, PyObject *args)
4343
{
44-
return PyInt_FromLong((long) gist.getAudioFrameSize());
44+
return PyLong_FromLong((long) gist.getAudioFrameSize());
4545
}
4646

4747
//=======================================================================
4848
static PyObject* getSamplingFrequency (PyObject *dummy, PyObject *args)
4949
{
50-
return PyInt_FromLong((long) gist.getSamplingFrequency());
50+
return PyLong_FromLong((long) gist.getSamplingFrequency());
5151
}
5252

5353
//=======================================================================
@@ -301,24 +301,33 @@ static PyMethodDef gist_methods[] = {
301301
{NULL, NULL, 0, NULL} /* Sentinel */
302302
};
303303

304-
304+
static struct PyModuleDef gist_definition = {
305+
PyModuleDef_HEAD_INIT,
306+
"gist",
307+
"Python bindings for the Gist C++ based audio analysis library.",
308+
-1,
309+
gist_methods
310+
};
305311

306312
//=======================================================================
307-
PyMODINIT_FUNC initgist (void)
313+
PyMODINIT_FUNC PyInit_gist (void)
308314
{
309-
(void)Py_InitModule("gist", gist_methods);
310315
import_array();
316+
return PyModule_Create(&gist_definition);
311317
}
312318

313319
//=======================================================================
314320
int main (int argc, char *argv[])
315321
{
322+
//Convert char* to const wchar_t
323+
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
324+
316325
/* Pass argv[0] to the Python interpreter */
317-
Py_SetProgramName(argv[0]);
326+
Py_SetProgramName(program);
318327

319328
/* Initialize the Python interpreter. Required. */
320329
Py_Initialize();
321330

322331
/* Add a static module */
323-
initgist();
332+
PyInit_gist();
324333
}

python-module/example.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,54 @@
3131

3232
# ====================== Core Time Domain Features ===================
3333

34-
print ""
35-
print "--- CORE TIME DOMAIN FEATURES ---"
36-
print ""
37-
print "RMS:", gist.rms()
38-
print "Peak Energy:", gist.peakEnergy()
39-
print "Zero Crossing Rate:", gist.zeroCrossingRate()
40-
print ""
34+
print("")
35+
print("--- CORE TIME DOMAIN FEATURES ---")
36+
print("")
37+
print("RMS:", gist.rms())
38+
print("Peak Energy:", gist.peakEnergy())
39+
print("Zero Crossing Rate:", gist.zeroCrossingRate())
40+
print("")
4141

4242
# ====================== Core Frequency Domain Features ===================
4343

44-
print "--- CORE FREQUENCY DOMAIN FEATURES ---"
45-
print ""
46-
print "Spectral Centroid: ", gist.spectralCentroid()
47-
print "Spectral Crest:", gist.spectralCrest()
48-
print "Spectral Flatness:", gist.spectralFlatness()
49-
print "Spectral Rolloff:", gist.spectralRolloff()
50-
print "Spectral Kurtosis:", gist.spectralKurtosis()
51-
print ""
44+
print("--- CORE FREQUENCY DOMAIN FEATURES ---")
45+
print("")
46+
print("Spectral Centroid: ", gist.spectralCentroid())
47+
print("Spectral Crest:", gist.spectralCrest())
48+
print("Spectral Flatness:", gist.spectralFlatness())
49+
print("Spectral Rolloff:", gist.spectralRolloff())
50+
print("Spectral Kurtosis:", gist.spectralKurtosis())
51+
print("")
5252

5353
# ========================= Onset Detection Functions =======================
5454

55-
print "--- ONSET DETECTION FUNCTIONS ---"
56-
print ""
57-
print "Energy Difference:", gist.energyDifference()
58-
print "Spectral Difference:", gist.spectralDifference()
59-
print "Spectral Difference (half-wave rectified):", gist.spectralDifferenceHWR()
60-
print "Complex Spectral Difference:", gist.complexSpectralDifference()
61-
print "High Frequency Content:", gist.highFrequencyContent()
62-
print ""
55+
print("--- ONSET DETECTION FUNCTIONS ---")
56+
print("")
57+
print("Energy Difference:", gist.energyDifference())
58+
print("Spectral Difference:", gist.spectralDifference())
59+
print("Spectral Difference (half-wave rectified):", gist.spectralDifferenceHWR())
60+
print("Complex Spectral Difference:", gist.complexSpectralDifference())
61+
print("High Frequency Content:", gist.highFrequencyContent())
62+
print("")
6363

6464
# ========================= Pitch =======================
6565

66-
print "--- PITCH ---"
67-
print ""
68-
print "Pitch:", gist.pitch()
69-
print ""
66+
print("--- PITCH ---")
67+
print("")
68+
print("Pitch:", gist.pitch())
69+
print("")
7070

7171
# ======================= Spectra ========================
7272

73-
print "--- SPECTRA ---"
74-
print ""
73+
print("--- SPECTRA ---")
74+
print("")
7575
magnitudeSpectrum = gist.magnitudeSpectrum()
76-
print "Magnitude Spectrum has", magnitudeSpectrum.size, "samples"
76+
print("Magnitude Spectrum has", magnitudeSpectrum.size, "samples")
7777

7878
melFrequencySpectrum = gist.melFrequencySpectrum()
79-
print "Mel-Frequency Spectrum has", melFrequencySpectrum.size, "samples"
79+
print("Mel-Frequency Spectrum has", melFrequencySpectrum.size, "samples")
8080

8181
mfccs = gist.mfccs()
82-
print "MFCCs has", mfccs.size, "samples"
82+
print("MFCCs has", mfccs.size, "samples")
8383

8484

python-module/setup.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
# build command : python setup.py build build_ext --inplace
33
from numpy.distutils.core import setup, Extension
44
import os, numpy
5+
import sys
6+
7+
if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
8+
print ("")
9+
print ("Python Version Error")
10+
print ("")
11+
print ("This script must be run using Python 3.6 or later.")
12+
print ("You are trying to run it using Python " + str (sys.version_info.major) + "." + str (sys.version_info.minor))
13+
print ("")
14+
exit()
515

616
name = 'gist'
717
sources = [

0 commit comments

Comments
 (0)