From 9f2cbf4b752ee79d7c91195443341b70461d48c5 Mon Sep 17 00:00:00 2001 From: weil Date: Wed, 24 Jan 2024 01:37:09 +0100 Subject: [PATCH] Reworked sampling handling; added speex resampler --- src/GodotOpus.cpp | 132 +++++++++++++++++++++++++++++----------------- src/GodotOpus.h | 19 ++++--- 2 files changed, 96 insertions(+), 55 deletions(-) diff --git a/src/GodotOpus.cpp b/src/GodotOpus.cpp index 88379d5..28424e6 100644 --- a/src/GodotOpus.cpp +++ b/src/GodotOpus.cpp @@ -7,6 +7,7 @@ namespace godot { void Opus::_bind_methods() { + ClassDB::bind_method(D_METHOD("update_mix_rate"), &Opus::update_mix_rate); ClassDB::bind_method(D_METHOD("encode"), &Opus::encode); ClassDB::bind_method(D_METHOD("decode"), &Opus::decode); ClassDB::bind_method(D_METHOD("decode_and_play"), &Opus::decode_and_play); @@ -16,11 +17,12 @@ Opus::Opus() { int err{}; - m_encoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err); + // Opus + m_encoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err); ERR_FAIL_COND(err != OPUS_OK); ERR_FAIL_COND(m_encoder == nullptr); - m_decoder = opus_decoder_create(48000, 1, &err); + m_decoder = opus_decoder_create(48000, 2, &err); ERR_FAIL_COND(err != OPUS_OK); ERR_FAIL_COND(m_decoder == nullptr); @@ -30,79 +32,111 @@ Opus::Opus() 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(28000)); ERR_FAIL_COND(err < 0); - m_encodeInputBuffer.resize(SampleFrames); - m_encodeOutputBuffer.resize(SampleFrames); - m_decodeOutputBuffer.resize(SampleFrames); + // Speex + m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err); + ERR_FAIL_COND(err != 0); + + m_decodeResampler = speex_resampler_init(2, 48000, m_outputMixRate, 10, &err); + ERR_FAIL_COND(err != 0); + + // Setup buffers + m_encodeSampleBuffer.resize(SampleFrames); + m_decodeSampleBuffer.resize(SampleFrames); } Opus::~Opus() { opus_encoder_destroy(m_encoder); opus_decoder_destroy(m_decoder); + speex_resampler_destroy(m_encodeResampler); + speex_resampler_destroy(m_decodeResampler); } -PackedFloat32Array Opus::encode(PackedVector2Array input) +void Opus::update_mix_rate(size_t input, size_t output) { - if (input.size() < SampleFrames) { - return {}; - } + int err{}; + m_inputMixRate = input; + m_outputMixRate = output; - for (size_t i = 0; i < SampleFrames; i++) { - m_encodeInputBuffer[i] = input[i].x; - } + speex_resampler_destroy(m_encodeResampler); + m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err); + ERR_FAIL_COND(err != 0); - const auto r = opus_encode_float( - m_encoder, - m_encodeInputBuffer.data(), - SampleFrames, - m_encodeOutputBuffer.data(), - m_encodeOutputBuffer.size() + speex_resampler_destroy(m_decodeResampler); + m_decodeResampler = speex_resampler_init(2, 48000, m_outputMixRate, 10, &err); + ERR_FAIL_COND(err != 0); + + /* + UtilityFunctions::print( + (std::string("Input encoder mix rate set to: ") + std::to_string(m_inputMixRate)).c_str() ); - if (r == -1) { - return {}; - } - - auto outputArray = PackedFloat32Array{}; - outputArray.resize(r); - for (size_t i = 0; i < r; i++) { - outputArray[i] = m_encodeOutputBuffer[i]; - } - - return outputArray; + UtilityFunctions::print( + (std::string("Output encoder mix rate set to: ") + std::to_string(m_outputMixRate)).c_str() + ); + */ } -PackedVector2Array Opus::decode(PackedFloat32Array input) +PackedByteArray Opus::encode(PackedVector2Array samples) { - std::vector inputData(input.size()); - for (size_t i = 0; i < input.size(); i++) { - inputData[i] = input[i]; - } + PackedByteArray encoded; + encoded.resize(sizeof(float) * SampleFrames * 2); - const auto r = opus_decode_float( + unsigned int inlen = samples.size(); + unsigned int outlen = SampleFrames; + + speex_resampler_process_interleaved_float( + m_encodeResampler, + (float*) samples.ptr(), + &inlen, + (float*) m_encodeSampleBuffer.ptrw(), + &outlen + ); + + const auto encodedSize = opus_encode_float( + m_encoder, + (float*) m_encodeSampleBuffer.ptr(), + SampleFrames, + (unsigned char*) encoded.ptrw(), + encoded.size() + ); + encoded.resize(encodedSize); + + return encoded; +} + +PackedVector2Array Opus::decode(PackedByteArray encoded) +{ + PackedVector2Array output; + output.resize(SampleFrames * m_outputMixRate / 48000); + + opus_decode_float( m_decoder, - inputData.data(), - input.size(), - m_decodeOutputBuffer.data(), + encoded.ptr(), + encoded.size(), + (float*) m_decodeSampleBuffer.ptrw(), SampleFrames, 0 ); - if (r != SampleFrames) { - return {}; - } - auto packedOutput = PackedVector2Array{}; - packedOutput.resize(r); - for (size_t i = 0; i < r; i++) { - packedOutput[i] = Vector2{m_decodeOutputBuffer[i], m_decodeOutputBuffer[i]}; - } + unsigned int inlen = m_decodeSampleBuffer.size(); + unsigned int outlen = output.size(); - return packedOutput; + speex_resampler_process_interleaved_float( + m_decodeResampler, + (float*) m_decodeSampleBuffer.ptr(), + &inlen, + (float*) output.ptrw(), + &outlen + ); + output.resize(outlen); + + return output; } -void Opus::decode_and_play(Ref buffer, PackedFloat32Array input) +void Opus::decode_and_play(Ref buffer, PackedByteArray input) { const auto decoded = decode(input); buffer->push_buffer(decoded); diff --git a/src/GodotOpus.h b/src/GodotOpus.h index 7b41a2a..f65609b 100644 --- a/src/GodotOpus.h +++ b/src/GodotOpus.h @@ -6,6 +6,7 @@ #include #include "opus.h" +#include "speex/speex_resampler.h" namespace godot { @@ -22,19 +23,25 @@ public: Opus(); ~Opus(); - PackedFloat32Array encode(PackedVector2Array input); - PackedVector2Array decode(PackedFloat32Array input); + void update_mix_rate(size_t input, size_t output); - void decode_and_play(Ref buffer, PackedFloat32Array input); + PackedByteArray encode(PackedVector2Array input); + PackedVector2Array decode(PackedByteArray input); + + void decode_and_play(Ref buffer, PackedByteArray input); private: - std::vector m_encodeInputBuffer; - std::vector m_decodeOutputBuffer; - std::vector m_encodeOutputBuffer; + PackedVector2Array m_encodeSampleBuffer; + PackedVector2Array m_decodeSampleBuffer; + + size_t m_outputMixRate{44100}; + size_t m_inputMixRate{44100}; OpusEncoder* m_encoder; OpusDecoder* m_decoder; + SpeexResamplerState* m_encodeResampler; + SpeexResamplerState* m_decodeResampler; }; }