| #include <Python.h> |
| |
| #include "g722_enc_dec.h" |
| #include "g722_typedefs.h" |
| |
| |
| static PyObject* g722_dec (PyObject* self, PyObject* args) { |
| g722_decode_state_t s; |
| int opts = 0; |
| uint16_t gain = 0xFFFF; |
| int ratio = 4; |
| |
| PyObject* obj0 = NULL; |
| PyObject* obj1 = NULL; |
| PyObject* resultobj = NULL; |
| |
| if (!PyArg_ParseTuple(args,(char *)"OO:decode", &obj0, &obj1)) { |
| return 0; |
| } |
| |
| int rate = PyInt_AsLong(obj0); |
| char* in = PyString_AsString(obj1); |
| int len = PyString_Size(obj1); |
| |
| g722_decode_init(&s, rate, opts); |
| |
| void* out = malloc(len*ratio); |
| |
| g722_decode(&s, out, (uint8_t*)in, len, gain); |
| |
| resultobj = PyString_FromStringAndSize(out, len*ratio); |
| free(out); |
| |
| return resultobj; |
| } |
| |
| |
| |
| static PyObject* g722_enc (PyObject* self, PyObject* args) { |
| g722_encode_state_t s; |
| int opts = 0; |
| int ratio = 4; |
| |
| PyObject* obj0 = NULL; |
| PyObject* obj1 = NULL; |
| PyObject* resultobj = NULL; |
| if (!PyArg_ParseTuple(args, (char *)"OO:encode", &obj0, &obj1)) { |
| return 0; |
| } |
| |
| memset(&s, 0, sizeof(s)); |
| int rate = PyInt_AsLong(obj0); |
| char* in = PyString_AsString(obj1); |
| int len = PyString_Size(obj1); |
| |
| g722_encode_init(&s, rate, opts); |
| |
| void* out = calloc(len/ratio + 1, 1); |
| |
| int res = g722_encode(&s, out, (int16_t*)in, (len/4)*2); |
| |
| resultobj = PyString_FromStringAndSize(out, res); |
| free(out); |
| |
| return resultobj; |
| } |
| |
| |
| static PyMethodDef g722Methods[] = { |
| { (char *)"decode", g722_dec, METH_VARARGS, "decode g722 to PCM"}, |
| { (char *)"encode", g722_enc, METH_VARARGS, "encode PCM data to g722"}, |
| { NULL, NULL, 0, NULL } |
| }; |
| |
| |
| PyMODINIT_FUNC |
| initg722(void) |
| { |
| (void) Py_InitModule("g722", g722Methods); |
| } |