0
votes

In my iOS Application , i am using AudioQueue for Audio recording and playback, basically i have OSX Version running and porting it on iOS.
I realize in iOS I need to configure / set the AV Session and i have done following till now,

-(void)initAudioSession{

    //get your app's audioSession singleton object

    AVAudioSession* session = [AVAudioSession sharedInstance];

    //error handling
    BOOL success;
    NSError* error;

    //set the audioSession category.
    //Needs to be Record or PlayAndRecord to use audioRouteOverride:

    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                             error:&error];

    if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);

    //set the audioSession override
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                         error:&error];
    if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

    //activate the audio session
    success = [session setActive:YES error:&error];
    if (!success) NSLog(@"AVAudioSession error activating: %@",error);
    else NSLog(@"audioSession active");
}

Now what is happening is, Speaker AudioQueue callback is never getting called, i checked many answers, comments on so , google etc... and looks to be correct , the way i did is

  • Create AudioQueue for input and output : Configuration Linear PCM , 16000 Sampling rate
  • Allocate buffer
  • Setup queue with valid callback,
  • Start Queue,

It seems to be fine, i can able to hear Output on other end ( i.e. Input AudioQueue is working ) but output AudioQueue ( i.e. AudioQueueOutputCallback is never getting called).

I am suspecting i need to set the Proper AVSessionCatogery that i am trying with all possible option but didn't able to hear anything in the speaker,

I Compare my Implementation with Apple example Speakhere running AudioQueue on the main thread.
Even if i don't start Input AudioQueue ( mic ) then also i same behavior. and its difficult to have Speakhere behavior i.e. stop record and play

Thanks for looking at it, expecting your comments/help. Will be able to share code snippet.

1

1 Answers

0
votes

Thanks for looking at it , i realize the problem, this is my callback,

void AudioStream::AQBufferCallback(void *                   inUserData,
                                   AudioQueueRef            inAQ,
                                   AudioQueueBufferRef      inCompleteAQBuffer)
{
    AudioStream *THIS = (AudioStream *)inUserData;
    if (THIS->mIsDone) {
        return;
    }

    if ( !THIS->IsRunning()){
        NSLog(@" AudioQueue is not running");
        **return;** // Error part 

    }


    int bytes = THIS->bufferByteSize;       
    if ( !THIS->pSingleBuffer){
        THIS->pSingleBuffer = new unsigned char[bytes];
    }
    unsigned char *buffer = THIS->pSingleBuffer;



    if ((THIS->mNumPacketsToRead) > 0) {
        /* lets read only firt packet */
        memset(buffer,0x00,bytes);


        float volume = THIS->volume();

        if (THIS->volumeChange){
            SInt16 *editBuffer = (SInt16 *)buffer;

            // loop over every packet

            for (int nb = 0; nb < (sizeof(buffer) / 2); nb++) {

                // we check if the gain has been modified to save resoures
                if (volume != 0) {
                    // we need more accuracy in our calculation so we calculate with doubles
                    double gainSample = ((double)editBuffer[nb]) / 32767.0;

                    /*
                     at this point we multiply with our gain factor
                     we dont make a addition to prevent generation of sound where no sound is.

                     no noise
                     0*10=0

                     noise if zero
                     0+10=10
                     */
                    gainSample *= volume;

                    /**
                     our signal range cant be higher or lesser -1.0/1.0
                     we prevent that the signal got outside our range
                     */
                    gainSample = (gainSample < -1.0) ? -1.0 : (gainSample > 1.0) ? 1.0 : gainSample;

                    /*
                     This thing here is a little helper to shape our incoming wave.
                     The sound gets pretty warm and better and the noise is reduced a lot.
                     Feel free to outcomment this line and here again.

                     You can see here what happens here http://silentmatt.com/javascript-function-plotter/
                     Copy this to the command line and hit enter: plot y=(1.5*x)-0.5*x*x*x
                     */

                    gainSample = (1.5 * gainSample) - 0.5 * gainSample * gainSample * gainSample;

                    // multiply the new signal back to short
                    gainSample = gainSample * 32767.0;

                    // write calculate sample back to the buffer
                    editBuffer[nb] = (SInt16)gainSample;
                }
            }
        }
        else{
            //            NSLog(@" No change in the volume");
        }


        memcpy(inCompleteAQBuffer->mAudioData, buffer, 640);

        inCompleteAQBuffer->mAudioDataByteSize = 640;
        inCompleteAQBuffer->mPacketDescriptionCount = 320;
        show_err(AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, NULL));
    }
}

as i was not enqueue when its allocated and i believe it had to enqueue few buffers before it gets started, removing the return part solved my problem.