2015-06-14 28 views
1

我用n音訊在指定的頻率一樣,產生口氣:信號發生器類 - 持續時間玩

private void gen_Sinus(double frequency) 
    { 
     WaveOut _myWaveOut = new WaveOut(); 

     SignalGenerator mySinus = new SignalGenerator(44100, 1);//using NAudio.Wave.SampleProviders; 
     mySinus.Frequency = frequency; 
     mySinus.Type = SignalGeneratorType.Sin; 
     _myWaveOut.Init(mySinus); 
     _myWaveOut.Play(); 
    } 

我想在點擊一個按鈕時,將播放音特定時間會傳遞給這個方法。讓我們把它叫做例如:

double toneDuration 

我更喜歡防止一些睡眠的方法,因爲它必須儘可能準確。

回答

2

您可以使用OffsetSampleProvider要做到這一點,並設置Take時間:

var trimmed = new OffsetSampleProvider(signalGenerator); 
trimmed.Take = TimeSpan.FromSeconds(10); 
waveOut.Init(trimmed); 
waveOut.Play(); 
+0

偉大的解決方案,謝謝! – axcelenator

+0

你好,有沒有類或照顧頻率轉換的使用超過1個singalGenerator的情況下別的東西? – axcelenator

1

使用一個計時器,並等待處理

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      timer1.Tick += new EventHandler(timer1_Tick); 
     } 
     static AutoResetEvent autoEvent = new AutoResetEvent(false); 

     private void gen_Sinus(double frequency) 
     { 
      WaveOut _myWaveOut = new WaveOut(); 

      SignalGenerator mySinus = new SignalGenerator(44100, 1);//using NAudio.Wave.SampleProviders; 
      mySinus.Frequency = frequency; 
      mySinus.Type = SignalGeneratorType.Sin; 
      _myWaveOut.Init(mySinus); 

      timer1.Interval = 5000; 
      timer1.Start() 

      _myWaveOut.Play(); 
      autoEvent.Reset(); 
      autoEvent.WaitOne(); 
      _myWaveOut.Stop(); 
     } 
     private void timer1_Tick(object sender, EventArgs e) 
     { 
      autoEvent.Set(); 

     } 
    } 
} 
​ 
+0

你好,我使用WPF,我在哪裏需要聲明timer1對象? – axcelenator

+0

參見以下網頁:https://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx – jdweng

+0

您好感謝您的鏈接。我看到如果我使用WPF「timer」類,那麼稱爲dispatcherTimer。它是一樣的嗎? – axcelenator