GodotOpus is no longer a singleton; multiple performance improvements

This commit is contained in:
weil 2024-01-17 08:29:33 +01:00
parent 4824499682
commit cd69c2b1ca
3 changed files with 51 additions and 39 deletions

View File

@ -5,25 +5,15 @@
namespace godot { namespace godot {
Opus *Opus::singleton = nullptr;
constexpr auto sampleFrames = 480;
void Opus::_bind_methods() void Opus::_bind_methods()
{ {
ClassDB::bind_method(D_METHOD("encode"), &Opus::encode); ClassDB::bind_method(D_METHOD("encode"), &Opus::encode);
ClassDB::bind_method(D_METHOD("decode"), &Opus::decode); ClassDB::bind_method(D_METHOD("decode"), &Opus::decode);
} ClassDB::bind_method(D_METHOD("decode_and_play"), &Opus::decode_and_play);
Opus *Opus::get_singleton()
{
return singleton;
} }
Opus::Opus() Opus::Opus()
{ {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
int err{}; int err{};
m_encoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err); m_encoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err);
@ -37,31 +27,40 @@ Opus::Opus()
err = opus_encoder_ctl(m_encoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); err = opus_encoder_ctl(m_encoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
ERR_FAIL_COND(err < 0); ERR_FAIL_COND(err < 0);
err = opus_encoder_ctl(m_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
ERR_FAIL_COND(err < 0);
err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(24000)); err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(24000));
ERR_FAIL_COND(err < 0); ERR_FAIL_COND(err < 0);
m_encodeInputBuffer.resize(SampleFrames);
m_encodeOutputBuffer.resize(SampleFrames);
m_decodeOutputBuffer.resize(SampleFrames);
} }
Opus::~Opus() Opus::~Opus()
{ {
ERR_FAIL_COND(singleton != this);
opus_encoder_destroy(m_encoder); opus_encoder_destroy(m_encoder);
opus_decoder_destroy(m_decoder); opus_decoder_destroy(m_decoder);
singleton = nullptr;
} }
PackedFloat32Array Opus::encode(PackedVector2Array input) PackedFloat32Array Opus::encode(PackedVector2Array input)
{ {
if (input.size() < sampleFrames) { if (input.size() < SampleFrames) {
return {}; return {};
} }
std::vector<float> data(sampleFrames); for (size_t i = 0; i < SampleFrames; i++) {
for (size_t i = 0; i < sampleFrames; i++) { m_encodeInputBuffer[i] = input[i].x;
data[i] = input[i].x;
} }
std::vector<unsigned char> output(sampleFrames * 2); const auto r = opus_encode_float(
const auto r = opus_encode_float(m_encoder, data.data(), sampleFrames, output.data(), output.size()); m_encoder,
m_encodeInputBuffer.data(),
SampleFrames,
m_encodeOutputBuffer.data(),
m_encodeOutputBuffer.size()
);
if (r == -1) { if (r == -1) {
return {}; return {};
} }
@ -69,7 +68,7 @@ PackedFloat32Array Opus::encode(PackedVector2Array input)
auto outputArray = PackedFloat32Array{}; auto outputArray = PackedFloat32Array{};
outputArray.resize(r); outputArray.resize(r);
for (size_t i = 0; i < r; i++) { for (size_t i = 0; i < r; i++) {
outputArray[i] = output[i]; outputArray[i] = m_encodeOutputBuffer[i];
} }
return outputArray; return outputArray;
@ -77,24 +76,36 @@ PackedFloat32Array Opus::encode(PackedVector2Array input)
PackedVector2Array Opus::decode(PackedFloat32Array input) PackedVector2Array Opus::decode(PackedFloat32Array input)
{ {
std::vector<unsigned char> inputData(sampleFrames*2); std::vector<unsigned char> inputData(input.size());
for (size_t i = 0; i < input.size(); i++) { for (size_t i = 0; i < input.size(); i++) {
inputData[i] = input[i]; inputData[i] = input[i];
} }
std::vector<float> output(sampleFrames*2); const auto r = opus_decode_float(
const auto r = opus_decode_float(m_decoder, inputData.data(), input.size(), output.data(), sampleFrames, 0); m_decoder,
if (r != sampleFrames) { inputData.data(),
input.size(),
m_decodeOutputBuffer.data(),
SampleFrames,
0
);
if (r != SampleFrames) {
return {}; return {};
} }
auto packedOutput = PackedVector2Array{}; auto packedOutput = PackedVector2Array{};
packedOutput.resize(r); packedOutput.resize(r);
for (size_t i = 0; i < r; i++) { for (size_t i = 0; i < r; i++) {
packedOutput[i] = Vector2{output[i], output[i]}; packedOutput[i] = Vector2{m_decodeOutputBuffer[i], m_decodeOutputBuffer[i]};
} }
return packedOutput; return packedOutput;
} }
void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedFloat32Array input)
{
const auto decoded = decode(input);
buffer->push_buffer(decoded);
}
} }

View File

@ -1,32 +1,37 @@
#pragma once #pragma once
#include <godot_cpp/classes/object.hpp> #include <godot_cpp/classes/node.hpp>
#include <godot_cpp/core/class_db.hpp> #include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/typed_array.hpp> #include <godot_cpp/classes/audio_stream_generator.hpp>
#include <godot_cpp/classes/audio_stream_generator_playback.hpp>
#include "opus.h" #include "opus.h"
using namespace godot;
namespace godot { namespace godot {
class Opus : public Object constexpr size_t SampleFrames{480};
class Opus : public Node
{ {
GDCLASS(Opus, Object); GDCLASS(Opus, Node);
static Opus *singleton;
protected: protected:
static void _bind_methods(); static void _bind_methods();
public: public:
static Opus *get_singleton();
Opus(); Opus();
~Opus(); ~Opus();
PackedFloat32Array encode(PackedVector2Array input); PackedFloat32Array encode(PackedVector2Array input);
PackedVector2Array decode(PackedFloat32Array input); PackedVector2Array decode(PackedFloat32Array input);
void decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedFloat32Array input);
private: private:
std::vector<float> m_encodeInputBuffer;
std::vector<float> m_decodeOutputBuffer;
std::vector<unsigned char> m_encodeOutputBuffer;
OpusEncoder* m_encoder; OpusEncoder* m_encoder;
OpusDecoder* m_decoder; OpusDecoder* m_decoder;

View File

@ -22,9 +22,6 @@ void gdextension_initialize(ModuleInitializationLevel p_level)
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{ {
ClassDB::register_class<Opus>(); ClassDB::register_class<Opus>();
_godot_opus_singleton = memnew(Opus);
Engine::get_singleton()->register_singleton("Opus", Opus::get_singleton());
} }
} }
@ -32,8 +29,7 @@ void gdextension_terminate(ModuleInitializationLevel p_level)
{ {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{ {
Engine::get_singleton()->unregister_singleton("Opus");
memdelete(_godot_opus_singleton);
} }
} }