Skip to content

Commit 36a50ff

Browse files
committed
Created a 'write_audio_to_buffer' function.
This function accepts a USoundWaveProcedural and a Python buffer object. The 'QueueAudio' function on the USoundWaveProcedural is called, passing along the binary data within the buffer. This audio can then be played via normal Unreal methods (adding to an AudioComponent, playing sound at a location, etc.).
1 parent 7500ba7 commit 36a50ff

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Source/UnrealEnginePython/Private/UEPyAudio.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
#include "UnrealEnginePythonPrivatePCH.h"
2+
#include "Sound/SoundWaveProcedural.h"
23

4+
PyObject *py_write_audio_to_buffer(ue_PyUObject *self, PyObject * args) {
5+
// Writes from a Python buffer object to a USoundWaveProcedural class
6+
ue_py_check(self);
7+
8+
PyObject *sound_procedural;
9+
Py_buffer sound_buffer;
10+
11+
if (!PyArg_ParseTuple(args, "Oy*:write_audio_to_buffer", &sound_procedural, &sound_buffer)) {
12+
return NULL;
13+
}
14+
15+
// Convert to USoundWaveProcedural
16+
USoundWaveProcedural *sound_wave_procedural = ue_py_check_type<USoundWaveProcedural>(sound_procedural);
17+
if (!sound_wave_procedural)
18+
return PyErr_Format(PyExc_Exception, "Invalid procedural sound wave object.");
19+
20+
// Convert the buffer
21+
uint8 *buffer = (uint8 *)sound_buffer.buf;
22+
if (buffer == nullptr)
23+
return PyErr_Format(PyExc_Exception, "Invalid sound buffer.");
24+
25+
// Add the audio to the Sound Wave's audio buffer
26+
sound_wave_procedural->QueueAudio(buffer, sound_buffer.len);
27+
28+
// Clean up
29+
PyBuffer_Release(&sound_buffer);
30+
31+
Py_INCREF(Py_None);
32+
return Py_None;
33+
}
334

435
PyObject *py_ue_play_sound_at_location(ue_PyUObject *self, PyObject * args) {
536

Source/UnrealEnginePython/Private/UEPyAudio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
#include "UnrealEnginePython.h"
66

7+
PyObject *py_write_audio_to_buffer(ue_PyUObject *self, PyObject * args);
78
PyObject *py_ue_play_sound_at_location(ue_PyUObject *, PyObject *);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
524524
{ "get_actor_velocity", (PyCFunction)py_ue_get_actor_velocity, METH_VARARGS, "" },
525525

526526
{ "play_sound_at_location", (PyCFunction)py_ue_play_sound_at_location, METH_VARARGS, "" },
527+
{ "write_audio_to_buffer", (PyCFunction)py_write_audio_to_buffer, METH_VARARGS, "" },
527528

528529
{ "world_tick", (PyCFunction)py_ue_world_tick, METH_VARARGS, "" },
529530

0 commit comments

Comments
 (0)