Compare commits

..

No commits in common. "main" and "0.1.1" have entirely different histories.
main ... 0.1.1

22 changed files with 72 additions and 354 deletions

8
.gitignore vendored
View File

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

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

View File

@ -4,41 +4,32 @@ A GDExtension that adds to Godot4 support for encoding voice data using the Opus
## Overview
This extension adds a new node to Godot: `Opus` with three methods: `encode`, `decode` and `decode_and_play`.
This extension adds a new singleton to Godot: `Opus` with two methods: `encode` and `decode`.
These can be used to compress audio obtained `AudioEffectCapture` and then to decode it so it's usable in Godot again.
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:
Quick and dirty example (full demo coming soon):
```GDScript
var _encoder := Opus.new()
...
func _audio_process():
while true:
if has_data() and active:
var data = get_data()
call_deferred("rpc", "play_data", data)
else:
OS.delay_msec(10)
func _process_audio() -> void:
if has_data() and active:
call_deferred("rpc", "play_data", get_data())
...
func get_data() -> PackedFloat32Array:
var data = effect.get_buffer(BUFFER_SIZE)
return _encoder.encode(data)
var data = effect.get_buffer(BUFFER_SIZE)
return Opus.encode(data)
...
@rpc("any_peer", "call_remote", "reliable")
@rpc("any_peer", "call_remote", "unreliable_ordered")
func play_data(data: PackedFloat32Array) -> void:
var id = client.multiplayer.get_remote_sender_id()
_update_player_pool()
_get_opus_instance(id).decode_and_play(_get_generator(id), data)
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])
```

View File

@ -1,29 +1,24 @@
#!/usr/bin/env python
from glob import glob
from pathlib import Path
from SCons.Script import *
from SCons.Script import MSVSProject
# Initialize environment and SCons script for godot-cpp
env = SConscript("godot-cpp/SConstruct")
env.Tool('msvs')
# Sources and include paths
# Sources
env.Append(CPPPATH=["src/"])
sources = Glob("src/*.cpp")
sources += Glob("src/platform/win32/*.cpp")
# 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'])
# Opus (Windows x64)
env.Append(CPPPATH=['#3rdparty/opus/include'])
env.Append(LIBPATH=['#3rdparty/opus/lib'])
env.Append(LIBS=['opus'])
# 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(
@ -47,19 +42,4 @@ else:
source=sources,
)
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)
Default(library)

View File

@ -1,122 +0,0 @@
#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;
}
}

View File

@ -1,40 +0,0 @@
#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

@ -7,7 +7,6 @@ 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);
@ -17,12 +16,11 @@ Opus::Opus()
{
int err{};
// Opus
m_encoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
m_encoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err);
ERR_FAIL_COND(err != OPUS_OK);
ERR_FAIL_COND(m_encoder == nullptr);
m_decoder = opus_decoder_create(48000, 2, &err);
m_decoder = opus_decoder_create(48000, 1, &err);
ERR_FAIL_COND(err != OPUS_OK);
ERR_FAIL_COND(m_decoder == nullptr);
@ -32,111 +30,79 @@ 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(28000));
err = opus_encoder_ctl(m_encoder, OPUS_SET_BITRATE(24000));
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);
m_encodeInputBuffer.resize(SampleFrames);
m_encodeOutputBuffer.resize(SampleFrames);
m_decodeOutputBuffer.resize(SampleFrames);
}
Opus::~Opus()
{
opus_encoder_destroy(m_encoder);
opus_decoder_destroy(m_decoder);
speex_resampler_destroy(m_encodeResampler);
speex_resampler_destroy(m_decodeResampler);
}
void Opus::update_mix_rate(size_t input, size_t output)
PackedFloat32Array Opus::encode(PackedVector2Array input)
{
int err{};
m_inputMixRate = input;
m_outputMixRate = output;
if (input.size() < SampleFrames) {
return {};
}
speex_resampler_destroy(m_encodeResampler);
m_encodeResampler = speex_resampler_init(2, m_inputMixRate, 48000, 10, &err);
ERR_FAIL_COND(err != 0);
for (size_t i = 0; i < SampleFrames; i++) {
m_encodeInputBuffer[i] = input[i].x;
}
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()
);
*/
}
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
);
const auto encodedSize = opus_encode_float(
const auto r = opus_encode_float(
m_encoder,
(float*) m_encodeSampleBuffer.ptr(),
m_encodeInputBuffer.data(),
SampleFrames,
(unsigned char*) encoded.ptrw(),
encoded.size()
m_encodeOutputBuffer.data(),
m_encodeOutputBuffer.size()
);
encoded.resize(encodedSize);
if (r == -1) {
return {};
}
return encoded;
auto outputArray = PackedFloat32Array{};
outputArray.resize(r);
for (size_t i = 0; i < r; i++) {
outputArray[i] = m_encodeOutputBuffer[i];
}
return outputArray;
}
PackedVector2Array Opus::decode(PackedByteArray encoded)
PackedVector2Array Opus::decode(PackedFloat32Array input)
{
PackedVector2Array output;
output.resize(SampleFrames * m_outputMixRate / 48000);
std::vector<unsigned char> inputData(input.size());
for (size_t i = 0; i < input.size(); i++) {
inputData[i] = input[i];
}
opus_decode_float(
const auto r = opus_decode_float(
m_decoder,
encoded.ptr(),
encoded.size(),
(float*) m_decodeSampleBuffer.ptrw(),
inputData.data(),
input.size(),
m_decodeOutputBuffer.data(),
SampleFrames,
0
);
if (r != SampleFrames) {
return {};
}
unsigned int inlen = m_decodeSampleBuffer.size();
unsigned int outlen = output.size();
auto packedOutput = PackedVector2Array{};
packedOutput.resize(r);
for (size_t i = 0; i < r; i++) {
packedOutput[i] = Vector2{m_decodeOutputBuffer[i], m_decodeOutputBuffer[i]};
}
speex_resampler_process_interleaved_float(
m_decodeResampler,
(float*) m_decodeSampleBuffer.ptr(),
&inlen,
(float*) output.ptrw(),
&outlen
);
output.resize(outlen);
return output;
return packedOutput;
}
void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedByteArray input)
void Opus::decode_and_play(Ref<AudioStreamGeneratorPlayback> buffer, PackedFloat32Array input)
{
const auto decoded = decode(input);
buffer->push_buffer(decoded);

View File

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

View File

@ -12,7 +12,6 @@
#include <godot_cpp/variant/utility_functions.hpp>
#include "GodotOpus.h"
#include "AudioProcessor.h"
using namespace godot;
@ -23,7 +22,6 @@ void gdextension_initialize(ModuleInitializationLevel p_level)
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE)
{
ClassDB::register_class<Opus>();
ClassDB::register_class<AudioProcessor>();
}
}