audio_driver_android.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*************************************************************************/
  2. /* audio_driver_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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_android.h"
  30. #include <string.h>
  31. #ifdef ANDROID_NATIVE_ACTIVITY
  32. #define MAX_NUMBER_INTERFACES 3
  33. #define MAX_NUMBER_OUTPUT_DEVICES 6
  34. /* Structure for passing information to callback function */
  35. void AudioDriverAndroid::_buffer_callback(
  36. SLAndroidSimpleBufferQueueItf queueItf
  37. /* SLuint32 eventFlags,
  38. const void * pBuffer,
  39. SLuint32 bufferSize,
  40. SLuint32 dataUsed*/) {
  41. if (mutex)
  42. mutex->lock();
  43. audio_server_process(buffer_size,mixdown_buffer);
  44. if (mutex)
  45. mutex->unlock();
  46. const int32_t* src_buff=mixdown_buffer;
  47. int16_t *ptr = (int16_t*)buffers[last_free];
  48. last_free=(last_free+1)%BUFFER_COUNT;
  49. for(int i=0;i<buffer_size*2;i++) {
  50. ptr[i]=src_buff[i]>>16;
  51. }
  52. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  53. #if 0
  54. SLresult res;
  55. CallbackCntxt *pCntxt = (CallbackCntxt*)pContext;
  56. if(pCntxt->pData < (pCntxt->pDataBase + pCntxt->size))
  57. {
  58. res = (*queueItf)->Enqueue(queueItf, (void*) pCntxt->pData,
  59. 2 * AUDIO_DATA_BUFFER_SIZE, SL_BOOLEAN_FALSE); /* Size given
  60. in bytes. */
  61. CheckErr(res);
  62. /* Increase data pointer by buffer size */
  63. pCntxt->pData += AUDIO_DATA_BUFFER_SIZE;
  64. }
  65. }
  66. #endif
  67. }
  68. void AudioDriverAndroid::_buffer_callbacks(
  69. SLAndroidSimpleBufferQueueItf queueItf,
  70. /*SLuint32 eventFlags,
  71. const void * pBuffer,
  72. SLuint32 bufferSize,
  73. SLuint32 dataUsed,*/
  74. void *pContext) {
  75. AudioDriverAndroid *ad = (AudioDriverAndroid*)pContext;
  76. // ad->_buffer_callback(queueItf,eventFlags,pBuffer,bufferSize,dataUsed);
  77. ad->_buffer_callback(queueItf);
  78. }
  79. AudioDriverAndroid* AudioDriverAndroid::s_ad=NULL;
  80. const char* AudioDriverAndroid::get_name() const {
  81. return "Android";
  82. }
  83. #if 0
  84. int AudioDriverAndroid::thread_func(SceSize args, void *argp) {
  85. AudioDriverAndroid* ad = s_ad;
  86. sceAudioOutput2Reserve(AUDIO_OUTPUT_SAMPLE);
  87. int half=0;
  88. while(!ad->exit_thread) {
  89. int16_t *ptr = &ad->outbuff[AUDIO_OUTPUT_SAMPLE*2*half];
  90. if (!ad->active) {
  91. for(int i=0;i<AUDIO_OUTPUT_SAMPLE*2;i++) {
  92. ptr[i]=0;
  93. }
  94. } else {
  95. //printf("samples: %i\n",AUDIO_OUTPUT_SAMPLE);
  96. ad->lock();
  97. ad->audio_server_process(AUDIO_OUTPUT_SAMPLE,ad->outbuff_32);
  98. ad->unlock();
  99. const int32_t* src_buff=ad->outbuff_32;
  100. for(int i=0;i<AUDIO_OUTPUT_SAMPLE*2;i++) {
  101. ptr[i]=src_buff[i]>>16;
  102. }
  103. }
  104. /* Output 16-bit PCM STEREO data that is in pcmBuf without changing the volume */
  105. sceAudioOutput2OutputBlocking(
  106. SCE_AUDIO_VOLUME_0dB*3, //0db at 0x8000, that's obvious
  107. ptr
  108. );
  109. if (half)
  110. half=0;
  111. else
  112. half=1;
  113. }
  114. sceAudioOutput2Release();
  115. sceKernelExitThread(SCE_KERNEL_EXIT_SUCCESS);
  116. ad->thread_exited=true;
  117. return SCE_KERNEL_EXIT_SUCCESS;
  118. }
  119. #endif
  120. Error AudioDriverAndroid::init(){
  121. SLresult
  122. res;
  123. SLEngineOption EngineOption[] = {
  124. (SLuint32) SL_ENGINEOPTION_THREADSAFE,
  125. (SLuint32) SL_BOOLEAN_TRUE
  126. };
  127. res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
  128. if (res!=SL_RESULT_SUCCESS) {
  129. ERR_EXPLAIN("Could not Initialize OpenSL");
  130. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  131. }
  132. res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
  133. if (res!=SL_RESULT_SUCCESS) {
  134. ERR_EXPLAIN("Could not Realize OpenSL");
  135. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  136. }
  137. print_line("OpenSL Init OK!");
  138. return OK;
  139. }
  140. void AudioDriverAndroid::start(){
  141. mutex = Mutex::create();
  142. active=false;
  143. SLint32 numOutputs = 0;
  144. SLuint32 deviceID = 0;
  145. SLresult res;
  146. buffer_size = 1024;
  147. for(int i=0;i<BUFFER_COUNT;i++) {
  148. buffers[i]=memnew_arr( int16_t,buffer_size*2 );
  149. memset(buffers[i],0,buffer_size*4);
  150. }
  151. mixdown_buffer = memnew_arr( int32_t,buffer_size* 2);
  152. /* Callback context for the buffer queue callback function */
  153. /* Get the SL Engine Interface which is implicit */
  154. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
  155. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  156. /* Initialize arrays required[] and iidArray[] */
  157. int i;
  158. SLboolean required[MAX_NUMBER_INTERFACES];
  159. SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
  160. #if 0
  161. for (i=0; i<MAX_NUMBER_INTERFACES; i++)
  162. {
  163. required[i] = SL_BOOLEAN_FALSE;
  164. iidArray[i] = SL_IID_NULL;
  165. }
  166. // Set arrays required[] and iidArray[] for VOLUME interface
  167. required[0] = SL_BOOLEAN_TRUE;
  168. iidArray[0] = SL_IID_VOLUME;
  169. // Create Output Mix object to be used by player
  170. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 1,
  171. iidArray, required);
  172. #else
  173. {
  174. const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB};
  175. const SLboolean req[1] = {SL_BOOLEAN_FALSE};
  176. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
  177. ids, req);
  178. }
  179. #endif
  180. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  181. // Realizing the Output Mix object in synchronous mode.
  182. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  183. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  184. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT};
  185. // bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
  186. // bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */
  187. /* Setup the format of the content in the buffer queue */
  188. pcm.formatType = SL_DATAFORMAT_PCM;
  189. pcm.numChannels = 2;
  190. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  191. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  192. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  193. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  194. #ifdef BIG_ENDIAN_ENABLED
  195. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  196. #else
  197. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  198. #endif
  199. audioSource.pFormat = (void *)&pcm;
  200. audioSource.pLocator = (void *)&loc_bufq;
  201. /* Setup the data sink structure */
  202. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  203. locator_outputmix.outputMix= OutputMix;
  204. audioSink.pLocator = (void *)&locator_outputmix;
  205. audioSink.pFormat = NULL;
  206. /* Initialize the context for Buffer queue callbacks */
  207. // cntxt.pDataBase = (void*)&pcmData;
  208. //cntxt.pData = cntxt.pDataBase;
  209. //cntxt.size = sizeof(pcmData);
  210. /* Set arrays required[] and iidArray[] for SEEK interface
  211. (PlayItf is implicit) */
  212. required[0] = SL_BOOLEAN_TRUE;
  213. iidArray[0] = SL_IID_BUFFERQUEUE;
  214. /* Create the music player */
  215. {
  216. const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND};
  217. const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
  218. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player,
  219. &audioSource, &audioSink, 1, ids, req);
  220. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  221. }
  222. /* Realizing the player in synchronous mode. */
  223. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  224. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  225. /* Get seek and play interfaces */
  226. res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
  227. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  228. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  229. (void*)&bufferQueueItf);
  230. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  231. /* Setup to receive buffer queue event callbacks */
  232. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf,
  233. _buffer_callbacks, this);
  234. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  235. /* Before we start set volume to -3dB (-300mB) */
  236. #if 0
  237. res = (*OutputMix)->GetInterface(OutputMix, SL_IID_VOLUME,
  238. (void*)&volumeItf);
  239. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  240. /* Setup the data source structure for the buffer queue */
  241. res = (*volumeItf)->SetVolumeLevel(volumeItf, -300);
  242. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  243. #endif
  244. last_free=0;
  245. #if 1
  246. //fill up buffers
  247. for(int i=0;i<BUFFER_COUNT;i++) {
  248. /* Enqueue a few buffers to get the ball rolling */
  249. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i],
  250. 4 * buffer_size); /* Size given in */
  251. }
  252. #endif
  253. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  254. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  255. #if 0
  256. res = (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  257. ERR_FAIL_COND( res !=SL_RESULT_SUCCESS );
  258. while(state.count)
  259. {
  260. (*bufferQueueItf)->GetState(bufferQueueItf, &state);
  261. }
  262. /* Make sure player is stopped */
  263. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
  264. CheckErr(res);
  265. /* Destroy the player */
  266. (*player)->Destroy(player);
  267. /* Destroy Output Mix object */
  268. (*OutputMix)->Destroy(OutputMix);
  269. #endif
  270. active=true;
  271. }
  272. int AudioDriverAndroid::get_mix_rate() const {
  273. return 44100;
  274. }
  275. AudioDriverSW::OutputFormat AudioDriverAndroid::get_output_format() const{
  276. return OUTPUT_STEREO;
  277. }
  278. void AudioDriverAndroid::lock(){
  279. //if (active && mutex)
  280. // mutex->lock();
  281. }
  282. void AudioDriverAndroid::unlock() {
  283. //if (active && mutex)
  284. // mutex->unlock();
  285. }
  286. void AudioDriverAndroid::finish(){
  287. (*sl)->Destroy(sl);
  288. }
  289. AudioDriverAndroid::AudioDriverAndroid()
  290. {
  291. s_ad=this;
  292. mutex=NULL;
  293. }
  294. #endif