我使用NAudio创建具有两个通道的不同音频波。对于正弦波,我有下一个代码:
int sampleRate = WaveFormat.SampleRate;
for (int n = 0; n < sampleCount; n += 2)
{
buffer[n + offset] = (float)(AmplitudeLeft * Math.Sin((2 * Math.PI * sample * FrequencyLeft) / sampleRate));
buffer[n + offset + 1] = (float)(AmplitudeRight * Math.Sin((2 * Math.PI * sample * FrequencyRight) / sampleRate));
sample++;
if (sample >= sampleRate)
sample = 0;
}
return sampleCount;
我需要一个简单的公式来创建三角形音频波。
答案 0 :(得分:0)
将工作分开以使其更容易:
void makesamples_mono(float[] buffer, int offset, int sampleCount, int rate, double amp, double freq)
{
double one_over_half_per = freq * 2.0 / rate;
for (int i = 0; i < sampleCount; i++)
{
double div = i * one_over_half_per;
buffer[offset + 2 * i] = (float)(((((int)div) % 2 == 0) ? -amp : amp) * (1.0 - 2.0 * (div - (int)div)));
}
}
void writesamples_stereo(float[] buffer, int offset, int sampleCount, int rate, double l_amp, double r_amp, double l_freq, double r_freq)
{
makesamples_mono(buffer, offset, sampleCount, rate, l_amp, l_freq); // left channel
makesamples_mono(buffer, offset+1, sampleCount, rate, r_amp, r_freq); // right channel
}