2013-03-14 43 views
4

我需要一個快速的方法來存儲數組中的所有wav文件樣本。我目前正在通過播放音樂並存儲來自示例提供程序的值來解決此問題,但這不太優雅。在數組中存儲一個wav文件

從n音訊演示我有Audioplayer類具有此方法:

private ISampleProvider CreateInputStream(string fileName) 
    { 
     if (fileName.EndsWith(".wav")) 
     { 
      fileStream = OpenWavStream(fileName); 
     } 
      throw new InvalidOperationException("Unsupported extension"); 
     } 
     var inputStream = new SampleChannel(fileStream, true); 
     var sampleStream = new NotifyingSampleProvider(inputStream); 
     SampleRate = sampleStream.WaveFormat.SampleRate; 
     sampleStream.Sample += (s, e) => { aggregator.Add(e.Left); }; // at this point the aggregator gets the current sample value, while playing the wav file 
     return sampleStream; 
    } 

我想跳過獲取樣本值,同時播放該文件的這一進展,而不是我立刻想要的值,而無需等待,直到文件結尾。基本上像matlab中的wavread命令。

+0

爲什麼它不優雅?你能粘貼到目前爲止已經嘗試過的任何東西,在幫助你時給別人什麼東西繼續? – 2013-03-14 20:41:47

+0

因爲我想從一個wav文件創建一個STFT,我想通過一個按鈕單擊來計算它。爲此,我需要所有樣本值。 我會嘗試添加一些代碼並編輯我的第一篇文章 – 2013-03-14 20:50:54

回答

1

使用AudioFileReader來讀取文件。這將自動轉換爲IEEE浮點樣本。然後重複調用Read方法將樣本塊讀取到float []數組中。

+0

謝謝,我會在稍後嘗試! – 2013-03-15 07:57:24

+0

它與AudioFileReader.ReadAsync()和一個字節數組一起工作,但我無法正確使用AudioFileReader.Read()和一個浮點數組。我總是以索引超出範圍異常結束。 I.e我想要將浮點數組樣本中的前1024個浮點值存儲起來: SampleReader.Read(samples,0,1024); 下一步將是: SampleReader.Read(samples,1024,2048); 但這一步一直崩潰.. – 2013-03-15 11:33:03

+0

通常你保持與(樣本,0,1024)呼叫。 – 2013-03-15 13:07:21

相關問題