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 { namespace godot {
void AudioProcessor::_bind_methods() void AudioProcessor::_bind_methods()
{ {
ClassDB::bind_method(D_METHOD("process"), &AudioProcessor::process); 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() AudioProcessor::AudioProcessor()
@ -27,37 +27,81 @@ AudioProcessor::AudioProcessor()
return; return;
} }
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultDevice); // Initialize default output device
hr = m_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_defaultOutputDevice);
if (FAILED(hr)) { if (FAILED(hr)) {
std::cerr << "Failed to get default audio device" << std::endl; std::cerr << "Failed to get default output audio device" << std::endl;
m_deviceEnumerator->Release();
return; 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)) { if (FAILED(hr)) {
std::cerr << "Failed to activate audio client" << std::endl; std::cerr << "Failed to activate output audio client" << std::endl;
m_defaultDevice->Release();
m_deviceEnumerator->Release();
return; return;
} }
hr = m_audioClient->GetMixFormat(&m_pwfx); hr = m_outputAudioClient->GetMixFormat(&m_outputMixFormat);
if (FAILED(hr)) { if (FAILED(hr)) {
std::cerr << "Failed to get mix format" << std::endl; std::cerr << "Failed to get output mix format" << std::endl;
m_audioClient->Release(); return;
m_defaultDevice->Release(); }
m_deviceEnumerator->Release();
// 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; return;
} }
} }
AudioProcessor::~AudioProcessor() AudioProcessor::~AudioProcessor() {
{ if (m_outputMixFormat != nullptr) {
CoTaskMemFree(m_pwfx); CoTaskMemFree(m_outputMixFormat);
m_audioClient->Release(); m_outputMixFormat = nullptr;
m_defaultDevice->Release(); }
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->Release();
m_deviceEnumerator = nullptr;
}
CoUninitialize(); 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(); ~AudioProcessor();
void process(); void process();
size_t get_mix_rate() const;
size_t get_input_mix_rate() const;
size_t get_output_mix_rate() const;
private: private:
IMMDeviceEnumerator *m_deviceEnumerator{}; IMMDeviceEnumerator* m_deviceEnumerator = nullptr;
IMMDevice *m_defaultDevice{}; IMMDevice* m_defaultOutputDevice = nullptr;
IAudioClient *m_audioClient{}; IAudioClient* m_outputAudioClient = nullptr;
WAVEFORMATEX *m_pwfx{}; WAVEFORMATEX* m_outputMixFormat = nullptr;
IMMDevice* m_defaultInputDevice = nullptr;
IAudioClient* m_inputAudioClient = nullptr;
WAVEFORMATEX* m_inputMixFormat = nullptr;
}; };
} }