|
| 1 | + |
| 2 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 3 | + |
| 4 | +#include "UEPyIVoiceCapture.h" |
| 5 | + |
| 6 | + |
| 7 | +PyObject *py_ue_ivoice_capture_start(ue_PyIVoiceCapture *self, PyObject * args) { |
| 8 | + |
| 9 | + self->voice_capture->Start(); |
| 10 | + |
| 11 | + Py_RETURN_NONE; |
| 12 | +} |
| 13 | + |
| 14 | +PyObject *py_ue_ivoice_capture_stop(ue_PyIVoiceCapture *self, PyObject * args) { |
| 15 | + |
| 16 | + self->voice_capture->Stop(); |
| 17 | + |
| 18 | + Py_RETURN_NONE; |
| 19 | +} |
| 20 | + |
| 21 | +PyObject *py_ue_ivoice_capture_shutdown(ue_PyIVoiceCapture *self, PyObject * args) { |
| 22 | + |
| 23 | + self->voice_capture->Shutdown(); |
| 24 | + |
| 25 | + Py_RETURN_NONE; |
| 26 | +} |
| 27 | + |
| 28 | +PyObject *py_ue_ivoice_capture_init_method(ue_PyIVoiceCapture *self, PyObject * args) { |
| 29 | + |
| 30 | + int sample_rate; |
| 31 | + int channels; |
| 32 | + if (!PyArg_ParseTuple(args, "ii", &sample_rate, &channels)) { |
| 33 | + return nullptr; |
| 34 | + } |
| 35 | + |
| 36 | + if (self->voice_capture->Init(sample_rate, channels)) { |
| 37 | + Py_RETURN_TRUE; |
| 38 | + } |
| 39 | + |
| 40 | + Py_RETURN_FALSE; |
| 41 | +} |
| 42 | + |
| 43 | +PyObject *py_ue_ivoice_capture_is_capturing(ue_PyIVoiceCapture *self, PyObject * args) { |
| 44 | + |
| 45 | + if (self->voice_capture->IsCapturing()) { |
| 46 | + Py_RETURN_TRUE; |
| 47 | + } |
| 48 | + |
| 49 | + Py_RETURN_FALSE; |
| 50 | +} |
| 51 | + |
| 52 | +PyObject *py_ue_ivoice_capture_get_capture_state(ue_PyIVoiceCapture *self, PyObject * args) { |
| 53 | + uint32 available_data; |
| 54 | + EVoiceCaptureState::Type state = self->voice_capture->GetCaptureState(available_data); |
| 55 | + |
| 56 | + return Py_BuildValue("ii", (int)state, available_data); |
| 57 | +} |
| 58 | + |
| 59 | +PyObject *py_ue_ivoice_capture_get_voice_data(ue_PyIVoiceCapture *self, PyObject * args) { |
| 60 | + int len; |
| 61 | + if (!PyArg_ParseTuple(args, "i", &len)) { |
| 62 | + return nullptr; |
| 63 | + } |
| 64 | + uint32 available_data; |
| 65 | + uint8 *data = (uint8*)FMemory_Alloca(len); |
| 66 | + EVoiceCaptureState::Type state = self->voice_capture->GetVoiceData(data, len, available_data); |
| 67 | + |
| 68 | + return Py_BuildValue("iO", (int)state, PyByteArray_FromStringAndSize((char *)data, (Py_ssize_t)available_data)); |
| 69 | +} |
| 70 | + |
| 71 | +static PyMethodDef ue_PyIVoiceCapture_methods[] = { |
| 72 | + { "start", (PyCFunction)py_ue_ivoice_capture_start, METH_VARARGS, "" }, |
| 73 | + { "stop", (PyCFunction)py_ue_ivoice_capture_stop, METH_VARARGS, "" }, |
| 74 | + { "shutdown", (PyCFunction)py_ue_ivoice_capture_shutdown, METH_VARARGS, "" }, |
| 75 | + { "init", (PyCFunction)py_ue_ivoice_capture_init_method, METH_VARARGS, "" }, |
| 76 | + { "is_capturing", (PyCFunction)py_ue_ivoice_capture_is_capturing, METH_VARARGS, "" }, |
| 77 | + { "get_capture_state", (PyCFunction)py_ue_ivoice_capture_get_capture_state, METH_VARARGS, "" }, |
| 78 | + { "get_voice_data", (PyCFunction)py_ue_ivoice_capture_get_voice_data, METH_VARARGS, "" }, |
| 79 | + { NULL } /* Sentinel */ |
| 80 | +}; |
| 81 | + |
| 82 | + |
| 83 | +static PyTypeObject ue_PyIVoiceCaptureType = { |
| 84 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 85 | + "unreal_engine.IVoiceCapture", /* tp_name */ |
| 86 | + sizeof(ue_PyIVoiceCapture), /* tp_basicsize */ |
| 87 | + 0, /* tp_itemsize */ |
| 88 | + 0, /* tp_dealloc */ |
| 89 | + 0, /* tp_print */ |
| 90 | + 0, /* tp_getattr */ |
| 91 | + 0, /* tp_setattr */ |
| 92 | + 0, /* tp_reserved */ |
| 93 | + 0, /* tp_repr */ |
| 94 | + 0, /* tp_as_number */ |
| 95 | + 0, /* tp_as_sequence */ |
| 96 | + 0, /* tp_as_mapping */ |
| 97 | + 0, /* tp_hash */ |
| 98 | + 0, /* tp_call */ |
| 99 | + 0, /* tp_str */ |
| 100 | + 0, /* tp_getattro */ |
| 101 | + 0, /* tp_setattro */ |
| 102 | + 0, /* tp_as_buffer */ |
| 103 | + Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 104 | + "Unreal Engine VoiceCapture Interface", /* tp_doc */ |
| 105 | + 0, /* tp_traverse */ |
| 106 | + 0, /* tp_clear */ |
| 107 | + 0, /* tp_richcompare */ |
| 108 | + 0, /* tp_weaklistoffset */ |
| 109 | + 0, /* tp_iter */ |
| 110 | + 0, /* tp_iternext */ |
| 111 | + ue_PyIVoiceCapture_methods, /* tp_methods */ |
| 112 | + 0, |
| 113 | + 0, |
| 114 | +}; |
| 115 | + |
| 116 | +static int py_ue_ivoice_capture_init(ue_PyIVoiceCapture *self, PyObject * args) { |
| 117 | + |
| 118 | + TSharedPtr<IVoiceCapture> voice_capture_ptr = FVoiceModule::Get().CreateVoiceCapture(); |
| 119 | + if (!voice_capture_ptr.IsValid()) { |
| 120 | + PyErr_SetString(PyExc_Exception, "unable to create a new VoiceCapture"); |
| 121 | + return -1; |
| 122 | + } |
| 123 | + |
| 124 | + new(&self->voice_capture) TSharedRef<IVoiceCapture>(voice_capture_ptr.ToSharedRef()); |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +void ue_python_init_ivoice_capture(PyObject *ue_module) { |
| 129 | + ue_PyIVoiceCaptureType.tp_new = PyType_GenericNew; |
| 130 | + ue_PyIVoiceCaptureType.tp_init = (initproc)py_ue_ivoice_capture_init; |
| 131 | + |
| 132 | + if (PyType_Ready(&ue_PyIVoiceCaptureType) < 0) |
| 133 | + return; |
| 134 | + |
| 135 | + Py_INCREF(&ue_PyIVoiceCaptureType); |
| 136 | + PyModule_AddObject(ue_module, "IVoiceCapture", (PyObject *)&ue_PyIVoiceCaptureType); |
| 137 | +} |
0 commit comments