Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void cb(SLAndroidSimpleBufferQueueItf bq, void * context) {
- OSLBufferPlayer * player = (OSLBufferPlayer *)context;
- if(!playerStarted)
- {
- return;
- }
- int bufferSize = player->getContext()->getBufferSize();
- short * bufferToPlay = secondBuffer ?
- player->currentPitch==0 ?
- player->buffer2 :
- player->bufferPitch2
- :
- player->currentPitch==0 ?
- player->buffer1 :
- player->bufferPitch1
- ;
- (*player->bufferQueue)->Enqueue(player->bufferQueue, bufferToPlay , player->currentBufferSize);
- // Define pitch koefficient
- // It's experimental value
- int pitch = player->currentPitch;
- float koefPitch = 0.05629;
- if (pitch > 4) {
- koefPitch = 0.05029;
- }
- if (pitch < 0) {
- koefPitch = 0.05729;
- }
- short * buffer = secondBuffer ? player->buffer1 : player->buffer2;
- short * bufferPitch = secondBuffer ? player->bufferPitch1 : player->bufferPitch2;
- float sizeToRead = pitch==0 ? (player->getContext()->getBufferSize()) : (player->getContext()->getBufferSize()/(1-koefPitch * pitch));
- for(int i = 0;i<sizeToRead;i++) {
- buffer[i] = 0;
- }
- for(int i = 0;i<MAX_SOUND_COUNT;i++) {
- if(player->sound[i]!=0 && player->sound[i]->isPlaying()) {
- short * buff = player->sound[i]->getBufferWithOffset(sizeToRead/2);
- for(int j = 0;j<sizeToRead;j++) {
- float mult = player->sound[i]->getMultiplier();
- short res = buff[j]*mult;
- buffer[j] = mixSamples(buffer[j], res);
- }
- }
- }
- // Define how many frames must be deleted/added to sample
- float framesToAdjustCount = (float) sizeToRead * koefPitch * abs(pitch);
- // Calculate new frames count
- float origFrameCount = sizeToRead;
- float newFramesCount = origFrameCount;
- newFramesCount += (pitch > 0) ? -framesToAdjustCount : framesToAdjustCount;
- // Update frames
- if (pitch == 0) {
- // Use original frames for null pitch
- } else {
- // Define which frame must be added/deleted
- // float type is used for more accuracy
- // int is not recommended, it gives wrong behaviour, too much roundation
- float frameToAdjustNumber = (float) sizeToRead / (float)framesToAdjustCount;
- int k = 0;
- int currentIteration = 0;
- bool adjustableStep = false;
- for (int i = 0; i < origFrameCount; i++) {
- if (k < newFramesCount) {
- adjustableStep = false;
- // Check if current frame is 'adjustable' frame
- // Checking is required since frameToAdjustNumber is float value, not integer.
- // Float value gives more accuracy
- float tmp1 = (float) i / frameToAdjustNumber;
- if (roundf(tmp1) - tmp1 >= 0 && currentIteration != roundf(tmp1)) {
- currentIteration = roundf(tmp1);
- adjustableStep = true;
- }
- if (pitch > 0) {
- // remove one frame
- if (adjustableStep) {
- continue;
- }
- bufferPitch[k] = buffer[i];
- } else {
- // add new frame
- bufferPitch[k] = buffer[i];
- if (adjustableStep) {
- if (i < origFrameCount - 2) {
- k += 1;
- bufferPitch[k] = ((int)buffer[i] + (int)buffer[i+2])/2;
- }
- }
- }
- k += 1;
- }
- }
- }
- if(player->recording) {
- short * bufferToWrite = secondBuffer ?
- player->currentPitch==0 ?
- player->buffer1 :
- player->bufferPitch1
- :
- player->currentPitch==0 ?
- player->buffer2 :
- player->bufferPitch2
- ;
- for(int i = 0;i<player->getContext()->getBufferSize();i++) {
- player->bufferRecord[i] = bufferToWrite[i];
- }
- write(player->recordFileFD, player->bufferRecord, player->getContext()->getBufferSize());
- }
- secondBuffer = !secondBuffer;
- }
- void OSLBufferPlayer::setCurrentPitch(int pitch) {
- currentPitch = pitch;
- }
- void OSLBufferPlayer::init(){
- SLresult result;
- SLDataLocator_AndroidSimpleBufferQueue locatorBufferQueue;
- locatorBufferQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
- locatorBufferQueue.numBuffers = 16;
- SLDataFormat_PCM formatPCM;
- formatPCM.formatType = SL_DATAFORMAT_PCM;
- formatPCM.numChannels = 2;
- int sampleRate = context->getSampleRate()==48000 ? SL_SAMPLINGRATE_48 : SL_SAMPLINGRATE_44_1;
- formatPCM.samplesPerSec = sampleRate;
- formatPCM.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16 ;
- formatPCM.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
- formatPCM.channelMask = SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT ;
- formatPCM.endianness = SL_BYTEORDER_LITTLEENDIAN;
- audioSrc.pLocator = &locatorBufferQueue;
- audioSrc.pFormat = &formatPCM;
- locatorOutMix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
- locatorOutMix.outputMix = context->getOutputMixObject();
- audioSnk.pLocator = &locatorOutMix;
- audioSnk.pFormat = NULL;
- // create audio player
- const SLInterfaceID ids[2] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE,SL_IID_VOLUME};
- const SLboolean req[2] = {SL_BOOLEAN_TRUE,SL_BOOLEAN_TRUE};
- result = (*context->getEngine())->CreateAudioPlayer(context->getEngine(),
- &playerObj, &audioSrc, &audioSnk,2, ids, req);
- assert(SL_RESULT_SUCCESS == result);
- result = (*playerObj)->Realize(playerObj, SL_BOOLEAN_FALSE );
- assert(SL_RESULT_SUCCESS == result);
- if (result != SL_RESULT_SUCCESS ) {
- playerObj = NULL;
- }
- result = (*playerObj)->GetInterface(playerObj, SL_IID_PLAY, &player);
- assert(SL_RESULT_SUCCESS == result);
- result = (*playerObj)->GetInterface(playerObj, SL_IID_VOLUME, &fdPlayerVolume);
- assert(SL_RESULT_SUCCESS == result);
- result = (*playerObj)->GetInterface(playerObj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bufferQueue);
- assert(SL_RESULT_SUCCESS == result);
- result = (*bufferQueue)->RegisterCallback(bufferQueue, cb, (void *) this);
- result = (*player)->SetCallbackEventsMask(player, SL_PLAYEVENT_HEADATEND);
- //Initialize buffers
- //allocate current buffer size with minumum pitch available (-5)
- currentPitch =0;
- float koefPitch = 0.05029f;
- currentBufferSize = getContext()->getBufferSize();
- // Calculate new frames count
- int origFrameCount = currentBufferSize;
- int newFramesCount = origFrameCount;
- newFramesCount = 2+getContext()->getBufferSize()/(1-koefPitch * 10); //max frames count
- buffer2 = (short*)malloc(newFramesCount*sizeof(short));
- buffer1 = (short*)malloc(newFramesCount*sizeof(short));
- bufferPitch2 = (short*)malloc(newFramesCount*sizeof(short));
- bufferPitch1 = (short*)malloc(newFramesCount*sizeof(short));
- bufferRecord = (short*)malloc(newFramesCount*sizeof(short));
- for(int i = 0;i<newFramesCount;i++) {
- buffer2[i] = 0;
- buffer1[i] = 0;
- bufferRecord[i] = 0;
- }
- for(int i = 0;i<newFramesCount;i++) {
- bufferPitch2[i] = 0;
- bufferPitch1[i] = 0;
- }
- recording = false;
- currentEffect = OSLContext::NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement