iOS 正弦波代-卡嗒

标签: ios
发布时间: 2015/3/30 19:21:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我创造的 iOS 的电子发声器的过程中。后玩耍,试图了解核心音频,我遇到了我的头不能避开的问题。我的正弦波进行的定期,哪些 Im 猜测与月相有关发出卡嗒声。我看过几个指南和关于这个问题的书籍和所有建议正确的做它。

如果有人会如此种来看看我的代码对我来说它将不胜感激。

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{


    // Get a reference to the object that was passed with the callback
    // In this case, the AudioController passed itself so
    // that you can access its data.
    AudioController *THIS = (AudioController*)inRefCon;

    // Get a pointer to the dataBuffer of the AudioBufferList
    AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData;

    float freq = THIS->Frequency;   
    float phase = THIS->sinPhase;

    float envValue;
    float sinSignal;

    // The amount the phase changes in  single sample
    double phaseIncrement = 2 * M_PI * freq / kGraphSampleRate;

    // Loop through the callback buffer, generating samples
    for (UInt32 i = 0; i < inNumberFrames; ++i) {       

        sinSignal = sin(phase);

        envValue = THIS->env.tick();

        // Put the sample into the buffer
        // Scale the -1 to 1 values float to
        // -32767 to 32767 and then cast to an integer
        outA[i] = (SInt16)(((sinSignal * 32767.0f) / 2) * envValue);

        phase = phase + phaseIncrement;

        if (phase >= (2 * M_PI * freq)) {
            phase = phase - (2 * M_PI * freq);
        }
    }

    // Store the phase for the next callback.
    THIS->sinPhase = phase;

    return noErr;
}

解决方法 1:

阶段可以溢出错误点

将以下内容:

if (phase >= (2 * M_PI * freq)) {
    phase = phase - (2 * M_PI * freq); 
} 

if (phase >= (2 * M_PI)) { 
    phase = phase - (2 * M_PI); 
} 
官方微信
官方QQ群
31647020