Core Audio Queues Cracking

I get a cracking at the beginning and end of a core audio output queue. The code should simply generate a tone.

Edit: created a sample project https://github.com/MrMatthias/CoreAudioCrackle

Here is the setup:

-(void) startOutputQueue {
    if(userData.outputQueue != NULL) {
        if(!checkError(AudioQueuePrime(userData.outputQueue, 0, NULL), "AudioQueuePrime")) {
            NSLog(@"Error priming QutputQueue");
        }

        if(!checkError(AudioQueueStart(userData.outputQueue, NULL), "AudioQueueStart Output")) {
            NSLog(@"Error starting OutputQueue");
        }
    }
}

-(void) setupOutputQueue {
    memset(&userData.outputDesc, 0, sizeof(userData.outputDesc));
    userData.outputDesc.mFormatID = kAudioFormatLinearPCM;
    userData.outputDesc.mFramesPerPacket = 1;
    userData.outputDesc.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    userData.outputDesc.mChannelsPerFrame = 1;
    userData.outputDesc.mSampleRate = 44100;
    userData.outputDesc.mBitsPerChannel = 16;
    userData.outputDesc.mBytesPerFrame = userData.outputDesc.mBytesPerPacket = 2;
    userData.outputSamplePosition = 0;
    if (userData.outputQueue == NULL) {
        if(!checkError(AudioQueueNewOutput(&userData.outputDesc, outputCallback, &userData, NULL, NULL, 0, &userData.outputQueue), "AudioQueueNewOutput")) {
            return;
        }
        UInt32 bufferSize = userData.outputDesc.mBytesPerFrame * userData.outputDesc.mSampleRate * DURATION;
        for (int i=0; i<3; ++i) {

            if(!checkError(AudioQueueAllocateBuffer(userData.outputQueue, bufferSize, &userData.outputBuffers[i]), "AudioQueueAllocateBuffer")) {
                return;
            }
            outputCallback(&userData, userData.outputQueue, userData.outputBuffers[i]);
        }
    }
}

In the output callback i call a block that fills the buffers:

userData->outputBlock(userData, inAQ, inBuffer);

            AudioQueueEnqueueBuffer(userData->outputQueue, inBuffer, 0, NULL);

Filling of the Buffer looks like this:

 UInt32 sampleCount = DURATION * userData->outputDesc.mSampleRate;
 double f1 = userData->outputDesc.mSampleRate / 10000.0f;
 for (int i=0; i<sampleCount; ++i) {
     SInt16 sample = CFSwapInt16HostToBig(SHRT_MAX * ( sin((userData->outputSamplePosition + i) * 2 * M_PI / f1) ));
     ((SInt16*)inBuffer->mAudioData)[i] = sample;
  }
  userData->outputSamplePosition += sampleCount;
  inBuffer->mAudioDataByteSize = sampleCount * 2;

The recording looks like this:

Answers


Try adding (SInt16) just before SHRT_MAX ( sin((userData->... such that your entire code block looks like this SInt16 sample = CFSwapInt16HostToBig((SInt16)SHRT_MAX * ( sin((userData->outputSamplePosition + i) * 2 * M_PI / f1) ));


Need Your Help

PHP Login Form No Database

php forms login passwords

This form does not submit but I am trying to get it to redirect to a page if the password is correct and if not then go to the home page. I am also trying to put in a remember me cookie feature. A ...

In an activity diagram, are you allowed dead ends and joins without forks? (includes example)

uml activity-diagram

My question actually consists of two questions regarding the same process modelling in an activity diagram.