summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorSaracen <SaracenOne@gmail.com>2018-02-27 07:54:56 +0000
committerSaracen <SaracenOne@gmail.com>2018-07-27 16:50:09 +0100
commitf85062495c3ee477b7ab38f4de435435aedaf375 (patch)
tree2b1e6f45e8c733e32ebec13a72d6e10e9f65c206 /drivers
parent6afabc03350f468b253524d600c978ae3aa66e07 (diff)
Experimental microphone support.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/alsa/audio_driver_alsa.cpp22
-rw-r--r--drivers/alsa/audio_driver_alsa.h5
-rw-r--r--drivers/coreaudio/audio_driver_coreaudio.cpp22
-rw-r--r--drivers/coreaudio/audio_driver_coreaudio.h5
-rw-r--r--drivers/pulseaudio/audio_driver_pulseaudio.cpp22
-rw-r--r--drivers/pulseaudio/audio_driver_pulseaudio.h5
-rw-r--r--drivers/rtaudio/audio_driver_rtaudio.cpp22
-rw-r--r--drivers/rtaudio/audio_driver_rtaudio.h5
-rw-r--r--drivers/wasapi/audio_driver_wasapi.cpp380
-rw-r--r--drivers/wasapi/audio_driver_wasapi.h25
-rw-r--r--drivers/xaudio2/audio_driver_xaudio2.cpp22
-rw-r--r--drivers/xaudio2/audio_driver_xaudio2.h5
12 files changed, 514 insertions, 26 deletions
diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp
index a44a11a46d..29b1207927 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 e2a2325cf3..59bc7c4968 100644
--- a/drivers/alsa/audio_driver_alsa.h
+++ b/drivers/alsa/audio_driver_alsa.h
@@ -83,6 +83,11 @@ public:
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 ac21de91e4..e13b81ddab 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 99c910498e..0d3efca28d 100644
--- a/drivers/coreaudio/audio_driver_coreaudio.h
+++ b/drivers/coreaudio/audio_driver_coreaudio.h
@@ -86,6 +86,11 @@ public:
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 6db0e58737..49d3ab2070 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 b471f5f9d5..bfbd340e26 100644
--- a/drivers/pulseaudio/audio_driver_pulseaudio.h
+++ b/drivers/pulseaudio/audio_driver_pulseaudio.h
@@ -98,6 +98,11 @@ public:
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 365788e192..0fad96f133 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 2a64652d5f..789a21ee32 100644
--- a/drivers/rtaudio/audio_driver_rtaudio.h
+++ b/drivers/rtaudio/audio_driver_rtaudio.h
@@ -58,6 +58,11 @@ public:
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 5982955c4f..db09a61066 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 <Functiondiscoverykeys_devpkey.h>
+
#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 @@ public:
}
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 @@ public:
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(&notif_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 **)&microphone_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 **)&microphone_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<MicrophoneDeviceOutputDirectWASAPI *>(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<StringName, StringName>::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<MicrophoneDeviceOutputDirectWASAPI *>(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<MicrophoneDeviceOutputDirectWASAPI *>(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<MicrophoneDeviceOutputDirectWASAPI *>(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<MicrophoneDeviceOutputDirectWASAPI *>(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 f3ee5976eb..084d0c2e3f 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<int32_t> samples_in;
+ Map<StringName, StringName> 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 @@ public:
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 a1002ef4f9..2a320b620e 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 42e1adb2b7..b78ab136da 100644
--- a/drivers/xaudio2/audio_driver_xaudio2.h
+++ b/drivers/xaudio2/audio_driver_xaudio2.h
@@ -103,6 +103,11 @@ public:
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();
};