diff options
Diffstat (limited to 'platform/android/audio_driver_opensl.cpp')
-rw-r--r-- | platform/android/audio_driver_opensl.cpp | 163 |
1 files changed, 69 insertions, 94 deletions
diff --git a/platform/android/audio_driver_opensl.cpp b/platform/android/audio_driver_opensl.cpp index 2ec80f23c3..bba98dd623 100644 --- a/platform/android/audio_driver_opensl.cpp +++ b/platform/android/audio_driver_opensl.cpp @@ -35,48 +35,45 @@ /* Structure for passing information to callback function */ - void AudioDriverOpenSL::_buffer_callback( - SLAndroidSimpleBufferQueueItf queueItf - /* SLuint32 eventFlags, + SLAndroidSimpleBufferQueueItf queueItf + /* SLuint32 eventFlags, const void * pBuffer, SLuint32 bufferSize, SLuint32 dataUsed*/) { - bool mix=true; + bool mix = true; if (pause) { - mix=false; + mix = false; } else if (mutex) { - mix = mutex->try_lock()==OK; + mix = mutex->try_lock() == OK; } if (mix) { - audio_server_process(buffer_size,mixdown_buffer); + audio_server_process(buffer_size, mixdown_buffer); } else { - int32_t* src_buff=mixdown_buffer; - for(int i=0;i<buffer_size*2;i++) { - src_buff[i]=0; + int32_t *src_buff = mixdown_buffer; + for (int i = 0; i < buffer_size * 2; i++) { + src_buff[i] = 0; } } if (mutex && mix) mutex->unlock(); + const int32_t *src_buff = mixdown_buffer; - const int32_t* src_buff=mixdown_buffer; - - int16_t *ptr = (int16_t*)buffers[last_free]; - last_free=(last_free+1)%BUFFER_COUNT; + int16_t *ptr = (int16_t *)buffers[last_free]; + last_free = (last_free + 1) % BUFFER_COUNT; - for(int i=0;i<buffer_size*2;i++) { + for (int i = 0; i < buffer_size * 2; i++) { - ptr[i]=src_buff[i]>>16; + ptr[i] = src_buff[i] >> 16; } - (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size); - + (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size); #if 0 SLresult res; @@ -95,25 +92,22 @@ void AudioDriverOpenSL::_buffer_callback( } void AudioDriverOpenSL::_buffer_callbacks( - SLAndroidSimpleBufferQueueItf queueItf, - /*SLuint32 eventFlags, + SLAndroidSimpleBufferQueueItf queueItf, + /*SLuint32 eventFlags, const void * pBuffer, SLuint32 bufferSize, SLuint32 dataUsed,*/ - void *pContext) { - + void *pContext) { - AudioDriverOpenSL *ad = (AudioDriverOpenSL*)pContext; + AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext; //ad->_buffer_callback(queueItf,eventFlags,pBuffer,bufferSize,dataUsed); ad->_buffer_callback(queueItf); - } +AudioDriverOpenSL *AudioDriverOpenSL::s_ad = NULL; -AudioDriverOpenSL* AudioDriverOpenSL::s_ad=NULL; - -const char* AudioDriverOpenSL::get_name() const { +const char *AudioDriverOpenSL::get_name() const { return "Android"; } @@ -177,23 +171,23 @@ int AudioDriverOpenSL::thread_func(SceSize args, void *argp) { } #endif -Error AudioDriverOpenSL::init(){ +Error AudioDriverOpenSL::init() { SLresult - res; + res; SLEngineOption EngineOption[] = { - (SLuint32) SL_ENGINEOPTION_THREADSAFE, - (SLuint32) SL_BOOLEAN_TRUE + (SLuint32)SL_ENGINEOPTION_THREADSAFE, + (SLuint32)SL_BOOLEAN_TRUE }; - res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); - if (res!=SL_RESULT_SUCCESS) { + res = slCreateEngine(&sl, 1, EngineOption, 0, NULL, NULL); + if (res != SL_RESULT_SUCCESS) { ERR_EXPLAIN("Could not Initialize OpenSL"); ERR_FAIL_V(ERR_INVALID_PARAMETER); } res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); - if (res!=SL_RESULT_SUCCESS) { + if (res != SL_RESULT_SUCCESS) { ERR_EXPLAIN("Could not Realize OpenSL"); ERR_FAIL_V(ERR_INVALID_PARAMETER); @@ -202,37 +196,33 @@ Error AudioDriverOpenSL::init(){ print_line("OpenSL Init OK!"); return OK; - } -void AudioDriverOpenSL::start(){ - +void AudioDriverOpenSL::start() { mutex = Mutex::create(); - active=false; - + active = false; SLint32 numOutputs = 0; SLuint32 deviceID = 0; SLresult res; - buffer_size = 1024; - for(int i=0;i<BUFFER_COUNT;i++) { + for (int i = 0; i < BUFFER_COUNT; i++) { - buffers[i]=memnew_arr( int16_t,buffer_size*2 ); - memset(buffers[i],0,buffer_size*4); + buffers[i] = memnew_arr(int16_t, buffer_size * 2); + memset(buffers[i], 0, buffer_size * 4); } - mixdown_buffer = memnew_arr( int32_t,buffer_size* 2); + mixdown_buffer = memnew_arr(int32_t, buffer_size * 2); /* Callback context for the buffer queue callback function */ /* Get the SL Engine Interface which is implicit */ - res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); + res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); /* Initialize arrays required[] and iidArray[] */ SLboolean required[MAX_NUMBER_INTERFACES]; SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; @@ -254,20 +244,18 @@ void AudioDriverOpenSL::start(){ #else { - const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; - const SLboolean req[1] = {SL_BOOLEAN_FALSE}; - res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, - ids, req); + const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB }; + const SLboolean req[1] = { SL_BOOLEAN_FALSE }; + res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req); } - #endif - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); - // Realizing the Output Mix object in synchronous mode. + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); + // Realizing the Output Mix object in synchronous mode. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); - SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT}; + SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT }; //bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; //bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */ /* Setup the format of the content in the buffer queue */ @@ -285,10 +273,9 @@ void AudioDriverOpenSL::start(){ audioSource.pFormat = (void *)&pcm; audioSource.pLocator = (void *)&loc_bufq; - /* Setup the data sink structure */ locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; - locator_outputmix.outputMix= OutputMix; + locator_outputmix.outputMix = OutputMix; audioSink.pLocator = (void *)&locator_outputmix; audioSink.pFormat = NULL; /* Initialize the context for Buffer queue callbacks */ @@ -302,27 +289,25 @@ void AudioDriverOpenSL::start(){ /* Create the music player */ { - const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND}; - const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; + const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND }; + const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; - res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, - &audioSource, &audioSink, 1, ids, req); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); } /* Realizing the player in synchronous mode. */ res = (*player)->Realize(player, SL_BOOLEAN_FALSE); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); /* Get seek and play interfaces */ - res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE, - (void*)&bufferQueueItf); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + (void *)&bufferQueueItf); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); /* Setup to receive buffer queue event callbacks */ - res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, - _buffer_callbacks, this); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); - /* Before we start set volume to -3dB (-300mB) */ + res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); +/* Before we start set volume to -3dB (-300mB) */ #if 0 res = (*OutputMix)->GetInterface(OutputMix, SL_IID_VOLUME, (void*)&volumeItf); @@ -332,19 +317,17 @@ void AudioDriverOpenSL::start(){ res = (*volumeItf)->SetVolumeLevel(volumeItf, -300); ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); #endif - last_free=0; + last_free = 0; #if 1 //fill up buffers - for(int i=0;i<BUFFER_COUNT;i++) { + for (int i = 0; i < BUFFER_COUNT; i++) { /* Enqueue a few buffers to get the ball rolling */ - res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], - 4 * buffer_size); /* Size given in */ - + res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */ } #endif res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); #if 0 res = (*bufferQueueItf)->GetState(bufferQueueItf, &state); @@ -362,7 +345,7 @@ void AudioDriverOpenSL::start(){ (*OutputMix)->Destroy(OutputMix); #endif - active=true; + active = true; } int AudioDriverOpenSL::get_mix_rate() const { @@ -370,34 +353,31 @@ int AudioDriverOpenSL::get_mix_rate() const { return 44100; } -AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const{ +AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const { return SPEAKER_MODE_STEREO; } -void AudioDriverOpenSL::lock(){ +void AudioDriverOpenSL::lock() { if (active && mutex) mutex->lock(); - } void AudioDriverOpenSL::unlock() { if (active && mutex) mutex->unlock(); - } -void AudioDriverOpenSL::finish(){ +void AudioDriverOpenSL::finish() { (*sl)->Destroy(sl); - } void AudioDriverOpenSL::set_pause(bool p_pause) { - pause=p_pause; + pause = p_pause; if (active) { if (pause) { @@ -408,13 +388,8 @@ void AudioDriverOpenSL::set_pause(bool p_pause) { } } - -AudioDriverOpenSL::AudioDriverOpenSL() -{ - s_ad=this; - mutex=Mutex::create();//NULL; - pause=false; +AudioDriverOpenSL::AudioDriverOpenSL() { + s_ad = this; + mutex = Mutex::create(); //NULL; + pause = false; } - - - |