Compare commits

...

8 Commits
0.1.0 ... main

Author SHA1 Message Date
weil de45aecda6 Adjust gitignore for vs 2024-01-29 10:53:33 +01:00
weil ad816f16dd Add visual studio project generation to scons 2024-01-29 10:50:58 +01:00
weil aec78c782e Add get_input_mix_rate method 2024-01-29 10:46:18 +01:00
weil 9f2cbf4b75 Reworked sampling handling; added speex resampler 2024-01-24 01:37:09 +01:00
weil 09d72665ce Added libspeex dependency 2024-01-24 01:05:41 +01:00
weil c09287c4fe Add method for fetching default device sampling rate 2024-01-22 20:16:23 +01:00
jan-weil cc22cb9959
Minor changes to the readme 2024-01-17 08:42:18 +01:00
weil cd69c2b1ca GodotOpus is no longer a singleton; multiple performance improvements 2024-01-17 08:29:33 +01:00
22 changed files with 382 additions and 88 deletions

6
.gitignore vendored
View File

@ -4,3 +4,9 @@ export
addons/**/~*.dll
.sconsign.dblite
*.obj
# Visual-studio related
.vs
*.sln
*.vcxproj*
x64

BIN
3rdparty/speex/include/speex/speex_jitter.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/include/speex/speex_resampler.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/include/speex/speexdsp_config_types.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/include/speex/speexdsp_types.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/lib/libspeex.lib (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/lib/libspeexdsp.lib (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/arch.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/jitter.c (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/os_support.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/resample.c (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/resample_neon.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/libspeexdsp/resample_sse.h (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/speex_license.txt (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
3rdparty/speex/speexdsp_license.txt (Stored with Git LFS) vendored Normal file

Binary file not shown.

View File

@ -4,32 +4,41 @@ A GDExtension that adds to Godot4 support for encoding voice data using the Opus
## Overview
This extension adds a new singleton to Godot: `Opus` with two methods: `encode` and `decode`.
This extension adds a new node to Godot: `Opus` with three methods: `encode`, `decode` and `decode_and_play`.
These can be used to compress audio obtained `AudioEffectCapture` and then to decode it so it's usable in Godot again.
Quick and dirty example (full demo coming soon):
Usage is best illustrated in this demo: https://github.com/microtaur/godot4-p2p-voip
Most interesting part from the demo code illustrating how this works:
```GDScript
func _process_audio() -> void:
var _encoder := Opus.new()
...
func _audio_process():
while true:
if has_data() and active:
call_deferred("rpc", "play_data", get_data())
var data = get_data()
call_deferred("rpc", "play_data", data)
else:
OS.delay_msec(10)
...
func get_data() -> PackedFloat32Array:
var data = effect.get_buffer(BUFFER_SIZE)
return Opus.encode(data)
return _encoder.encode(data)
...
@rpc("any_peer", "call_remote", "unreliable_ordered")
@rpc("any_peer", "call_remote", "reliable")
func play_data(data: PackedFloat32Array) -> void:
var id = client.multiplayer.get_remote_sender_id()
var decoded = Opus.decode(data)
for b in range(0, BUFFER_SIZE):
_get_generator(id).push_frame(decoded[b])
_update_player_pool()
_get_opus_instance(id).decode_and_play(_get_generator(id), data)
```

View File

@ -1,24 +1,29 @@
#!/usr/bin/env python
from glob import glob
from pathlib import Path
from SCons.Script import MSVSProject
from SCons.Script import *
# Initialize environment and SCons script for godot-cpp
env = SConscript("godot-cpp/SConstruct")
env.Tool('msvs')
# Sources
# Sources and include paths
env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp")
sources += Glob("src/platform/win32/*.cpp")
# Opus (Windows x64)
env.Append(CPPPATH=['#3rdparty/opus/include'])
env.Append(LIBPATH=['#3rdparty/opus/lib'])
env.Append(LIBS=['opus'])
# Append additional library paths and libraries for Opus, Speex, and vpx
env.Append(CPPPATH=['#3rdparty/opus/include', '#3rdparty/speex/include', '#3rdparty/libvpx/include'])
env.Append(LIBPATH=['#3rdparty/opus/lib', '#3rdparty/speex/lib', '#3rdparty/libvpx/lib/x64'])
env.Append(LIBS=['opus', 'libspeex', 'libspeexdsp', 'vpx'])
# Determine extension and addon path
(extension_path,) = glob("export/addons/*/*.gdextension")
addon_path = Path(extension_path).parent
project_name = Path(extension_path).stem
debug_or_release = "release" if env["target"] == "template_release" else "debug"
# Generate library based on platform
if env["platform"] == "macos":
library = env.SharedLibrary(
"{0}/lib/lib{1}.{2}.{3}.framework/{1}.{2}.{3}".format(
@ -42,4 +47,19 @@ else:
source=sources,
)
Default(library)
srcs = []
for s in sources:
srcs.append(s.abspath)
msvs_project = env.MSVSProject(
target = project_name + env['MSVSPROJECTSUFFIX'],
srcs = srcs,
include_dirs = env['CPPPATH'],
lib_dirs = env['LIBPATH'],
libs = env['LIBS'],
variant = 'Debug|x64',
)
# Make sure the default build includes the Visual Studio project
Default(library, msvs_project)

122
src/AudioProcessor.cpp Normal file
View File

@ -0,0 +1,122 @@
#include "AudioProcessor.h"
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "Mmdevapi.lib")
#pragma comment(lib, "Uuid.lib")
namespace godot {
void AudioProcessor::_bind_methods()
{
ClassDB::bind_method(D_METHOD("process"), &AudioProcessor::process);
ClassDB::bind_method(D_METHOD("get_output_mix_rate"), &AudioProcessor::get_output_mix_rate);
ClassDB::bind_method(D_METHOD("get_input_mix_rate"), &AudioProcessor::get_input_mix_rate);
}
AudioProcessor::AudioProcessor()
{
HRESULT hr;
CoInitialize(NULL);
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&m_deviceEnumerator);
if (FAILED(hr)) {
std::cerr << "Failed to create device enumerator" << std::endl;
return;
}
// Initialize default output device
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultOutputDevice);
if (FAILED(hr)) {
std::cerr << "Failed to get default output audio device" << std::endl;
return;
}
hr = m_defaultOutputDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_outputAudioClient);
if (FAILED(hr)) {
std::cerr << "Failed to activate output audio client" << std::endl;
return;
}
hr = m_outputAudioClient->GetMixFormat(&m_outputMixFormat);
if (FAILED(hr)) {
std::cerr << "Failed to get output mix format" << std::endl;
return;
}
// Initialize default input device
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &m_defaultInputDevice);
if (FAILED(hr)) {
std::cerr << "Failed to get default input audio device" << std::endl;
return;
}
hr = m_defaultInputDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_inputAudioClient);
if (FAILED(hr)) {
std::cerr << "Failed to activate input audio client" << std::endl;
return;
}
hr = m_inputAudioClient->GetMixFormat(&m_inputMixFormat);
if (FAILED(hr)) {
std::cerr << "Failed to get input mix format" << std::endl;
return;
}
}
AudioProcessor::~AudioProcessor() {
if (m_outputMixFormat != nullptr) {
CoTaskMemFree(m_outputMixFormat);
m_outputMixFormat = nullptr;
}
if (m_outputAudioClient != nullptr) {
m_outputAudioClient->Release();
m_outputAudioClient = nullptr;
}
if (m_defaultOutputDevice != nullptr) {
m_defaultOutputDevice->Release();
m_defaultOutputDevice = nullptr;
}
if (m_inputMixFormat != nullptr) {
CoTaskMemFree(m_inputMixFormat);
m_inputMixFormat = nullptr;
}
if (m_inputAudioClient != nullptr) {
m_inputAudioClient->Release();
m_inputAudioClient = nullptr;
}
if (m_defaultInputDevice != nullptr) {
m_defaultInputDevice->Release();
m_defaultInputDevice = nullptr;
}
if (m_deviceEnumerator != nullptr) {
m_deviceEnumerator->Release();
m_deviceEnumerator = nullptr;
}
CoUninitialize();
}
void AudioProcessor::process()
{
}
size_t AudioProcessor::get_output_mix_rate() const
{
return m_outputMixFormat->nSamplesPerSec;
}
size_t AudioProcessor::get_input_mix_rate() const {
return m_inputMixFormat->nSamplesPerSec;
}
}

40
src/AudioProcessor.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <mmdeviceapi.h>
#include <Audioclient.h>
#include "opus.h"
namespace godot {
class AudioProcessor : public Node
{
GDCLASS(AudioProcessor, Node);
protected:
static void _bind_methods();
public:
AudioProcessor();
~AudioProcessor();
void process();
size_t get_input_mix_rate() const;
size_t get_output_mix_rate() const;
private:
IMMDeviceEnumerator* m_deviceEnumerator = nullptr;
IMMDevice* m_defaultOutputDevice = nullptr;
IAudioClient* m_outputAudioClient = nullptr;
WAVEFORMATEX* m_outputMixFormat = nullptr;
IMMDevice* m_defaultInputDevice = nullptr;
IAudioClient* m_inputAudioClient = nullptr;
WAVEFORMATEX* m_inputMixFormat = nullptr;
};
}

View File

@ -5,96 +5,141 @@
namespace godot {
Opus *Opus::singleton = nullptr;
constexpr auto sampleFrames = 480;
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);
}
Opus *Opus::get_singleton()
{
return singleton;
ClassDB::bind_method(D_METHOD("decode_and_play"), &Opus::decode_and_play);
}
Opus::Opus()
{
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
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);
err = opus_encoder_ctl(m_encoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
ERR_FAIL_COND(err < 0);
err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(24000));
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(28000));
ERR_FAIL_COND(err < 0);
// 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()
{
ERR_FAIL_COND(singleton != this);
opus_encoder_destroy(m_encoder);
opus_decoder_destroy(m_decoder);
singleton = nullptr;
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;
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()
);
*/
}
std::vector<float> data(sampleFrames);
for (size_t i = 0; i < sampleFrames; i++) {
data[i] = input[i].x;
}
std::vector<unsigned char> output(sampleFrames * 2);
const auto r = opus_encode_float(m_encoder, data.data(), sampleFrames, output.data(), output.size());
if (r == -1) {
return {};
}
auto outputArray = PackedFloat32Array{};
outputArray.resize(r);
for (size_t i = 0; i < r; i++) {
outputArray[i] = output[i];
}
return outputArray;
}
PackedVector2Array Opus::decode(PackedFloat32Array input)
PackedByteArray Opus::encode(PackedVector2Array samples)
{
std::vector<unsigned char> inputData(sampleFrames*2);
for (size_t i = 0; i < input.size(); i++) {
inputData[i] = input[i];
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
);
const auto encodedSize = opus_encode_float(
m_encoder,
(float*) m_encodeSampleBuffer.ptr(),
SampleFrames,
(unsigned char*) encoded.ptrw(),
encoded.size()
);
encoded.resize(encodedSize);
return encoded;
}
std::vector<float> output(sampleFrames*2);
const auto r = opus_decode_float(m_decoder, inputData.data(), input.size(), output.data(), sampleFrames, 0);
if (r != sampleFrames) {
return {};
PackedVector2Array Opus::decode(PackedByteArray encoded)
{
PackedVector2Array output;
output.resize(SampleFrames * m_outputMixRate / 48000);
opus_decode_float(
m_decoder,
encoded.ptr(),
encoded.size(),
(float*) m_decodeSampleBuffer.ptrw(),
SampleFrames,
0
);
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);
return output;
}
auto packedOutput = PackedVector2Array{};
packedOutput.resize(r);
for (size_t i = 0; i < r; i++) {
packedOutput[i] = Vector2{output[i], output[i]};
}
return packedOutput;
void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input)
{
const auto decoded = decode(input);
buffer->push_buffer(decoded);
}
}

