gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:26:16 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com