我正在尝试修改生成方波的方法
它可以产生8个不同脉冲宽度的脉冲,每个脉冲延迟0.3ms。我已经看到 sampleBuffer 负责生成脉冲信号,但我不确定如何为这种特定模式创建脉冲函数。请告诉我 AudioTrack.h 中是否有用于生成脉冲的库函数?
以下是我的代码
产生方波
void generateSquare(SInt16 *sampleBuffer, int numFrames, float sampleRate, float frequency, float amp) {
if(amp>1) amp=1;
if(amp<0) amp=0;
amp = amp*SHRT_MAX;
float samplesPerCycle = sampleRate/frequency;
for(int i = 0; i < numFrames; i++) {
if(fmodf(squareIndex, samplesPerCycle)/samplesPerCycle > 0.5) {
sampleBuffer[i] = amp;
} else {
sampleBuffer[i] = -1*amp;
}
squareIndex = squareIndex+1;
if(squareIndex >= samplesPerCycle) squareIndex-=samplesPerCycle;
}
}