-
Notifications
You must be signed in to change notification settings - Fork 398
/
pywebrtcvad.c
168 lines (148 loc) · 3.97 KB
/
pywebrtcvad.c
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
#include <Python.h>
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
#if PY_MAJOR_VERSION >= 3
#define PY3
#endif
static PyObject *VadError;
const char WebRtcVadDoc[] = "hello.";
static void vad_free(PyObject* vadptr)
{
VadInst* handle = PyCapsule_GetPointer(vadptr, "WebRtcVadPtr");
WebRtcVad_Free(handle);
}
static PyObject* vad_create(PyObject *self, PyObject *args)
{
VadInst *handle = WebRtcVad_Create();
PyObject *vadptr = PyCapsule_New(handle, "WebRtcVadPtr", vad_free);
return Py_BuildValue("O", vadptr);
}
static PyObject* vad_init(PyObject *self, PyObject *vadptr)
{
VadInst* handle = PyCapsule_GetPointer(vadptr, "WebRtcVadPtr");
if (WebRtcVad_Init(handle)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject* vad_set_mode(PyObject *self, PyObject *args)
{
PyObject *vadptr;
long mode;
if (!PyArg_ParseTuple(args, "Ol", &vadptr, &mode)) {
return NULL;
}
if (mode < 0) {
return NULL;
} else if (mode > 3) {
PyErr_Format(PyExc_ValueError,
"%ld is an invalid mode, must be 0-3",
mode);
return NULL;
}
if (WebRtcVad_set_mode(PyCapsule_GetPointer(vadptr, "WebRtcVadPtr"), mode)) {
PyErr_Format(VadError, "Unable to set mode to %ld", mode);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject* valid_rate_and_frame_length(PyObject *self, PyObject *args)
{
long rate, frame_length;
if (!PyArg_ParseTuple(args, "ll", &rate, &frame_length)) {
return NULL;
}
if (rate > INT_MAX) {
PyErr_Format(PyExc_ValueError,
"%ld is an invalid rate",
rate);
return NULL;
}
if (frame_length > INT_MAX) {
PyErr_Format(PyExc_ValueError,
"%ld is an invalid frame length",
frame_length);
return NULL;
}
if (WebRtcVad_ValidRateAndFrameLength(rate, frame_length)) {
Py_RETURN_FALSE;
} else {
Py_RETURN_TRUE;
}
}
static PyObject* vad_process(PyObject *self, PyObject *args)
{
PyObject *vadptr;
long fs;
Py_buffer audio_frame = {NULL, NULL};
long frame_length;
int result;
#ifdef PY3
if (!PyArg_ParseTuple(args, "Oly*l", &vadptr, &fs, &audio_frame, &frame_length)) {
#else
if (!PyArg_ParseTuple(args, "Ols*l", &vadptr, &fs, &audio_frame, &frame_length)) {
#endif
return NULL;
}
result = WebRtcVad_Process(PyCapsule_GetPointer(vadptr, "WebRtcVadPtr"),
fs,
audio_frame.buf,
frame_length);
PyBuffer_Release(&audio_frame);
switch (result) {
case 1:
Py_RETURN_TRUE;
break;
case 0:
Py_RETURN_FALSE;
break;
case -1:
PyErr_Format(VadError, "Error while processing frame");
}
return NULL;
}
static PyMethodDef WebRtcVadMethods[] = {
{"create", vad_create, METH_NOARGS,
"Create a vad."},
{"init", vad_init, METH_O,
"Init a vad."},
{"set_mode", vad_set_mode, METH_VARARGS,
"Set mode."},
{"process", vad_process, METH_VARARGS,
"Set mode."},
{"valid_rate_and_frame_length", valid_rate_and_frame_length, METH_VARARGS,
"Set mode."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#ifdef PY3
static struct PyModuleDef WebRtcVadModule = {
PyModuleDef_HEAD_INIT,
"_webrtcvad", /* name of module */
WebRtcVadDoc, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
WebRtcVadMethods
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit__webrtcvad(void)
#else
#define INITERROR return
void
init_webrtcvad(void)
#endif
{
#ifdef PY3
PyObject *module = PyModule_Create(&WebRtcVadModule);
#else
PyObject *module = Py_InitModule3("_webrtcvad", WebRtcVadMethods, WebRtcVadDoc);
#endif
if (module == NULL) {
INITERROR;
}
VadError = PyErr_NewException("webrtcvad.Error", NULL, NULL);
Py_INCREF(VadError);
PyModule_AddObject(module, "Error", VadError);
#ifdef PY3
return module;
#endif
}