核心音频队列破解

时间:2016-04-05 14:53:30

标签: audio queue core

我在核心音频输出队列的开头和结尾都搞砸了。代码应该只生成一个音调。

修改:创建了一个示例项目 https://github.com/MrMatthias/CoreAudioCrackle

以下是设置:

-(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]);
        }
    }
}

在输出回调中,我调用一个填充缓冲区的块:

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

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

填充缓冲区如下所示:

 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;

录音如下:

cracking

1 个答案:

答案 0 :(得分:0)

尝试在SHRT_MAX(sin((userData-&gt; ...)之前添加(SInt16) 这样你的整个代码块就像这样         SInt16 sample = CFSwapInt16HostToBig((SInt16)SHRT_MAX *(sin((userData-&gt; outputSamplePosition + i)* 2 * M_PI / f1)));