audio_driver_opensl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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-2015 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. int i;
  168. SLboolean required[MAX_NUMBER_INTERFACES];
  169. SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
  170. #if 0
  171. for (i=0; i<MAX_NUMBER_INTERFACES; i++)
  172. {
  173. required[i] = SL_BOOLEAN_FALSE;
  174. iidArray[i] = SL_IID_NULL;
  175. }
  176. // Set arrays required[] and iidArray[] for VOLUME interface
  177. required[0] = SL_BOOLEAN_TRUE;
  178. iidArray[0] = SL_IID_VOLUME;
  179. // Create Output Mix object to be used by player
  180. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 1,
  181. iidArray, required);
  182. #else
  183. {
  184. const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB};
  185. const SLboolean req[1] = {SL_BOOLEAN_FALSE};
  186. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
  187. ids, req);
  188. }
  189. #endif
  190. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  191. // Realizing the Output Mix object in synchronous mode.
  192. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  193. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  194. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT};
  195. // bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
  196. // bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */
  197. /* Setup the format of the content in the buffer queue */
  198. pcm.formatType = SL_DATAFORMAT_PCM;
  199. pcm.numChannels = 2;
  200. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  201. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  202. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  203. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  204. #ifdef BIG_ENDIAN_ENABLED
  205. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  206. #else
  207. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  208. #endif
  209. audioSource.pFormat = (void *)&pcm;
  210. audioSource.pLocator = (void *)&loc_bufq;
  211. /* Setup the data sink structure */
  212. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  213. locator_outputmix.outputMix= OutputMix;
  214. audioSink.pLocator = (void *)&locator_outputmix;
  215. audioSink.pFormat = NULL;
  216. /* Initialize the context for Buffer queue callbacks */
  217. // cntxt.pDataBase = (void*)&pcmData;
  218. //cntxt.pData = cntxt.pDataBase;
  219. //cntxt.size = sizeof(pcmData);
  220. /* Set arrays required[] and iidArray[] for SEEK interface
  221. (PlayItf is implicit) */
  222. required[0] = SL_BOOLEAN_TRUE;
  223. iidArray[0] = SL_IID_BUFFERQUEUE;
  224. /* Create the music player */
  225. {
  226. const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND};
  227. const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
  228. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player,
  229. &audioSource, &audioSink, 1, ids, req);
  230. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  231. }
  232. /* Realizing the player in synchronous mode. */
  233. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  234. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  235. /* Get seek and play interfaces */
  236. res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
  237. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  238. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  239. (void*)&bufferQueueItf);
  240. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  241. /* Setup to receive buffer queue event callbacks */
  242. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf,
  243. _buffer_callbacks, this);
  244. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  245. /* Before we start set volume to -3dB (-300mB) */
  246. #if 0
  247. res = (*OutputMix)->GetInterface(OutputMix, SL_IID_VOLUME,
  248. (void*)&volumeItf);
  249. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  250. /* Setup the data source structure for the buffer queue */
  251. res = (*volumeItf)->SetVolumeLevel(volumeItf, -300);
  252. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  253. #endif
  254. last_free=0;
  255. #if 1
  256. //fill up buffers
  257. for(int i=0;i<BUFFER_COUNT;i++) {
  258. /* Enqueue a few buffers to get the ball rolling */
  259. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i],
  260. 4 * buffer_size); /* Size given in */
  261. }
  262. #endif
  263. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  264. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  265. #if 0
  266. res = (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  267. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  268. while(state.count)
  269. {
  270. (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  271. }
  272. /* Make sure player is stopped */
  273. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
  274. CheckErr(res);
  275. /* Destroy the player */
  276. (*player)->Destroy(player);
  277. /* Destroy Output Mix object */
  278. (*OutputMix)->Destroy(OutputMix);
  279. #endif
  280. active=true;
  281. }
  282. int AudioDriverOpenSL::get_mix_rate() const {
  283. return 44100;
  284. }
  285. AudioDriverSW::OutputFormat AudioDriverOpenSL::get_output_format() const{
  286. return OUTPUT_STEREO;
  287. }
  288. void AudioDriverOpenSL::lock(){
  289. if (active && mutex)
  290. mutex->lock();
  291. }
  292. void AudioDriverOpenSL::unlock() {
  293. if (active && mutex)
  294. mutex->unlock();
  295. }
  296. void AudioDriverOpenSL::finish(){
  297. (*sl)->Destroy(sl);
  298. }
  299. void AudioDriverOpenSL::set_pause(bool p_pause) {
  300. pause=p_pause;
  301. if (active) {
  302. if (pause) {
  303. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  304. } else {
  305. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  306. }
  307. }
  308. }
  309. AudioDriverOpenSL::AudioDriverOpenSL()
  310. {
  311. s_ad=this;
  312. mutex=Mutex::create();//NULL;
  313. pause=false;
  314. }