iOS 正弦波生成 - 可听见的点击

标签 ios core-audio audiounit synthesis synthesizer

我正在为 iOS 创建一个合成器。在玩弄并尝试学习核心音频之后,我遇到了一个我无法理解的问题。我的正弦波定期发出咔嗒声,我猜这与相位有关。我查看了有关该主题的几本指南和书籍,所有这些都表明我的做法是正确的。

如果有人愿意为我查看我的代码,我们将不胜感激。

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;
}

最佳答案

相位可能在错误的点上溢出

替换这个:

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

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

关于iOS 正弦波生成 - 可听见的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000552/

相关文章:

ios - iOS 中独家访问 AudioUnit

iphone - AudioUnit 设置属性错误(错误代码-10868)

ios - Cocos2d + Box2d - 如何调试/显示 body ?

objective-c - 有没有办法手动或以编程方式选择/删除 ipad 上相册中的所有/多个图像?

iOS 从 RSS Feed 获取日期字符串

ios - 是否可以使用 AVAssetReader 恢复立体声 channel 布局?

iOS 核心音频 : Converting between kAudioFormatFlagsCanonical and kAudioFormatFlagsAudioUnitCanonical

swift - 为什么我的 Core Audio 程序会出现爆裂声?

macos - 音频单元主机需要做什么才能使用非 Apple 音频单元?

ios - 如何使用 Storyboard为不同的 iPhone 屏幕尺寸设置不同的字体大小?