From f85062495c3ee477b7ab38f4de435435aedaf375 Mon Sep 17 00:00:00 2001 From: Saracen Date: Tue, 27 Feb 2018 07:54:56 +0000 Subject: [PATCH 01/14] Experimental microphone support. --- drivers/alsa/audio_driver_alsa.cpp | 22 + drivers/alsa/audio_driver_alsa.h | 5 + drivers/coreaudio/audio_driver_coreaudio.cpp | 22 + drivers/coreaudio/audio_driver_coreaudio.h | 5 + .../pulseaudio/audio_driver_pulseaudio.cpp | 22 + drivers/pulseaudio/audio_driver_pulseaudio.h | 5 + drivers/rtaudio/audio_driver_rtaudio.cpp | 22 + drivers/rtaudio/audio_driver_rtaudio.h | 5 + drivers/wasapi/audio_driver_wasapi.cpp | 380 ++++++++++++++++-- drivers/wasapi/audio_driver_wasapi.h | 25 +- drivers/xaudio2/audio_driver_xaudio2.cpp | 22 + drivers/xaudio2/audio_driver_xaudio2.h | 5 + platform/android/audio_driver_jandroid.cpp | 22 + platform/android/audio_driver_jandroid.h | 5 + platform/android/audio_driver_opensl.cpp | 22 + platform/android/audio_driver_opensl.h | 5 + servers/audio/audio_driver_dummy.cpp | 22 + servers/audio/audio_driver_dummy.h | 5 + servers/audio/audio_stream.cpp | 123 ++++++ servers/audio/audio_stream.h | 60 +++ servers/audio_server.cpp | 135 +++++++ servers/audio_server.h | 145 ++++++- servers/register_server_types.cpp | 1 + 23 files changed, 1058 insertions(+), 27 deletions(-) diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index a44a11a46d25..29b120792735 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -339,6 +339,28 @@ void AudioDriverALSA::finish() { finish_device(); } +bool AudioDriverALSA::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverALSA::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverALSA::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverALSA::capture_device_get_default_name() { + + return ""; +} + AudioDriverALSA::AudioDriverALSA() { mutex = NULL; diff --git a/drivers/alsa/audio_driver_alsa.h b/drivers/alsa/audio_driver_alsa.h index e2a2325cf368..59bc7c496847 100644 --- a/drivers/alsa/audio_driver_alsa.h +++ b/drivers/alsa/audio_driver_alsa.h @@ -83,6 +83,11 @@ class AudioDriverALSA : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + AudioDriverALSA(); ~AudioDriverALSA(); }; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index ac21de91e472..e13b81ddabc4 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -434,6 +434,28 @@ void AudioDriverCoreAudio::finish() { } }; +bool AudioDriverCoreAudio::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverCoreAudio::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverCoreAudio::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverCoreAudio::capture_device_get_default_name() { + + return ""; +} + AudioDriverCoreAudio::AudioDriverCoreAudio() { active = false; mutex = NULL; diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 99c910498eaf..0d3efca28d83 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -86,6 +86,11 @@ class AudioDriverCoreAudio : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + bool try_lock(); void stop(); diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 6db0e58737b0..49d3ab207041 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -510,6 +510,28 @@ void AudioDriverPulseAudio::finish() { thread = NULL; } +bool AudioDriverPulseAudio::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverPulseAudio::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverPulseAudio::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverPulseAudio::capture_device_get_default_name() { + + return ""; +} + AudioDriverPulseAudio::AudioDriverPulseAudio() { pa_ml = NULL; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index b471f5f9d502..bfbd340e26b5 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -98,6 +98,11 @@ class AudioDriverPulseAudio : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + virtual float get_latency(); AudioDriverPulseAudio(); diff --git a/drivers/rtaudio/audio_driver_rtaudio.cpp b/drivers/rtaudio/audio_driver_rtaudio.cpp index 365788e19272..0fad96f13358 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.cpp +++ b/drivers/rtaudio/audio_driver_rtaudio.cpp @@ -194,6 +194,28 @@ void AudioDriverRtAudio::finish() { } } +bool AudioDriverRtAudio::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverRtAudio::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverRtAudio::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverRtAudio::capture_device_get_default_name() { + + return ""; +} + AudioDriverRtAudio::AudioDriverRtAudio() { active = false; diff --git a/drivers/rtaudio/audio_driver_rtaudio.h b/drivers/rtaudio/audio_driver_rtaudio.h index 2a64652d5f34..789a21ee328d 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.h +++ b/drivers/rtaudio/audio_driver_rtaudio.h @@ -58,6 +58,11 @@ class AudioDriverRtAudio : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + AudioDriverRtAudio(); }; diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 5982955c4f9e..db09a61066c1 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -32,6 +32,8 @@ #include "audio_driver_wasapi.h" +#include + #include "os/os.h" #include "project_settings.h" @@ -52,8 +54,20 @@ const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); const IID IID_IAudioClient = __uuidof(IAudioClient); const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); +const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient); + +#define SAFE_RELEASE(memory) \ + if ((memory) != NULL) { \ + (memory)->Release(); \ + (memory) = NULL; \ + } -static bool default_device_changed = false; +#define REFTIMES_PER_SEC 10000000 +#define REFTIMES_PER_MILLISEC 10000 + +static StringName capture_device_id; +static bool default_render_device_changed = false; +static bool default_capture_device_changed = false; class CMMNotificationClient : public IMMNotificationClient { LONG _cRef; @@ -109,8 +123,13 @@ class CMMNotificationClient : public IMMNotificationClient { } HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) { - if (flow == eRender && role == eConsole) { - default_device_changed = true; + if (role == eConsole) { + if (flow == eRender) { + default_render_device_changed = true; + } else if (flow == eCapture) { + default_capture_device_changed = true; + capture_device_id = String(pwstrDeviceId); + } } return S_OK; @@ -123,7 +142,7 @@ class CMMNotificationClient : public IMMNotificationClient { static CMMNotificationClient notif_client; -Error AudioDriverWASAPI::init_device(bool reinit) { +Error AudioDriverWASAPI::init_render_device(bool reinit) { WAVEFORMATEX *pwfex; IMMDeviceEnumerator *enumerator = NULL; @@ -200,11 +219,15 @@ Error AudioDriverWASAPI::init_device(bool reinit) { } hr = enumerator->RegisterEndpointNotificationCallback(¬if_client); + SAFE_RELEASE(enumerator) + if (hr != S_OK) { ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error"); } hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client); + SAFE_RELEASE(device) + if (reinit) { if (hr != S_OK) { return ERR_CANT_OPEN; @@ -288,10 +311,181 @@ Error AudioDriverWASAPI::init_device(bool reinit) { print_line("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); } + // Free memory + CoTaskMemFree(pwfex); + return OK; } -Error AudioDriverWASAPI::finish_device() { +StringName AudioDriverWASAPI::get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator) { + // Setup default device + IMMDevice *default_device = NULL; + LPWSTR pwszID = NULL; + IPropertyStore *props = NULL; + + HRESULT hr = p_enumerator->GetDefaultAudioEndpoint( + eCapture, eConsole, &default_device); + ERR_FAIL_COND_V(hr != S_OK, ""); + + // Get the device ID + hr = default_device->GetId(&pwszID); + ERR_FAIL_COND_V(hr != S_OK, ""); + + // Get the device properties + hr = default_device->OpenPropertyStore( + STGM_READ, &props); + ERR_FAIL_COND_V(hr != S_OK, ""); + + PROPVARIANT var_name; + PropVariantInit(&var_name); + + // Get the name of the device + hr = props->GetValue(PKEY_Device_FriendlyName, &var_name); + ERR_FAIL_COND_V(hr != S_OK, ""); + + // Return the name of device + return String(var_name.pwszVal); +} + +Error AudioDriverWASAPI::init_capture_devices(bool reinit) { + + WAVEFORMATEX *pwfex; + IMMDeviceEnumerator *enumerator = NULL; + IMMDeviceCollection *device_collection = NULL; + IPropertyStore *props = NULL; + + capture_device_id_map.clear(); + + HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + capture_device_default_name = get_default_capture_device_name(enumerator); + + // Enumerate a collection of valid devices + hr = enumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &device_collection); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + SAFE_RELEASE(enumerator); + + UINT count; + hr = device_collection->GetCount(&count); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Loop through the device count + for (unsigned int i = 0; i < count; i++) { + IMMDevice *device = NULL; + LPWSTR pwszID = NULL; + + // Get the device + hr = device_collection->Item(i, &device); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Get the device ID + hr = device->GetId(&pwszID); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Get the device properties + hr = device->OpenPropertyStore(STGM_READ, &props); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + PROPVARIANT var_name; + PropVariantInit(&var_name); + + // Get the name of the device + hr = props->GetValue(PKEY_Device_FriendlyName, &var_name); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Save the name of device + StringName name = String(var_name.pwszVal); + + // DEBUG: print the device name and ID + printf("Endpoint %d: \"%S\" (%S)\n", i, var_name.pwszVal, pwszID); + + capture_device_id_map[StringName(pwszID)] = name; + + // Cleanup the ID and properties + CoTaskMemFree(pwszID); + pwszID = NULL; + PropVariantClear(&var_name); + SAFE_RELEASE(props) + + // Create a new audio in block descriptor + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = memnew(MicrophoneDeviceOutputDirectWASAPI); + microphone_device_output_wasapi->name = name; + microphone_device_output_wasapi->active = false; + + // Push it into the list and assign it to the hash map for quick access + microphone_device_outputs.push_back(microphone_device_output_wasapi); + microphone_device_output_map[name] = microphone_device_output_wasapi; + + // Activate the device + hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)µphone_device_output_wasapi->audio_client); + SAFE_RELEASE(device) + + // Get the sample rate (hz) + hr = microphone_device_output_wasapi->audio_client->GetMixFormat(&pwfex); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + microphone_device_output_wasapi->channels = pwfex->nChannels; + microphone_device_output_wasapi->mix_rate = pwfex->nSamplesPerSec; + microphone_device_output_wasapi->bits_per_sample = pwfex->wBitsPerSample; + microphone_device_output_wasapi->frame_size = (microphone_device_output_wasapi->bits_per_sample / 8) * microphone_device_output_wasapi->channels; + + microphone_device_output_wasapi->current_capture_index = 0; + + if (pwfex->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex; + + if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) { + microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_PCM; + } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) { + microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_FLOAT; + } else { + ERR_PRINT("WASAPI: Format not supported"); + ERR_FAIL_V(ERR_CANT_OPEN); + } + } else { + if (pwfex->wFormatTag != WAVE_FORMAT_PCM && pwfex->wFormatTag != WAVE_FORMAT_IEEE_FLOAT) { + ERR_PRINT("WASAPI: Format not supported"); + ERR_FAIL_V(ERR_CANT_OPEN); + } else { + if (pwfex->wFormatTag == WAVE_FORMAT_PCM) { + microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_PCM; + } else { + microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_FLOAT; + } + } + } + + hr = microphone_device_output_wasapi->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, REFTIMES_PER_SEC, 0, pwfex, NULL); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Get the max frames + UINT32 max_frames; + hr = microphone_device_output_wasapi->audio_client->GetBufferSize(&max_frames); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // Set the buffer size + microphone_device_output_wasapi->buffer.resize(max_frames * 10); // 10 second test buffer (will crash after it's been filled due to lack of looping) + memset(microphone_device_output_wasapi->buffer.ptrw(), 0x00, microphone_device_output_wasapi->buffer.size() * microphone_device_output_wasapi->frame_size); + + // Get the capture client + hr = microphone_device_output_wasapi->audio_client->GetService(IID_IAudioCaptureClient, (void **)µphone_device_output_wasapi->capture_client); + ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + + // TODO: set audio write stream to correct format + REFERENCE_TIME hns_actual_duration = (double)REFTIMES_PER_SEC * max_frames / pwfex->nSamplesPerSec; + + // Free memory + CoTaskMemFree(pwfex); + SAFE_RELEASE(device) + } + SAFE_RELEASE(device_collection) + + return OK; +} + +Error AudioDriverWASAPI::finish_render_device() { if (audio_client) { if (active) { @@ -303,14 +497,21 @@ Error AudioDriverWASAPI::finish_device() { audio_client = NULL; } - if (render_client) { - render_client->Release(); - render_client = NULL; - } + SAFE_RELEASE(render_client) + SAFE_RELEASE(audio_client) - if (audio_client) { - audio_client->Release(); - audio_client = NULL; + return OK; +} + +Error AudioDriverWASAPI::finish_capture_devices() { + + microphone_device_output_map.clear(); + while (microphone_device_outputs.size() > 0) { + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output = static_cast(microphone_device_outputs.get(0)); + SAFE_RELEASE(microphone_device_output->capture_client) + SAFE_RELEASE(microphone_device_output->audio_client) + microphone_device_outputs.erase(microphone_device_output); + memdelete(microphone_device_output); } return OK; @@ -320,9 +521,14 @@ Error AudioDriverWASAPI::init() { mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); - Error err = init_device(); + Error err = init_render_device(); if (err != OK) { - ERR_PRINT("WASAPI: init_device error"); + ERR_PRINT("WASAPI: init_render_device error"); + } + + err = init_capture_devices(); + if (err != OK) { + ERR_PRINT("WASAPI: init_capture_device error"); } active = false; @@ -439,6 +645,78 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata; while (!ad->exit_thread) { + // Capture + + if (default_capture_device_changed) { + if (ad->capture_device_id_map.has(capture_device_id)) { + Map::Element *e = ad->capture_device_id_map.find(capture_device_id); + ad->lock(); + ad->start_counting_ticks(); + + ad->capture_device_default_name = e->get(); + ad->update_microphone_default(ad->capture_device_default_name); + + default_capture_device_changed = false; + + ad->stop_counting_ticks(); + ad->unlock(); + } + } + + for (int i = 0; i < ad->microphone_device_outputs.size(); i++) { + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(ad->microphone_device_outputs[i]); + + if (microphone_device_output_wasapi->active == false) { + continue; + } + + UINT32 packet_length = 0; + BYTE *data; + UINT32 num_frames_available; + DWORD flags; + + HRESULT hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length); + ERR_BREAK(hr != S_OK); + + while (packet_length != 0) { + hr = microphone_device_output_wasapi->capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL); + ERR_BREAK(hr != S_OK); + + unsigned int frames_to_copy = num_frames_available; + + if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { + memset((char *)(microphone_device_output_wasapi->buffer.ptrw()) + (microphone_device_output_wasapi->current_capture_index * microphone_device_output_wasapi->frame_size), 0, frames_to_copy * microphone_device_output_wasapi->frame_size); + } else { + // fixme: Only works for floating point atm + if (microphone_device_output_wasapi->channels == 2) { + for (int j = 0; j < frames_to_copy; j++) { + float left = *(((float *)data) + (j * 2)); + float right = *(((float *)data) + (j * 2) + 1); + microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index + j] = AudioFrame(left, right); + } + } else if (microphone_device_output_wasapi->channels == 1) { + for (int j = 0; j < frames_to_copy; j++) { + float value = *(((float *)data) + j); + microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index + j] = AudioFrame(value, value); + } + } else { + ERR_PRINT("WASAPI: unsupported channel count in microphone!"); + } + } + + hr = microphone_device_output_wasapi->capture_client->ReleaseBuffer(num_frames_available); + ERR_BREAK(hr != S_OK); + + hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length); + ERR_BREAK(hr != S_OK); + + microphone_device_output_wasapi->current_capture_index += frames_to_copy; + + // Test: ensuring the read index is always behind the capture index keeps the input and output reliably in sync, but it + // also results in clipping, stutter and other audio artefacts + microphone_device_output_wasapi->set_read_index(microphone_device_output_wasapi->current_capture_index - 8192); + } + } ad->lock(); ad->start_counting_ticks(); @@ -514,9 +792,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { // Device is not valid anymore WARN_PRINT("WASAPI: Current device invalidated, closing device"); - Error err = ad->finish_device(); + Error err = ad->finish_render_device(); if (err != OK) { - ERR_PRINT("WASAPI: finish_device error"); + ERR_PRINT("WASAPI: finish_render_device error"); } } @@ -528,26 +806,26 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { ad->start_counting_ticks(); // If we're using the Default device and it changed finish it so we'll re-init the device - if (ad->device_name == "Default" && default_device_changed) { - Error err = ad->finish_device(); + if (ad->device_name == "Default" && default_render_device_changed) { + Error err = ad->finish_render_device(); if (err != OK) { - ERR_PRINT("WASAPI: finish_device error"); + ERR_PRINT("WASAPI: finish_render_device error"); } - default_device_changed = false; + default_render_device_changed = false; } // User selected a new device, finish the current one so we'll init the new device if (ad->device_name != ad->new_device) { ad->device_name = ad->new_device; - Error err = ad->finish_device(); + Error err = ad->finish_render_device(); if (err != OK) { - ERR_PRINT("WASAPI: finish_device error"); + ERR_PRINT("WASAPI: finish_render_device error"); } } if (!ad->audio_client) { - Error err = ad->init_device(true); + Error err = ad->init_render_device(true); if (err == OK) { ad->start(); } @@ -594,7 +872,8 @@ void AudioDriverWASAPI::finish() { thread = NULL; } - finish_device(); + finish_capture_devices(); + finish_render_device(); if (mutex) { memdelete(mutex); @@ -602,6 +881,58 @@ void AudioDriverWASAPI::finish() { } } +bool AudioDriverWASAPI::capture_device_start(StringName p_name) { + + if (microphone_device_output_map.has(p_name)) { + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_output_map[p_name]); + if (microphone_device_output_wasapi->active == false) { + microphone_device_output_wasapi->audio_client->Start(); + microphone_device_output_wasapi->active = true; + microphone_device_output_wasapi->set_read_index(-2048); + } + + return true; + } + + return false; +} + +bool AudioDriverWASAPI::capture_device_stop(StringName p_name) { + + if (microphone_device_output_map.has(p_name)) { + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_output_map[p_name]); + if (microphone_device_output_wasapi->active == true) { + microphone_device_output_wasapi->audio_client->Stop(); + microphone_device_output_wasapi->active = false; + } + + return true; + } + + return false; +} + +PoolStringArray AudioDriverWASAPI::capture_device_get_names() { + + PoolStringArray names; + + for (int i = 0; i < microphone_device_outputs.size(); i++) { + MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_outputs.get(i)); + names.push_back(microphone_device_output_wasapi->name); + } + + return names; +} + +StringName AudioDriverWASAPI::capture_device_get_default_name() { + + lock(); + StringName capture_device_default_name_local = capture_device_default_name; + unlock(); + + return capture_device_default_name_local; +} + AudioDriverWASAPI::AudioDriverWASAPI() { audio_client = NULL; @@ -626,6 +957,7 @@ AudioDriverWASAPI::AudioDriverWASAPI() { device_name = "Default"; new_device = "Default"; + capture_device_default_name = ""; } #endif diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index f3ee5976eb24..084d0c2e3fde 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -44,19 +44,30 @@ class AudioDriverWASAPI : public AudioDriver { HANDLE event; + // Audio out IAudioClient *audio_client; IAudioRenderClient *render_client; + // Microphone + class MicrophoneDeviceOutputDirectWASAPI : public MicrophoneDeviceOutputDirect { + public: + IAudioClient *audio_client; + IAudioCaptureClient *capture_client; + }; + // Mutex *mutex; Thread *thread; String device_name; String new_device; + String capture_device_default_name; WORD format_tag; WORD bits_per_sample; Vector samples_in; + Map capture_device_id_map; + unsigned int buffer_size; unsigned int channels; unsigned int wasapi_channels; @@ -70,8 +81,13 @@ class AudioDriverWASAPI : public AudioDriver { _FORCE_INLINE_ void write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample); static void thread_func(void *p_udata); - Error init_device(bool reinit = false); - Error finish_device(); + StringName get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator); + + Error init_render_device(bool reinit = false); + Error init_capture_devices(bool reinit = false); + + Error finish_render_device(); + Error finish_capture_devices(); public: virtual const char *get_name() const { @@ -89,6 +105,11 @@ class AudioDriverWASAPI : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + AudioDriverWASAPI(); }; diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index a1002ef4f924..2a320b620e7c 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -210,6 +210,28 @@ void AudioDriverXAudio2::finish() { thread = NULL; }; +bool AudioDriverXAudio2::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverXAudio2::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverXAudio2::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverXAudio2::capture_device_get_default_name() { + + return ""; +} + AudioDriverXAudio2::AudioDriverXAudio2() { mutex = NULL; diff --git a/drivers/xaudio2/audio_driver_xaudio2.h b/drivers/xaudio2/audio_driver_xaudio2.h index 42e1adb2b735..b78ab136da3d 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.h +++ b/drivers/xaudio2/audio_driver_xaudio2.h @@ -103,6 +103,11 @@ class AudioDriverXAudio2 : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + AudioDriverXAudio2(); ~AudioDriverXAudio2(); }; diff --git a/platform/android/audio_driver_jandroid.cpp b/platform/android/audio_driver_jandroid.cpp index b9f1f1eab05b..561ad106e467 100644 --- a/platform/android/audio_driver_jandroid.cpp +++ b/platform/android/audio_driver_jandroid.cpp @@ -195,6 +195,28 @@ void AudioDriverAndroid::finish() { active = false; } +bool AudioDriverAndroid::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverAndroid::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverAndroid::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverAndroid::capture_device_get_default_name() { + + return ""; +} + void AudioDriverAndroid::set_pause(bool p_pause) { JNIEnv *env = ThreadAndroid::get_env(); diff --git a/platform/android/audio_driver_jandroid.h b/platform/android/audio_driver_jandroid.h index 763f0e9b5a70..a54f0cae6a02 100644 --- a/platform/android/audio_driver_jandroid.h +++ b/platform/android/audio_driver_jandroid.h @@ -70,6 +70,11 @@ class AudioDriverAndroid : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + virtual void set_pause(bool p_pause); static void setup(jobject p_io); diff --git a/platform/android/audio_driver_opensl.cpp b/platform/android/audio_driver_opensl.cpp index 28e3ea962fd4..38f19d41e9d4 100644 --- a/platform/android/audio_driver_opensl.cpp +++ b/platform/android/audio_driver_opensl.cpp @@ -249,6 +249,28 @@ void AudioDriverOpenSL::finish() { (*sl)->Destroy(sl); } +bool AudioDriverOpenSL::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverOpenSL::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverOpenSL::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverOpenSL::capture_device_get_default_name() { + + return ""; +} + void AudioDriverOpenSL::set_pause(bool p_pause) { pause = p_pause; diff --git a/platform/android/audio_driver_opensl.h b/platform/android/audio_driver_opensl.h index 88cb12241470..f3f193973385 100644 --- a/platform/android/audio_driver_opensl.h +++ b/platform/android/audio_driver_opensl.h @@ -98,6 +98,11 @@ class AudioDriverOpenSL : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + virtual void set_pause(bool p_pause); AudioDriverOpenSL(); diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp index be36c3b748c3..7074dbca3aa4 100644 --- a/servers/audio/audio_driver_dummy.cpp +++ b/servers/audio/audio_driver_dummy.cpp @@ -125,6 +125,28 @@ void AudioDriverDummy::finish() { thread = NULL; }; +bool AudioDriverDummy::capture_device_start(StringName p_name) { + + return false; +} + +bool AudioDriverDummy::capture_device_stop(StringName p_name) { + + return false; +} + +PoolStringArray AudioDriverDummy::capture_device_get_names() { + + PoolStringArray names; + + return names; +} + +StringName AudioDriverDummy::capture_device_get_default_name() { + + return ""; +} + AudioDriverDummy::AudioDriverDummy() { mutex = NULL; diff --git a/servers/audio/audio_driver_dummy.h b/servers/audio/audio_driver_dummy.h index ba4f69edf45e..48e8c731ea63 100644 --- a/servers/audio/audio_driver_dummy.h +++ b/servers/audio/audio_driver_dummy.h @@ -68,6 +68,11 @@ class AudioDriverDummy : public AudioDriver { virtual void unlock(); virtual void finish(); + virtual bool capture_device_start(StringName p_name); + virtual bool capture_device_stop(StringName p_name); + virtual PoolStringArray capture_device_get_names(); + virtual StringName capture_device_get_default_name(); + AudioDriverDummy(); ~AudioDriverDummy(); }; diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 113f23f8f224..8efcb5bf0734 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -99,6 +99,129 @@ void AudioStream::_bind_methods() { //////////////////////////////// +Ref AudioStreamMicrophone::instance_playback() { + Ref playback; + playback.instance(); + + playbacks.insert(playback.ptr()); + + playback->microphone = Ref((AudioStreamMicrophone *)this); + playback->active = false; + + return playback; +} + +String AudioStreamMicrophone::get_stream_name() const { + + //if (audio_stream.is_valid()) { + //return "Random: " + audio_stream->get_name(); + //} + return "Microphone"; +} + +void AudioStreamMicrophone::set_microphone_name(const String &p_name) { + if (microphone_name != p_name) { + microphone_name = p_name; + + for (Set::Element *E = playbacks.front(); E; E = E->next()) { + if (E->get()->active) { + // Is this the right thing to do? + E->get()->stop(); + E->get()->start(); + } + } + } +} + +StringName AudioStreamMicrophone::get_microphone_name() const { + return microphone_name; +} + +float AudioStreamMicrophone::get_length() const { + return 0; +} + +void AudioStreamMicrophone::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_microphone_name", "name"), &AudioStreamMicrophone::set_microphone_name); + ClassDB::bind_method(D_METHOD("get_microphone_name"), &AudioStreamMicrophone::get_microphone_name); + + ADD_PROPERTY(PropertyInfo(Variant::STRING, "microphone_name"), "set_microphone_name", "get_microphone_name"); +} + +AudioStreamMicrophone::AudioStreamMicrophone() { +} + +void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) { + + AudioDriver::MicrophoneDeviceOutput *microphone_device_output = reciever->owner; + const Vector &source_buffer = microphone_device_output->get_buffer(); + + if (microphone_device_output->get_read_index() >= 0) { + for (int i = 0; i < p_frames; i++) { + p_buffer[i] = source_buffer[internal_mic_offset + microphone_device_output->get_read_index() + i]; + } + } + + internal_mic_offset += p_frames; +} + +void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { + AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames); + internal_mic_offset = 0; // Reset +} + +float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() { + return reciever->owner->get_mix_rate(); +} + +void AudioStreamPlaybackMicrophone::start(float p_from_pos) { + active = true; + + // note: can this be called twice? + reciever = AudioServer::get_singleton()->create_microphone_reciever(microphone->get_microphone_name()); + if (reciever == NULL) { + active = false; + } + + _begin_resample(); +} + +void AudioStreamPlaybackMicrophone::stop() { + active = false; + if (reciever != NULL) { + AudioServer::get_singleton()->destroy_microphone_reciever(reciever); + reciever = NULL; + } +} + +bool AudioStreamPlaybackMicrophone::is_playing() const { + return active; +} + +int AudioStreamPlaybackMicrophone::get_loop_count() const { + return 0; +} + +float AudioStreamPlaybackMicrophone::get_playback_position() const { + return 0; +} + +void AudioStreamPlaybackMicrophone::seek(float p_time) { + return; // Can't seek a microphone input +} + +AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() { + microphone->playbacks.erase(this); + stop(); +} + +AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() { + internal_mic_offset = 0; + reciever = NULL; +} + +//////////////////////////////// + void AudioStreamRandomPitch::set_audio_stream(const Ref &p_audio_stream) { audio_stream = p_audio_stream; diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 3312ce1ff6bb..cb3b999cebb7 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -94,6 +94,66 @@ class AudioStream : public Resource { virtual float get_length() const = 0; //if supported, otherwise return 0 }; +// Microphone + +class AudioStreamPlaybackMicrophone; + +class AudioStreamMicrophone : public AudioStream { + + GDCLASS(AudioStreamMicrophone, AudioStream) + friend class AudioStreamPlaybackMicrophone; + + Set playbacks; + StringName microphone_name; + +protected: + static void _bind_methods(); + +public: + virtual Ref instance_playback(); + virtual String get_stream_name() const; + + void set_microphone_name(const String &p_name); + StringName get_microphone_name() const; + + virtual float get_length() const; //if supported, otherwise return 0 + + AudioStreamMicrophone(); +}; + +class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled { + + GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlayback) + friend class AudioStreamMicrophone; + + bool active; + uint64_t internal_mic_offset; + + Ref microphone; + AudioDriver::MicrophoneReciever *reciever; + +protected: + virtual void _mix_internal(AudioFrame *p_buffer, int p_frames); + virtual float get_stream_sampling_rate(); + +public: + virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames); + + virtual void start(float p_from_pos = 0.0); + virtual void stop(); + virtual bool is_playing() const; + + virtual int get_loop_count() const; //times it looped + + virtual float get_playback_position() const; + virtual void seek(float p_time); + + ~AudioStreamPlaybackMicrophone(); + AudioStreamPlaybackMicrophone(); +}; + +// + class AudioStreamPlaybackRandomPitch; class AudioStreamRandomPitch : public AudioStream { diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 2eaa2ce8e72a..10fa69a12ee2 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -33,6 +33,7 @@ #include "os/file_access.h" #include "os/os.h" #include "project_settings.h" +#include "scene/resources/audio_stream_sample.h" #include "servers/audio/audio_driver_dummy.h" #include "servers/audio/effects/audio_effect_compressor.h" #ifdef TOOLS_ENABLED @@ -72,6 +73,42 @@ void AudioDriver::update_mix_time(int p_frames) { _last_mix_time = OS::get_singleton()->get_ticks_usec(); } +void AudioDriver::update_microphone_default(StringName p_device_name) { + if (default_microphone_device_output != NULL) { + MicrophoneDeviceOutput *output = default_microphone_device_output->owner; + output->remove_reciever(default_microphone_device_output); + + while (output != NULL) { + MicrophoneDeviceOutput *owner = output->owner; + if (output->recievers.size() == 0) { + if (owner == NULL) { + if (output->active == true) { + capture_device_stop(output->name); + output->active == false; + } + } else { + owner->remove_reciever(output); + memdelete(output); + } + output = owner; + } + } + + if (microphone_device_output_map.has(p_device_name)) { + + Map::Element *e = microphone_device_output_map.find(p_device_name); + MicrophoneDeviceOutput *new_output = e->get(); + new_output->add_reciever(default_microphone_device_output); + if (new_output->active == false) { + capture_device_start(p_device_name); + new_output->active = true; + } + } + + output = default_microphone_device_output->owner; + } +} + double AudioDriver::get_mix_time() const { double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0; @@ -101,6 +138,74 @@ int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_m ERR_FAIL_V(2); } +AudioDriver::MicrophoneReciever *AudioDriver::create_microphone_reciever(const StringName &p_device_name) { + + MicrophoneReciever *microphone_reciever = NULL; + MicrophoneDeviceOutput *reciever_output = NULL; + MicrophoneDeviceOutput *device_output = NULL; + + StringName device_name = capture_device_get_default_name(); + + if (microphone_device_output_map.has(device_name)) { + + Map::Element *e = microphone_device_output_map.find(device_name); + device_output = e->get(); + } + + if (device_output) { + if (p_device_name == "") { + if (default_microphone_device_output != NULL) { + reciever_output = default_microphone_device_output; + } else { + // Default reciever does not exist, create it and connect it + default_microphone_device_output = memnew(MicrophoneDeviceOutputIndirect); + reciever_output = default_microphone_device_output; + device_output->add_reciever(reciever_output); + } + } else { + if (microphone_device_output_map.has(p_device_name)) { + reciever_output = device_output; + } + } + + if (reciever_output) { + microphone_reciever = memnew(MicrophoneReciever); + reciever_output->add_reciever(microphone_reciever); + if (device_output->active == false) { + capture_device_start(device_name); + device_output->active = true; + } + } + } + + return microphone_reciever; +} + +void AudioDriver::destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever) { + + if (p_microphone_reciever != NULL) { + MicrophoneDeviceOutput *output = p_microphone_reciever->owner; + output->remove_reciever(p_microphone_reciever); + + while (output != NULL) { + MicrophoneDeviceOutput *owner = output->owner; + if (output->recievers.size() == 0) { + if (owner == NULL) { + if (output->active == true) { + capture_device_stop(output->name); + output->active == false; + } + } else { + owner->remove_reciever(output); + memdelete(output); + } + output = owner; + } + } + memdelete(p_microphone_reciever); + } +} + Array AudioDriver::get_device_list() { Array list; @@ -118,6 +223,8 @@ AudioDriver::AudioDriver() { _last_mix_time = 0; _mix_amount = 0; + default_microphone_device_output = NULL; + #ifdef DEBUG_ENABLED prof_time = 0; #endif @@ -1201,6 +1308,34 @@ void AudioServer::set_device(String device) { AudioDriver::get_singleton()->set_device(device); } +PoolStringArray AudioServer::audio_in_get_device_names() { + + lock(); + PoolStringArray device_names = AudioDriver::get_singleton()->capture_device_get_names(); + unlock(); + + return device_names; +} + +AudioDriver::MicrophoneReciever *AudioServer::create_microphone_reciever(const StringName &p_device_name) { + AudioDriver::MicrophoneReciever *microphone_reciever = NULL; + + lock(); + microphone_reciever = AudioDriver::get_singleton()->create_microphone_reciever(p_device_name); + unlock(); + + return microphone_reciever; +} + +void AudioServer::destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever) { + lock(); + AudioDriver::get_singleton()->destroy_microphone_reciever(p_microphone_reciever); + unlock(); +} + +void AudioServer::_change_default_device(StringName p_recording_device_default_name) { +} + void AudioServer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_bus_count", "amount"), &AudioServer::set_bus_count); diff --git a/servers/audio_server.h b/servers/audio_server.h index 258fd1d9b0f7..f2c3aa0a6f90 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -38,6 +38,8 @@ #include "variant.h" class AudioDriverDummy; +class AudioStream; +class AudioStreamSample; class AudioDriver { @@ -53,6 +55,7 @@ class AudioDriver { protected: void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true); void update_mix_time(int p_frames); + void update_microphone_default(StringName p_device_name); #ifdef DEBUG_ENABLED _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); } @@ -65,6 +68,121 @@ class AudioDriver { public: double get_mix_time() const; //useful for video -> audio sync + class MicrophoneDeviceOutput; + + class MicrophoneReciever { + public: + MicrophoneDeviceOutput *owner; + + MicrophoneReciever() { + owner = NULL; + } + + ~MicrophoneReciever() { + } + }; + + class MicrophoneDeviceOutput : public MicrophoneReciever { + public: + StringName name; + bool active; + Vector recievers; + + virtual unsigned int get_mix_rate() = 0; + virtual Vector &get_buffer() = 0; + virtual int get_read_index() = 0; + virtual void set_read_index(int p_temp_index) = 0; + + void add_reciever(MicrophoneReciever *p_reciever) { + if (p_reciever == NULL) { + ERR_PRINT("Attempted to add NULL reciever") + return; + } + if (recievers.find(p_reciever) == -1) { + recievers.push_back(p_reciever); + p_reciever->owner = this; + } else { + ERR_PRINT("Duplicate reciever added") + } + } + + void remove_reciever(MicrophoneReciever *p_reciever) { + if (p_reciever == NULL) { + ERR_PRINT("Attempted to remove NULL reciever") + return; + } + + int index = recievers.find(p_reciever); + + if (index != -1) { + recievers.remove(index); + p_reciever->owner = NULL; + } else { + ERR_PRINT("Attempted to remove invalid reciever") + } + } + }; + + class MicrophoneDeviceOutputDirect : public MicrophoneDeviceOutput { + public: + enum MicrophoneFormat { + FORMAT_FLOAT, + FORMAT_PCM + }; + + MicrophoneFormat microphone_format; + unsigned short bits_per_sample; + unsigned int channels; + unsigned int mix_rate; + unsigned short frame_size; + int read_index = -2048; + + unsigned int current_capture_index; + Vector buffer; + + unsigned int get_mix_rate() { + return mix_rate; + }; + + Vector &get_buffer() { + return buffer; + }; + + int get_read_index() { + return read_index; + } + + void set_read_index(int p_read_index) { + read_index = p_read_index; + } + }; + + class MicrophoneDeviceOutputIndirect : public MicrophoneDeviceOutput { + public: + unsigned int get_mix_rate() { + return owner->get_mix_rate(); + }; + + Vector &get_buffer() { + return owner->get_buffer(); + }; + + int get_read_index() { + return owner->get_read_index(); + } + + void set_read_index(int p_read_index) { + owner->set_read_index(p_read_index); + } + }; + + MicrophoneDeviceOutputIndirect *default_microphone_device_output; + + Vector microphone_device_outputs; + Map microphone_device_output_map; + + Vector direct_recievers; + enum SpeakerMode { SPEAKER_MODE_STEREO, SPEAKER_SURROUND_31, @@ -91,11 +209,19 @@ class AudioDriver { virtual void unlock() = 0; virtual void finish() = 0; + virtual bool capture_device_start(StringName p_name) = 0; + virtual bool capture_device_stop(StringName p_name) = 0; + virtual PoolStringArray capture_device_get_names() = 0; + virtual StringName capture_device_get_default_name() = 0; + virtual float get_latency() { return 0; } SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const; int get_total_channels_by_speaker_mode(SpeakerMode) const; + AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name); + void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever); + #ifdef DEBUG_ENABLED uint64_t get_profiling_time() const { return prof_time; } void reset_profiling_time() { prof_time = 0; } @@ -222,6 +348,18 @@ class AudioServer : public Object { void _mix_step(); +#if 0 + struct AudioInBlock { + + Ref audio_stream; + int current_position; + bool loops; + }; + + Map audio_in_block_map; + Vector audio_in_blocks; +#endif + struct CallbackItem { AudioCallback callback; @@ -236,6 +374,7 @@ class AudioServer : public Object { friend class AudioDriver; void _driver_process(int p_frames, int32_t *p_buffer); + void _change_default_device(StringName p_recording_device_default_name); protected: static void _bind_methods(); @@ -335,8 +474,12 @@ class AudioServer : public Object { String get_device(); void set_device(String device); - float get_output_latency() { return output_latency; } + AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name); + void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever); + + PoolStringArray audio_in_get_device_names(); + float get_output_latency() { return output_latency; } AudioServer(); virtual ~AudioServer(); }; diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index aa0e5c289b79..1dcfdb217490 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -104,6 +104,7 @@ void register_server_types() { ClassDB::register_virtual_class(); ClassDB::register_virtual_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_virtual_class(); ClassDB::register_class(); From 76fd9d215c25874b1c5d33355de0ed983922c32d Mon Sep 17 00:00:00 2001 From: Saracen Date: Thu, 24 May 2018 21:35:39 +0100 Subject: [PATCH 02/14] Fixes for microphone clipping and latency (marcelofg55) --- drivers/wasapi/audio_driver_wasapi.cpp | 88 ++++++++++++++++++-------- drivers/wasapi/audio_driver_wasapi.h | 2 + servers/audio/audio_stream.cpp | 16 +++-- servers/audio/audio_stream.h | 2 +- servers/audio_server.h | 5 ++ 5 files changed, 78 insertions(+), 35 deletions(-) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index db09a61066c1..8fe83a3be506 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -432,30 +432,27 @@ Error AudioDriverWASAPI::init_capture_devices(bool reinit) { microphone_device_output_wasapi->frame_size = (microphone_device_output_wasapi->bits_per_sample / 8) * microphone_device_output_wasapi->channels; microphone_device_output_wasapi->current_capture_index = 0; + microphone_device_output_wasapi->current_capture_size = 0; - if (pwfex->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + WORD format_tag = pwfex->wFormatTag; + if (format_tag == WAVE_FORMAT_EXTENSIBLE) { WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex; if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) { - microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_PCM; + format_tag = WAVE_FORMAT_PCM; } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) { - microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_FLOAT; + format_tag = WAVE_FORMAT_IEEE_FLOAT; } else { ERR_PRINT("WASAPI: Format not supported"); ERR_FAIL_V(ERR_CANT_OPEN); } } else { - if (pwfex->wFormatTag != WAVE_FORMAT_PCM && pwfex->wFormatTag != WAVE_FORMAT_IEEE_FLOAT) { + if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) { ERR_PRINT("WASAPI: Format not supported"); ERR_FAIL_V(ERR_CANT_OPEN); - } else { - if (pwfex->wFormatTag == WAVE_FORMAT_PCM) { - microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_PCM; - } else { - microphone_device_output_wasapi->microphone_format = MicrophoneDeviceOutputDirect::FORMAT_FLOAT; - } } } + microphone_device_output_wasapi->capture_format_tag = format_tag; hr = microphone_device_output_wasapi->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, REFTIMES_PER_SEC, 0, pwfex, NULL); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); @@ -466,7 +463,7 @@ Error AudioDriverWASAPI::init_capture_devices(bool reinit) { ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Set the buffer size - microphone_device_output_wasapi->buffer.resize(max_frames * 10); // 10 second test buffer (will crash after it's been filled due to lack of looping) + microphone_device_output_wasapi->buffer.resize(max_frames); memset(microphone_device_output_wasapi->buffer.ptrw(), 0x00, microphone_device_output_wasapi->buffer.size() * microphone_device_output_wasapi->frame_size); // Get the capture client @@ -611,6 +608,39 @@ void AudioDriverWASAPI::set_device(String device) { unlock(); } +float AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) { + if (format_tag == WAVE_FORMAT_PCM) { + int32_t sample = 0; + switch (bits_per_sample) { + case 8: + sample = int32_t(((int8_t *)buffer)[i]) << 24; + break; + + case 16: + sample = int32_t(((int16_t *)buffer)[i]) << 16; + break; + + case 24: + sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24; + sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16; + sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8; + break; + + case 32: + sample = ((int32_t *)buffer)[i]; + break; + } + + return (sample >> 16) / 32768.f; + } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) { + return ((float *)buffer)[i]; + } else { + ERR_PRINT("WASAPI: Unknown format tag"); + } + + return 0.f; +} + void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample) { if (ad->format_tag == WAVE_FORMAT_PCM) { switch (ad->bits_per_sample) { @@ -688,19 +718,27 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { memset((char *)(microphone_device_output_wasapi->buffer.ptrw()) + (microphone_device_output_wasapi->current_capture_index * microphone_device_output_wasapi->frame_size), 0, frames_to_copy * microphone_device_output_wasapi->frame_size); } else { // fixme: Only works for floating point atm - if (microphone_device_output_wasapi->channels == 2) { - for (int j = 0; j < frames_to_copy; j++) { - float left = *(((float *)data) + (j * 2)); - float right = *(((float *)data) + (j * 2) + 1); - microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index + j] = AudioFrame(left, right); + for (int j = 0; j < frames_to_copy; j++) { + float l, r; + + if (microphone_device_output_wasapi->channels == 2) { + l = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2); + r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2 + 1); + } else if (microphone_device_output_wasapi->channels == 1) { + l = r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j); + } else { + l = r = 0.f; + ERR_PRINT("WASAPI: unsupported channel count in microphone!"); } - } else if (microphone_device_output_wasapi->channels == 1) { - for (int j = 0; j < frames_to_copy; j++) { - float value = *(((float *)data) + j); - microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index + j] = AudioFrame(value, value); + + microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index++] = AudioFrame(l, r); + + if (microphone_device_output_wasapi->current_capture_index >= microphone_device_output_wasapi->buffer.size()) { + microphone_device_output_wasapi->current_capture_index = 0; + } + if (microphone_device_output_wasapi->current_capture_size < microphone_device_output_wasapi->buffer.size()) { + microphone_device_output_wasapi->current_capture_size++; } - } else { - ERR_PRINT("WASAPI: unsupported channel count in microphone!"); } } @@ -709,12 +747,6 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length); ERR_BREAK(hr != S_OK); - - microphone_device_output_wasapi->current_capture_index += frames_to_copy; - - // Test: ensuring the read index is always behind the capture index keeps the input and output reliably in sync, but it - // also results in clipping, stutter and other audio artefacts - microphone_device_output_wasapi->set_read_index(microphone_device_output_wasapi->current_capture_index - 8192); } } diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index 084d0c2e3fde..e722d85353ae 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -52,6 +52,7 @@ class AudioDriverWASAPI : public AudioDriver { public: IAudioClient *audio_client; IAudioCaptureClient *capture_client; + WORD capture_format_tag; }; // Mutex *mutex; @@ -79,6 +80,7 @@ class AudioDriverWASAPI : public AudioDriver { bool active; _FORCE_INLINE_ void write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample); + static _FORCE_INLINE_ float read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i); static void thread_func(void *p_udata); StringName get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator); diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 8efcb5bf0734..206f1861a35c 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -155,19 +155,22 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr AudioDriver::MicrophoneDeviceOutput *microphone_device_output = reciever->owner; const Vector &source_buffer = microphone_device_output->get_buffer(); + int current_buffer_size = microphone_device_output->get_current_buffer_size(); - if (microphone_device_output->get_read_index() >= 0) { - for (int i = 0; i < p_frames; i++) { - p_buffer[i] = source_buffer[internal_mic_offset + microphone_device_output->get_read_index() + i]; + for (int i = 0; i < p_frames; i++) { + if (current_buffer_size >= internal_mic_offset) { + if (internal_mic_offset >= source_buffer.size()) { + internal_mic_offset = 0; + } + p_buffer[i] = source_buffer[internal_mic_offset++]; + } else { + p_buffer[i] = AudioFrame(0.f, 0.f); } } - - internal_mic_offset += p_frames; } void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames); - internal_mic_offset = 0; // Reset } float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() { @@ -175,6 +178,7 @@ float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() { } void AudioStreamPlaybackMicrophone::start(float p_from_pos) { + internal_mic_offset = 0; active = true; // note: can this be called twice? diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index cb3b999cebb7..352cb8c5078a 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -127,7 +127,7 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled { friend class AudioStreamMicrophone; bool active; - uint64_t internal_mic_offset; + uint32_t internal_mic_offset; Ref microphone; AudioDriver::MicrophoneReciever *reciever; diff --git a/servers/audio_server.h b/servers/audio_server.h index f2c3aa0a6f90..68a56c38f37c 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -90,6 +90,7 @@ class AudioDriver { virtual unsigned int get_mix_rate() = 0; virtual Vector &get_buffer() = 0; + virtual int get_current_buffer_size() = 0; virtual int get_read_index() = 0; virtual void set_read_index(int p_temp_index) = 0; @@ -138,6 +139,7 @@ class AudioDriver { int read_index = -2048; unsigned int current_capture_index; + unsigned int current_capture_size; Vector buffer; unsigned int get_mix_rate() { @@ -148,6 +150,8 @@ class AudioDriver { return buffer; }; + int get_current_buffer_size() { return current_capture_size; } + int get_read_index() { return read_index; } @@ -174,6 +178,7 @@ class AudioDriver { void set_read_index(int p_read_index) { owner->set_read_index(p_read_index); } + int get_current_buffer_size() { return owner->get_current_buffer_size(); } }; MicrophoneDeviceOutputIndirect *default_microphone_device_output; From 061358d8385a78a32a30ac5acf5443c465c8ec61 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Tue, 3 Jul 2018 22:08:43 -0300 Subject: [PATCH 03/14] Modified Microphone implementation to handle only one device at a time (WIP) --- drivers/alsa/audio_driver_alsa.cpp | 22 - drivers/alsa/audio_driver_alsa.h | 5 - drivers/coreaudio/audio_driver_coreaudio.cpp | 81 ++- drivers/coreaudio/audio_driver_coreaudio.h | 14 +- .../pulseaudio/audio_driver_pulseaudio.cpp | 184 +++-- drivers/pulseaudio/audio_driver_pulseaudio.h | 10 +- drivers/rtaudio/audio_driver_rtaudio.cpp | 22 - drivers/rtaudio/audio_driver_rtaudio.h | 5 - drivers/wasapi/audio_driver_wasapi.cpp | 646 +++++++----------- drivers/wasapi/audio_driver_wasapi.h | 71 +- drivers/xaudio2/audio_driver_xaudio2.cpp | 22 - drivers/xaudio2/audio_driver_xaudio2.h | 5 - platform/android/audio_driver_jandroid.cpp | 22 - platform/android/audio_driver_jandroid.h | 5 - platform/android/audio_driver_opensl.cpp | 22 - platform/android/audio_driver_opensl.h | 5 - servers/audio/audio_driver_dummy.cpp | 22 - servers/audio/audio_driver_dummy.h | 5 - servers/audio/audio_stream.cpp | 67 +- servers/audio/audio_stream.h | 7 +- servers/audio_server.cpp | 137 +--- servers/audio_server.h | 145 +--- 22 files changed, 531 insertions(+), 993 deletions(-) diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index 29b120792735..a44a11a46d25 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -339,28 +339,6 @@ void AudioDriverALSA::finish() { finish_device(); } -bool AudioDriverALSA::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverALSA::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverALSA::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverALSA::capture_device_get_default_name() { - - return ""; -} - AudioDriverALSA::AudioDriverALSA() { mutex = NULL; diff --git a/drivers/alsa/audio_driver_alsa.h b/drivers/alsa/audio_driver_alsa.h index 59bc7c496847..e2a2325cf368 100644 --- a/drivers/alsa/audio_driver_alsa.h +++ b/drivers/alsa/audio_driver_alsa.h @@ -83,11 +83,6 @@ class AudioDriverALSA : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - AudioDriverALSA(); ~AudioDriverALSA(); }; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index e13b81ddabc4..261ba7809cf5 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -35,6 +35,7 @@ #include "os/os.h" #define kOutputBus 0 +#define kInputBus 1 #ifdef OSX_ENABLED OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID, @@ -117,6 +118,11 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); + strdesc.mChannelsPerFrame = 2; + + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc)); + ERR_FAIL_COND_V(result != noErr, FAILED); + int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) buffer_frames = closest_power_of_2(latency * mix_rate / 1000); @@ -126,8 +132,14 @@ Error AudioDriverCoreAudio::init() { ERR_FAIL_COND_V(result != noErr, FAILED); #endif - buffer_size = buffer_frames * channels; + unsigned int buffer_size = buffer_frames * channels; samples_in.resize(buffer_size); + input_buf.resize(buffer_size); + audio_input_buffer.resize(buffer_size * 8); + for (int i = 0; i < audio_input_buffer.size(); i++) { + audio_input_buffer.write[i] = 0; + } + audio_input_position = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("CoreAudio: detected " + itos(channels) + " channels"); @@ -141,6 +153,12 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); ERR_FAIL_COND_V(result != noErr, FAILED); + zeromem(&callback, sizeof(AURenderCallbackStruct)); + callback.inputProc = &AudioDriverCoreAudio::input_callback; + callback.inputProcRefCon = this; + result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback)); + ERR_FAIL_COND_V(result != noErr, FAILED); + result = AudioUnitInitialize(audio_unit); ERR_FAIL_COND_V(result != noErr, FAILED); @@ -192,6 +210,42 @@ OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon, return 0; }; +OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, + AudioUnitRenderActionFlags *ioActionFlags, + const AudioTimeStamp *inTimeStamp, + UInt32 inBusNumber, UInt32 inNumberFrames, + AudioBufferList *ioData) { + + AudioDriverCoreAudio *ad = (AudioDriverCoreAudio *)inRefCon; + if (!ad->active) { + return 0; + } + + ad->lock(); + + AudioBufferList bufferList; + bufferList.mNumberBuffers = 1; + bufferList.mBuffers[0].mData = ad->input_buf.ptrw(); + bufferList.mBuffers[0].mNumberChannels = 2; + bufferList.mBuffers[0].mDataByteSize = ad->input_buf.size() * sizeof(int16_t); + + OSStatus result = AudioUnitRender(ad->audio_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); + if (result == noErr) { + for (int i = 0; i < inNumberFrames * 2; i++) { + ad->audio_input_buffer.write[ad->audio_input_position++] = ad->input_buf[i] << 16; + if (ad->audio_input_position >= ad->audio_input_buffer.size()) { + ad->audio_input_position = 0; + } + } + } else { + ERR_PRINT(("AudioUnitRender failed, code: " + itos(result)).utf8().get_data()); + } + + ad->unlock(); + + return result; +} + void AudioDriverCoreAudio::start() { if (!active) { OSStatus result = AudioOutputUnitStart(audio_unit); @@ -434,26 +488,22 @@ void AudioDriverCoreAudio::finish() { } }; -bool AudioDriverCoreAudio::capture_device_start(StringName p_name) { +Error AudioDriverCoreAudio::capture_start() { - return false; -} - -bool AudioDriverCoreAudio::capture_device_stop(StringName p_name) { + UInt32 flag = 1; + OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); - return false; + return OK; } -PoolStringArray AudioDriverCoreAudio::capture_device_get_names() { - - PoolStringArray names; +Error AudioDriverCoreAudio::capture_stop() { - return names; -} - -StringName AudioDriverCoreAudio::capture_device_get_default_name() { + UInt32 flag = 0; + OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); - return ""; + return OK; } AudioDriverCoreAudio::AudioDriverCoreAudio() { @@ -463,7 +513,6 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() { mix_rate = 0; channels = 2; - buffer_size = 0; buffer_frames = 0; samples_in.clear(); diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 0d3efca28d83..7629e5668600 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -52,9 +52,9 @@ class AudioDriverCoreAudio : public AudioDriver { int mix_rate; unsigned int channels; unsigned int buffer_frames; - unsigned int buffer_size; Vector samples_in; + Vector input_buf; #ifdef OSX_ENABLED static OSStatus output_device_address_cb(AudioObjectID inObjectID, @@ -68,6 +68,12 @@ class AudioDriverCoreAudio : public AudioDriver { UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData); + static OSStatus input_callback(void *inRefCon, + AudioUnitRenderActionFlags *ioActionFlags, + const AudioTimeStamp *inTimeStamp, + UInt32 inBusNumber, UInt32 inNumberFrames, + AudioBufferList *ioData); + public: const char *get_name() const { return "CoreAudio"; @@ -86,10 +92,8 @@ class AudioDriverCoreAudio : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); + virtual Error capture_start(); + virtual Error capture_stop(); bool try_lock(); void stop(); diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 49d3ab207041..2b1a3366305e 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -287,74 +287,71 @@ float AudioDriverPulseAudio::get_latency() { void AudioDriverPulseAudio::thread_func(void *p_udata) { AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata; + unsigned int write_ofs = 0; + size_t avail_bytes = 0; while (!ad->exit_thread) { - ad->lock(); - ad->start_counting_ticks(); + size_t read_bytes = 0; + size_t written_bytes = 0; - if (!ad->active) { - for (unsigned int i = 0; i < ad->pa_buffer_size; i++) { - ad->samples_out.write[i] = 0; - } + if (avail_bytes == 0) { + ad->lock(); + ad->start_counting_ticks(); - } else { - ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw()); - - if (ad->channels == ad->pa_map.channels) { + if (!ad->active) { for (unsigned int i = 0; i < ad->pa_buffer_size; i++) { - ad->samples_out.write[i] = ad->samples_in[i] >> 16; + ad->samples_out.write[i] = 0; } } else { - // Uneven amount of channels - unsigned int in_idx = 0; - unsigned int out_idx = 0; + ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw()); - for (unsigned int i = 0; i < ad->buffer_frames; i++) { - for (unsigned int j = 0; j < ad->pa_map.channels - 1; j++) { - ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16; + if (ad->channels == ad->pa_map.channels) { + for (unsigned int i = 0; i < ad->pa_buffer_size; i++) { + ad->samples_out.write[i] = ad->samples_in[i] >> 16; + } + } else { + // Uneven amount of channels + unsigned int in_idx = 0; + unsigned int out_idx = 0; + + for (unsigned int i = 0; i < ad->buffer_frames; i++) { + for (unsigned int j = 0; j < ad->pa_map.channels - 1; j++) { + ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16; + } + uint32_t l = ad->samples_in[in_idx++]; + uint32_t r = ad->samples_in[in_idx++]; + ad->samples_out.write[out_idx++] = (l >> 1 + r >> 1) >> 16; } - uint32_t l = ad->samples_in[in_idx++]; - uint32_t r = ad->samples_in[in_idx++]; - ad->samples_out.write[out_idx++] = (l >> 1 + r >> 1) >> 16; } } + + avail_bytes = ad->pa_buffer_size * sizeof(int16_t); + write_ofs = 0; + ad->stop_counting_ticks(); + ad->unlock(); } - int error_code; - int byte_size = ad->pa_buffer_size * sizeof(int16_t); + ad->lock(); + ad->start_counting_ticks(); + int ret; do { ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL); } while (ret > 0); - if (pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) { - const void *ptr = ad->samples_out.ptr(); - while (byte_size > 0) { - size_t bytes = pa_stream_writable_size(ad->pa_str); - if (bytes > 0) { - if (bytes > byte_size) { - bytes = byte_size; - } - - ret = pa_stream_write(ad->pa_str, ptr, bytes, NULL, 0LL, PA_SEEK_RELATIVE); - if (ret >= 0) { - byte_size -= bytes; - ptr = (const char *)ptr + bytes; - } + if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) { + size_t bytes = pa_stream_writable_size(ad->pa_str); + if (bytes > 0) { + size_t bytes_to_write = MIN(bytes, avail_bytes); + const void *ptr = ad->samples_out.ptr(); + ret = pa_stream_write(ad->pa_str, ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE); + if (ret != 0) { + ERR_PRINT("pa_stream_write error"); } else { - ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL); - if (ret == 0) { - // If pa_mainloop_iterate returns 0 sleep for 1 msec to wait - // for the stream to be able to process more bytes - ad->stop_counting_ticks(); - ad->unlock(); - - OS::get_singleton()->delay_usec(1000); - - ad->lock(); - ad->start_counting_ticks(); - } + avail_bytes -= bytes_to_write; + write_ofs += bytes_to_write; + written_bytes += bytes_to_write; } } } @@ -379,8 +376,41 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { } } + if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) { + size_t bytes = pa_stream_readable_size(ad->pa_rec_str); + if (bytes > 0) { + const void *ptr = NULL; + size_t maxbytes = ad->audio_input_buffer.size() * sizeof(int16_t); + + bytes = MIN(bytes, maxbytes); + ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes); + if (ret != 0) { + ERR_PRINT("pa_stream_peek error"); + } else { + int16_t *srcptr = (int16_t *)ptr; + for (size_t i = bytes >> 1; i > 0; i--) { + ad->audio_input_buffer.write[ad->audio_input_position++] = int32_t(*srcptr++) << 16; + if (ad->audio_input_position >= ad->audio_input_buffer.size()) { + ad->audio_input_position = 0; + } + } + + read_bytes += bytes; + ret = pa_stream_drop(ad->pa_rec_str); + if (ret != 0) { + ERR_PRINT("pa_stream_drop error"); + } + } + } + } + ad->stop_counting_ticks(); ad->unlock(); + + // Let the thread rest a while if we haven't read or write anything + if (written_bytes == 0 && read_bytes == 0) { + OS::get_singleton()->delay_usec(1000); + } } ad->thread_exited = true; @@ -510,26 +540,60 @@ void AudioDriverPulseAudio::finish() { thread = NULL; } -bool AudioDriverPulseAudio::capture_device_start(StringName p_name) { +Error AudioDriverPulseAudio::capture_start() { - return false; -} + Error err = OK; -bool AudioDriverPulseAudio::capture_device_stop(StringName p_name) { + lock(); - return false; -} + pa_sample_spec spec; -PoolStringArray AudioDriverPulseAudio::capture_device_get_names() { + spec.format = PA_SAMPLE_S16LE; + spec.channels = 2; + spec.rate = mix_rate; + + int latency = 30; + input_buffer_frames = closest_power_of_2(latency * mix_rate / 1000); + int buffer_size = input_buffer_frames * spec.channels; - PoolStringArray names; + pa_buffer_attr attr; + attr.fragsize = buffer_size * sizeof(int16_t); + + pa_channel_map pa_rec_map; + pa_channel_map_init_stereo(&pa_rec_map); - return names; + pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map); + if (pa_rec_str == NULL) { + ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx)))); + ERR_FAIL_V(ERR_CANT_OPEN); + } + + pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE); + int error_code = pa_stream_connect_record(pa_rec_str, NULL, &attr, flags); + if (error_code < 0) { + ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code))); + err = ERR_CANT_OPEN; + } + + audio_input_buffer.resize(input_buffer_frames * 8); + for (int i = 0; i < audio_input_buffer.size(); i++) { + audio_input_buffer.write[i] = 0; + } + audio_input_position = 0; + + unlock(); + + return err; } -StringName AudioDriverPulseAudio::capture_device_get_default_name() { +Error AudioDriverPulseAudio::capture_stop() { + if (pa_rec_str) { + pa_stream_disconnect(pa_rec_str); + pa_stream_unref(pa_rec_str); + pa_rec_str = NULL; + } - return ""; + return OK; } AudioDriverPulseAudio::AudioDriverPulseAudio() { @@ -537,6 +601,7 @@ AudioDriverPulseAudio::AudioDriverPulseAudio() { pa_ml = NULL; pa_ctx = NULL; pa_str = NULL; + pa_rec_str = NULL; mutex = NULL; thread = NULL; @@ -550,6 +615,7 @@ AudioDriverPulseAudio::AudioDriverPulseAudio() { mix_rate = 0; buffer_frames = 0; + input_buffer_frames = 0; pa_buffer_size = 0; channels = 0; pa_ready = 0; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index bfbd340e26b5..67b99133fb4d 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -47,6 +47,7 @@ class AudioDriverPulseAudio : public AudioDriver { pa_mainloop *pa_ml; pa_context *pa_ctx; pa_stream *pa_str; + pa_stream *pa_rec_str; pa_channel_map pa_map; String device_name; @@ -58,6 +59,7 @@ class AudioDriverPulseAudio : public AudioDriver { unsigned int mix_rate; unsigned int buffer_frames; + unsigned int input_buffer_frames; unsigned int pa_buffer_size; int channels; int pa_ready; @@ -98,13 +100,11 @@ class AudioDriverPulseAudio : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - virtual float get_latency(); + virtual Error capture_start(); + virtual Error capture_stop(); + AudioDriverPulseAudio(); ~AudioDriverPulseAudio(); }; diff --git a/drivers/rtaudio/audio_driver_rtaudio.cpp b/drivers/rtaudio/audio_driver_rtaudio.cpp index 0fad96f13358..365788e19272 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.cpp +++ b/drivers/rtaudio/audio_driver_rtaudio.cpp @@ -194,28 +194,6 @@ void AudioDriverRtAudio::finish() { } } -bool AudioDriverRtAudio::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverRtAudio::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverRtAudio::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverRtAudio::capture_device_get_default_name() { - - return ""; -} - AudioDriverRtAudio::AudioDriverRtAudio() { active = false; diff --git a/drivers/rtaudio/audio_driver_rtaudio.h b/drivers/rtaudio/audio_driver_rtaudio.h index 789a21ee328d..2a64652d5f34 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.h +++ b/drivers/rtaudio/audio_driver_rtaudio.h @@ -58,11 +58,6 @@ class AudioDriverRtAudio : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - AudioDriverRtAudio(); }; diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 8fe83a3be506..f8d7516f1f6f 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -142,7 +142,7 @@ class CMMNotificationClient : public IMMNotificationClient { static CMMNotificationClient notif_client; -Error AudioDriverWASAPI::init_render_device(bool reinit) { +Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit) { WAVEFORMATEX *pwfex; IMMDeviceEnumerator *enumerator = NULL; @@ -153,12 +153,12 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - if (device_name == "Default") { - hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device); + if (p_device->device_name == "Default") { + hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device); } else { IMMDeviceCollection *devices = NULL; - hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices); + hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); LPWSTR strId = NULL; @@ -184,7 +184,7 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { hr = props->GetValue(PKEY_Device_FriendlyName, &propvar); ERR_BREAK(hr != S_OK); - if (device_name == String(propvar.pwszVal)) { + if (p_device->device_name == String(propvar.pwszVal)) { hr = device->GetId(&strId); ERR_BREAK(hr != S_OK); @@ -205,9 +205,10 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { } if (device == NULL) { - hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device); + hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device); } } + if (reinit) { // In case we're trying to re-initialize the device prevent throwing this error on the console, // otherwise if there is currently no device available this will spam the console. @@ -225,7 +226,7 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error"); } - hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client); + hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&p_device->audio_client); SAFE_RELEASE(device) if (reinit) { @@ -236,282 +237,141 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); } - hr = audio_client->GetMixFormat(&pwfex); + hr = p_device->audio_client->GetMixFormat(&pwfex); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along - wasapi_channels = pwfex->nChannels; - format_tag = pwfex->wFormatTag; - bits_per_sample = pwfex->wBitsPerSample; - - switch (wasapi_channels) { - case 2: // Stereo - case 4: // Surround 3.1 - case 6: // Surround 5.1 - case 8: // Surround 7.1 - channels = wasapi_channels; - break; + p_device->channels = pwfex->nChannels; + p_device->format_tag = pwfex->wFormatTag; + p_device->bits_per_sample = pwfex->wBitsPerSample; + p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels; - default: - WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(wasapi_channels)); - channels = 2; - break; - } - - if (format_tag == WAVE_FORMAT_EXTENSIBLE) { + if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) { WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex; if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) { - format_tag = WAVE_FORMAT_PCM; + p_device->format_tag = WAVE_FORMAT_PCM; } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) { - format_tag = WAVE_FORMAT_IEEE_FLOAT; + p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT; } else { ERR_PRINT("WASAPI: Format not supported"); ERR_FAIL_V(ERR_CANT_OPEN); } } else { - if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) { + if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) { ERR_PRINT("WASAPI: Format not supported"); ERR_FAIL_V(ERR_CANT_OPEN); } } - DWORD streamflags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK; + DWORD streamflags = 0; if (mix_rate != pwfex->nSamplesPerSec) { streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST; pwfex->nSamplesPerSec = mix_rate; pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8); } - hr = audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, 0, 0, pwfex, NULL); + hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, 0, p_capture ? REFTIMES_PER_SEC : 0, pwfex, NULL); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - event = CreateEvent(NULL, FALSE, FALSE, NULL); - ERR_FAIL_COND_V(event == NULL, ERR_CANT_OPEN); - - hr = audio_client->SetEventHandle(event); + if (p_capture) { + hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client); + } else { + hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client); + } ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - hr = audio_client->GetService(IID_IAudioRenderClient, (void **)&render_client); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); + // Free memory + CoTaskMemFree(pwfex); + SAFE_RELEASE(device) + + return OK; +} + +Error AudioDriverWASAPI::init_render_device(bool reinit) { + + Error err = audio_device_init(&audio_output, false, reinit); + if (err != OK) + return err; + + switch (audio_output.channels) { + case 2: // Stereo + case 4: // Surround 3.1 + case 6: // Surround 5.1 + case 8: // Surround 7.1 + channels = audio_output.channels; + break; + + default: + WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(audio_output.channels)); + channels = 2; + break; + } UINT32 max_frames; - hr = audio_client->GetBufferSize(&max_frames); + HRESULT hr = audio_output.audio_client->GetBufferSize(&max_frames); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Due to WASAPI Shared Mode we have no control of the buffer size buffer_frames = max_frames; // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) - buffer_size = buffer_frames * channels; - samples_in.resize(buffer_size); + samples_in.resize(buffer_frames * channels); if (OS::get_singleton()->is_stdout_verbose()) { print_line("WASAPI: detected " + itos(channels) + " channels"); print_line("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); } - // Free memory - CoTaskMemFree(pwfex); - return OK; } -StringName AudioDriverWASAPI::get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator) { - // Setup default device - IMMDevice *default_device = NULL; - LPWSTR pwszID = NULL; - IPropertyStore *props = NULL; - - HRESULT hr = p_enumerator->GetDefaultAudioEndpoint( - eCapture, eConsole, &default_device); - ERR_FAIL_COND_V(hr != S_OK, ""); - - // Get the device ID - hr = default_device->GetId(&pwszID); - ERR_FAIL_COND_V(hr != S_OK, ""); - - // Get the device properties - hr = default_device->OpenPropertyStore( - STGM_READ, &props); - ERR_FAIL_COND_V(hr != S_OK, ""); - - PROPVARIANT var_name; - PropVariantInit(&var_name); - - // Get the name of the device - hr = props->GetValue(PKEY_Device_FriendlyName, &var_name); - ERR_FAIL_COND_V(hr != S_OK, ""); - - // Return the name of device - return String(var_name.pwszVal); -} - -Error AudioDriverWASAPI::init_capture_devices(bool reinit) { - - WAVEFORMATEX *pwfex; - IMMDeviceEnumerator *enumerator = NULL; - IMMDeviceCollection *device_collection = NULL; - IPropertyStore *props = NULL; - - capture_device_id_map.clear(); - - HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); +Error AudioDriverWASAPI::init_capture_device(bool reinit) { - capture_device_default_name = get_default_capture_device_name(enumerator); + Error err = audio_device_init(&audio_input, true, reinit); + if (err != OK) + return err; - // Enumerate a collection of valid devices - hr = enumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &device_collection); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - SAFE_RELEASE(enumerator); - - UINT count; - hr = device_collection->GetCount(&count); + // Get the max frames + UINT32 max_frames; + HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - // Loop through the device count - for (unsigned int i = 0; i < count; i++) { - IMMDevice *device = NULL; - LPWSTR pwszID = NULL; - - // Get the device - hr = device_collection->Item(i, &device); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // Get the device ID - hr = device->GetId(&pwszID); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // Get the device properties - hr = device->OpenPropertyStore(STGM_READ, &props); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - PROPVARIANT var_name; - PropVariantInit(&var_name); - - // Get the name of the device - hr = props->GetValue(PKEY_Device_FriendlyName, &var_name); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // Save the name of device - StringName name = String(var_name.pwszVal); - - // DEBUG: print the device name and ID - printf("Endpoint %d: \"%S\" (%S)\n", i, var_name.pwszVal, pwszID); - - capture_device_id_map[StringName(pwszID)] = name; - - // Cleanup the ID and properties - CoTaskMemFree(pwszID); - pwszID = NULL; - PropVariantClear(&var_name); - SAFE_RELEASE(props) - - // Create a new audio in block descriptor - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = memnew(MicrophoneDeviceOutputDirectWASAPI); - microphone_device_output_wasapi->name = name; - microphone_device_output_wasapi->active = false; - - // Push it into the list and assign it to the hash map for quick access - microphone_device_outputs.push_back(microphone_device_output_wasapi); - microphone_device_output_map[name] = microphone_device_output_wasapi; - - // Activate the device - hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)µphone_device_output_wasapi->audio_client); - SAFE_RELEASE(device) - - // Get the sample rate (hz) - hr = microphone_device_output_wasapi->audio_client->GetMixFormat(&pwfex); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - microphone_device_output_wasapi->channels = pwfex->nChannels; - microphone_device_output_wasapi->mix_rate = pwfex->nSamplesPerSec; - microphone_device_output_wasapi->bits_per_sample = pwfex->wBitsPerSample; - microphone_device_output_wasapi->frame_size = (microphone_device_output_wasapi->bits_per_sample / 8) * microphone_device_output_wasapi->channels; - - microphone_device_output_wasapi->current_capture_index = 0; - microphone_device_output_wasapi->current_capture_size = 0; - - WORD format_tag = pwfex->wFormatTag; - if (format_tag == WAVE_FORMAT_EXTENSIBLE) { - WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex; - - if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) { - format_tag = WAVE_FORMAT_PCM; - } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) { - format_tag = WAVE_FORMAT_IEEE_FLOAT; - } else { - ERR_PRINT("WASAPI: Format not supported"); - ERR_FAIL_V(ERR_CANT_OPEN); - } - } else { - if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) { - ERR_PRINT("WASAPI: Format not supported"); - ERR_FAIL_V(ERR_CANT_OPEN); - } - } - microphone_device_output_wasapi->capture_format_tag = format_tag; - - hr = microphone_device_output_wasapi->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, REFTIMES_PER_SEC, 0, pwfex, NULL); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // Get the max frames - UINT32 max_frames; - hr = microphone_device_output_wasapi->audio_client->GetBufferSize(&max_frames); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // Set the buffer size - microphone_device_output_wasapi->buffer.resize(max_frames); - memset(microphone_device_output_wasapi->buffer.ptrw(), 0x00, microphone_device_output_wasapi->buffer.size() * microphone_device_output_wasapi->frame_size); - - // Get the capture client - hr = microphone_device_output_wasapi->audio_client->GetService(IID_IAudioCaptureClient, (void **)µphone_device_output_wasapi->capture_client); - ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - - // TODO: set audio write stream to correct format - REFERENCE_TIME hns_actual_duration = (double)REFTIMES_PER_SEC * max_frames / pwfex->nSamplesPerSec; - - // Free memory - CoTaskMemFree(pwfex); - SAFE_RELEASE(device) + // Set the buffer size + audio_input_buffer.resize(max_frames * 8); + for (int i = 0; i < audio_input_buffer.size(); i++) { + audio_input_buffer.write[i] = 0; } - SAFE_RELEASE(device_collection) + audio_input_position = 0; return OK; } -Error AudioDriverWASAPI::finish_render_device() { +Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) { - if (audio_client) { - if (active) { - audio_client->Stop(); - active = false; + if (p_device->active) { + if (p_device->audio_client) { + p_device->audio_client->Stop(); } - audio_client->Release(); - audio_client = NULL; + p_device->active = false; } - SAFE_RELEASE(render_client) - SAFE_RELEASE(audio_client) + SAFE_RELEASE(p_device->audio_client) + SAFE_RELEASE(p_device->render_client) + SAFE_RELEASE(p_device->capture_client) return OK; } -Error AudioDriverWASAPI::finish_capture_devices() { +Error AudioDriverWASAPI::finish_render_device() { - microphone_device_output_map.clear(); - while (microphone_device_outputs.size() > 0) { - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output = static_cast(microphone_device_outputs.get(0)); - SAFE_RELEASE(microphone_device_output->capture_client) - SAFE_RELEASE(microphone_device_output->audio_client) - microphone_device_outputs.erase(microphone_device_output); - memdelete(microphone_device_output); - } + return audio_device_finish(&audio_output); +} - return OK; +Error AudioDriverWASAPI::finish_capture_device() { + + return audio_device_finish(&audio_input); } Error AudioDriverWASAPI::init() { @@ -523,12 +383,6 @@ Error AudioDriverWASAPI::init() { ERR_PRINT("WASAPI: init_render_device error"); } - err = init_capture_devices(); - if (err != OK) { - ERR_PRINT("WASAPI: init_capture_device error"); - } - - active = false; exit_thread = false; thread_exited = false; @@ -548,7 +402,7 @@ AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const { return get_speaker_mode_by_total_channels(channels); } -Array AudioDriverWASAPI::get_device_list() { +Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) { Array list; IMMDeviceCollection *devices = NULL; @@ -561,7 +415,7 @@ Array AudioDriverWASAPI::get_device_list() { HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); ERR_FAIL_COND_V(hr != S_OK, Array()); - hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices); + hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices); ERR_FAIL_COND_V(hr != S_OK, Array()); UINT count = 0; @@ -596,19 +450,28 @@ Array AudioDriverWASAPI::get_device_list() { return list; } +Array AudioDriverWASAPI::get_device_list() { + + return audio_device_get_list(false); +} + String AudioDriverWASAPI::get_device() { - return device_name; + lock(); + String name = audio_output.device_name; + unlock(); + + return name; } void AudioDriverWASAPI::set_device(String device) { lock(); - new_device = device; + audio_output.new_device = device; unlock(); } -float AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) { +int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) { if (format_tag == WAVE_FORMAT_PCM) { int32_t sample = 0; switch (bits_per_sample) { @@ -631,19 +494,19 @@ float AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE break; } - return (sample >> 16) / 32768.f; + return sample; } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) { - return ((float *)buffer)[i]; + return int32_t(((float *)buffer)[i] * 32768.0) << 16; } else { ERR_PRINT("WASAPI: Unknown format tag"); } - return 0.f; + return 0; } -void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample) { - if (ad->format_tag == WAVE_FORMAT_PCM) { - switch (ad->bits_per_sample) { +void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) { + if (format_tag == WAVE_FORMAT_PCM) { + switch (bits_per_sample) { case 8: ((int8_t *)buffer)[i] = sample >> 24; break; @@ -662,157 +525,99 @@ void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, ((int32_t *)buffer)[i] = sample; break; } - } else if (ad->format_tag == WAVE_FORMAT_IEEE_FLOAT) { + } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) { ((float *)buffer)[i] = (sample >> 16) / 32768.f; } else { ERR_PRINT("WASAPI: Unknown format tag"); - ad->exit_thread = true; } } void AudioDriverWASAPI::thread_func(void *p_udata) { AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata; + uint32_t avail_frames = 0; + uint32_t write_ofs = 0; while (!ad->exit_thread) { - // Capture - - if (default_capture_device_changed) { - if (ad->capture_device_id_map.has(capture_device_id)) { - Map::Element *e = ad->capture_device_id_map.find(capture_device_id); - ad->lock(); - ad->start_counting_ticks(); - - ad->capture_device_default_name = e->get(); - ad->update_microphone_default(ad->capture_device_default_name); - - default_capture_device_changed = false; - - ad->stop_counting_ticks(); - ad->unlock(); - } - } - - for (int i = 0; i < ad->microphone_device_outputs.size(); i++) { - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(ad->microphone_device_outputs[i]); - - if (microphone_device_output_wasapi->active == false) { - continue; - } - - UINT32 packet_length = 0; - BYTE *data; - UINT32 num_frames_available; - DWORD flags; - HRESULT hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length); - ERR_BREAK(hr != S_OK); + uint32_t read_frames = 0; + uint32_t written_frames = 0; - while (packet_length != 0) { - hr = microphone_device_output_wasapi->capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL); - ERR_BREAK(hr != S_OK); - - unsigned int frames_to_copy = num_frames_available; - - if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { - memset((char *)(microphone_device_output_wasapi->buffer.ptrw()) + (microphone_device_output_wasapi->current_capture_index * microphone_device_output_wasapi->frame_size), 0, frames_to_copy * microphone_device_output_wasapi->frame_size); - } else { - // fixme: Only works for floating point atm - for (int j = 0; j < frames_to_copy; j++) { - float l, r; - - if (microphone_device_output_wasapi->channels == 2) { - l = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2); - r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2 + 1); - } else if (microphone_device_output_wasapi->channels == 1) { - l = r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j); - } else { - l = r = 0.f; - ERR_PRINT("WASAPI: unsupported channel count in microphone!"); - } - - microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index++] = AudioFrame(l, r); + if (avail_frames == 0) { + ad->lock(); + ad->start_counting_ticks(); - if (microphone_device_output_wasapi->current_capture_index >= microphone_device_output_wasapi->buffer.size()) { - microphone_device_output_wasapi->current_capture_index = 0; - } - if (microphone_device_output_wasapi->current_capture_size < microphone_device_output_wasapi->buffer.size()) { - microphone_device_output_wasapi->current_capture_size++; - } - } + if (ad->audio_output.active) { + ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw()); + } else { + for (unsigned int i = 0; i < ad->samples_in.size(); i++) { + ad->samples_in.write[i] = 0; } + } - hr = microphone_device_output_wasapi->capture_client->ReleaseBuffer(num_frames_available); - ERR_BREAK(hr != S_OK); + avail_frames = ad->buffer_frames; + write_ofs = 0; - hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length); - ERR_BREAK(hr != S_OK); - } + ad->stop_counting_ticks(); + ad->unlock(); } ad->lock(); ad->start_counting_ticks(); - if (ad->active) { - ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw()); - } else { - for (unsigned int i = 0; i < ad->buffer_size; i++) { - ad->samples_in.write[i] = 0; - } - } - - ad->stop_counting_ticks(); - ad->unlock(); - - unsigned int left_frames = ad->buffer_frames; - unsigned int buffer_idx = 0; - while (left_frames > 0 && ad->audio_client) { - WaitForSingleObject(ad->event, 1000); - - ad->lock(); - ad->start_counting_ticks(); + if (avail_frames > 0 && ad->audio_output.audio_client) { UINT32 cur_frames; bool invalidated = false; - HRESULT hr = ad->audio_client->GetCurrentPadding(&cur_frames); + HRESULT hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames); if (hr == S_OK) { - // Check how much frames are available on the WASAPI buffer - UINT32 avail_frames = ad->buffer_frames - cur_frames; - UINT32 write_frames = avail_frames > left_frames ? left_frames : avail_frames; - - BYTE *buffer = NULL; - hr = ad->render_client->GetBuffer(write_frames, &buffer); - if (hr == S_OK) { - // We're using WASAPI Shared Mode so we must convert the buffer - if (ad->channels == ad->wasapi_channels) { - for (unsigned int i = 0; i < write_frames * ad->channels; i++) { - ad->write_sample(ad, buffer, i, ad->samples_in[buffer_idx++]); - } - } else { - for (unsigned int i = 0; i < write_frames; i++) { - for (unsigned int j = 0; j < MIN(ad->channels, ad->wasapi_channels); j++) { - ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, ad->samples_in[buffer_idx++]); + // Check how much frames are available on the WASAPI buffer + UINT32 write_frames = MIN(ad->buffer_frames - cur_frames, avail_frames); + if (write_frames > 0) { + BYTE *buffer = NULL; + hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer); + if (hr == S_OK) { + + // We're using WASAPI Shared Mode so we must convert the buffer + if (ad->channels == ad->audio_output.channels) { + for (unsigned int i = 0; i < write_frames * ad->channels; i++) { + ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]); } - if (ad->wasapi_channels > ad->channels) { - for (unsigned int j = ad->channels; j < ad->wasapi_channels; j++) { - ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, 0); + } else { + for (unsigned int i = 0; i < write_frames; i++) { + for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) { + ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]); + } + if (ad->audio_output.channels > ad->channels) { + for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) { + ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0); + } } } } - } - hr = ad->render_client->ReleaseBuffer(write_frames, 0); - if (hr != S_OK) { - ERR_PRINT("WASAPI: Release buffer error"); - } + hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0); + if (hr != S_OK) { + ERR_PRINT("WASAPI: Release buffer error"); + } + + avail_frames -= write_frames; + written_frames += write_frames; + } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) { + // Device is not valid anymore, reopen it - left_frames -= write_frames; - } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) { - invalidated = true; - } else { - ERR_PRINT("WASAPI: Get buffer error"); - ad->exit_thread = true; + Error err = ad->finish_render_device(); + if (err != OK) { + ERR_PRINT("WASAPI: finish_render_device error"); + } else { + // We reopened the device and samples_in may have resized, so invalidate the current avail_frames + avail_frames = 0; + } + } else { + ERR_PRINT("WASAPI: Get buffer error"); + ad->exit_thread = true; + } } } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) { invalidated = true; @@ -829,16 +634,10 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { ERR_PRINT("WASAPI: finish_render_device error"); } } - - ad->stop_counting_ticks(); - ad->unlock(); } - ad->lock(); - ad->start_counting_ticks(); - // If we're using the Default device and it changed finish it so we'll re-init the device - if (ad->device_name == "Default" && default_render_device_changed) { + if (ad->audio_output.device_name == "Default" && default_render_device_changed) { Error err = ad->finish_render_device(); if (err != OK) { ERR_PRINT("WASAPI: finish_render_device error"); @@ -848,23 +647,67 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { } // User selected a new device, finish the current one so we'll init the new device - if (ad->device_name != ad->new_device) { - ad->device_name = ad->new_device; + if (ad->audio_output.device_name != ad->audio_output.new_device) { + ad->audio_output.device_name = ad->audio_output.new_device; Error err = ad->finish_render_device(); if (err != OK) { ERR_PRINT("WASAPI: finish_render_device error"); } } - if (!ad->audio_client) { + if (!ad->audio_output.audio_client) { Error err = ad->init_render_device(true); if (err == OK) { ad->start(); } } + if (ad->audio_input.active) { + UINT32 packet_length = 0; + BYTE *data; + UINT32 num_frames_available; + DWORD flags; + + HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length); + if (hr == S_OK) { + while (packet_length != 0) { + hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL); + ERR_BREAK(hr != S_OK); + + // fixme: Only works for floating point atm + for (int j = 0; j < num_frames_available * ad->audio_input.channels; j++) { + int32_t sample; + + if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { + sample = 0; + } else { + sample = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); + } + + ad->audio_input_buffer.write[ad->audio_input_position++] = sample; + if (ad->audio_input_position >= ad->audio_input_buffer.size()) { + ad->audio_input_position = 0; + } + } + + read_frames += num_frames_available; + + hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available); + ERR_BREAK(hr != S_OK); + + hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length); + ERR_BREAK(hr != S_OK); + } + } + } + ad->stop_counting_ticks(); ad->unlock(); + + // Let the thread rest a while if we haven't read or write anything + if (written_frames == 0 && read_frames == 0) { + OS::get_singleton()->delay_usec(1000); + } } ad->thread_exited = true; @@ -872,12 +715,12 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { void AudioDriverWASAPI::start() { - if (audio_client) { - HRESULT hr = audio_client->Start(); + if (audio_output.audio_client) { + HRESULT hr = audio_output.audio_client->Start(); if (hr != S_OK) { ERR_PRINT("WASAPI: Start failed"); } else { - active = true; + audio_output.active = true; } } } @@ -904,7 +747,7 @@ void AudioDriverWASAPI::finish() { thread = NULL; } - finish_capture_devices(); + finish_capture_device(); finish_render_device(); if (mutex) { @@ -913,83 +756,70 @@ void AudioDriverWASAPI::finish() { } } -bool AudioDriverWASAPI::capture_device_start(StringName p_name) { +Error AudioDriverWASAPI::capture_start() { - if (microphone_device_output_map.has(p_name)) { - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_output_map[p_name]); - if (microphone_device_output_wasapi->active == false) { - microphone_device_output_wasapi->audio_client->Start(); - microphone_device_output_wasapi->active = true; - microphone_device_output_wasapi->set_read_index(-2048); - } + Error err = init_capture_device(); + if (err != OK) { + ERR_PRINT("WASAPI: init_capture_device error"); + return err; + } + + if (audio_input.active == false) { + audio_input.audio_client->Start(); + audio_input.active = true; - return true; + return OK; } - return false; + return FAILED; } -bool AudioDriverWASAPI::capture_device_stop(StringName p_name) { +Error AudioDriverWASAPI::capture_stop() { - if (microphone_device_output_map.has(p_name)) { - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_output_map[p_name]); - if (microphone_device_output_wasapi->active == true) { - microphone_device_output_wasapi->audio_client->Stop(); - microphone_device_output_wasapi->active = false; - } + if (audio_input.active == true) { + audio_input.audio_client->Stop(); + audio_input.active = false; - return true; + return OK; } - return false; + return FAILED; } -PoolStringArray AudioDriverWASAPI::capture_device_get_names() { +void AudioDriverWASAPI::capture_set_device(StringName p_name) { - PoolStringArray names; + lock(); + audio_input.new_device = p_name; + unlock(); +} - for (int i = 0; i < microphone_device_outputs.size(); i++) { - MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast(microphone_device_outputs.get(i)); - names.push_back(microphone_device_output_wasapi->name); - } +Array AudioDriverWASAPI::capture_get_device_list() { - return names; + return audio_device_get_list(true); } -StringName AudioDriverWASAPI::capture_device_get_default_name() { +StringName AudioDriverWASAPI::capture_get_device() { lock(); - StringName capture_device_default_name_local = capture_device_default_name; + StringName name = audio_input.device_name; unlock(); - return capture_device_default_name_local; + return name; } AudioDriverWASAPI::AudioDriverWASAPI() { - audio_client = NULL; - render_client = NULL; mutex = NULL; thread = NULL; - format_tag = 0; - bits_per_sample = 0; - samples_in.clear(); - buffer_size = 0; channels = 0; - wasapi_channels = 0; mix_rate = 0; buffer_frames = 0; thread_exited = false; exit_thread = false; - active = false; - - device_name = "Default"; - new_device = "Default"; - capture_device_default_name = ""; } #endif diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index e722d85353ae..a131eb5dae2d 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -43,53 +43,63 @@ class AudioDriverWASAPI : public AudioDriver { - HANDLE event; - // Audio out - IAudioClient *audio_client; - IAudioRenderClient *render_client; - // Microphone - class MicrophoneDeviceOutputDirectWASAPI : public MicrophoneDeviceOutputDirect { + class AudioDeviceWASAPI { public: IAudioClient *audio_client; + IAudioRenderClient *render_client; IAudioCaptureClient *capture_client; - WORD capture_format_tag; + bool active; + + WORD format_tag; + WORD bits_per_sample; + unsigned int channels; + unsigned int frame_size; + + String device_name; + String new_device; + + AudioDeviceWASAPI() { + audio_client = NULL; + render_client = NULL; + capture_client = NULL; + active = false; + format_tag = 0; + bits_per_sample = 0; + channels = 0; + frame_size = 0; + device_name = "Default"; + new_device = "Default"; + } }; - // - Mutex *mutex; - Thread *thread; - String device_name; - String new_device; - String capture_device_default_name; + AudioDeviceWASAPI audio_input; + AudioDeviceWASAPI audio_output; - WORD format_tag; - WORD bits_per_sample; + Mutex *mutex; + Thread *thread; Vector samples_in; - Map capture_device_id_map; - - unsigned int buffer_size; unsigned int channels; - unsigned int wasapi_channels; int mix_rate; int buffer_frames; bool thread_exited; mutable bool exit_thread; - bool active; - _FORCE_INLINE_ void write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample); - static _FORCE_INLINE_ float read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i); + static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample); + static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i); static void thread_func(void *p_udata); - StringName get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator); - Error init_render_device(bool reinit = false); - Error init_capture_devices(bool reinit = false); + Error init_capture_device(bool reinit = false); Error finish_render_device(); - Error finish_capture_devices(); + Error finish_capture_device(); + + Error audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit); + Error audio_device_finish(AudioDeviceWASAPI *p_device); + Array audio_device_get_list(bool p_capture); public: virtual const char *get_name() const { @@ -107,10 +117,11 @@ class AudioDriverWASAPI : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); + virtual Error capture_start(); + virtual Error capture_stop(); + virtual Array capture_get_device_list(); + virtual void capture_set_device(StringName p_name); + virtual StringName capture_get_device(); AudioDriverWASAPI(); }; diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index 2a320b620e7c..a1002ef4f924 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -210,28 +210,6 @@ void AudioDriverXAudio2::finish() { thread = NULL; }; -bool AudioDriverXAudio2::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverXAudio2::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverXAudio2::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverXAudio2::capture_device_get_default_name() { - - return ""; -} - AudioDriverXAudio2::AudioDriverXAudio2() { mutex = NULL; diff --git a/drivers/xaudio2/audio_driver_xaudio2.h b/drivers/xaudio2/audio_driver_xaudio2.h index b78ab136da3d..42e1adb2b735 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.h +++ b/drivers/xaudio2/audio_driver_xaudio2.h @@ -103,11 +103,6 @@ class AudioDriverXAudio2 : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - AudioDriverXAudio2(); ~AudioDriverXAudio2(); }; diff --git a/platform/android/audio_driver_jandroid.cpp b/platform/android/audio_driver_jandroid.cpp index 561ad106e467..b9f1f1eab05b 100644 --- a/platform/android/audio_driver_jandroid.cpp +++ b/platform/android/audio_driver_jandroid.cpp @@ -195,28 +195,6 @@ void AudioDriverAndroid::finish() { active = false; } -bool AudioDriverAndroid::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverAndroid::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverAndroid::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverAndroid::capture_device_get_default_name() { - - return ""; -} - void AudioDriverAndroid::set_pause(bool p_pause) { JNIEnv *env = ThreadAndroid::get_env(); diff --git a/platform/android/audio_driver_jandroid.h b/platform/android/audio_driver_jandroid.h index a54f0cae6a02..763f0e9b5a70 100644 --- a/platform/android/audio_driver_jandroid.h +++ b/platform/android/audio_driver_jandroid.h @@ -70,11 +70,6 @@ class AudioDriverAndroid : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - virtual void set_pause(bool p_pause); static void setup(jobject p_io); diff --git a/platform/android/audio_driver_opensl.cpp b/platform/android/audio_driver_opensl.cpp index 38f19d41e9d4..28e3ea962fd4 100644 --- a/platform/android/audio_driver_opensl.cpp +++ b/platform/android/audio_driver_opensl.cpp @@ -249,28 +249,6 @@ void AudioDriverOpenSL::finish() { (*sl)->Destroy(sl); } -bool AudioDriverOpenSL::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverOpenSL::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverOpenSL::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverOpenSL::capture_device_get_default_name() { - - return ""; -} - void AudioDriverOpenSL::set_pause(bool p_pause) { pause = p_pause; diff --git a/platform/android/audio_driver_opensl.h b/platform/android/audio_driver_opensl.h index f3f193973385..88cb12241470 100644 --- a/platform/android/audio_driver_opensl.h +++ b/platform/android/audio_driver_opensl.h @@ -98,11 +98,6 @@ class AudioDriverOpenSL : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - virtual void set_pause(bool p_pause); AudioDriverOpenSL(); diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp index 7074dbca3aa4..be36c3b748c3 100644 --- a/servers/audio/audio_driver_dummy.cpp +++ b/servers/audio/audio_driver_dummy.cpp @@ -125,28 +125,6 @@ void AudioDriverDummy::finish() { thread = NULL; }; -bool AudioDriverDummy::capture_device_start(StringName p_name) { - - return false; -} - -bool AudioDriverDummy::capture_device_stop(StringName p_name) { - - return false; -} - -PoolStringArray AudioDriverDummy::capture_device_get_names() { - - PoolStringArray names; - - return names; -} - -StringName AudioDriverDummy::capture_device_get_default_name() { - - return ""; -} - AudioDriverDummy::AudioDriverDummy() { mutex = NULL; diff --git a/servers/audio/audio_driver_dummy.h b/servers/audio/audio_driver_dummy.h index 48e8c731ea63..ba4f69edf45e 100644 --- a/servers/audio/audio_driver_dummy.h +++ b/servers/audio/audio_driver_dummy.h @@ -68,11 +68,6 @@ class AudioDriverDummy : public AudioDriver { virtual void unlock(); virtual void finish(); - virtual bool capture_device_start(StringName p_name); - virtual bool capture_device_stop(StringName p_name); - virtual PoolStringArray capture_device_get_names(); - virtual StringName capture_device_get_default_name(); - AudioDriverDummy(); ~AudioDriverDummy(); }; diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 206f1861a35c..0c8a1810d271 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "audio_stream.h" +#include "os/os.h" ////////////////////////////// @@ -119,33 +120,11 @@ String AudioStreamMicrophone::get_stream_name() const { return "Microphone"; } -void AudioStreamMicrophone::set_microphone_name(const String &p_name) { - if (microphone_name != p_name) { - microphone_name = p_name; - - for (Set::Element *E = playbacks.front(); E; E = E->next()) { - if (E->get()->active) { - // Is this the right thing to do? - E->get()->stop(); - E->get()->start(); - } - } - } -} - -StringName AudioStreamMicrophone::get_microphone_name() const { - return microphone_name; -} - float AudioStreamMicrophone::get_length() const { return 0; } void AudioStreamMicrophone::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_microphone_name", "name"), &AudioStreamMicrophone::set_microphone_name); - ClassDB::bind_method(D_METHOD("get_microphone_name"), &AudioStreamMicrophone::get_microphone_name); - - ADD_PROPERTY(PropertyInfo(Variant::STRING, "microphone_name"), "set_microphone_name", "get_microphone_name"); } AudioStreamMicrophone::AudioStreamMicrophone() { @@ -153,20 +132,25 @@ AudioStreamMicrophone::AudioStreamMicrophone() { void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) { - AudioDriver::MicrophoneDeviceOutput *microphone_device_output = reciever->owner; - const Vector &source_buffer = microphone_device_output->get_buffer(); - int current_buffer_size = microphone_device_output->get_current_buffer_size(); + AudioDriver::get_singleton()->lock(); + + Vector buf = AudioDriver::get_singleton()->get_audio_input_buffer(); for (int i = 0; i < p_frames; i++) { - if (current_buffer_size >= internal_mic_offset) { - if (internal_mic_offset >= source_buffer.size()) { - internal_mic_offset = 0; - } - p_buffer[i] = source_buffer[internal_mic_offset++]; - } else { - p_buffer[i] = AudioFrame(0.f, 0.f); + + float l = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; + } + float r = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; } + + p_buffer[i] = AudioFrame(l, r); } + + AudioDriver::get_singleton()->unlock(); } void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { @@ -174,28 +158,21 @@ void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale } float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() { - return reciever->owner->get_mix_rate(); + return AudioDriver::get_singleton()->get_mix_rate(); } void AudioStreamPlaybackMicrophone::start(float p_from_pos) { - internal_mic_offset = 0; - active = true; + input_ofs = 0; - // note: can this be called twice? - reciever = AudioServer::get_singleton()->create_microphone_reciever(microphone->get_microphone_name()); - if (reciever == NULL) { - active = false; - } + AudioDriver::get_singleton()->capture_start(); + active = true; _begin_resample(); } void AudioStreamPlaybackMicrophone::stop() { + AudioDriver::get_singleton()->capture_stop(); active = false; - if (reciever != NULL) { - AudioServer::get_singleton()->destroy_microphone_reciever(reciever); - reciever = NULL; - } } bool AudioStreamPlaybackMicrophone::is_playing() const { @@ -220,8 +197,6 @@ AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() { } AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() { - internal_mic_offset = 0; - reciever = NULL; } //////////////////////////////// diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 352cb8c5078a..f0f756129e27 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -104,7 +104,6 @@ class AudioStreamMicrophone : public AudioStream { friend class AudioStreamPlaybackMicrophone; Set playbacks; - StringName microphone_name; protected: static void _bind_methods(); @@ -113,9 +112,6 @@ class AudioStreamMicrophone : public AudioStream { virtual Ref instance_playback(); virtual String get_stream_name() const; - void set_microphone_name(const String &p_name); - StringName get_microphone_name() const; - virtual float get_length() const; //if supported, otherwise return 0 AudioStreamMicrophone(); @@ -127,10 +123,9 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled { friend class AudioStreamMicrophone; bool active; - uint32_t internal_mic_offset; + unsigned int input_ofs; Ref microphone; - AudioDriver::MicrophoneReciever *reciever; protected: virtual void _mix_internal(AudioFrame *p_buffer, int p_frames); diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 10fa69a12ee2..eb034fa615fb 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -73,42 +73,6 @@ void AudioDriver::update_mix_time(int p_frames) { _last_mix_time = OS::get_singleton()->get_ticks_usec(); } -void AudioDriver::update_microphone_default(StringName p_device_name) { - if (default_microphone_device_output != NULL) { - MicrophoneDeviceOutput *output = default_microphone_device_output->owner; - output->remove_reciever(default_microphone_device_output); - - while (output != NULL) { - MicrophoneDeviceOutput *owner = output->owner; - if (output->recievers.size() == 0) { - if (owner == NULL) { - if (output->active == true) { - capture_device_stop(output->name); - output->active == false; - } - } else { - owner->remove_reciever(output); - memdelete(output); - } - output = owner; - } - } - - if (microphone_device_output_map.has(p_device_name)) { - - Map::Element *e = microphone_device_output_map.find(p_device_name); - MicrophoneDeviceOutput *new_output = e->get(); - new_output->add_reciever(default_microphone_device_output); - if (new_output->active == false) { - capture_device_start(p_device_name); - new_output->active = true; - } - } - - output = default_microphone_device_output->owner; - } -} - double AudioDriver::get_mix_time() const { double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0; @@ -138,75 +102,19 @@ int AudioDriver::get_total_channels_by_speaker_mode(AudioDriver::SpeakerMode p_m ERR_FAIL_V(2); } -AudioDriver::MicrophoneReciever *AudioDriver::create_microphone_reciever(const StringName &p_device_name) { - - MicrophoneReciever *microphone_reciever = NULL; - MicrophoneDeviceOutput *reciever_output = NULL; - MicrophoneDeviceOutput *device_output = NULL; - - StringName device_name = capture_device_get_default_name(); - - if (microphone_device_output_map.has(device_name)) { - - Map::Element *e = microphone_device_output_map.find(device_name); - device_output = e->get(); - } - - if (device_output) { - if (p_device_name == "") { - if (default_microphone_device_output != NULL) { - reciever_output = default_microphone_device_output; - } else { - // Default reciever does not exist, create it and connect it - default_microphone_device_output = memnew(MicrophoneDeviceOutputIndirect); - reciever_output = default_microphone_device_output; - device_output->add_reciever(reciever_output); - } - } else { - if (microphone_device_output_map.has(p_device_name)) { - reciever_output = device_output; - } - } +Array AudioDriver::get_device_list() { + Array list; - if (reciever_output) { - microphone_reciever = memnew(MicrophoneReciever); - reciever_output->add_reciever(microphone_reciever); - if (device_output->active == false) { - capture_device_start(device_name); - device_output->active = true; - } - } - } + list.push_back("Default"); - return microphone_reciever; + return list; } -void AudioDriver::destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever) { - - if (p_microphone_reciever != NULL) { - MicrophoneDeviceOutput *output = p_microphone_reciever->owner; - output->remove_reciever(p_microphone_reciever); - - while (output != NULL) { - MicrophoneDeviceOutput *owner = output->owner; - if (output->recievers.size() == 0) { - if (owner == NULL) { - if (output->active == true) { - capture_device_stop(output->name); - output->active == false; - } - } else { - owner->remove_reciever(output); - memdelete(output); - } - output = owner; - } - } - memdelete(p_microphone_reciever); - } +String AudioDriver::get_device() { + return "Default"; } -Array AudioDriver::get_device_list() { +Array AudioDriver::capture_get_device_list() { Array list; list.push_back("Default"); @@ -214,17 +122,11 @@ Array AudioDriver::get_device_list() { return list; } -String AudioDriver::get_device() { - return "Default"; -} - AudioDriver::AudioDriver() { _last_mix_time = 0; _mix_amount = 0; - default_microphone_device_output = NULL; - #ifdef DEBUG_ENABLED prof_time = 0; #endif @@ -1308,32 +1210,19 @@ void AudioServer::set_device(String device) { AudioDriver::get_singleton()->set_device(device); } -PoolStringArray AudioServer::audio_in_get_device_names() { - - lock(); - PoolStringArray device_names = AudioDriver::get_singleton()->capture_device_get_names(); - unlock(); +Array AudioServer::capture_get_device_list() { - return device_names; + return AudioDriver::get_singleton()->capture_get_device_list(); } -AudioDriver::MicrophoneReciever *AudioServer::create_microphone_reciever(const StringName &p_device_name) { - AudioDriver::MicrophoneReciever *microphone_reciever = NULL; - - lock(); - microphone_reciever = AudioDriver::get_singleton()->create_microphone_reciever(p_device_name); - unlock(); +StringName AudioServer::capture_get_device() { - return microphone_reciever; + return AudioDriver::get_singleton()->capture_get_device(); } -void AudioServer::destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever) { - lock(); - AudioDriver::get_singleton()->destroy_microphone_reciever(p_microphone_reciever); - unlock(); -} +void AudioServer::capture_set_device(StringName device) { -void AudioServer::_change_default_device(StringName p_recording_device_default_name) { + AudioDriver::get_singleton()->capture_set_device(device); } void AudioServer::_bind_methods() { diff --git a/servers/audio_server.h b/servers/audio_server.h index 68a56c38f37c..035942cf896e 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -53,9 +53,11 @@ class AudioDriver { #endif protected: + Vector audio_input_buffer; + unsigned int audio_input_position; + void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true); void update_mix_time(int p_frames); - void update_microphone_default(StringName p_device_name); #ifdef DEBUG_ENABLED _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); } @@ -68,126 +70,6 @@ class AudioDriver { public: double get_mix_time() const; //useful for video -> audio sync - class MicrophoneDeviceOutput; - - class MicrophoneReciever { - public: - MicrophoneDeviceOutput *owner; - - MicrophoneReciever() { - owner = NULL; - } - - ~MicrophoneReciever() { - } - }; - - class MicrophoneDeviceOutput : public MicrophoneReciever { - public: - StringName name; - bool active; - Vector recievers; - - virtual unsigned int get_mix_rate() = 0; - virtual Vector &get_buffer() = 0; - virtual int get_current_buffer_size() = 0; - virtual int get_read_index() = 0; - virtual void set_read_index(int p_temp_index) = 0; - - void add_reciever(MicrophoneReciever *p_reciever) { - if (p_reciever == NULL) { - ERR_PRINT("Attempted to add NULL reciever") - return; - } - if (recievers.find(p_reciever) == -1) { - recievers.push_back(p_reciever); - p_reciever->owner = this; - } else { - ERR_PRINT("Duplicate reciever added") - } - } - - void remove_reciever(MicrophoneReciever *p_reciever) { - if (p_reciever == NULL) { - ERR_PRINT("Attempted to remove NULL reciever") - return; - } - - int index = recievers.find(p_reciever); - - if (index != -1) { - recievers.remove(index); - p_reciever->owner = NULL; - } else { - ERR_PRINT("Attempted to remove invalid reciever") - } - } - }; - - class MicrophoneDeviceOutputDirect : public MicrophoneDeviceOutput { - public: - enum MicrophoneFormat { - FORMAT_FLOAT, - FORMAT_PCM - }; - - MicrophoneFormat microphone_format; - unsigned short bits_per_sample; - unsigned int channels; - unsigned int mix_rate; - unsigned short frame_size; - int read_index = -2048; - - unsigned int current_capture_index; - unsigned int current_capture_size; - Vector buffer; - - unsigned int get_mix_rate() { - return mix_rate; - }; - - Vector &get_buffer() { - return buffer; - }; - - int get_current_buffer_size() { return current_capture_size; } - - int get_read_index() { - return read_index; - } - - void set_read_index(int p_read_index) { - read_index = p_read_index; - } - }; - - class MicrophoneDeviceOutputIndirect : public MicrophoneDeviceOutput { - public: - unsigned int get_mix_rate() { - return owner->get_mix_rate(); - }; - - Vector &get_buffer() { - return owner->get_buffer(); - }; - - int get_read_index() { - return owner->get_read_index(); - } - - void set_read_index(int p_read_index) { - owner->set_read_index(p_read_index); - } - int get_current_buffer_size() { return owner->get_current_buffer_size(); } - }; - - MicrophoneDeviceOutputIndirect *default_microphone_device_output; - - Vector microphone_device_outputs; - Map microphone_device_output_map; - - Vector direct_recievers; - enum SpeakerMode { SPEAKER_MODE_STEREO, SPEAKER_SURROUND_31, @@ -214,18 +96,19 @@ class AudioDriver { virtual void unlock() = 0; virtual void finish() = 0; - virtual bool capture_device_start(StringName p_name) = 0; - virtual bool capture_device_stop(StringName p_name) = 0; - virtual PoolStringArray capture_device_get_names() = 0; - virtual StringName capture_device_get_default_name() = 0; + virtual Error capture_start() { return FAILED; } + virtual Error capture_stop() { return FAILED; } + virtual void capture_set_device(StringName p_name) {} + virtual StringName capture_get_device() { return "Default"; } + virtual Array capture_get_device_list(); // TODO: convert this and get_device_list to PoolStringArray virtual float get_latency() { return 0; } SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const; int get_total_channels_by_speaker_mode(SpeakerMode) const; - AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name); - void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever); + Vector get_audio_input_buffer() { return audio_input_buffer; } + unsigned int get_audio_input_position() { return audio_input_position; } #ifdef DEBUG_ENABLED uint64_t get_profiling_time() const { return prof_time; } @@ -379,7 +262,6 @@ class AudioServer : public Object { friend class AudioDriver; void _driver_process(int p_frames, int32_t *p_buffer); - void _change_default_device(StringName p_recording_device_default_name); protected: static void _bind_methods(); @@ -479,10 +361,9 @@ class AudioServer : public Object { String get_device(); void set_device(String device); - AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name); - void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever); - - PoolStringArray audio_in_get_device_names(); + Array capture_get_device_list(); + StringName capture_get_device(); + void capture_set_device(StringName device); float get_output_latency() { return output_latency; } AudioServer(); From 2cf8da9d9f5e73bcd123cd497a0adbaa65fcc7a6 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Wed, 25 Jul 2018 15:35:52 -0300 Subject: [PATCH 04/14] Implemented capture device selection for CoreAudio --- drivers/coreaudio/audio_driver_coreaudio.cpp | 235 +++++++++++-------- drivers/coreaudio/audio_driver_coreaudio.h | 24 +- drivers/wasapi/audio_driver_wasapi.cpp | 2 +- drivers/wasapi/audio_driver_wasapi.h | 4 +- servers/audio_server.cpp | 10 +- servers/audio_server.h | 8 +- 6 files changed, 174 insertions(+), 109 deletions(-) diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index 261ba7809cf5..cbd5fbe74324 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -38,6 +38,20 @@ #define kInputBus 1 #ifdef OSX_ENABLED +OSStatus AudioDriverCoreAudio::input_device_address_cb(AudioObjectID inObjectID, + UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, + void *inClientData) { + AudioDriverCoreAudio *driver = (AudioDriverCoreAudio *)inClientData; + + // If our selected device is the Default call set_device to update the + // kAudioOutputUnitProperty_CurrentDevice property + if (driver->capture_device_name == "Default") { + driver->capture_set_device("Default"); + } + + return noErr; +} + OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *inClientData) { @@ -80,6 +94,11 @@ Error AudioDriverCoreAudio::init() { result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); ERR_FAIL_COND_V(result != noErr, FAILED); + + prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; + + result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); + ERR_FAIL_COND_V(result != noErr, FAILED); #endif AudioStreamBasicDescription strdesc; @@ -276,9 +295,94 @@ AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const { return get_speaker_mode_by_total_channels(channels); }; +void AudioDriverCoreAudio::lock() { + if (mutex) + mutex->lock(); +}; + +void AudioDriverCoreAudio::unlock() { + if (mutex) + mutex->unlock(); +}; + +bool AudioDriverCoreAudio::try_lock() { + if (mutex) + return mutex->try_lock() == OK; + return true; +} + +void AudioDriverCoreAudio::finish() { + OSStatus result; + + lock(); + + AURenderCallbackStruct callback; + zeromem(&callback, sizeof(AURenderCallbackStruct)); + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); + if (result != noErr) { + ERR_PRINT("AudioUnitSetProperty failed"); + } + + if (active) { + result = AudioOutputUnitStop(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioOutputUnitStop failed"); + } + + active = false; + } + + result = AudioUnitUninitialize(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioUnitUninitialize failed"); + } + #ifdef OSX_ENABLED + AudioObjectPropertyAddress prop; + prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; + prop.mScope = kAudioObjectPropertyScopeGlobal; + prop.mElement = kAudioObjectPropertyElementMaster; -Array AudioDriverCoreAudio::get_device_list() { + result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); + if (result != noErr) { + ERR_PRINT("AudioObjectRemovePropertyListener failed"); + } +#endif + + result = AudioComponentInstanceDispose(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioComponentInstanceDispose failed"); + } + + unlock(); + + if (mutex) { + memdelete(mutex); + mutex = NULL; + } +}; + +Error AudioDriverCoreAudio::capture_start() { + + UInt32 flag = 1; + OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); + + return OK; +} + +Error AudioDriverCoreAudio::capture_stop() { + + UInt32 flag = 0; + OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); + + return OK; +} + +#ifdef OSX_ENABLED + +Array AudioDriverCoreAudio::_get_device_list(bool capture) { Array list; @@ -297,20 +401,20 @@ Array AudioDriverCoreAudio::get_device_list() { UInt32 deviceCount = size / sizeof(AudioDeviceID); for (UInt32 i = 0; i < deviceCount; i++) { - prop.mScope = kAudioDevicePropertyScopeOutput; + prop.mScope = capture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; prop.mSelector = kAudioDevicePropertyStreamConfiguration; AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size); AudioBufferList *bufferList = (AudioBufferList *)malloc(size); AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList); - UInt32 outputChannelCount = 0; + UInt32 channelCount = 0; for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) - outputChannelCount += bufferList->mBuffers[j].mNumberChannels; + channelCount += bufferList->mBuffers[j].mNumberChannels; free(bufferList); - if (outputChannelCount >= 1) { + if (channelCount >= 1) { CFStringRef cfname; size = sizeof(CFStringRef); @@ -335,21 +439,11 @@ Array AudioDriverCoreAudio::get_device_list() { return list; } -String AudioDriverCoreAudio::get_device() { - - return device_name; -} - -void AudioDriverCoreAudio::set_device(String device) { - - device_name = device; - if (!active) { - return; - } +void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { AudioDeviceID deviceId; bool found = false; - if (device_name != "Default") { + if (device != "Default") { AudioObjectPropertyAddress prop; prop.mSelector = kAudioHardwarePropertyDevices; @@ -363,20 +457,20 @@ void AudioDriverCoreAudio::set_device(String device) { UInt32 deviceCount = size / sizeof(AudioDeviceID); for (UInt32 i = 0; i < deviceCount && !found; i++) { - prop.mScope = kAudioDevicePropertyScopeOutput; + prop.mScope = capture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; prop.mSelector = kAudioDevicePropertyStreamConfiguration; AudioObjectGetPropertyDataSize(audioDevices[i], &prop, 0, NULL, &size); AudioBufferList *bufferList = (AudioBufferList *)malloc(size); AudioObjectGetPropertyData(audioDevices[i], &prop, 0, NULL, &size, bufferList); - UInt32 outputChannelCount = 0; + UInt32 channelCount = 0; for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) - outputChannelCount += bufferList->mBuffers[j].mNumberChannels; + channelCount += bufferList->mBuffers[j].mNumberChannels; free(bufferList); - if (outputChannelCount >= 1) { + if (channelCount >= 1) { CFStringRef cfname; size = sizeof(CFStringRef); @@ -389,7 +483,7 @@ void AudioDriverCoreAudio::set_device(String device) { char *buffer = (char *)malloc(maxSize); if (CFStringGetCString(cfname, buffer, maxSize, kCFStringEncodingUTF8)) { String name = String(buffer) + " (" + itos(audioDevices[i]) + ")"; - if (name == device_name) { + if (name == device) { deviceId = audioDevices[i]; found = true; } @@ -405,7 +499,8 @@ void AudioDriverCoreAudio::set_device(String device) { if (!found) { // If we haven't found the desired device get the system default one UInt32 size = sizeof(AudioDeviceID); - AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; + UInt32 elem = capture ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice; + AudioObjectPropertyAddress property = { elem, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, NULL, &size, &deviceId); ERR_FAIL_COND(result != noErr); @@ -414,98 +509,49 @@ void AudioDriverCoreAudio::set_device(String device) { } if (found) { - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID)); + OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, capture ? kInputBus : kOutputBus, &deviceId, sizeof(AudioDeviceID)); ERR_FAIL_COND(result != noErr); } } -#endif - -void AudioDriverCoreAudio::lock() { - if (mutex) - mutex->lock(); -}; - -void AudioDriverCoreAudio::unlock() { - if (mutex) - mutex->unlock(); -}; +Array AudioDriverCoreAudio::get_device_list() { -bool AudioDriverCoreAudio::try_lock() { - if (mutex) - return mutex->try_lock() == OK; - return true; + return _get_device_list(); } -void AudioDriverCoreAudio::finish() { - OSStatus result; +String AudioDriverCoreAudio::get_device() { - lock(); + return device_name; +} - AURenderCallbackStruct callback; - zeromem(&callback, sizeof(AURenderCallbackStruct)); - result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); - if (result != noErr) { - ERR_PRINT("AudioUnitSetProperty failed"); - } +void AudioDriverCoreAudio::set_device(String device) { + device_name = device; if (active) { - result = AudioOutputUnitStop(audio_unit); - if (result != noErr) { - ERR_PRINT("AudioOutputUnitStop failed"); - } - - active = false; - } - - result = AudioUnitUninitialize(audio_unit); - if (result != noErr) { - ERR_PRINT("AudioUnitUninitialize failed"); - } - -#ifdef OSX_ENABLED - AudioObjectPropertyAddress prop; - prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; - prop.mScope = kAudioObjectPropertyScopeGlobal; - prop.mElement = kAudioObjectPropertyElementMaster; - - result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); - if (result != noErr) { - ERR_PRINT("AudioObjectRemovePropertyListener failed"); - } -#endif - - result = AudioComponentInstanceDispose(audio_unit); - if (result != noErr) { - ERR_PRINT("AudioComponentInstanceDispose failed"); + _set_device(device_name); } +} - unlock(); +void AudioDriverCoreAudio::capture_set_device(const String &p_name) { - if (mutex) { - memdelete(mutex); - mutex = NULL; + capture_device_name = p_name; + if (active) { + _set_device(capture_device_name, true); } -}; - -Error AudioDriverCoreAudio::capture_start() { +} - UInt32 flag = 1; - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); - ERR_FAIL_COND_V(result != noErr, FAILED); +Array AudioDriverCoreAudio::capture_get_device_list() { - return OK; + return _get_device_list(true); } -Error AudioDriverCoreAudio::capture_stop() { - - UInt32 flag = 0; - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); - ERR_FAIL_COND_V(result != noErr, FAILED); +String AudioDriverCoreAudio::capture_get_device() { - return OK; + return capture_device_name; } +#endif + AudioDriverCoreAudio::AudioDriverCoreAudio() { active = false; mutex = NULL; @@ -518,7 +564,8 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() { samples_in.clear(); device_name = "Default"; -}; + capture_device_name = "Default"; +} AudioDriverCoreAudio::~AudioDriverCoreAudio(){}; diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 7629e5668600..a416a162b368 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -48,6 +48,7 @@ class AudioDriverCoreAudio : public AudioDriver { Mutex *mutex; String device_name; + String capture_device_name; int mix_rate; unsigned int channels; @@ -57,6 +58,13 @@ class AudioDriverCoreAudio : public AudioDriver { Vector input_buf; #ifdef OSX_ENABLED + Array _get_device_list(bool capture = false); + void _set_device(const String &device, bool capture = false); + + static OSStatus input_device_address_cb(AudioObjectID inObjectID, + UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, + void *inClientData); + static OSStatus output_device_address_cb(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *inClientData); @@ -83,11 +91,7 @@ class AudioDriverCoreAudio : public AudioDriver { virtual void start(); virtual int get_mix_rate() const; virtual SpeakerMode get_speaker_mode() const; -#ifdef OSX_ENABLED - virtual Array get_device_list(); - virtual String get_device(); - virtual void set_device(String device); -#endif + virtual void lock(); virtual void unlock(); virtual void finish(); @@ -98,6 +102,16 @@ class AudioDriverCoreAudio : public AudioDriver { bool try_lock(); void stop(); +#ifdef OSX_ENABLED + virtual Array get_device_list(); + virtual String get_device(); + virtual void set_device(String device); + + virtual Array capture_get_device_list(); + virtual void capture_set_device(const String &p_name); + virtual String capture_get_device(); +#endif + AudioDriverCoreAudio(); ~AudioDriverCoreAudio(); }; diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index f8d7516f1f6f..7113a7773555 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -786,7 +786,7 @@ Error AudioDriverWASAPI::capture_stop() { return FAILED; } -void AudioDriverWASAPI::capture_set_device(StringName p_name) { +void AudioDriverWASAPI::capture_set_device(const String &p_name) { lock(); audio_input.new_device = p_name; diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index a131eb5dae2d..3d94f3ba490b 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -120,8 +120,8 @@ class AudioDriverWASAPI : public AudioDriver { virtual Error capture_start(); virtual Error capture_stop(); virtual Array capture_get_device_list(); - virtual void capture_set_device(StringName p_name); - virtual StringName capture_get_device(); + virtual void capture_set_device(const String &p_name); + virtual String capture_get_device(); AudioDriverWASAPI(); }; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index eb034fa615fb..7c8d61f5456b 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -1215,14 +1215,14 @@ Array AudioServer::capture_get_device_list() { return AudioDriver::get_singleton()->capture_get_device_list(); } -StringName AudioServer::capture_get_device() { +String AudioServer::capture_get_device() { return AudioDriver::get_singleton()->capture_get_device(); } -void AudioServer::capture_set_device(StringName device) { +void AudioServer::capture_set_device(const String &p_name) { - AudioDriver::get_singleton()->capture_set_device(device); + AudioDriver::get_singleton()->capture_set_device(p_name); } void AudioServer::_bind_methods() { @@ -1275,6 +1275,10 @@ void AudioServer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_device"), &AudioServer::get_device); ClassDB::bind_method(D_METHOD("set_device"), &AudioServer::set_device); + ClassDB::bind_method(D_METHOD("capture_get_device_list"), &AudioServer::capture_get_device_list); + ClassDB::bind_method(D_METHOD("capture_get_device"), &AudioServer::capture_get_device); + ClassDB::bind_method(D_METHOD("capture_set_device"), &AudioServer::capture_set_device); + ClassDB::bind_method(D_METHOD("set_bus_layout", "bus_layout"), &AudioServer::set_bus_layout); ClassDB::bind_method(D_METHOD("generate_bus_layout"), &AudioServer::generate_bus_layout); diff --git a/servers/audio_server.h b/servers/audio_server.h index 035942cf896e..c199a337eb89 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -98,8 +98,8 @@ class AudioDriver { virtual Error capture_start() { return FAILED; } virtual Error capture_stop() { return FAILED; } - virtual void capture_set_device(StringName p_name) {} - virtual StringName capture_get_device() { return "Default"; } + virtual void capture_set_device(const String &p_name) {} + virtual String capture_get_device() { return "Default"; } virtual Array capture_get_device_list(); // TODO: convert this and get_device_list to PoolStringArray virtual float get_latency() { return 0; } @@ -362,8 +362,8 @@ class AudioServer : public Object { void set_device(String device); Array capture_get_device_list(); - StringName capture_get_device(); - void capture_set_device(StringName device); + String capture_get_device(); + void capture_set_device(const String &p_name); float get_output_latency() { return output_latency; } AudioServer(); From 909cf03568b2b9ca62cd72a7d20e494fac21be66 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Wed, 25 Jul 2018 17:58:56 -0300 Subject: [PATCH 05/14] Fixed device capture switching for WASAPI driver --- drivers/wasapi/audio_driver_wasapi.cpp | 30 ++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 7113a7773555..a01d991450a2 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -699,6 +699,32 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { ERR_BREAK(hr != S_OK); } } + + // If we're using the Default device and it changed finish it so we'll re-init the device + if (ad->audio_input.device_name == "Default" && default_capture_device_changed) { + Error err = ad->finish_capture_device(); + if (err != OK) { + ERR_PRINT("WASAPI: finish_capture_device error"); + } + + default_capture_device_changed = false; + } + + // User selected a new device, finish the current one so we'll init the new device + if (ad->audio_input.device_name != ad->audio_input.new_device) { + ad->audio_input.device_name = ad->audio_input.new_device; + Error err = ad->finish_capture_device(); + if (err != OK) { + ERR_PRINT("WASAPI: finish_capture_device error"); + } + } + + if (!ad->audio_input.audio_client) { + Error err = ad->init_capture_device(true); + if (err == OK) { + ad->capture_start(); + } + } } ad->stop_counting_ticks(); @@ -798,10 +824,10 @@ Array AudioDriverWASAPI::capture_get_device_list() { return audio_device_get_list(true); } -StringName AudioDriverWASAPI::capture_get_device() { +String AudioDriverWASAPI::capture_get_device() { lock(); - StringName name = audio_input.device_name; + String name = audio_input.device_name; unlock(); return name; From 7142e1d3f7af98850d64ec881d21ed82222396a0 Mon Sep 17 00:00:00 2001 From: Saracen Date: Thu, 26 Jul 2018 23:36:16 +0100 Subject: [PATCH 06/14] Fix single channel capture devices on WASAPI. --- drivers/wasapi/audio_driver_wasapi.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index a01d991450a2..7ae87f04a872 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -675,18 +675,28 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { ERR_BREAK(hr != S_OK); // fixme: Only works for floating point atm - for (int j = 0; j < num_frames_available * ad->audio_input.channels; j++) { - int32_t sample; + for (int j = 0; j < num_frames_available; j++) { + int32_t sample_channel[2]; if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { - sample = 0; + sample_channel[0] = sample_channel[1] = 0; } else { - sample = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); + if (ad->audio_input.channels == 2) { + sample_channel[0] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2); + sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1); + } else if (ad->audio_input.channels == 1) { + sample_channel[0] = sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); + } else { + sample_channel[0] = sample_channel[1] = 0; + ERR_PRINT("WASAPI: unsupported channel count in microphone!"); + } } - ad->audio_input_buffer.write[ad->audio_input_position++] = sample; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; + for (int k = 0; k < 2; k++) { + ad->audio_input_buffer.write[ad->audio_input_position++] = sample_channel[k]; + if (ad->audio_input_position >= ad->audio_input_buffer.size()) { + ad->audio_input_position = 0; + } } } From aca6e291d64fcbd253ea9c34756db6f8c6915c72 Mon Sep 17 00:00:00 2001 From: Saracen Date: Fri, 27 Jul 2018 03:47:22 +0100 Subject: [PATCH 07/14] Fixed audio clipping on WASAPI by fixing argument order on AudioClient Initialize method ensuring a larger capture buffer and adding bounds to the capture and stream. --- drivers/wasapi/audio_driver_wasapi.cpp | 14 +++++++---- servers/audio/audio_stream.cpp | 32 ++++++++++++++++++-------- servers/audio_server.h | 2 ++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 7ae87f04a872..f4d0d3c1dc58 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -65,6 +65,8 @@ const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient); #define REFTIMES_PER_SEC 10000000 #define REFTIMES_PER_MILLISEC 10000 +#define CAPTURE_BUFFER_CHANNELS 2 + static StringName capture_device_id; static bool default_render_device_changed = false; static bool default_capture_device_changed = false; @@ -271,7 +273,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8); } - hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, 0, p_capture ? REFTIMES_PER_SEC : 0, pwfex, NULL); + hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_capture ? REFTIMES_PER_SEC : 0, 0, pwfex, NULL); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); if (p_capture) { @@ -338,11 +340,12 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) { ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Set the buffer size - audio_input_buffer.resize(max_frames * 8); + audio_input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); for (int i = 0; i < audio_input_buffer.size(); i++) { audio_input_buffer.write[i] = 0; } audio_input_position = 0; + audio_input_size = 0; return OK; } @@ -676,7 +679,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { // fixme: Only works for floating point atm for (int j = 0; j < num_frames_available; j++) { - int32_t sample_channel[2]; + int32_t sample_channel[CAPTURE_BUFFER_CHANNELS]; if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { sample_channel[0] = sample_channel[1] = 0; @@ -692,11 +695,14 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { } } - for (int k = 0; k < 2; k++) { + for (int k = 0; k < CAPTURE_BUFFER_CHANNELS; k++) { ad->audio_input_buffer.write[ad->audio_input_position++] = sample_channel[k]; if (ad->audio_input_position >= ad->audio_input_buffer.size()) { ad->audio_input_position = 0; } + if (ad->audio_input_size < ad->audio_input_buffer.size()) { + ad->audio_input_size++; + } } } diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 0c8a1810d271..f7622b8a4ec9 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -135,19 +135,31 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr AudioDriver::get_singleton()->lock(); Vector buf = AudioDriver::get_singleton()->get_audio_input_buffer(); + unsigned int audio_input_size = AudioDriver::get_singleton()->get_audio_input_size(); - for (int i = 0; i < p_frames; i++) { - - float l = (buf[input_ofs++] >> 16) / 32768.f; - if (input_ofs >= buf.size()) { - input_ofs = 0; - } - float r = (buf[input_ofs++] >> 16) / 32768.f; - if (input_ofs >= buf.size()) { - input_ofs = 0; + // p_frames is multipled by two since an AudioFrame is stereo + if ((p_frames * 2) > audio_input_size) { + for (int i = 0; i < p_frames; i++) { + p_buffer[i] = AudioFrame(0.0f, 0.0f); } + input_ofs = 0; + } else { + for (int i = 0; i < p_frames; i++) { + if (audio_input_size >= input_ofs) { + float l = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; + } + float r = (buf[input_ofs++] >> 16) / 32768.f; + if (input_ofs >= buf.size()) { + input_ofs = 0; + } - p_buffer[i] = AudioFrame(l, r); + p_buffer[i] = AudioFrame(l, r); + } else { + p_buffer[i] = AudioFrame(0.0f, 0.0f); + } + } } AudioDriver::get_singleton()->unlock(); diff --git a/servers/audio_server.h b/servers/audio_server.h index c199a337eb89..a9ed66a2d59e 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -55,6 +55,7 @@ class AudioDriver { protected: Vector audio_input_buffer; unsigned int audio_input_position; + unsigned int audio_input_size; void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true); void update_mix_time(int p_frames); @@ -109,6 +110,7 @@ class AudioDriver { Vector get_audio_input_buffer() { return audio_input_buffer; } unsigned int get_audio_input_position() { return audio_input_position; } + unsigned int get_audio_input_size() { return audio_input_size; } #ifdef DEBUG_ENABLED uint64_t get_profiling_time() const { return prof_time; } From 0a877b58b8adb08ab137393b84036b8be94097ca Mon Sep 17 00:00:00 2001 From: Saracen Date: Fri, 27 Jul 2018 05:43:22 +0100 Subject: [PATCH 08/14] Reset audio input during render device change to keep synchronisation. --- drivers/wasapi/audio_driver_wasapi.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index f4d0d3c1dc58..b641e213a817 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -320,6 +320,9 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) samples_in.resize(buffer_frames * channels); + audio_input_position = 0; + audio_input_size = 0; + if (OS::get_singleton()->is_stdout_verbose()) { print_line("WASAPI: detected " + itos(channels) + " channels"); print_line("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); From 09eafaba6289adab27057a6c1b737ec20498a0f6 Mon Sep 17 00:00:00 2001 From: Saracen Date: Fri, 27 Jul 2018 16:15:24 +0100 Subject: [PATCH 09/14] Implemented capture device selection for PulseAudio (marcelofg55) --- .../pulseaudio/audio_driver_pulseaudio.cpp | 128 ++++++++++++++++-- drivers/pulseaudio/audio_driver_pulseaudio.h | 15 ++ 2 files changed, 132 insertions(+), 11 deletions(-) diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 2b1a3366305e..71e75915660f 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -402,6 +402,26 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { } } } + + // User selected a new device, finish the current one so we'll init the new device + if (ad->capture_device_name != ad->capture_new_device) { + ad->capture_device_name = ad->capture_new_device; + ad->capture_finish_device(); + + Error err = ad->capture_init_device(); + if (err != OK) { + ERR_PRINT("PulseAudio: capture_init_device error"); + ad->capture_device_name = "Default"; + ad->capture_new_device = "Default"; + + err = ad->capture_init_device(); + if (err != OK) { + ad->active = false; + ad->exit_thread = true; + break; + } + } + } } ad->stop_counting_ticks(); @@ -540,11 +560,16 @@ void AudioDriverPulseAudio::finish() { thread = NULL; } -Error AudioDriverPulseAudio::capture_start() { - - Error err = OK; +Error AudioDriverPulseAudio::capture_init_device() { - lock(); + // If there is a specified device check that it is really present + if (capture_device_name != "Default") { + Array list = capture_get_device_list(); + if (list.find(capture_device_name) == -1) { + capture_device_name = "Default"; + capture_new_device = "Default"; + } + } pa_sample_spec spec; @@ -568,11 +593,12 @@ Error AudioDriverPulseAudio::capture_start() { ERR_FAIL_V(ERR_CANT_OPEN); } + const char *dev = capture_device_name == "Default" ? NULL : capture_device_name.utf8().get_data(); pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE); - int error_code = pa_stream_connect_record(pa_rec_str, NULL, &attr, flags); + int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags); if (error_code < 0) { ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code))); - err = ERR_CANT_OPEN; + ERR_FAIL_V(ERR_CANT_OPEN); } audio_input_buffer.resize(input_buffer_frames * 8); @@ -581,21 +607,101 @@ Error AudioDriverPulseAudio::capture_start() { } audio_input_position = 0; - unlock(); - - return err; + return OK; } -Error AudioDriverPulseAudio::capture_stop() { +void AudioDriverPulseAudio::capture_finish_device() { + if (pa_rec_str) { - pa_stream_disconnect(pa_rec_str); + int ret = pa_stream_disconnect(pa_rec_str); + if (ret != 0) { + ERR_PRINTS("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret))); + } pa_stream_unref(pa_rec_str); pa_rec_str = NULL; } +} + +Error AudioDriverPulseAudio::capture_start() { + + lock(); + Error err = capture_init_device(); + unlock(); + + return err; +} + +Error AudioDriverPulseAudio::capture_stop() { + lock(); + capture_finish_device(); + unlock(); return OK; } +void AudioDriverPulseAudio::capture_set_device(const String &p_name) { + + lock(); + capture_new_device = p_name; + unlock(); +} + +void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) { + AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata; + + // If eol is set to a positive number, you're at the end of the list + if (eol > 0) { + return; + } + + if (l->monitor_of_sink == PA_INVALID_INDEX) { + ad->pa_rec_devices.push_back(l->name); + } + + ad->pa_status++; +} + +Array AudioDriverPulseAudio::capture_get_device_list() { + + pa_rec_devices.clear(); + pa_rec_devices.push_back("Default"); + + if (pa_ctx == NULL) { + return pa_rec_devices; + } + + lock(); + + // Get the device list + pa_status = 0; + pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this); + if (pa_op) { + while (pa_status == 0) { + int ret = pa_mainloop_iterate(pa_ml, 1, NULL); + if (ret < 0) { + ERR_PRINT("pa_mainloop_iterate error"); + } + } + + pa_operation_unref(pa_op); + } else { + ERR_PRINT("pa_context_get_server_info error"); + } + + unlock(); + + return pa_rec_devices; +} + +String AudioDriverPulseAudio::capture_get_device() { + + lock(); + String name = capture_device_name; + unlock(); + + return name; +} + AudioDriverPulseAudio::AudioDriverPulseAudio() { pa_ml = NULL; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index 67b99133fb4d..e8b2f19e6e07 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -54,6 +54,10 @@ class AudioDriverPulseAudio : public AudioDriver { String new_device; String default_device; + String capture_device_name; + String capture_new_device; + String capture_default_device; + Vector samples_in; Vector samples_out; @@ -65,6 +69,7 @@ class AudioDriverPulseAudio : public AudioDriver { int pa_ready; int pa_status; Array pa_devices; + Array pa_rec_devices; bool active; bool thread_exited; @@ -76,10 +81,14 @@ class AudioDriverPulseAudio : public AudioDriver { static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); static void pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata); static void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); + static void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata); Error init_device(); void finish_device(); + Error capture_init_device(); + void capture_finish_device(); + void detect_channels(); static void thread_func(void *p_udata); @@ -93,9 +102,15 @@ class AudioDriverPulseAudio : public AudioDriver { virtual void start(); virtual int get_mix_rate() const; virtual SpeakerMode get_speaker_mode() const; + virtual Array get_device_list(); virtual String get_device(); virtual void set_device(String device); + + virtual Array capture_get_device_list(); + virtual void capture_set_device(const String &p_name); + virtual String capture_get_device(); + virtual void lock(); virtual void unlock(); virtual void finish(); From e5e4dbb6c9a750e217659caea1fc06e4a7270a91 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Fri, 27 Jul 2018 13:54:30 -0300 Subject: [PATCH 10/14] Added support for single channel inputs for CoreAudio --- drivers/coreaudio/audio_driver_coreaudio.cpp | 55 ++++++++++++++++---- drivers/coreaudio/audio_driver_coreaudio.h | 2 + 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index cbd5fbe74324..1344a9075e65 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -122,6 +122,26 @@ Error AudioDriverCoreAudio::init() { break; } + zeromem(&strdesc, sizeof(strdesc)); + size = sizeof(strdesc); + result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, &size); + ERR_FAIL_COND_V(result != noErr, FAILED); + + switch (strdesc.mChannelsPerFrame) { + case 1: // Mono + capture_channels = 1; + break; + + case 2: // Stereo + capture_channels = 2; + break; + + default: + // Unknown number of channels, default to stereo + capture_channels = 2; + break; + } + mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); zeromem(&strdesc, sizeof(strdesc)); @@ -137,7 +157,7 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); - strdesc.mChannelsPerFrame = 2; + strdesc.mChannelsPerFrame = capture_channels; result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); @@ -155,10 +175,8 @@ Error AudioDriverCoreAudio::init() { samples_in.resize(buffer_size); input_buf.resize(buffer_size); audio_input_buffer.resize(buffer_size * 8); - for (int i = 0; i < audio_input_buffer.size(); i++) { - audio_input_buffer.write[i] = 0; - } audio_input_position = 0; + audio_input_size = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("CoreAudio: detected " + itos(channels) + " channels"); @@ -229,6 +247,17 @@ OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon, return 0; }; +void AudioDriverCoreAudio::_input_write_sample(int32_t sample) { + + audio_input_buffer.write[audio_input_position++] = sample; + if (audio_input_position >= audio_input_buffer.size()) { + audio_input_position = 0; + } + if (audio_input_size < audio_input_buffer.size()) { + audio_input_size++; + } +} + OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, @@ -245,15 +274,18 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, AudioBufferList bufferList; bufferList.mNumberBuffers = 1; bufferList.mBuffers[0].mData = ad->input_buf.ptrw(); - bufferList.mBuffers[0].mNumberChannels = 2; + bufferList.mBuffers[0].mNumberChannels = ad->capture_channels; bufferList.mBuffers[0].mDataByteSize = ad->input_buf.size() * sizeof(int16_t); OSStatus result = AudioUnitRender(ad->audio_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); if (result == noErr) { - for (int i = 0; i < inNumberFrames * 2; i++) { - ad->audio_input_buffer.write[ad->audio_input_position++] = ad->input_buf[i] << 16; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; + for (int i = 0; i < inNumberFrames * ad->capture_channels; i++) { + int32_t sample = ad->input_buf[i] << 16; + ad->_input_write_sample(sample); + + if (ad->capture_channels == 1) { + // In case input device is single channel convert it to Stereo + ad->_input_write_sample(sample); } } } else { @@ -511,6 +543,10 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { if (found) { OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, capture ? kInputBus : kOutputBus, &deviceId, sizeof(AudioDeviceID)); ERR_FAIL_COND(result != noErr); + + // Reset audio input to keep synchronisation. + audio_input_position = 0; + audio_input_size = 0; } } @@ -558,6 +594,7 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() { mix_rate = 0; channels = 2; + capture_channels = 2; buffer_frames = 0; diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index a416a162b368..53a3e5e038f2 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -52,6 +52,7 @@ class AudioDriverCoreAudio : public AudioDriver { int mix_rate; unsigned int channels; + unsigned int capture_channels; unsigned int buffer_frames; Vector samples_in; @@ -60,6 +61,7 @@ class AudioDriverCoreAudio : public AudioDriver { #ifdef OSX_ENABLED Array _get_device_list(bool capture = false); void _set_device(const String &device, bool capture = false); + void _input_write_sample(int32_t sample); static OSStatus input_device_address_cb(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, From 597301db349c2c0c70af6b411465a5f578765052 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Fri, 27 Jul 2018 15:09:03 -0300 Subject: [PATCH 11/14] Renamed AudioDriver audio_input_* vars to input_* --- drivers/coreaudio/audio_driver_coreaudio.cpp | 25 ++++--------- drivers/coreaudio/audio_driver_coreaudio.h | 1 - .../pulseaudio/audio_driver_pulseaudio.cpp | 16 ++++----- drivers/wasapi/audio_driver_wasapi.cpp | 36 +++++++------------ servers/audio/audio_stream.cpp | 8 ++--- servers/audio_server.cpp | 11 ++++++ servers/audio_server.h | 13 +++---- 7 files changed, 50 insertions(+), 60 deletions(-) diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index 1344a9075e65..e1f47cb8c2f0 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -174,9 +174,9 @@ Error AudioDriverCoreAudio::init() { unsigned int buffer_size = buffer_frames * channels; samples_in.resize(buffer_size); input_buf.resize(buffer_size); - audio_input_buffer.resize(buffer_size * 8); - audio_input_position = 0; - audio_input_size = 0; + input_buffer.resize(buffer_size * 8); + input_position = 0; + input_size = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("CoreAudio: detected " + itos(channels) + " channels"); @@ -247,17 +247,6 @@ OSStatus AudioDriverCoreAudio::output_callback(void *inRefCon, return 0; }; -void AudioDriverCoreAudio::_input_write_sample(int32_t sample) { - - audio_input_buffer.write[audio_input_position++] = sample; - if (audio_input_position >= audio_input_buffer.size()) { - audio_input_position = 0; - } - if (audio_input_size < audio_input_buffer.size()) { - audio_input_size++; - } -} - OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, @@ -281,11 +270,11 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, if (result == noErr) { for (int i = 0; i < inNumberFrames * ad->capture_channels; i++) { int32_t sample = ad->input_buf[i] << 16; - ad->_input_write_sample(sample); + ad->input_buffer_write(sample); if (ad->capture_channels == 1) { // In case input device is single channel convert it to Stereo - ad->_input_write_sample(sample); + ad->input_buffer_write(sample); } } } else { @@ -545,8 +534,8 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { ERR_FAIL_COND(result != noErr); // Reset audio input to keep synchronisation. - audio_input_position = 0; - audio_input_size = 0; + input_position = 0; + input_size = 0; } } diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 53a3e5e038f2..d3f7c8d59692 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -61,7 +61,6 @@ class AudioDriverCoreAudio : public AudioDriver { #ifdef OSX_ENABLED Array _get_device_list(bool capture = false); void _set_device(const String &device, bool capture = false); - void _input_write_sample(int32_t sample); static OSStatus input_device_address_cb(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 71e75915660f..66fdfc9c1b27 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -380,7 +380,7 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { size_t bytes = pa_stream_readable_size(ad->pa_rec_str); if (bytes > 0) { const void *ptr = NULL; - size_t maxbytes = ad->audio_input_buffer.size() * sizeof(int16_t); + size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t); bytes = MIN(bytes, maxbytes); ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes); @@ -389,9 +389,9 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { } else { int16_t *srcptr = (int16_t *)ptr; for (size_t i = bytes >> 1; i > 0; i--) { - ad->audio_input_buffer.write[ad->audio_input_position++] = int32_t(*srcptr++) << 16; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; + ad->input_buffer.write[ad->input_position++] = int32_t(*srcptr++) << 16; + if (ad->input_position >= ad->input_buffer.size()) { + ad->input_position = 0; } } @@ -601,11 +601,11 @@ Error AudioDriverPulseAudio::capture_init_device() { ERR_FAIL_V(ERR_CANT_OPEN); } - audio_input_buffer.resize(input_buffer_frames * 8); - for (int i = 0; i < audio_input_buffer.size(); i++) { - audio_input_buffer.write[i] = 0; + input_buffer.resize(input_buffer_frames * 8); + for (int i = 0; i < input_buffer.size(); i++) { + input_buffer.write[i] = 0; } - audio_input_position = 0; + input_position = 0; return OK; } diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index b641e213a817..827aecd1c8aa 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -320,8 +320,8 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) samples_in.resize(buffer_frames * channels); - audio_input_position = 0; - audio_input_size = 0; + input_position = 0; + input_size = 0; if (OS::get_singleton()->is_stdout_verbose()) { print_line("WASAPI: detected " + itos(channels) + " channels"); @@ -343,12 +343,9 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) { ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); // Set the buffer size - audio_input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); - for (int i = 0; i < audio_input_buffer.size(); i++) { - audio_input_buffer.write[i] = 0; - } - audio_input_position = 0; - audio_input_size = 0; + input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); + input_position = 0; + input_size = 0; return OK; } @@ -682,31 +679,24 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { // fixme: Only works for floating point atm for (int j = 0; j < num_frames_available; j++) { - int32_t sample_channel[CAPTURE_BUFFER_CHANNELS]; + int32_t l, r; if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { - sample_channel[0] = sample_channel[1] = 0; + l = r = 0; } else { if (ad->audio_input.channels == 2) { - sample_channel[0] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2); - sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1); + l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2); + r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1); } else if (ad->audio_input.channels == 1) { - sample_channel[0] = sample_channel[1] = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); + l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j); } else { - sample_channel[0] = sample_channel[1] = 0; + l = r = 0; ERR_PRINT("WASAPI: unsupported channel count in microphone!"); } } - for (int k = 0; k < CAPTURE_BUFFER_CHANNELS; k++) { - ad->audio_input_buffer.write[ad->audio_input_position++] = sample_channel[k]; - if (ad->audio_input_position >= ad->audio_input_buffer.size()) { - ad->audio_input_position = 0; - } - if (ad->audio_input_size < ad->audio_input_buffer.size()) { - ad->audio_input_size++; - } - } + input_buffer_write(l); + input_buffer_write(r); } read_frames += num_frames_available; diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index f7622b8a4ec9..618ebd526d5b 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -134,18 +134,18 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr AudioDriver::get_singleton()->lock(); - Vector buf = AudioDriver::get_singleton()->get_audio_input_buffer(); - unsigned int audio_input_size = AudioDriver::get_singleton()->get_audio_input_size(); + Vector buf = AudioDriver::get_singleton()->get_input_buffer(); + unsigned int input_size = AudioDriver::get_singleton()->get_input_size(); // p_frames is multipled by two since an AudioFrame is stereo - if ((p_frames * 2) > audio_input_size) { + if ((p_frames * 2) > input_size) { for (int i = 0; i < p_frames; i++) { p_buffer[i] = AudioFrame(0.0f, 0.0f); } input_ofs = 0; } else { for (int i = 0; i < p_frames; i++) { - if (audio_input_size >= input_ofs) { + if (input_size >= input_ofs) { float l = (buf[input_ofs++] >> 16) / 32768.f; if (input_ofs >= buf.size()) { input_ofs = 0; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 7c8d61f5456b..14318f282bdb 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -80,6 +80,17 @@ double AudioDriver::get_mix_time() const { return total; } +void AudioDriver::input_buffer_write(int32_t sample) { + + input_buffer.write[input_position++] = sample; + if (input_position >= input_buffer.size()) { + input_position = 0; + } + if (input_size < input_buffer.size()) { + input_size++; + } +} + AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const { switch (p_channels) { case 4: return SPEAKER_SURROUND_31; diff --git a/servers/audio_server.h b/servers/audio_server.h index a9ed66a2d59e..2663a0f968d7 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -53,12 +53,13 @@ class AudioDriver { #endif protected: - Vector audio_input_buffer; - unsigned int audio_input_position; - unsigned int audio_input_size; + Vector input_buffer; + unsigned int input_position; + unsigned int input_size; void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true); void update_mix_time(int p_frames); + void input_buffer_write(int32_t sample); #ifdef DEBUG_ENABLED _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); } @@ -108,9 +109,9 @@ class AudioDriver { SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const; int get_total_channels_by_speaker_mode(SpeakerMode) const; - Vector get_audio_input_buffer() { return audio_input_buffer; } - unsigned int get_audio_input_position() { return audio_input_position; } - unsigned int get_audio_input_size() { return audio_input_size; } + Vector get_input_buffer() { return input_buffer; } + unsigned int get_input_position() { return input_position; } + unsigned int get_input_size() { return input_size; } #ifdef DEBUG_ENABLED uint64_t get_profiling_time() const { return prof_time; } From d91183be524c2a2d13d5c40473297cbc713f43da Mon Sep 17 00:00:00 2001 From: Saracen Date: Fri, 27 Jul 2018 19:34:07 +0100 Subject: [PATCH 12/14] Added pointer to WASAPI input_buffer_write method. --- drivers/wasapi/audio_driver_wasapi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 827aecd1c8aa..a2f619a6ef85 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -695,8 +695,8 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { } } - input_buffer_write(l); - input_buffer_write(r); + ad->input_buffer_write(l); + ad->input_buffer_write(r); } read_frames += num_frames_available; From 913eec5b04fa349efe5ad37ef14ecfffb8f35118 Mon Sep 17 00:00:00 2001 From: Marcelo Fernandez Date: Fri, 27 Jul 2018 17:28:41 -0300 Subject: [PATCH 13/14] Added support for single channel inputs for PulseAudio --- .../pulseaudio/audio_driver_pulseaudio.cpp | 80 ++++++++++++++----- drivers/pulseaudio/audio_driver_pulseaudio.h | 4 +- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 66fdfc9c1b27..987cd9c85f9d 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -64,18 +64,32 @@ void AudioDriverPulseAudio::pa_sink_info_cb(pa_context *c, const pa_sink_info *l ad->pa_status++; } +void AudioDriverPulseAudio::pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) { + AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata; + + // If eol is set to a positive number, you're at the end of the list + if (eol > 0) { + return; + } + + ad->pa_rec_map = l->channel_map; + ad->pa_status++; +} + void AudioDriverPulseAudio::pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) { AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata; + ad->capture_default_device = i->default_source_name; ad->default_device = i->default_sink_name; ad->pa_status++; } -void AudioDriverPulseAudio::detect_channels() { +void AudioDriverPulseAudio::detect_channels(bool capture) { - pa_channel_map_init_stereo(&pa_map); + pa_channel_map_init_stereo(capture ? &pa_rec_map : &pa_map); - if (device_name == "Default") { + String device = capture ? capture_device_name : device_name; + if (device == "Default") { // Get the default output device name pa_status = 0; pa_operation *pa_op = pa_context_get_server_info(pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)this); @@ -93,16 +107,22 @@ void AudioDriverPulseAudio::detect_channels() { } } - char device[1024]; - if (device_name == "Default") { - strcpy(device, default_device.utf8().get_data()); + char dev[1024]; + if (device == "Default") { + strcpy(dev, capture ? capture_default_device.utf8().get_data() : default_device.utf8().get_data()); } else { - strcpy(device, device_name.utf8().get_data()); + strcpy(dev, device.utf8().get_data()); } // Now using the device name get the amount of channels pa_status = 0; - pa_operation *pa_op = pa_context_get_sink_info_by_name(pa_ctx, device, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this); + pa_operation *pa_op; + if (capture) { + pa_op = pa_context_get_source_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_source_info_cb, (void *)this); + } else { + pa_op = pa_context_get_sink_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this); + } + if (pa_op) { while (pa_status == 0) { int ret = pa_mainloop_iterate(pa_ml, 1, NULL); @@ -113,7 +133,11 @@ void AudioDriverPulseAudio::detect_channels() { pa_operation_unref(pa_op); } else { - ERR_PRINT("pa_context_get_sink_info_by_name error"); + if (capture) { + ERR_PRINT("pa_context_get_source_info_by_name error"); + } else { + ERR_PRINT("pa_context_get_sink_info_by_name error"); + } } } @@ -195,6 +219,10 @@ Error AudioDriverPulseAudio::init_device() { samples_in.resize(buffer_frames * channels); samples_out.resize(pa_buffer_size); + // Reset audio input to keep synchronisation. + input_position = 0; + input_size = 0; + return OK; } @@ -389,9 +417,12 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { } else { int16_t *srcptr = (int16_t *)ptr; for (size_t i = bytes >> 1; i > 0; i--) { - ad->input_buffer.write[ad->input_position++] = int32_t(*srcptr++) << 16; - if (ad->input_position >= ad->input_buffer.size()) { - ad->input_position = 0; + int32_t sample = int32_t(*srcptr++) << 16; + ad->input_buffer_write(sample); + + if (ad->pa_rec_map.channels == 1) { + // In case input device is single channel convert it to Stereo + ad->input_buffer_write(sample); } } @@ -571,10 +602,26 @@ Error AudioDriverPulseAudio::capture_init_device() { } } + detect_channels(true); + switch (pa_rec_map.channels) { + case 1: // Mono + case 2: // Stereo + break; + + default: + WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels)); + pa_channel_map_init_stereo(&pa_rec_map); + break; + } + + if (OS::get_singleton()->is_stdout_verbose()) { + print_line("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels"); + } + pa_sample_spec spec; spec.format = PA_SAMPLE_S16LE; - spec.channels = 2; + spec.channels = pa_rec_map.channels; spec.rate = mix_rate; int latency = 30; @@ -584,9 +631,6 @@ Error AudioDriverPulseAudio::capture_init_device() { pa_buffer_attr attr; attr.fragsize = buffer_size * sizeof(int16_t); - pa_channel_map pa_rec_map; - pa_channel_map_init_stereo(&pa_rec_map); - pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map); if (pa_rec_str == NULL) { ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx)))); @@ -602,10 +646,8 @@ Error AudioDriverPulseAudio::capture_init_device() { } input_buffer.resize(input_buffer_frames * 8); - for (int i = 0; i < input_buffer.size(); i++) { - input_buffer.write[i] = 0; - } input_position = 0; + input_size = 0; return OK; } diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index e8b2f19e6e07..f8358a452bce 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -49,6 +49,7 @@ class AudioDriverPulseAudio : public AudioDriver { pa_stream *pa_str; pa_stream *pa_rec_str; pa_channel_map pa_map; + pa_channel_map pa_rec_map; String device_name; String new_device; @@ -79,6 +80,7 @@ class AudioDriverPulseAudio : public AudioDriver { static void pa_state_cb(pa_context *c, void *userdata); static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); + static void pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata); static void pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata); static void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); static void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata); @@ -89,7 +91,7 @@ class AudioDriverPulseAudio : public AudioDriver { Error capture_init_device(); void capture_finish_device(); - void detect_channels(); + void detect_channels(bool capture = false); static void thread_func(void *p_udata); From 59b703c4f80253a64a2a13d76fb75ecd45fc3e86 Mon Sep 17 00:00:00 2001 From: Saracen Date: Tue, 31 Jul 2018 04:46:29 +0100 Subject: [PATCH 14/14] Add extra latency to microphone playback to reduce clipping. --- servers/audio/audio_stream.cpp | 2 +- servers/audio/audio_stream.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 618ebd526d5b..eef8aba0c436 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -138,7 +138,7 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr unsigned int input_size = AudioDriver::get_singleton()->get_input_size(); // p_frames is multipled by two since an AudioFrame is stereo - if ((p_frames * 2) > input_size) { + if ((p_frames + MICROPHONE_PLAYBACK_DELAY * 2) > input_size) { for (int i = 0; i < p_frames; i++) { p_buffer[i] = AudioFrame(0.0f, 0.0f); } diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index f0f756129e27..66e1b6ee2fd8 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -122,6 +122,8 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled { GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlayback) friend class AudioStreamMicrophone; + const int MICROPHONE_PLAYBACK_DELAY = 256; + bool active; unsigned int input_ofs;