Mor*_*pps 3 synthesizer synthesis core-audio audiounit ios
我正在为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;
}
Run Code Online (Sandbox Code Playgroud)
相位可能在错误的点上溢出
替换这个:
if (phase >= (2 * M_PI * freq)) {
phase = phase - (2 * M_PI * freq);
}
Run Code Online (Sandbox Code Playgroud)
同
if (phase >= (2 * M_PI)) {
phase = phase - (2 * M_PI);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1699 次 |
| 最近记录: |