Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AudioPlayer::AudioPlayer(SampleFormat *sampleFormat, SLEngineItf slEngine) :
- sLmillibelMax_(0), sLmillibelMin_(0), eQBandN_(0), freeQueue_(nullptr),
- playQueue_(nullptr), devShadowQueue_(nullptr), callback_(nullptr)
- {
- SLresult result;
- assert(sampleFormat);
- sampleInfo_ = *sampleFormat;
- // Interfaces.
- SLInterfaceID ids[2] = {SL_IID_NULL, SL_IID_NULL};
- SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
- ids[0] = SL_IID_EQUALIZER;
- result = (*slEngine)->CreateOutputMix(slEngine, &outputMixObjectItf_,
- 1, ids, req);
- SLASSERT(result);
- // realize the output mix
- result = (*outputMixObjectItf_)->Realize(outputMixObjectItf_, SL_BOOLEAN_FALSE);
- SLASSERT(result);
- // Get Equalizer.
- result = (*outputMixObjectItf_)->GetInterface(outputMixObjectItf_,
- SL_IID_EQUALIZER,
- &slEqualizerItf_);
- SLASSERT(result);
- // configure audio source
- SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
- SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
- DEVICE_SHADOW_BUFFER_QUEUE_LEN };
- SLAndroidDataFormat_PCM_EX format_pcm;
- ConvertToSLSampleFormat(&format_pcm, &sampleInfo_);
- SLDataSource audioSrc = {&loc_bufq, &format_pcm};
- // configure audio sink
- SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObjectItf_};
- SLDataSink audioSnk = {&loc_outmix, NULL};
- /*
- * create fast path audio player: SL_IID_BUFFERQUEUE and SL_IID_VOLUME interfaces ok,
- * NO others!
- */
- ids[0] = SL_IID_BUFFERQUEUE;
- ids[1] = SL_IID_VOLUME;
- result = (*slEngine)->CreateAudioPlayer(slEngine, &playerObjectItf_, &audioSrc, &audioSnk,
- 2, ids, req);
- SLASSERT(result);
- // realize the player
- result = (*playerObjectItf_)->Realize(playerObjectItf_, SL_BOOLEAN_FALSE);
- SLASSERT(result);
- // get the play interface
- result = (*playerObjectItf_)->GetInterface(playerObjectItf_, SL_IID_PLAY, &playItf_);
- SLASSERT(result);
- // get the buffer queue interface
- result = (*playerObjectItf_)->GetInterface(playerObjectItf_, SL_IID_BUFFERQUEUE,
- &playBufferQueueItf_);
- SLASSERT(result);
- // register callback on the buffer queue
- result = (*playBufferQueueItf_)->RegisterCallback(playBufferQueueItf_, bqPlayerCallback, this);
- SLASSERT(result);
- result = (*playerObjectItf_)->GetInterface(playerObjectItf_,
- SL_IID_VOLUME, &volumeItf_);
- SLASSERT(result);
- result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
- SLASSERT(result);
- // create an empty queue to track deviceQueue
- devShadowQueue_ = new AudioQueue(DEVICE_SHADOW_BUFFER_QUEUE_LEN);
- assert(devShadowQueue_);
- }
- SLresult AudioPlayer::Start(void) {
- SLuint32 state;
- SLresult result = (*playItf_)->GetPlayState(playItf_, &state);
- if (result != SL_RESULT_SUCCESS) {
- return SL_BOOLEAN_FALSE;
- }
- if(state == SL_PLAYSTATE_PLAYING) {
- return SL_BOOLEAN_TRUE;
- }
- result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
- SLASSERT(result);
- result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_PLAYING);
- SLASSERT(result);
- // Equalizing
- result = (*slEqualizerItf_)->GetNumberOfBands(slEqualizerItf_, &eQBandN_);
- SLASSERT(result);
- result = (*slEqualizerItf_)->GetBandLevelRange(slEqualizerItf_,
- &sLmillibelMin_,
- &sLmillibelMax_);
- SLASSERT(result);
- // Función de equalización, se puede/debe cambiar.
- for (int i = 0; i < eQBandN_; ++i) {
- if (!i % 2)
- result = (*slEqualizerItf_)->SetBandLevel(slEqualizerItf_,
- i, sLmillibelMax_);
- else
- result = (*slEqualizerItf_)->SetBandLevel(slEqualizerItf_,
- i, sLmillibelMin_);
- SLASSERT(result);
- }
- result = (*slEqualizerItf_)->SetEnabled(slEqualizerItf_, SL_BOOLEAN_TRUE);
- SLASSERT(result);
- // send pre-defined audio buffers to device
- int i = PLAY_KICKSTART_BUFFER_COUNT;
- while(i--) {
- sample_buf *buf;
- if(!playQueue_->front(&buf)) //we have buffers for sure
- break;
- if(SL_RESULT_SUCCESS !=
- (*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, buf, buf->size_))
- {
- LOGE("====failed to enqueue (%d) in %s", i, __FUNCTION__);
- return SL_BOOLEAN_FALSE;
- } else {
- playQueue_->pop();
- devShadowQueue_->push(buf);
- }
- }
- return SL_BOOLEAN_TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement