Add method for fetching default device sampling rate

This commit is contained in:
weil 2024-01-22 20:16:23 +01:00
parent cc22cb9959
commit c09287c4fe
3 changed files with 110 additions and 0 deletions

74
src/AudioProcessor.cpp Normal file
View File

@ -0,0 +1,74 @@
#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_mix_rate"), &AudioProcessor::get_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;
}
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultDevice);
if (FAILED(hr)) {
std::cerr << "Failed to get default audio device" << std::endl;
m_deviceEnumerator->Release();
return;
}
hr = m_defaultDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_audioClient);
if (FAILED(hr)) {
std::cerr << "Failed to activate audio client" << std::endl;
m_defaultDevice->Release();
m_deviceEnumerator->Release();
return;
}
hr = m_audioClient->GetMixFormat(&m_pwfx);
if (FAILED(hr)) {
std::cerr << "Failed to get mix format" << std::endl;
m_audioClient->Release();
m_defaultDevice->Release();
m_deviceEnumerator->Release();
return;
}
}
AudioProcessor::~AudioProcessor()
{
CoTaskMemFree(m_pwfx);
m_audioClient->Release();
m_defaultDevice->Release();
m_deviceEnumerator->Release();
CoUninitialize();
}
void AudioProcessor::process()
{
}
size_t AudioProcessor::get_mix_rate() const
{
return m_pwfx->nSamplesPerSec;
}
}

34
src/AudioProcessor.h Normal file
View File

@ -0,0 +1,34 @@
#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_mix_rate() const;
private:
IMMDeviceEnumerator *m_deviceEnumerator{};
IMMDevice *m_defaultDevice{};
IAudioClient *m_audioClient{};
WAVEFORMATEX *m_pwfx{};
};
}

View File

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