audio_driver_opensl.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-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_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. print_line("OpenSL Init OK!");
  94. return OK;
  95. }
  96. void AudioDriverOpenSL::start() {
  97. mutex = Mutex::create();
  98. active = false;
  99. SLint32 numOutputs = 0;
  100. SLuint32 deviceID = 0;
  101. SLresult res;
  102. buffer_size = 1024;
  103. for (int i = 0; i < BUFFER_COUNT; i++) {
  104. buffers[i] = memnew_arr(int16_t, buffer_size * 2);
  105. memset(buffers[i], 0, buffer_size * 4);
  106. }
  107. mixdown_buffer = memnew_arr(int32_t, buffer_size * 2);
  108. /* Callback context for the buffer queue callback function */
  109. /* Get the SL Engine Interface which is implicit */
  110. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf);
  111. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  112. /* Initialize arrays required[] and iidArray[] */
  113. SLboolean required[MAX_NUMBER_INTERFACES];
  114. SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
  115. {
  116. const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB };
  117. const SLboolean req[1] = { SL_BOOLEAN_FALSE };
  118. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req);
  119. }
  120. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  121. // Realizing the Output Mix object in synchronous mode.
  122. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  123. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  124. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT };
  125. //bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
  126. //bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */
  127. /* Setup the format of the content in the buffer queue */
  128. pcm.formatType = SL_DATAFORMAT_PCM;
  129. pcm.numChannels = 2;
  130. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  131. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  132. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  133. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  134. #ifdef BIG_ENDIAN_ENABLED
  135. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  136. #else
  137. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  138. #endif
  139. audioSource.pFormat = (void *)&pcm;
  140. audioSource.pLocator = (void *)&loc_bufq;
  141. /* Setup the data sink structure */
  142. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  143. locator_outputmix.outputMix = OutputMix;
  144. audioSink.pLocator = (void *)&locator_outputmix;
  145. audioSink.pFormat = NULL;
  146. /* Initialize the context for Buffer queue callbacks */
  147. //cntxt.pDataBase = (void*)&pcmData;
  148. //cntxt.pData = cntxt.pDataBase;
  149. //cntxt.size = sizeof(pcmData);
  150. /* Set arrays required[] and iidArray[] for SEEK interface
  151. (PlayItf is implicit) */
  152. required[0] = SL_BOOLEAN_TRUE;
  153. iidArray[0] = SL_IID_BUFFERQUEUE;
  154. /* Create the music player */
  155. {
  156. const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND };
  157. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  158. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req);
  159. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  160. }
  161. /* Realizing the player in synchronous mode. */
  162. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  163. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  164. /* Get seek and play interfaces */
  165. res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf);
  166. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  167. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  168. (void *)&bufferQueueItf);
  169. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  170. /* Setup to receive buffer queue event callbacks */
  171. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this);
  172. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  173. last_free = 0;
  174. //fill up buffers
  175. for (int i = 0; i < BUFFER_COUNT; i++) {
  176. /* Enqueue a few buffers to get the ball rolling */
  177. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */
  178. }
  179. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  180. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  181. active = true;
  182. }
  183. int AudioDriverOpenSL::get_mix_rate() const {
  184. return 44100;
  185. }
  186. AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const {
  187. return SPEAKER_MODE_STEREO;
  188. }
  189. void AudioDriverOpenSL::lock() {
  190. if (active && mutex)
  191. mutex->lock();
  192. }
  193. void AudioDriverOpenSL::unlock() {
  194. if (active && mutex)
  195. mutex->unlock();
  196. }
  197. void AudioDriverOpenSL::finish() {
  198. (*sl)->Destroy(sl);
  199. }
  200. void AudioDriverOpenSL::set_pause(bool p_pause) {
  201. pause = p_pause;
  202. if (active) {
  203. if (pause) {
  204. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  205. } else {
  206. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  207. }
  208. }
  209. }
  210. AudioDriverOpenSL::AudioDriverOpenSL() {
  211. s_ad = this;
  212. mutex = Mutex::create(); //NULL;
  213. pause = false;
  214. }