-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyOkHound.cpp
executable file
·98 lines (69 loc) · 2.02 KB
/
pyOkHound.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
/*****************************************************************************
* Copyright 2017 SoundHound, Incorporated. All rights reserved.
*****************************************************************************/
#include <Python.h>
#include "PhraseSpotterAPI.h"
#if PY_MAJOR_VERSION >= 3
#define FORMAT_S "y#"
#else
#define FORMAT_S "s#"
#endif
static PyObject* okhound_ProcessSamples(PyObject* self, PyObject* args)
{
char* samples;
int size;
if (!PyArg_ParseTuple(args, FORMAT_S, &samples, &size))
{
PyErr_SetString(PyExc_TypeError, "processSamples() takes a byte string");
return NULL;
}
int result = PhraseSpotterProcessSamples(reinterpret_cast<int16_t*>(samples), size / 2);
PyObject* ret = Py_BuildValue("i", result);
return ret;
}
static PyObject* okhound_getThreshold(PyObject* self, PyObject* args)
{
float result = PhraseSpotterGetThreshold();
PyObject* ret = Py_BuildValue("f", result);
return ret;
}
static PyObject* okhound_setThreshold(PyObject* self, PyObject* args)
{
float threshold;
if (!PyArg_ParseTuple(args, "f", &threshold))
{
PyErr_SetString(PyExc_TypeError, "setThreshold() takes a float.");
return NULL;
}
PhraseSpotterSetThreshold(threshold);
return Py_None;
}
static PyMethodDef OkHoundMethods[] = {
{ "processSamples", okhound_ProcessSamples, METH_VARARGS, "Process 16 bit 16 kHz audio chunk, return true if the phrase is spotted." },
{ "getThreshold", okhound_getThreshold, METH_VARARGS, "Get sensitivity setting of phrase spotting." },
{ "setThreshold", okhound_setThreshold, METH_VARARGS, "Set sensitivity setting of phrase spotting." },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"okhound",
NULL,
-1,
OkHoundMethods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit_okhound(void)
{
PyObject *module = PyModule_Create(&moduledef);
return module;
}
#else
PyMODINIT_FUNC initokhound(void)
{
Py_InitModule("okhound", OkHoundMethods);
}
#endif