Skip to content

Commit 1c8fbc0

Browse files
author
Roberto De Ioris
committed
refactored audio system
1 parent 43cbddf commit 1c8fbc0

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

Source/UnrealEnginePython/Private/UEPyAudio.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
#include "UnrealEnginePythonPrivatePCH.h"
22
#include "Sound/SoundWaveProcedural.h"
33

4-
PyObject *py_queue_procedural_audio(ue_PyUObject *self, PyObject * args) {
4+
PyObject *py_ue_queue_audio(ue_PyUObject *self, PyObject * args) {
55
// Writes from a Python buffer object to a USoundWaveProcedural class
66
ue_py_check(self);
77

8-
if (!self->ue_object->IsA<USoundWaveProcedural>())
9-
return PyErr_Format(PyExc_Exception, "UObject is not a USoundWaveProcedural.");
10-
11-
USoundWaveProcedural *sound_wave_procedural = (USoundWaveProcedural *)self->ue_object;
128
Py_buffer sound_buffer;
139

14-
if(!PyArg_ParseTuple(args, "y*:write_audio_to_buffer", &sound_buffer)) {
10+
if (!PyArg_ParseTuple(args, "y*:queue_audio", &sound_buffer)) {
1511
return NULL;
1612
}
1713

14+
USoundWaveProcedural *sound_wave_procedural = ue_py_check_type<USoundWaveProcedural>(self);
15+
if (!sound_wave_procedural)
16+
return PyErr_Format(PyExc_Exception, "UObject is not a USoundWaveProcedural.");
17+
1818
// Convert the buffer
1919
uint8 *buffer = (uint8 *)sound_buffer.buf;
20-
if(buffer == nullptr)
20+
if (buffer == nullptr)
2121
return PyErr_Format(PyExc_Exception, "Invalid sound buffer.");
2222

2323
// Add the audio to the Sound Wave's audio buffer
2424
sound_wave_procedural->QueueAudio(buffer, sound_buffer.len);
25-
25+
2626
// Clean up
2727
PyBuffer_Release(&sound_buffer);
2828

2929
Py_INCREF(Py_None);
3030
return Py_None;
3131
}
3232

33+
PyObject *py_ue_sound_get_data(ue_PyUObject *self, PyObject * args) {
34+
ue_py_check(self);
35+
36+
USoundWave *sound = ue_py_check_type<USoundWave>(self);
37+
if (!sound)
38+
return PyErr_Format(PyExc_Exception, "UObject is not a USoundWave.");
39+
40+
FByteBulkData raw_data = sound->RawData;
41+
42+
char *data = (char *)raw_data.Lock(LOCK_READ_ONLY);
43+
int32 data_size = raw_data.GetBulkDataSize();
44+
PyObject *py_data = PyBytes_FromStringAndSize(data, data_size);
45+
raw_data.Unlock();
46+
return py_data;
47+
}
48+
3349
PyObject *py_ue_play_sound_at_location(ue_PyUObject *self, PyObject * args) {
3450

3551
ue_py_check(self);
@@ -44,13 +60,9 @@ PyObject *py_ue_play_sound_at_location(ue_PyUObject *self, PyObject * args) {
4460
return NULL;
4561
}
4662

47-
4863
USoundBase *sound_object = nullptr;
4964
if (ue_is_pyuobject(sound)) {
50-
ue_PyUObject *py_sound = (ue_PyUObject *)sound;
51-
if (py_sound->ue_object->IsA<USoundBase>()) {
52-
sound_object = (USoundBase *)py_sound->ue_object;
53-
}
65+
sound_object = ue_py_check_type<USoundBase>(sound);
5466
}
5567
else if (PyUnicodeOrString_Check(sound)) {
5668
sound_object = FindObject<USoundBase>(ANY_PACKAGE, UTF8_TO_TCHAR(PyUnicode_AsUTF8(sound)));

Source/UnrealEnginePython/Private/UEPyAudio.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
#include "UnrealEnginePython.h"
66

7-
PyObject *py_queue_procedural_audio(ue_PyUObject *self, PyObject * args);
8-
PyObject *py_ue_play_sound_at_location(ue_PyUObject *, PyObject *);
7+
PyObject *py_ue_queue_audio(ue_PyUObject *self, PyObject * args);
8+
PyObject *py_ue_play_sound_at_location(ue_PyUObject *, PyObject *);
9+
PyObject *py_ue_sound_get_data(ue_PyUObject *self, PyObject * args);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
528528
{ "get_actor_velocity", (PyCFunction)py_ue_get_actor_velocity, METH_VARARGS, "" },
529529

530530
{ "play_sound_at_location", (PyCFunction)py_ue_play_sound_at_location, METH_VARARGS, "" },
531-
{ "queue_audio", (PyCFunction)py_queue_procedural_audio, METH_VARARGS, "" },
531+
{ "queue_audio", (PyCFunction)py_ue_queue_audio, METH_VARARGS, "" },
532+
{ "sound_get_data", (PyCFunction)py_ue_sound_get_data, METH_VARARGS, "" },
532533

533534
{ "world_tick", (PyCFunction)py_ue_world_tick, METH_VARARGS, "" },
534535

0 commit comments

Comments
 (0)