【问题标题】:iOS sine wave generation - audible clickingiOS 正弦波生成 - 可听见的咔嗒声
【发布时间】:2012-04-03 20:06:13
【问题描述】:

我正在为 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;
}

【问题讨论】:

    标签: ios core-audio audiounit synthesis synthesizer


    【解决方案1】:

    相位可能在错误的点溢出

    替换这个:

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

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

    【讨论】:

      【解决方案2】:

      如果你的频率不完全是一个整数值,那么这一行:

      phase = phase - (2 * M_PI * freq);
      

      会将相位调整和旋转不等于 2pi 的量,从而产生不连续性。

      【讨论】:

        【解决方案3】:

        解决这类问题的一个很好的调试技术是在示波器或波形编辑器中查看您的音频。准确查看点击发生的时间和频率通常会提供一些关于点击发生原因的线索。

        同样的技术也可以用于非“音频”信号,例如包络发生器等。只要确保你关掉你的扬声器!

        【讨论】:

          猜你喜欢
          • 2021-05-13
          • 2016-02-19
          • 2016-11-16
          • 2012-01-08
          • 2023-03-22
          • 1970-01-01
          • 2012-07-11
          • 2011-06-17
          • 1970-01-01
          相关资源
          最近更新 更多