2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_rtaudio.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "audio_driver_rtaudio.h"
|
2016-10-13 20:58:40 +02:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
2016-10-13 20:58:40 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef RTAUDIO_ENABLED
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *AudioDriverRtAudio::get_name() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#ifdef OSX_ENABLED
|
|
|
|
return "RtAudio-OSX";
|
|
|
|
#elif defined(UNIX_ENABLED)
|
|
|
|
return "RtAudio-ALSA";
|
|
|
|
#elif defined(WINDOWS_ENABLED)
|
|
|
|
return "RtAudio-DirectSound";
|
|
|
|
#else
|
|
|
|
return "RtAudio-None";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int AudioDriverRtAudio::callback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-08 11:26:54 +02:00
|
|
|
if (status) {
|
|
|
|
if (status & RTAUDIO_INPUT_OVERFLOW) {
|
|
|
|
WARN_PRINT("RtAudio input overflow!");
|
|
|
|
}
|
|
|
|
if (status & RTAUDIO_OUTPUT_UNDERFLOW) {
|
|
|
|
WARN_PRINT("RtAudio output underflow!");
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
int32_t *buffer = (int32_t *)outputBuffer;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioDriverRtAudio *self = (AudioDriverRtAudio *)userData;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (self->mutex->try_lock() != OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// what should i do..
|
2017-03-05 16:44:50 +01:00
|
|
|
for (unsigned int i = 0; i < nBufferFrames; i++)
|
|
|
|
buffer[i] = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
self->audio_server_process(nBufferFrames, buffer);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 18:03:38 +01:00
|
|
|
self->mutex->unlock();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error AudioDriverRtAudio::init() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
active = false;
|
2017-08-02 16:45:19 +02:00
|
|
|
mutex = Mutex::create(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
dac = memnew(RtAudio);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_EXPLAIN("Cannot initialize RtAudio audio driver: No devices present.")
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(dac->getDeviceCount() < 1, ERR_UNAVAILABLE);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-16 19:19:45 +01:00
|
|
|
// FIXME: Adapt to the OutputFormat -> SpeakerMode change
|
|
|
|
/*
|
2018-07-19 23:58:15 +02:00
|
|
|
String channels = GLOBAL_DEF_RST("audio/output","stereo");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (channels=="5.1")
|
|
|
|
output_format=OUTPUT_5_1;
|
|
|
|
else if (channels=="quad")
|
|
|
|
output_format=OUTPUT_QUAD;
|
|
|
|
else if (channels=="mono")
|
|
|
|
output_format=OUTPUT_MONO;
|
|
|
|
else
|
|
|
|
output_format=OUTPUT_STEREO;
|
2017-01-16 19:19:45 +01:00
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
RtAudio::StreamParameters parameters;
|
|
|
|
parameters.deviceId = dac->getDefaultOutputDevice();
|
|
|
|
RtAudio::StreamOptions options;
|
2017-01-03 19:00:31 +01:00
|
|
|
|
|
|
|
// set the desired numberOfBuffers
|
2017-08-27 09:06:16 +02:00
|
|
|
options.numberOfBuffers = 4;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
parameters.firstChannel = 0;
|
2018-07-19 23:58:15 +02:00
|
|
|
mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-29 21:47:44 +02:00
|
|
|
int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
|
|
|
|
unsigned int buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
|
2018-08-24 08:47:34 +02:00
|
|
|
print_verbose("Audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-10-17 08:46:50 +02:00
|
|
|
short int tries = 4;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-10-17 08:46:50 +02:00
|
|
|
while (tries > 0) {
|
2017-08-27 09:06:16 +02:00
|
|
|
switch (speaker_mode) {
|
|
|
|
case SPEAKER_MODE_STEREO: parameters.nChannels = 2; break;
|
2018-10-17 08:46:50 +02:00
|
|
|
case SPEAKER_SURROUND_31: parameters.nChannels = 4; break;
|
2017-08-27 09:06:16 +02:00
|
|
|
case SPEAKER_SURROUND_51: parameters.nChannels = 6; break;
|
|
|
|
case SPEAKER_SURROUND_71: parameters.nChannels = 8; break;
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-27 09:06:16 +02:00
|
|
|
try {
|
2017-08-29 21:47:44 +02:00
|
|
|
dac->openStream(¶meters, NULL, RTAUDIO_SINT32, mix_rate, &buffer_frames, &callback, this, &options);
|
2017-08-27 09:06:16 +02:00
|
|
|
active = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-08-27 09:06:16 +02:00
|
|
|
break;
|
2018-10-04 15:38:52 +02:00
|
|
|
} catch (RtAudioError) {
|
2017-08-27 09:06:16 +02:00
|
|
|
// try with less channels
|
2018-04-22 19:36:01 +02:00
|
|
|
ERR_PRINT("Unable to open audio, retrying with fewer channels...");
|
2017-08-27 09:06:16 +02:00
|
|
|
|
|
|
|
switch (speaker_mode) {
|
2018-10-17 08:46:50 +02:00
|
|
|
case SPEAKER_MODE_STEREO: break; // Required to silence unhandled enum value warning.
|
|
|
|
case SPEAKER_SURROUND_31: speaker_mode = SPEAKER_MODE_STEREO; break;
|
|
|
|
case SPEAKER_SURROUND_51: speaker_mode = SPEAKER_SURROUND_31; break;
|
2017-08-27 09:06:16 +02:00
|
|
|
case SPEAKER_SURROUND_71: speaker_mode = SPEAKER_SURROUND_51; break;
|
2017-01-03 19:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tries--;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-01-03 19:00:31 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-09-13 18:34:22 +02:00
|
|
|
return active ? OK : ERR_UNAVAILABLE;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int AudioDriverRtAudio::get_mix_rate() const {
|
|
|
|
|
|
|
|
return mix_rate;
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:19:45 +01:00
|
|
|
AudioDriver::SpeakerMode AudioDriverRtAudio::get_speaker_mode() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-16 19:19:45 +01:00
|
|
|
return speaker_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverRtAudio::start() {
|
|
|
|
|
|
|
|
if (active)
|
|
|
|
dac->startStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverRtAudio::lock() {
|
|
|
|
|
|
|
|
if (mutex)
|
|
|
|
mutex->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverRtAudio::unlock() {
|
|
|
|
|
|
|
|
if (mutex)
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioDriverRtAudio::finish() {
|
|
|
|
|
2017-08-02 16:45:19 +02:00
|
|
|
lock();
|
|
|
|
if (active && dac->isStreamOpen()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
dac->closeStream();
|
2017-08-02 16:45:19 +02:00
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
if (mutex) {
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete(mutex);
|
2017-08-02 16:45:19 +02:00
|
|
|
mutex = NULL;
|
|
|
|
}
|
|
|
|
if (dac) {
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete(dac);
|
2017-08-02 16:45:19 +02:00
|
|
|
dac = NULL;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
AudioDriverRtAudio::AudioDriverRtAudio() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-27 19:01:34 +02:00
|
|
|
active = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
mutex = NULL;
|
2017-08-02 16:45:19 +02:00
|
|
|
dac = NULL;
|
2017-08-29 21:47:44 +02:00
|
|
|
mix_rate = DEFAULT_MIX_RATE;
|
2017-03-05 16:44:50 +01:00
|
|
|
speaker_mode = SPEAKER_MODE_STEREO;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|