2024-01-16 15:58:59 +00:00
|
|
|
#include "GodotOpus.h"
|
|
|
|
|
|
|
|
#include <godot_cpp/core/class_db.hpp>
|
|
|
|
#include <godot_cpp/variant/utility_functions.hpp>
|
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
|
|
|
void Opus::_bind_methods()
|
|
|
|
{
|
2024-01-24 00:37:09 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("update_mix_rate"), &Opus::update_mix_rate);
|
2024-01-16 15:58:59 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("encode"), &Opus::encode);
|
|
|
|
ClassDB::bind_method(D_METHOD("decode"), &Opus::decode);
|
2024-01-17 07:29:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("decode_and_play"), &Opus::decode_and_play);
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Opus::Opus()
|
|
|
|
{
|
|
|
|
int err{};
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
// Opus
|
|
|
|
m_encoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
|
2024-01-16 15:58:59 +00:00
|
|
|
ERR_FAIL_COND(err != OPUS_OK);
|
|
|
|
ERR_FAIL_COND(m_encoder == nullptr);
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
m_decoder = opus_decoder_create(48000, 2, &err);
|
2024-01-16 15:58:59 +00:00
|
|
|
ERR_FAIL_COND(err != OPUS_OK);
|
|
|
|
ERR_FAIL_COND(m_decoder == nullptr);
|
|
|
|
|
|
|
|
err = opus_encoder_ctl(m_encoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
|
|
|
|
ERR_FAIL_COND(err < 0);
|
|
|
|
|
2024-01-17 07:29:33 +00:00
|
|
|
err = opus_encoder_ctl(m_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
|
|
|
|
ERR_FAIL_COND(err < 0);
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(28000));
|
2024-01-16 15:58:59 +00:00
|
|
|
ERR_FAIL_COND(err < 0);
|
2024-01-17 07:29:33 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
// 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);
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Opus::~Opus()
|
|
|
|
{
|
|
|
|
opus_encoder_destroy(m_encoder);
|
|
|
|
opus_decoder_destroy(m_decoder);
|
2024-01-24 00:37:09 +00:00
|
|
|
speex_resampler_destroy(m_encodeResampler);
|
|
|
|
speex_resampler_destroy(m_decodeResampler);
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
void Opus::update_mix_rate(size_t input, size_t output)
|
2024-01-16 15:58:59 +00:00
|
|
|
{
|
2024-01-24 00:37:09 +00:00
|
|
|
int err{};
|
|
|
|
m_inputMixRate = input;
|
|
|
|
m_outputMixRate = output;
|
|
|
|
|
|
|
|
speex_resampler_destroy(m_encodeResampler);
|
|
|
|
m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err);
|
|
|
|
ERR_FAIL_COND(err != 0);
|
|
|
|
|
|
|
|
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()
|
|
|
|
);
|
|
|
|
UtilityFunctions::print(
|
|
|
|
(std::string("Output encoder mix rate set to: ") + std::to_string(m_outputMixRate)).c_str()
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
}
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
PackedByteArray Opus::encode(PackedVector2Array samples)
|
|
|
|
{
|
|
|
|
PackedByteArray encoded;
|
|
|
|
encoded.resize(sizeof(float) * SampleFrames * 2);
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
const auto encodedSize = opus_encode_float(
|
2024-01-17 07:29:33 +00:00
|
|
|
m_encoder,
|
2024-01-24 00:37:09 +00:00
|
|
|
(float*) m_encodeSampleBuffer.ptr(),
|
2024-01-17 07:29:33 +00:00
|
|
|
SampleFrames,
|
2024-01-24 00:37:09 +00:00
|
|
|
(unsigned char*) encoded.ptrw(),
|
|
|
|
encoded.size()
|
2024-01-17 07:29:33 +00:00
|
|
|
);
|
2024-01-24 00:37:09 +00:00
|
|
|
encoded.resize(encodedSize);
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
return encoded;
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
PackedVector2Array Opus::decode(PackedByteArray encoded)
|
2024-01-16 15:58:59 +00:00
|
|
|
{
|
2024-01-24 00:37:09 +00:00
|
|
|
PackedVector2Array output;
|
|
|
|
output.resize(SampleFrames * m_outputMixRate / 48000);
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
opus_decode_float(
|
2024-01-17 07:29:33 +00:00
|
|
|
m_decoder,
|
2024-01-24 00:37:09 +00:00
|
|
|
encoded.ptr(),
|
|
|
|
encoded.size(),
|
|
|
|
(float*) m_decodeSampleBuffer.ptrw(),
|
2024-01-17 07:29:33 +00:00
|
|
|
SampleFrames,
|
|
|
|
0
|
|
|
|
);
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
unsigned int inlen = m_decodeSampleBuffer.size();
|
|
|
|
unsigned int outlen = output.size();
|
|
|
|
|
|
|
|
speex_resampler_process_interleaved_float(
|
|
|
|
m_decodeResampler,
|
|
|
|
(float*) m_decodeSampleBuffer.ptr(),
|
|
|
|
&inlen,
|
|
|
|
(float*) output.ptrw(),
|
|
|
|
&outlen
|
|
|
|
);
|
|
|
|
output.resize(outlen);
|
2024-01-16 15:58:59 +00:00
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
return output;
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 00:37:09 +00:00
|
|
|
void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input)
|
2024-01-17 07:29:33 +00:00
|
|
|
{
|
|
|
|
const auto decoded = decode(input);
|
|
|
|
buffer->push_buffer(decoded);
|
|
|
|
}
|
|
|
|
|
2024-01-16 15:58:59 +00:00
|
|
|
}
|