我正在尝试使用iOS创建自定义波形信号 当添加第二个for循环来创建时,它可以在0.8T和0.9T之间创建脉冲,其中T是周期。
当我尝试添加第三个for循环来创建一个时,它会在0.7T后创建一个带有凸双曲线的奇怪信号,而不是产生新的脉冲。
请您告诉我,我将修改或重置缓冲变量,以便生成多个脉冲?
以下是我的代码
// Fixed amplitude is good enough for our purposes
const double amplitude = 1.0;
// 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;
double theta_increment = viewController->sampleRate / viewController->frequency;
(sampleRate = 44100;,频率= 44.44)
const int channel = 0;
Float32 *buffer = (Float32 *)ioData->mBuffers[channel].mData;
float squareIndex = 0.0;
//Generate the samples//
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
float k =0.0;
float y = 34.0/75.0;
if( fmodf(squareIndex, theta_increment)/theta_increment < y) {
k = 1.0;
} else {
k = 0.0;
}
buffer[frame] = k * amplitude;
squareIndex += 1.0;
if(squareIndex >= theta_increment) squareIndex-=theta_increment;
viewController->theta = theta;
}
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
float k =0.0;
float z = 0.8;
float y = 0.9;
if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
k = 0.0;
} else {
k = 1.0;
}
buffer[frame] += k * amplitude;
squareIndex += 1.0;
if(squareIndex >= theta_increment) squareIndex-=theta_increment;
viewController->theta = theta;
}
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
float k =0.0;
float z = 0.6;
float y = 0.7;
if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
k = 0.0;
} else {
k = 1.0;
}
buffer[frame] += k * amplitude;
squareIndex += 1.0;
if(squareIndex >= theta_increment) squareIndex-=theta_increment;
viewController->theta = theta;
}
答案 0 :(得分:1)
由于我们没有在提供的代码中看到缓冲区分配,我假设缓冲区有足够的大小来容纳所有样本(否则缓冲区溢出可能会导致各种未定义的行为)。
使用提供的代码,你应该在循环中意识到表达式
if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
从左到右评估为:
if( (z < fmodf(squareIndex, theta_increment)/theta_increment) < y) {
让我们看看第二个循环来说明效果:
只要squareIndex is less than
0.8 * theta_increment`,子表达式
(z < fmodf(squareIndex, theta_increment)/theta_increment)
评估为false
,在数字宣传后,y=0.9
小于true
,因此整体表达式为k=0
(因此squareIndex
)。 0.8*theta_increment
变得超过(z < fmodf(squareIndex, theta_increment)/theta_increment)
true
变为y=0.9
,在数字提升后,再次false
,因此整体表达式变为k=1
(因此 float t = fmodf(squareIndex, theta_increment)/theta_increment;
if (z < t && t < y) {
k = 1.0;
} else {
k = 0.0;
}
)。
然后循环生成以下曲线:
从上到下,您有第一个,第二个和第三个循环,然后是组合波形。
要解决此问题,您可以将条件更改为:
{{1}}
然后应生成以下波形: