audio_driver_alsa.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*************************************************************************/
  2. /* audio_driver_alsa.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_alsa.h"
  31. #ifdef ALSA_ENABLED
  32. #include "os/os.h"
  33. #include "project_settings.h"
  34. #include <errno.h>
  35. Error AudioDriverALSA::init() {
  36. active = false;
  37. thread_exited = false;
  38. exit_thread = false;
  39. pcm_open = false;
  40. samples_in = NULL;
  41. samples_out = NULL;
  42. mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
  43. speaker_mode = SPEAKER_MODE_STEREO;
  44. channels = 2;
  45. int status;
  46. snd_pcm_hw_params_t *hwparams;
  47. snd_pcm_sw_params_t *swparams;
  48. #define CHECK_FAIL(m_cond) \
  49. if (m_cond) { \
  50. fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \
  51. snd_pcm_close(pcm_handle); \
  52. ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \
  53. }
  54. //todo, add
  55. //6 chans - "plug:surround51"
  56. //4 chans - "plug:surround40";
  57. status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
  58. ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN);
  59. snd_pcm_hw_params_alloca(&hwparams);
  60. status = snd_pcm_hw_params_any(pcm_handle, hwparams);
  61. CHECK_FAIL(status < 0);
  62. status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  63. CHECK_FAIL(status < 0);
  64. //not interested in anything else
  65. status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
  66. CHECK_FAIL(status < 0);
  67. //todo: support 4 and 6
  68. status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
  69. CHECK_FAIL(status < 0);
  70. status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
  71. CHECK_FAIL(status < 0);
  72. // In ALSA the period size seems to be the one that will determine the actual latency
  73. // Ref: https://www.alsa-project.org/main/index.php/FramesPeriods
  74. unsigned int periods = 2;
  75. int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  76. buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
  77. buffer_size = buffer_frames * periods;
  78. period_size = buffer_frames;
  79. // set buffer size from project settings
  80. status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
  81. CHECK_FAIL(status < 0);
  82. status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
  83. CHECK_FAIL(status < 0);
  84. if (OS::get_singleton()->is_stdout_verbose()) {
  85. print_line("audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms");
  86. }
  87. status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
  88. CHECK_FAIL(status < 0);
  89. status = snd_pcm_hw_params(pcm_handle, hwparams);
  90. CHECK_FAIL(status < 0);
  91. //snd_pcm_hw_params_free(&hwparams);
  92. snd_pcm_sw_params_alloca(&swparams);
  93. status = snd_pcm_sw_params_current(pcm_handle, swparams);
  94. CHECK_FAIL(status < 0);
  95. status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
  96. CHECK_FAIL(status < 0);
  97. status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
  98. CHECK_FAIL(status < 0);
  99. status = snd_pcm_sw_params(pcm_handle, swparams);
  100. CHECK_FAIL(status < 0);
  101. samples_in = memnew_arr(int32_t, period_size * channels);
  102. samples_out = memnew_arr(int16_t, period_size * channels);
  103. snd_pcm_nonblock(pcm_handle, 0);
  104. mutex = Mutex::create();
  105. thread = Thread::create(AudioDriverALSA::thread_func, this);
  106. return OK;
  107. };
  108. void AudioDriverALSA::thread_func(void *p_udata) {
  109. AudioDriverALSA *ad = (AudioDriverALSA *)p_udata;
  110. while (!ad->exit_thread) {
  111. if (!ad->active) {
  112. for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
  113. ad->samples_out[i] = 0;
  114. };
  115. } else {
  116. ad->lock();
  117. ad->audio_server_process(ad->period_size, ad->samples_in);
  118. ad->unlock();
  119. for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
  120. ad->samples_out[i] = ad->samples_in[i] >> 16;
  121. }
  122. };
  123. int todo = ad->period_size;
  124. int total = 0;
  125. while (todo) {
  126. if (ad->exit_thread)
  127. break;
  128. uint8_t *src = (uint8_t *)ad->samples_out;
  129. int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
  130. if (wrote < 0) {
  131. if (ad->exit_thread)
  132. break;
  133. if (wrote == -EAGAIN) {
  134. //can't write yet (though this is blocking..)
  135. usleep(1000);
  136. continue;
  137. }
  138. wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
  139. if (wrote < 0) {
  140. //absolute fail
  141. fprintf(stderr, "ALSA failed and can't recover: %s\n", snd_strerror(wrote));
  142. ad->active = false;
  143. ad->exit_thread = true;
  144. break;
  145. }
  146. continue;
  147. };
  148. total += wrote;
  149. todo -= wrote;
  150. };
  151. };
  152. ad->thread_exited = true;
  153. };
  154. void AudioDriverALSA::start() {
  155. active = true;
  156. };
  157. int AudioDriverALSA::get_mix_rate() const {
  158. return mix_rate;
  159. };
  160. AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const {
  161. return speaker_mode;
  162. };
  163. void AudioDriverALSA::lock() {
  164. if (!thread || !mutex)
  165. return;
  166. mutex->lock();
  167. };
  168. void AudioDriverALSA::unlock() {
  169. if (!thread || !mutex)
  170. return;
  171. mutex->unlock();
  172. };
  173. void AudioDriverALSA::finish() {
  174. if (!thread)
  175. return;
  176. exit_thread = true;
  177. Thread::wait_to_finish(thread);
  178. if (pcm_open)
  179. snd_pcm_close(pcm_handle);
  180. if (samples_in) {
  181. memdelete_arr(samples_in);
  182. memdelete_arr(samples_out);
  183. };
  184. memdelete(thread);
  185. if (mutex)
  186. memdelete(mutex);
  187. thread = NULL;
  188. };
  189. AudioDriverALSA::AudioDriverALSA() {
  190. mutex = NULL;
  191. thread = NULL;
  192. pcm_handle = NULL;
  193. };
  194. AudioDriverALSA::~AudioDriverALSA(){
  195. };
  196. #endif