View File

@ -1,35 +1,47 @@
#pragma once
#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/classes/node.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"
using namespace godot;
#include "speex/speex_resampler.h"
namespace godot {
class Opus : public Object
constexpr size_t SampleFrames{480};
class Opus : public Node
{
GDCLASS(Opus, Object);
static Opus *singleton;
GDCLASS(Opus, Node);
protected:
static void _bind_methods();
public:
static Opus *get_singleton();
Opus();
~Opus();
PackedFloat32Array encode(PackedVector2Array input);
PackedVector2Array decode(PackedFloat32Array input);
void update_mix_rate(size_t input, size_t output);
PackedByteArray encode(PackedVector2Array input);
PackedVector2Array decode(PackedByteArray input);
void decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input);
private:
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;
};
}

View File

@ -12,6 +12,7 @@
#include <godot_cpp/variant/utility_functions.hpp>
#include "GodotOpus.h"
#include "AudioProcessor.h"
using namespace godot;
@ -22,9 +23,7 @@ void gdextension_initialize(ModuleInitializationLevel p_level)
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{
ClassDB::register_class<Opus>();
_godot_opus_singleton = memnew(Opus);
Engine::get_singleton()->register_singleton("Opus", Opus::get_singleton());
ClassDB::register_class<AudioProcessor>();
}
}
@ -32,8 +31,7 @@ void gdextension_terminate(ModuleInitializationLevel p_level)
{
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{
Engine::get_singleton()->unregister_singleton("Opus");
memdelete(_godot_opus_singleton);
}
}