audio_driver_opensl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* audio_driver_opensl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_driver_opensl.h"
  30. #include <string.h>
  31. #define MAX_NUMBER_INTERFACES 3
  32. #define MAX_NUMBER_OUTPUT_DEVICES 6
  33. /* Structure for passing information to callback function */
  34. void AudioDriverOpenSL::_buffer_callback(
  35. SLAndroidSimpleBufferQueueItf queueItf
  36. /* SLuint32 eventFlags,
  37. const void * pBuffer,
  38. SLuint32 bufferSize,
  39. SLuint32 dataUsed*/) {
  40. bool mix=true;
  41. if (pause) {
  42. mix=false;
  43. } else if (mutex) {
  44. mix = mutex->try_lock()==OK;
  45. }
  46. if (mix) {
  47. audio_server_process(buffer_size,mixdown_buffer);
  48. } else {
  49. int32_t* src_buff=mixdown_buffer;
  50. for(int i=0;i<buffer_size*2;i++) {
  51. src_buff[i]=0;
  52. }
  53. }
  54. if (mutex && mix)
  55. mutex->unlock();
  56. const int32_t* src_buff=mixdown_buffer;
  57. int16_t *ptr = (int16_t*)buffers[last_free];
  58. last_free=(last_free+1)%BUFFER_COUNT;
  59. for(int i=0;i<buffer_size*2;i++) {
  60. ptr[i]=src_buff[i]>>16;
  61. }
  62. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  63. #if 0
  64. SLresult res;
  65. CallbackCntxt *pCntxt = (CallbackCntxt*)pContext;
  66. if(pCntxt->pData < (pCntxt->pDataBase + pCntxt->size))
  67. {
  68. res = (*queueItf)->Enqueue(queueItf, (void*) pCntxt->pData,
  69. 2 * AUDIO_DATA_BUFFER_SIZE, SL_BOOLEAN_FALSE); /* Size given
  70. in bytes. */
  71. CheckErr(res);
  72. /* Increase data pointer by buffer size */
  73. pCntxt->pData += AUDIO_DATA_BUFFER_SIZE;
  74. }
  75. }
  76. #endif
  77. }
  78. void AudioDriverOpenSL::_buffer_callbacks(
  79. SLAndroidSimpleBufferQueueItf queueItf,
  80. /*SLuint32 eventFlags,
  81. const void * pBuffer,
  82. SLuint32 bufferSize,
  83. SLuint32 dataUsed,*/
  84. void *pContext) {
  85. AudioDriverOpenSL *ad = (AudioDriverOpenSL*)pContext;
  86. //ad->_buffer_callback(queueItf,eventFlags,pBuffer,bufferSize,dataUsed);
  87. ad->_buffer_callback(queueItf);
  88. }
  89. AudioDriverOpenSL* AudioDriverOpenSL::s_ad=NULL;
  90. const char* AudioDriverOpenSL::get_name() const {
  91. return "Android";
  92. }
  93. #if 0
  94. int AudioDriverOpenSL::thread_func(SceSize args, void *argp) {
  95. AudioDriverOpenSL* ad = s_ad;
  96. sceAudioOutput2Reserve(AUDIO_OUTPUT_SAMPLE);
  97. int half=0;
  98. while(!ad->exit_thread) {
  99. int16_t *ptr = &ad->outbuff[AUDIO_OUTPUT_SAMPLE*2*half];
  100. if (!ad->active) {
  101. for(int i=0;i<AUDIO_OUTPUT_SAMPLE*2;i++) {
  102. ptr[i]=0;
  103. }
  104. } else {
  105. //printf("samples: %i\n",AUDIO_OUTPUT_SAMPLE);
  106. ad->lock();
  107. ad->audio_server_process(AUDIO_OUTPUT_SAMPLE,ad->outbuff_32);
  108. ad->unlock();
  109. const int32_t* src_buff=ad->outbuff_32;
  110. for(int i=0;i<AUDIO_OUTPUT_SAMPLE*2;i++) {
  111. ptr[i]=src_buff[i]>>16;
  112. }
  113. }
  114. /* Output 16-bit PCM STEREO data that is in pcmBuf without changing the volume */
  115. sceAudioOutput2OutputBlocking(
  116. SCE_AUDIO_VOLUME_0dB*3, //0db at 0x8000, that's obvious
  117. ptr
  118. );
  119. if (half)
  120. half=0;
  121. else
  122. half=1;
  123. }
  124. sceAudioOutput2Release();
  125. sceKernelExitThread(SCE_KERNEL_EXIT_SUCCESS);
  126. ad->thread_exited=true;
  127. return SCE_KERNEL_EXIT_SUCCESS;
  128. }
  129. #endif
  130. Error AudioDriverOpenSL::init(){
  131. SLresult
  132. res;
  133. SLEngineOption EngineOption[] = {
  134. (SLuint32) SL_ENGINEOPTION_THREADSAFE,
  135. (SLuint32) SL_BOOLEAN_TRUE
  136. };
  137. res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
  138. if (res!=SL_RESULT_SUCCESS) {
  139. ERR_EXPLAIN("Could not Initialize OpenSL");
  140. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  141. }
  142. res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
  143. if (res!=SL_RESULT_SUCCESS) {
  144. ERR_EXPLAIN("Could not Realize OpenSL");
  145. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  146. }
  147. print_line("OpenSL Init OK!");
  148. return OK;
  149. }
  150. void AudioDriverOpenSL::start(){
  151. mutex = Mutex::create();
  152. active=false;
  153. SLint32 numOutputs = 0;
  154. SLuint32 deviceID = 0;
  155. SLresult res;
  156. buffer_size = 1024;
  157. for(int i=0;i<BUFFER_COUNT;i++) {
  158. buffers[i]=memnew_arr( int16_t,buffer_size*2 );
  159. memset(buffers[i],0,buffer_size*4);
  160. }
  161. mixdown_buffer = memnew_arr( int32_t,buffer_size* 2);
  162. /* Callback context for the buffer queue callback function */
  163. /* Get the SL Engine Interface which is implicit */
  164. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
  165. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  166. /* Initialize arrays required[] and iidArray[] */
  167. SLboolean required[MAX_NUMBER_INTERFACES];
  168. SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
  169. #if 0
  170. for (int i=0; i<MAX_NUMBER_INTERFACES; i++)
  171. {
  172. required[i] = SL_BOOLEAN_FALSE;
  173. iidArray[i] = SL_IID_NULL;
  174. }
  175. // Set arrays required[] and iidArray[] for VOLUME interface
  176. required[0] = SL_BOOLEAN_TRUE;
  177. iidArray[0] = SL_IID_VOLUME;
  178. // Create Output Mix object to be used by player
  179. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 1,
  180. iidArray, required);
  181. #else
  182. {
  183. const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB};
  184. const SLboolean req[1] = {SL_BOOLEAN_FALSE};
  185. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
  186. ids, req);
  187. }
  188. #endif
  189. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  190. // Realizing the Output Mix object in synchronous mode.
  191. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  192. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  193. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT};
  194. //bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
  195. //bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */
  196. /* Setup the format of the content in the buffer queue */
  197. pcm.formatType = SL_DATAFORMAT_PCM;
  198. pcm.numChannels = 2;
  199. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  200. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  201. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  202. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  203. #ifdef BIG_ENDIAN_ENABLED
  204. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  205. #else
  206. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  207. #endif
  208. audioSource.pFormat = (void *)&pcm;
  209. audioSource.pLocator = (void *)&loc_bufq;
  210. /* Setup the data sink structure */
  211. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  212. locator_outputmix.outputMix= OutputMix;
  213. audioSink.pLocator = (void *)&locator_outputmix;
  214. audioSink.pFormat = NULL;
  215. /* Initialize the context for Buffer queue callbacks */
  216. //cntxt.pDataBase = (void*)&pcmData;
  217. //cntxt.pData = cntxt.pDataBase;
  218. //cntxt.size = sizeof(pcmData);
  219. /* Set arrays required[] and iidArray[] for SEEK interface
  220. (PlayItf is implicit) */
  221. required[0] = SL_BOOLEAN_TRUE;
  222. iidArray[0] = SL_IID_BUFFERQUEUE;
  223. /* Create the music player */
  224. {
  225. const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND};
  226. const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
  227. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player,
  228. &audioSource, &audioSink, 1, ids, req);
  229. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  230. }
  231. /* Realizing the player in synchronous mode. */
  232. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  233. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  234. /* Get seek and play interfaces */
  235. res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
  236. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  237. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  238. (void*)&bufferQueueItf);
  239. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  240. /* Setup to receive buffer queue event callbacks */
  241. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf,
  242. _buffer_callbacks, this);
  243. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  244. /* Before we start set volume to -3dB (-300mB) */
  245. #if 0
  246. res = (*OutputMix)->GetInterface(OutputMix, SL_IID_VOLUME,
  247. (void*)&volumeItf);
  248. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  249. /* Setup the data source structure for the buffer queue */
  250. res = (*volumeItf)->SetVolumeLevel(volumeItf, -300);
  251. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  252. #endif
  253. last_free=0;
  254. #if 1
  255. //fill up buffers
  256. for(int i=0;i<BUFFER_COUNT;i++) {
  257. /* Enqueue a few buffers to get the ball rolling */
  258. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i],
  259. 4 * buffer_size); /* Size given in */
  260. }
  261. #endif
  262. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  263. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  264. #if 0
  265. res = (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  266. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  267. while(state.count)
  268. {
  269. (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  270. }
  271. /* Make sure player is stopped */
  272. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
  273. CheckErr(res);
  274. /* Destroy the player */
  275. (*player)->Destroy(player);
  276. /* Destroy Output Mix object */
  277. (*OutputMix)->Destroy(OutputMix);
  278. #endif
  279. active=true;
  280. }
  281. int AudioDriverOpenSL::get_mix_rate() const {
  282. return 44100;
  283. }
  284. AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const{
  285. return SPEAKER_MODE_STEREO;
  286. }
  287. void AudioDriverOpenSL::lock(){
  288. if (active && mutex)
  289. mutex->lock();
  290. }
  291. void AudioDriverOpenSL::unlock() {
  292. if (active && mutex)
  293. mutex->unlock();
  294. }
  295. void AudioDriverOpenSL::finish(){
  296. (*sl)->Destroy(sl);
  297. }
  298. void AudioDriverOpenSL::set_pause(bool p_pause) {
  299. pause=p_pause;
  300. if (active) {
  301. if (pause) {
  302. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  303. } else {
  304. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  305. }
  306. }
  307. }
  308. AudioDriverOpenSL::AudioDriverOpenSL()
  309. {
  310. s_ad=this;
  311. mutex=Mutex::create();//NULL;
  312. pause=false;
  313. }