audio_driver_opensl.cpp 8.4 KB

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