audio_driver_opensl.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*************************************************************************/
  2. /* audio_driver_opensl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_opensl.h"
  31. #include <string.h>
  32. #define MAX_NUMBER_INTERFACES 3
  33. #define MAX_NUMBER_OUTPUT_DEVICES 6
  34. /* Structure for passing information to callback function */
  35. void AudioDriverOpenSL::_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) {
  36. bool mix = true;
  37. if (pause) {
  38. mix = false;
  39. } else if (mutex) {
  40. mix = mutex->try_lock() == OK;
  41. }
  42. if (mix) {
  43. audio_server_process(buffer_size, mixdown_buffer);
  44. } else {
  45. int32_t *src_buff = mixdown_buffer;
  46. for (int i = 0; i < buffer_size * 2; i++) {
  47. src_buff[i] = 0;
  48. }
  49. }
  50. if (mutex && mix)
  51. mutex->unlock();
  52. const int32_t *src_buff = mixdown_buffer;
  53. int16_t *ptr = (int16_t *)buffers[last_free];
  54. last_free = (last_free + 1) % BUFFER_COUNT;
  55. for (int i = 0; i < buffer_size * 2; i++) {
  56. ptr[i] = src_buff[i] >> 16;
  57. }
  58. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  59. }
  60. void AudioDriverOpenSL::_buffer_callbacks(SLAndroidSimpleBufferQueueItf queueItf, void *pContext) {
  61. AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext;
  62. ad->_buffer_callback(queueItf);
  63. }
  64. AudioDriverOpenSL *AudioDriverOpenSL::s_ad = NULL;
  65. const char *AudioDriverOpenSL::get_name() const {
  66. return "Android";
  67. }
  68. Error AudioDriverOpenSL::init() {
  69. SLresult
  70. res;
  71. SLEngineOption EngineOption[] = {
  72. (SLuint32)SL_ENGINEOPTION_THREADSAFE,
  73. (SLuint32)SL_BOOLEAN_TRUE
  74. };
  75. res = slCreateEngine(&sl, 1, EngineOption, 0, NULL, NULL);
  76. if (res != SL_RESULT_SUCCESS) {
  77. ERR_EXPLAIN("Could not Initialize OpenSL");
  78. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  79. }
  80. res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
  81. if (res != SL_RESULT_SUCCESS) {
  82. ERR_EXPLAIN("Could not Realize OpenSL");
  83. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  84. }
  85. return OK;
  86. }
  87. void AudioDriverOpenSL::start() {
  88. mutex = Mutex::create();
  89. active = false;
  90. SLint32 numOutputs = 0;
  91. SLuint32 deviceID = 0;
  92. SLresult res;
  93. buffer_size = 1024;
  94. for (int i = 0; i < BUFFER_COUNT; i++) {
  95. buffers[i] = memnew_arr(int16_t, buffer_size * 2);
  96. memset(buffers[i], 0, buffer_size * 4);
  97. }
  98. mixdown_buffer = memnew_arr(int32_t, buffer_size * 2);
  99. /* Callback context for the buffer queue callback function */
  100. /* Get the SL Engine Interface which is implicit */
  101. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf);
  102. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  103. /* Initialize arrays required[] and iidArray[] */
  104. SLboolean required[MAX_NUMBER_INTERFACES];
  105. SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
  106. {
  107. const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB };
  108. const SLboolean req[1] = { SL_BOOLEAN_FALSE };
  109. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req);
  110. }
  111. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  112. // Realizing the Output Mix object in synchronous mode.
  113. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  114. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  115. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT };
  116. /* Setup the format of the content in the buffer queue */
  117. pcm.formatType = SL_DATAFORMAT_PCM;
  118. pcm.numChannels = 2;
  119. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  120. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  121. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  122. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  123. #ifdef BIG_ENDIAN_ENABLED
  124. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  125. #else
  126. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  127. #endif
  128. audioSource.pFormat = (void *)&pcm;
  129. audioSource.pLocator = (void *)&loc_bufq;
  130. /* Setup the data sink structure */
  131. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  132. locator_outputmix.outputMix = OutputMix;
  133. audioSink.pLocator = (void *)&locator_outputmix;
  134. audioSink.pFormat = NULL;
  135. /* Set arrays required[] and iidArray[] for SEEK interface (PlayItf is implicit) */
  136. required[0] = SL_BOOLEAN_TRUE;
  137. iidArray[0] = SL_IID_BUFFERQUEUE;
  138. /* Create the music player */
  139. {
  140. const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND };
  141. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  142. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req);
  143. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  144. }
  145. /* Realizing the player in synchronous mode. */
  146. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  147. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  148. /* Get seek and play interfaces */
  149. res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf);
  150. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  151. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  152. (void *)&bufferQueueItf);
  153. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  154. /* Setup to receive buffer queue event callbacks */
  155. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this);
  156. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  157. last_free = 0;
  158. //fill up buffers
  159. for (int i = 0; i < BUFFER_COUNT; i++) {
  160. /* Enqueue a few buffers to get the ball rolling */
  161. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */
  162. }
  163. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  164. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  165. active = true;
  166. }
  167. int AudioDriverOpenSL::get_mix_rate() const {
  168. return 44100;
  169. }
  170. AudioDriverSW::OutputFormat AudioDriverOpenSL::get_output_format() const {
  171. return OUTPUT_STEREO;
  172. }
  173. void AudioDriverOpenSL::lock() {
  174. if (active && mutex)
  175. mutex->lock();
  176. }
  177. void AudioDriverOpenSL::unlock() {
  178. if (active && mutex)
  179. mutex->unlock();
  180. }
  181. void AudioDriverOpenSL::finish() {
  182. (*sl)->Destroy(sl);
  183. }
  184. void AudioDriverOpenSL::set_pause(bool p_pause) {
  185. SLresult res;
  186. SLuint32 playState;
  187. pause = p_pause;
  188. if (active) {
  189. res = (*playItf)->GetPlayState(playItf, &playState);
  190. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  191. if (pause && playState == SL_PLAYSTATE_PLAYING) {
  192. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  193. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  194. } else if (!pause && playState == SL_PLAYSTATE_PAUSED) {
  195. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  196. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  197. }
  198. }
  199. }
  200. AudioDriverOpenSL::AudioDriverOpenSL() {
  201. s_ad = this;
  202. pause = false;
  203. }