audio_driver_opensl.cpp 8.4 KB

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