Experimental microphone support.
This commit is contained in:
parent
6afabc0335
commit
f85062495c
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
static bool default_device_changed = false;
|
||||
#define SAFE_RELEASE(memory) \
|
||||
if ((memory) != NULL) { \
|
||||
(memory)->Release(); \
|
||||
(memory) = NULL; \
|
||||
}
|
||||
|
||||
#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(¬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<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
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -70,6 +70,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 void set_pause(bool p_pause);
|
||||
|
||||
static void setup(jobject p_io);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 void set_pause(bool p_pause);
|
||||
|
||||
AudioDriverOpenSL();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -68,6 +68,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();
|
||||
|
||||
AudioDriverDummy();
|
||||
~AudioDriverDummy();
|
||||
};
|
||||
|
|
|
@ -99,6 +99,129 @@ void AudioStream::_bind_methods() {
|
|||
|
||||
////////////////////////////////
|
||||
|
||||
Ref<AudioStreamPlayback> AudioStreamMicrophone::instance_playback() {
|
||||
Ref<AudioStreamPlaybackMicrophone> playback;
|
||||
playback.instance();
|
||||
|
||||
playbacks.insert(playback.ptr());
|
||||
|
||||
playback->microphone = Ref<AudioStreamMicrophone>((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<AudioStreamPlaybackMicrophone *>::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<AudioFrame> &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<AudioStream> &p_audio_stream) {
|
||||
|
||||
audio_stream = p_audio_stream;
|
||||
|
|
|
@ -94,6 +94,66 @@ public:
|
|||
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<AudioStreamPlaybackMicrophone *> playbacks;
|
||||
StringName microphone_name;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
virtual Ref<AudioStreamPlayback> 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<AudioStreamMicrophone> 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 {
|
||||
|
|
|
@ -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<StringName, MicrophoneDeviceOutput *>::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<StringName, MicrophoneDeviceOutput *>::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);
|
||||
|
|
|
@ -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 @@ protected:
|
|||
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<MicrophoneReciever *> recievers;
|
||||
|
||||
virtual unsigned int get_mix_rate() = 0;
|
||||
virtual Vector<AudioFrame> &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<AudioFrame> buffer;
|
||||
|
||||
unsigned int get_mix_rate() {
|
||||
return mix_rate;
|
||||
};
|
||||
|
||||
Vector<AudioFrame> &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<AudioFrame> &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<MicrophoneDeviceOutput *> microphone_device_outputs;
|
||||
Map<StringName, MicrophoneDeviceOutput *> microphone_device_output_map;
|
||||
|
||||
Vector<MicrophoneReciever *> direct_recievers;
|
||||
|
||||
enum SpeakerMode {
|
||||
SPEAKER_MODE_STEREO,
|
||||
SPEAKER_SURROUND_31,
|
||||
|
@ -91,11 +209,19 @@ public:
|
|||
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 @@ private:
|
|||
|
||||
void _mix_step();
|
||||
|
||||
#if 0
|
||||
struct AudioInBlock {
|
||||
|
||||
Ref<AudioStreamSample> audio_stream;
|
||||
int current_position;
|
||||
bool loops;
|
||||
};
|
||||
|
||||
Map<StringName, AudioInBlock *> audio_in_block_map;
|
||||
Vector<AudioInBlock *> audio_in_blocks;
|
||||
#endif
|
||||
|
||||
struct CallbackItem {
|
||||
|
||||
AudioCallback callback;
|
||||
|
@ -236,6 +374,7 @@ private:
|
|||
|
||||
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 @@ public:
|
|||
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();
|
||||
};
|
||||
|
|
|
@ -104,6 +104,7 @@ void register_server_types() {
|
|||
|
||||
ClassDB::register_virtual_class<AudioStream>();
|
||||
ClassDB::register_virtual_class<AudioStreamPlayback>();
|
||||
ClassDB::register_class<AudioStreamMicrophone>();
|
||||
ClassDB::register_class<AudioStreamRandomPitch>();
|
||||
ClassDB::register_virtual_class<AudioEffect>();
|
||||
ClassDB::register_class<AudioEffectEQ>();
|
||||
|
|
Loading…
Reference in New Issue