0

我的代码是什么:

void SpeakThreadFunction()
    {
        while (SpeakThreadState)
        {
            Speaker.Play();
            Thread.Sleep(100);
            Speaker.Stop()
            Thread.Sleep(Interval);
        }
    }
//Speaker is WaveOut

并且Speaker.InitSineWaveProvider32

public class SineWaveProvider32 : WaveProvider32
{
    int sample;

    public SineWaveProvider32()
    {
        Frequency = 1000;
        Amplitude = 0.25f;       
    }

    public float Frequency { get; set; }
    public float Amplitude { get; set; }

    public override int Read(float[] buffer, int offset, int sampleCount)
    {
        int sampleRate = WaveFormat.SampleRate;
        for (int n = 0; n < sampleCount; n++)
        {
            buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) / sampleRate));
            sample++;
            if (sample >= sampleRate) sample = 0;
        }
        return sampleCount;
    }
}

在我的周期进行 10-15 次迭代后,声音停止了 :(。我需要做什么,让我的声音一直重复?

4

1 回答 1

0

尝试像这样不断地启动和停止声卡不会有太大的成功。NAudio 中的默认 WaveOut 缓冲区大小为 100 毫秒。最好打开声卡一次,然后向它发送部分正弦波,然后是零,以创建您想要的声音。

于 2013-07-09T10:51:30.647 回答