我想制作一些代码,在我的音源应用程序中生成振荡器波型。在这个例子中的一个是正弦波,有人能告诉我代码是如何工作的,因为我想在将来制作自定义波形类型和方形,锯齿形和三角形类型。正在生成制定振荡器波型代码并创建新的波型
OSStatus RenderTone(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Fixed amplitude is good enough for our purposes
const double amplitude = 0.25;
// Get the tone parameters out of the view controller
ToneGeneratorViewController *viewController =
(ToneGeneratorViewController *)inRefCon;
double theta = viewController->theta;
double theta_increment = 2.0 * M_PI * viewController->frequency/viewController->sampleRate;
// This is a mono tone generator so we only need the first buffer
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
buffer[frame] = sin(theta) * amplitude;
theta += theta_increment;
if (theta > 2.0 * M_PI)
{
theta -= 2.0 * M_PI;
}
}
// Store the theta back in the view controller
viewController->theta = theta;
return noErr;
}
什么是你的问题,具体是? – Brad 2012-02-08 17:32:37
我想知道“生成样本”是如何工作的。以及如何修改它以产生不同的波形,如锯齿形,三角形和方形。 – user1197773 2012-02-08 17:38:32