audio_driver_rtaudio.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* audio_driver_rtaudio.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "audio_driver_rtaudio.h"
  31. #include "globals.h"
  32. #include "os/os.h"
  33. #ifdef RTAUDIO_ENABLED
  34. const char *AudioDriverRtAudio::get_name() const {
  35. #ifdef OSX_ENABLED
  36. return "RtAudio-OSX";
  37. #elif defined(UNIX_ENABLED)
  38. return "RtAudio-ALSA";
  39. #elif defined(WINDOWS_ENABLED)
  40. return "RtAudio-DirectSound";
  41. #else
  42. return "RtAudio-None";
  43. #endif
  44. }
  45. int AudioDriverRtAudio::callback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData) {
  46. if (status) {
  47. if (status & RTAUDIO_INPUT_OVERFLOW) {
  48. WARN_PRINT("RtAudio input overflow!");
  49. }
  50. if (status & RTAUDIO_OUTPUT_UNDERFLOW) {
  51. WARN_PRINT("RtAudio output underflow!");
  52. }
  53. }
  54. int32_t *buffer = (int32_t *)outputBuffer;
  55. AudioDriverRtAudio *self = (AudioDriverRtAudio *)userData;
  56. if (self->mutex->try_lock() != OK) {
  57. // what should i do..
  58. for (unsigned int i = 0; i < nBufferFrames; i++)
  59. buffer[i] = 0;
  60. return 0;
  61. }
  62. self->audio_server_process(nBufferFrames, buffer);
  63. self->mutex->unlock();
  64. return 0;
  65. }
  66. Error AudioDriverRtAudio::init() {
  67. active = false;
  68. mutex = NULL;
  69. dac = memnew(RtAudio);
  70. ERR_EXPLAIN("Cannot initialize RtAudio audio driver: No devices present.")
  71. ERR_FAIL_COND_V(dac->getDeviceCount() < 1, ERR_UNAVAILABLE);
  72. String channels = GLOBAL_DEF("audio/output", "stereo");
  73. if (channels == "5.1")
  74. output_format = OUTPUT_5_1;
  75. else if (channels == "quad")
  76. output_format = OUTPUT_QUAD;
  77. else if (channels == "mono")
  78. output_format = OUTPUT_MONO;
  79. else
  80. output_format = OUTPUT_STEREO;
  81. RtAudio::StreamParameters parameters;
  82. parameters.deviceId = dac->getDefaultOutputDevice();
  83. RtAudio::StreamOptions options;
  84. // set the desired numberOfBuffers
  85. unsigned int target_number_of_buffers = 4;
  86. options.numberOfBuffers = target_number_of_buffers;
  87. // options.
  88. // RtAudioStreamFlags flags; /*!< A bit-mask of stream flags (RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE). *///
  89. // unsigned int numberOfBuffers; /*!< Number of stream buffers. */
  90. // std::string streamName; /*!< A stream name (currently used only in Jack). */
  91. // int priority; /*!< Scheduling priority of callback thread (only used with flag RTAUDIO_SCHEDULE_REALTIME). */
  92. parameters.firstChannel = 0;
  93. mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
  94. int latency = GLOBAL_DEF("audio/output_latency", 25);
  95. // calculate desired buffer_size, taking the desired numberOfBuffers into account (latency depends on numberOfBuffers*buffer_size)
  96. unsigned int buffer_size = next_power_of_2(latency * mix_rate / 1000 / target_number_of_buffers);
  97. if (OS::get_singleton()->is_stdout_verbose()) {
  98. print_line("audio buffer size: " + itos(buffer_size));
  99. }
  100. short int tries = 2;
  101. while (true) {
  102. while (true) {
  103. switch (output_format) {
  104. case OUTPUT_MONO: parameters.nChannels = 1; break;
  105. case OUTPUT_STEREO: parameters.nChannels = 2; break;
  106. case OUTPUT_QUAD: parameters.nChannels = 4; break;
  107. case OUTPUT_5_1: parameters.nChannels = 6; break;
  108. };
  109. try {
  110. dac->openStream(&parameters, NULL, RTAUDIO_SINT32, mix_rate, &buffer_size, &callback, this, &options);
  111. mutex = Mutex::create(true);
  112. active = true;
  113. break;
  114. } catch (RtAudioError &e) {
  115. // try with less channels
  116. ERR_PRINT("Unable to open audio, retrying with fewer channels..");
  117. switch (output_format) {
  118. case OUTPUT_MONO:
  119. ERR_EXPLAIN("Unable to open audio.");
  120. ERR_FAIL_V(ERR_UNAVAILABLE);
  121. break;
  122. case OUTPUT_STEREO: output_format = OUTPUT_MONO; break;
  123. case OUTPUT_QUAD: output_format = OUTPUT_STEREO; break;
  124. case OUTPUT_5_1: output_format = OUTPUT_QUAD; break;
  125. };
  126. }
  127. }
  128. // compare actual numberOfBuffers with the desired one. If not equal, close and reopen the stream with adjusted buffer size, so the desired output_latency is still correct
  129. if (target_number_of_buffers != options.numberOfBuffers) {
  130. if (tries <= 0) {
  131. ERR_EXPLAIN("RtAudio: Unable to set correct number of buffers.");
  132. ERR_FAIL_V(ERR_UNAVAILABLE);
  133. break;
  134. }
  135. try {
  136. dac->closeStream();
  137. } catch (RtAudioError &e) {
  138. ERR_PRINT(e.what());
  139. ERR_FAIL_V(ERR_UNAVAILABLE);
  140. break;
  141. }
  142. if (OS::get_singleton()->is_stdout_verbose())
  143. print_line("RtAudio: Desired number of buffers (" + itos(target_number_of_buffers) + ") not available. Using " + itos(options.numberOfBuffers) + " instead. Reopening stream with adjusted buffer_size.");
  144. // new buffer size dependent on the ratio between set and actual numberOfBuffers
  145. buffer_size = buffer_size / (options.numberOfBuffers / target_number_of_buffers);
  146. target_number_of_buffers = options.numberOfBuffers;
  147. tries--;
  148. } else {
  149. break;
  150. }
  151. }
  152. return active ? OK : ERR_UNAVAILABLE;
  153. }
  154. int AudioDriverRtAudio::get_mix_rate() const {
  155. return mix_rate;
  156. }
  157. AudioDriverSW::OutputFormat AudioDriverRtAudio::get_output_format() const {
  158. return output_format;
  159. }
  160. void AudioDriverRtAudio::start() {
  161. if (active)
  162. dac->startStream();
  163. }
  164. void AudioDriverRtAudio::lock() {
  165. if (mutex)
  166. mutex->lock();
  167. }
  168. void AudioDriverRtAudio::unlock() {
  169. if (mutex)
  170. mutex->unlock();
  171. }
  172. void AudioDriverRtAudio::finish() {
  173. if (active && dac->isStreamOpen())
  174. dac->closeStream();
  175. if (mutex)
  176. memdelete(mutex);
  177. if (dac)
  178. memdelete(dac);
  179. }
  180. AudioDriverRtAudio::AudioDriverRtAudio() {
  181. active = false;
  182. mutex = NULL;
  183. mix_rate = 44100;
  184. output_format = OUTPUT_STEREO;
  185. }
  186. #endif