Skip to content

Commit 0f27fec

Browse files
author
Roberto De Ioris
committed
added VoiceCapture api
1 parent a899b95 commit 0f27fec

5 files changed

Lines changed: 157 additions & 1 deletion

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,8 @@ void unreal_engine_init_py_module() {
12921292

12931293
ue_python_init_iconsole_manager(new_unreal_engine_module);
12941294

1295+
ue_python_init_ivoice_capture(new_unreal_engine_module);
1296+
12951297
PyObject *py_sys = PyImport_ImportModule("sys");
12961298
PyObject *py_sys_dict = PyModule_GetDict(py_sys);
12971299

‎Source/UnrealEnginePython/Private/UnrealEnginePythonPrivatePCH.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "Slate/UEPySlate.h"
6262
#include "Http/UEPyIHttp.h"
6363
#include "ConsoleManager/UEPyIConsoleManager.h"
64+
#include "Voice/UEPyIVoiceCapture.h"
6465

6566

6667
#define ue_py_check(py_u) if (!py_u->ue_object || !py_u->ue_object->IsValidLowLevel() || py_u->ue_object->IsPendingKillOrUnreachable())\
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "UnrealEnginePython.h"
4+
5+
#include "Runtime/Online/Voice/Public/VoiceModule.h"
6+
#include "Runtime/Online/Voice/Public/Interfaces/VoiceCapture.h"
7+
8+
typedef struct {
9+
PyObject_HEAD
10+
/* Type-specific fields go here. */
11+
TSharedRef<IVoiceCapture> voice_capture;
12+
} ue_PyIVoiceCapture;
13+
14+
15+
void ue_python_init_ivoice_capture(PyObject *);

‎Source/UnrealEnginePython/UnrealEnginePython.Build.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public UnrealEnginePython(TargetInfo Target)
113113
"HTTP",
114114
"UMG",
115115
"AppFramework",
116-
"RHI"
116+
"RHI",
117+
"Voice"
117118
// ... add private dependencies that you statically link with here ...
118119
}
119120
);

0 commit comments

Comments
 (0)