我试图一次从一个扬声器/耳机播放声音。所以我将一方的值归零。但它不起作用。 在一个wav文件中,偶数值来自右扬声器/听筒和右起扬声器? 那么为什么它不起作用
这是我的代码:
public override int Read(byte[] buffer, int offset, int sampleCount)
{
if (position == 0 && onetimeflag == false)
{
n2 = 0;
onetimeflag = true;
}
if (n2 >= Bufferlength && stopflag == false)
{
Dispose();
return 0;
}
float temp1;
for (int i = 0; i < (sampleCount / 4); i++)
{
if (Frequency_switch == true)
{
temp1 = (float)(Amplitude * Math.Sin(Math.PI * Frequency * n2 / 44100D));
Frequency_switch = false;
}
else
{
temp1 = (float)(Amplitude2 * Math.Sin((Math.PI * Frequency2 * n2) / 44100D));
Frequency_switch = true;
}
byte[] bytes = BitConverter.GetBytes(temp1);
buffer[i * 4 + 0] = bytes[0];
buffer[i * 4 + 1] = bytes[1];
buffer[i * 4 + 2] = bytes[2];
buffer[i * 4 + 3] = bytes[3];
tempSample++;
n2++;
}
return sampleCount;}} }
我正在使用NAudio库
这是我播放声音的方式:
private void button1_Click(object sender, EventArgs e)
{
Play(1, 240, 0);
}
binaural_beats BBeats;
WaveOut waveout;
void Play(double Amp, double Left, double Right)
{
BBeats = new binaural_beats();
BBeats.Amplitude = Amp;
BBeats.Amplitude2 = Amp;
BBeats.Frequency = Left;
BBeats.Frequency2 = Right;
BBeats.Bufferlength = 44100 * 2 * 3;// will play for 3 sec
waveout = new WaveOut();
WaveChannel32 temp = new WaveChannel32(BBeats);
temp.PadWithZeroes = false;
waveout.Init(temp);
waveout.Play();
}