2013-07-09 104 views
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重複,聲音停止:(。我需要做的,我的聲音重複所有的時間?

回答

0

你不會有太多的成功嘗試不斷啓動並停止這樣的聲卡,NAudio中默認的WaveOut緩衝區大小爲100ms,打開聲卡一次,然後發送正弦波部分和零部分,創建你想要的聲音效果要好得多