2010-10-14 36 views
3

的陣列我很新的n音訊,我需要輸入樣本的緩衝器從輸入裝置轉換爲double數組,範圍從-1到1n音訊輸入字節數組轉換爲雙打

我創建輸入設備如下:

WaveIn inputDevice = new WaveIn(); 

//change the input device to the one i want to receive audio from 
inputDevice.DeviceNumber = 1; 

//change the wave format to what i want it to be. 
inputDevice.WaveFormat = new WaveFormat(24000, 16, 2); 

//set up the event handlers 
inputDevice.DataAvailable += 
    new EventHandler<WaveInEventArgs>(inputDevice_DataAvailable); 
inputDevice.RecordingStopped += 
    new EventHandler(inputDevice_RecordingStopped); 

//start the device recording 
inputDevice.StartRecording(); 

現在,當「inputDevice_DataAvailable」回調函數被調用,我得到的音頻數據的緩衝區。我需要將這些數據轉換爲表示音量級別爲-1和1的雙打數組。如果任何人都可以幫我解決這個問題,那就太棒了。

回答

2

你得到的緩衝區將包含16位短值。您可以使用NAudio中的WaveBuffer類,它可以很容易地將樣本值作爲短褲讀出。除以32768來獲得你的double/float樣本值。

void waveIn_DataAvailable(object sender, WaveInEventArgs e) 
    { 
     byte[] buffer = e.Buffer; 

     for (int index = 0; index < e.BytesRecorded; index += 2) 
     { 
      short sample = (short)((buffer[index + 1] << 8) | 
            buffer[index]); 
      float sample32 = sample/32768f;     
     } 
    } 
+0

謝謝你的工作。 – Rob 2010-10-15 04:21:54

+2

max short = 32767,32767/32768f = 0.999969482421875。應該是:if(sample> 0)sample32 = sample/32767f;如果(sample <0)sample32 = sample/32768f; – zgnilec 2012-09-17 07:27:04