Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. AudioPlayer::AudioPlayer(SampleFormat *sampleFormat, SLEngineItf slEngine) :
  2. sLmillibelMax_(0), sLmillibelMin_(0), eQBandN_(0), freeQueue_(nullptr),
  3. playQueue_(nullptr), devShadowQueue_(nullptr), callback_(nullptr)
  4. {
  5. SLresult result;
  6. assert(sampleFormat);
  7. sampleInfo_ = *sampleFormat;
  8.  
  9. // Interfaces.
  10. SLInterfaceID ids[2] = {SL_IID_NULL, SL_IID_NULL};
  11. SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
  12.  
  13. ids[0] = SL_IID_EQUALIZER;
  14.  
  15. result = (*slEngine)->CreateOutputMix(slEngine, &outputMixObjectItf_,
  16. 1, ids, req);
  17. SLASSERT(result);
  18.  
  19. // realize the output mix
  20. result = (*outputMixObjectItf_)->Realize(outputMixObjectItf_, SL_BOOLEAN_FALSE);
  21. SLASSERT(result);
  22.  
  23. // Get Equalizer.
  24. result = (*outputMixObjectItf_)->GetInterface(outputMixObjectItf_,
  25. SL_IID_EQUALIZER,
  26. &slEqualizerItf_);
  27. SLASSERT(result);
  28.  
  29.  
  30. // configure audio source
  31. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
  32. SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
  33. DEVICE_SHADOW_BUFFER_QUEUE_LEN };
  34.  
  35. SLAndroidDataFormat_PCM_EX format_pcm;
  36. ConvertToSLSampleFormat(&format_pcm, &sampleInfo_);
  37. SLDataSource audioSrc = {&loc_bufq, &format_pcm};
  38.  
  39. // configure audio sink
  40. SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObjectItf_};
  41. SLDataSink audioSnk = {&loc_outmix, NULL};
  42.  
  43. /*
  44. * create fast path audio player: SL_IID_BUFFERQUEUE and SL_IID_VOLUME interfaces ok,
  45. * NO others!
  46. */
  47. ids[0] = SL_IID_BUFFERQUEUE;
  48. ids[1] = SL_IID_VOLUME;
  49.  
  50. result = (*slEngine)->CreateAudioPlayer(slEngine, &playerObjectItf_, &audioSrc, &audioSnk,
  51. 2, ids, req);
  52. SLASSERT(result);
  53.  
  54. // realize the player
  55. result = (*playerObjectItf_)->Realize(playerObjectItf_, SL_BOOLEAN_FALSE);
  56. SLASSERT(result);
  57.  
  58. // get the play interface
  59. result = (*playerObjectItf_)->GetInterface(playerObjectItf_, SL_IID_PLAY, &playItf_);
  60. SLASSERT(result);
  61.  
  62. // get the buffer queue interface
  63. result = (*playerObjectItf_)->GetInterface(playerObjectItf_, SL_IID_BUFFERQUEUE,
  64. &playBufferQueueItf_);
  65. SLASSERT(result);
  66.  
  67. // register callback on the buffer queue
  68. result = (*playBufferQueueItf_)->RegisterCallback(playBufferQueueItf_, bqPlayerCallback, this);
  69. SLASSERT(result);
  70.  
  71. result = (*playerObjectItf_)->GetInterface(playerObjectItf_,
  72. SL_IID_VOLUME, &volumeItf_);
  73. SLASSERT(result);
  74.  
  75. result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
  76. SLASSERT(result);
  77.  
  78. // create an empty queue to track deviceQueue
  79. devShadowQueue_ = new AudioQueue(DEVICE_SHADOW_BUFFER_QUEUE_LEN);
  80. assert(devShadowQueue_);
  81. }
  82.  
  83. SLresult AudioPlayer::Start(void) {
  84. SLuint32 state;
  85. SLresult result = (*playItf_)->GetPlayState(playItf_, &state);
  86. if (result != SL_RESULT_SUCCESS) {
  87. return SL_BOOLEAN_FALSE;
  88. }
  89. if(state == SL_PLAYSTATE_PLAYING) {
  90. return SL_BOOLEAN_TRUE;
  91. }
  92.  
  93. result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
  94. SLASSERT(result);
  95.  
  96. result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_PLAYING);
  97. SLASSERT(result);
  98.  
  99. // Equalizing
  100. result = (*slEqualizerItf_)->GetNumberOfBands(slEqualizerItf_, &eQBandN_);
  101. SLASSERT(result);
  102.  
  103. result = (*slEqualizerItf_)->GetBandLevelRange(slEqualizerItf_,
  104. &sLmillibelMin_,
  105. &sLmillibelMax_);
  106. SLASSERT(result);
  107.  
  108. // Función de equalización, se puede/debe cambiar.
  109. for (int i = 0; i < eQBandN_; ++i) {
  110. if (!i % 2)
  111. result = (*slEqualizerItf_)->SetBandLevel(slEqualizerItf_,
  112. i, sLmillibelMax_);
  113. else
  114. result = (*slEqualizerItf_)->SetBandLevel(slEqualizerItf_,
  115. i, sLmillibelMin_);
  116. SLASSERT(result);
  117. }
  118.  
  119. result = (*slEqualizerItf_)->SetEnabled(slEqualizerItf_, SL_BOOLEAN_TRUE);
  120. SLASSERT(result);
  121.  
  122. // send pre-defined audio buffers to device
  123. int i = PLAY_KICKSTART_BUFFER_COUNT;
  124. while(i--) {
  125. sample_buf *buf;
  126. if(!playQueue_->front(&buf)) //we have buffers for sure
  127. break;
  128. if(SL_RESULT_SUCCESS !=
  129. (*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, buf, buf->size_))
  130. {
  131. LOGE("====failed to enqueue (%d) in %s", i, __FUNCTION__);
  132. return SL_BOOLEAN_FALSE;
  133. } else {
  134. playQueue_->pop();
  135. devShadowQueue_->push(buf);
  136. }
  137. }
  138. return SL_BOOLEAN_TRUE;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement