Add get_input_mix_rate method

This commit is contained in:
weil 2024-01-29 10:46:18 +01:00
parent 9f2cbf4b75
commit aec78c782e
2 changed files with 97 additions and 43 deletions

View File

@ -9,11 +9,11 @@
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);
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()
@ -27,37 +27,81 @@ AudioProcessor::AudioProcessor()
return;
}
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultDevice);
// Initialize default output device
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultOutputDevice);
if (FAILED(hr)) {
std::cerr << "Failed to get default audio device" << std::endl;
m_deviceEnumerator->Release();
std::cerr << "Failed to get default output audio device" << std::endl;
return;
}
hr = m_defaultDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_audioClient);
hr = m_defaultOutputDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&m_outputAudioClient);
if (FAILED(hr)) {
std::cerr << "Failed to activate audio client" << std::endl;
m_defaultDevice->Release();
m_deviceEnumerator->Release();
std::cerr << "Failed to activate output audio client" << std::endl;
return;
}
hr = m_audioClient->GetMixFormat(&m_pwfx);
hr = m_outputAudioClient->GetMixFormat(&m_outputMixFormat);
if (FAILED(hr)) {
std::cerr << "Failed to get mix format" << std::endl;
m_audioClient->Release();
m_defaultDevice->Release();
m_deviceEnumerator->Release();
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()
{
CoTaskMemFree(m_pwfx);
m_audioClient->Release();
m_defaultDevice->Release();
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();
}
@ -66,9 +110,13 @@ void AudioProcessor::process()
}
size_t AudioProcessor::get_mix_rate() const
size_t AudioProcessor::get_output_mix_rate() const
{
return m_pwfx->nSamplesPerSec;
return m_outputMixFormat->nSamplesPerSec;
}
size_t AudioProcessor::get_input_mix_rate() const {
return m_inputMixFormat->nSamplesPerSec;
}
}

View File

@ -22,13 +22,19 @@ public:
~AudioProcessor();
void process();
size_t get_mix_rate() const;
size_t get_input_mix_rate() const;
size_t get_output_mix_rate() const;
private:
IMMDeviceEnumerator *m_deviceEnumerator{};
IMMDevice *m_defaultDevice{};
IAudioClient *m_audioClient{};
WAVEFORMATEX *m_pwfx{};
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;
};
}