Reworked sampling handling; added speex resampler

This commit is contained in:
weil 2024-01-24 01:37:09 +01:00
parent 09d72665ce
commit 9f2cbf4b75
2 changed files with 96 additions and 55 deletions

View File

@ -7,6 +7,7 @@ namespace godot {
void Opus::_bind_methods() 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("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); ClassDB::bind_method(D_METHOD("decode_and_play"), &Opus::decode_and_play);
@ -16,11 +17,12 @@ Opus::Opus()
{ {
int err{}; 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(err != OPUS_OK);
ERR_FAIL_COND(m_encoder == nullptr); 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(err != OPUS_OK);
ERR_FAIL_COND(m_decoder == nullptr); 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 = opus_encoder_ctl(m_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
ERR_FAIL_COND(err < 0); 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); ERR_FAIL_COND(err < 0);
m_encodeInputBuffer.resize(SampleFrames); // Speex
m_encodeOutputBuffer.resize(SampleFrames); m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err);
m_decodeOutputBuffer.resize(SampleFrames); 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::~Opus()
{ {
opus_encoder_destroy(m_encoder); opus_encoder_destroy(m_encoder);
opus_decoder_destroy(m_decoder); 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) { int err{};
return {}; m_inputMixRate = input;
} m_outputMixRate = output;
for (size_t i = 0; i < SampleFrames; i++) { speex_resampler_destroy(m_encodeResampler);
m_encodeInputBuffer[i] = input[i].x; m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err);
} ERR_FAIL_COND(err != 0);
const auto r = opus_encode_float( speex_resampler_destroy(m_decodeResampler);
m_encoder, m_decodeResampler = speex_resampler_init(2, 48000, m_outputMixRate, 10, &err);
m_encodeInputBuffer.data(), ERR_FAIL_COND(err != 0);
SampleFrames,
m_encodeOutputBuffer.data(), /*
m_encodeOutputBuffer.size() UtilityFunctions::print(
(std::string("Input encoder mix rate set to: ") + std::to_string(m_inputMixRate)).c_str()
); );
if (r == -1) { UtilityFunctions::print(
return {}; (std::string("Output encoder mix rate set to: ") + std::to_string(m_outputMixRate)).c_str()
} );
*/
auto outputArray = PackedFloat32Array{};
outputArray.resize(r);
for (size_t i = 0; i < r; i++) {
outputArray[i] = m_encodeOutputBuffer[i];
}
return outputArray;
} }
PackedVector2Array Opus::decode(PackedFloat32Array input) PackedByteArray Opus::encode(PackedVector2Array samples)
{ {
std::vector<unsigned char> inputData(input.size()); PackedByteArray encoded;
for (size_t i = 0; i < input.size(); i++) { encoded.resize(sizeof(float) * SampleFrames * 2);
inputData[i] = input[i];
}
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, m_decoder,
inputData.data(), encoded.ptr(),
input.size(), encoded.size(),
m_decodeOutputBuffer.data(), (float*) m_decodeSampleBuffer.ptrw(),
SampleFrames, SampleFrames,
0 0
); );
if (r != SampleFrames) {
return {};
}
auto packedOutput = PackedVector2Array{}; unsigned int inlen = m_decodeSampleBuffer.size();
packedOutput.resize(r); unsigned int outlen = output.size();
for (size_t i = 0; i < r; i++) {
packedOutput[i] = Vector2{m_decodeOutputBuffer[i], m_decodeOutputBuffer[i]};
}
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<AudioStreamGeneratorPlayback> buffer, PackedFloat32Array input) void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input)
{ {
const auto decoded = decode(input); const auto decoded = decode(input);
buffer->push_buffer(decoded); buffer->push_buffer(decoded);

View File

@ -6,6 +6,7 @@
#include <godot_cpp/classes/audio_stream_generator_playback.hpp> #include <godot_cpp/classes/audio_stream_generator_playback.hpp>
#include "opus.h" #include "opus.h"
#include "speex/speex_resampler.h"
namespace godot { namespace godot {
@ -22,19 +23,25 @@ public:
Opus(); Opus();
~Opus(); ~Opus();
PackedFloat32Array encode(PackedVector2Array input); void update_mix_rate(size_t input, size_t output);
PackedVector2Array decode(PackedFloat32Array input);
void decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedFloat32Array input); PackedByteArray encode(PackedVector2Array input);
PackedVector2Array decode(PackedByteArray input);
void decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input);
private: private:
std::vector<float> m_encodeInputBuffer; PackedVector2Array m_encodeSampleBuffer;
std::vector<float> m_decodeOutputBuffer; PackedVector2Array m_decodeSampleBuffer;
std::vector<unsigned char> m_encodeOutputBuffer;
size_t m_outputMixRate{44100};
size_t m_inputMixRate{44100};
OpusEncoder* m_encoder; OpusEncoder* m_encoder;
OpusDecoder* m_decoder; OpusDecoder* m_decoder;
SpeexResamplerState* m_encodeResampler;
SpeexResamplerState* m_decodeResampler;
}; };
} }