简体   繁体   English

重复正弦声音的错误

[英]Bug with repeat sine sound

What is my code: 我的代码是什么:

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

And Speaker.Init is SineWaveProvider32 . 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;
    }
}

After 10-15 iterations on my cycle, sound is stop :(. What i need to do, to my sound repeat all time? 在循环中经过10-15次迭代后,声音停止了:(。我需要做的是,我的声音一直重复吗?

You won't have much success trying to continually start and stop the soundcard like that. 尝试像这样连续地启动和停止声卡将不会有太大的成功。 The default WaveOut buffer sizes in NAudio are 100ms long. NAudio中默认的WaveOut缓冲区大小为100ms长。 It would be much better to open the soundcard once, and then send it portions of sine wave, followed by zeroes, to create the sound you want. 最好一次打开声卡,然后将其发送一部分正弦波,然后发送零,以创建所需的声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM