2015-09-07 103 views
0

我想記錄來自Kinect的音頻流並將其保存在我的電腦中,所以我在這裏引用示例代碼。 http://channel9.msdn.com/Series/KinectQuickstart/Audio-Fundamentals通過WAVEFORMATEX從Kinect錄製音頻

這是我的部分代碼。

public static void WriteWavHeader(Stream stream, int dataLength) 
     { 

      using (var memStream = new MemoryStream(64)) 
      { 
       int cbFormat = 18; //sizeof(WAVEFORMATEX) 
       WAVEFORMATEX format = new WAVEFORMATEX() 
       { 
        //wFormatTag = 0, 
        nChannels = 1, 
        nSamplesPerSec = 16000, 
        nAvgBytesPerSec = 32000, 
        nBlockAlign = 2, 
        wBitsPerSample = 16, 
        cbSize = 0 
       }; 

       using (var bw = new BinaryWriter(memStream)) 
       { 
        //RIFF header    
        WriteString(memStream, "data"); 
        bw.Write(cbFormat); 
        WriteString(memStream, "RIFF"); 
        bw.Write(dataLength + cbFormat + 4); //File size - 8 
        WriteString(memStream, "WAVE"); 
        WriteString(memStream, "fmt "); 
        bw.Write(cbFormat); 

        //WAVEFORMATEX 
        bw.Write(format.wFormatTag); 
        bw.Write(format.nChannels); 
        bw.Write(format.nSamplesPerSec); 
        bw.Write(format.nAvgBytesPerSec); 
        bw.Write(format.nBlockAlign); 
        bw.Write(format.wBitsPerSample); 
        bw.Write(format.cbSize); 

        //data header 
        WriteString(memStream, "data"); 
        bw.Write(dataLength); 
        memStream.WriteTo(stream); 
       } 
      } 
     } 

在行「bw.Write(format.wFormatTag);」中出現錯誤。當我編譯代碼時,WAVEFORMATEX中的其他行沒有錯誤時,我已將「Bass.Net」添加到我的引用並使用「Un4seen.Bass」,但我仍無法修復它。

+0

錯誤行顯示「無法傳輸從'Un4seen.Bass.WAVEFormatTag'bool'「 – Tony

+0

根據http://www.bass.radio42.com/help/html/56c44e65-9b99-fa0d-d74a-3d9de3b01e89.htm WAVEFormatTag是一個枚舉值。將其重新轉換爲正確大小的數字。我不知道它應該是一個'short'還是'int'或其他什麼,你需要試一試。所以'bw.Write((short)format.wFormatTag)''可以工作。 –

+0

非常感謝,它的工作原理。 – Tony

回答

0

看來你在混合幾個不同的SDK。在你提供的鏈接,該WAVEFORMATEX定義如下:

struct WAVEFORMATEX 
{ 
    public ushort wFormatTag; 
    public ushort nChannels; 
    public uint nSamplesPerSec; 
    public uint nAvgBytesPerSec; 
    public ushort nBlockAlign; 
    public ushort wBitsPerSample; 
    public ushort cbSize; 
} 

所以,不枚舉值或Un4seen.Bass引用。的函數寫頭文件是略低不同(僅在wFormatTag值不同):

/// <summary> 
/// A bare bones WAV file header writer 
/// </summary>   
static void WriteWavHeader(Stream stream, int dataLength) 
{ 
    //We need to use a memory stream because the BinaryWriter will close the underlying stream when it is closed 
    using (var memStream = new MemoryStream(64)) 
    { 
     int cbFormat = 18; //sizeof(WAVEFORMATEX) 
     WAVEFORMATEX format = new WAVEFORMATEX() 
     { 
      wFormatTag = 1, 
      nChannels = 1, 
      nSamplesPerSec = 16000, 
      nAvgBytesPerSec = 32000, 
      nBlockAlign = 2, 
      wBitsPerSample = 16, 
      cbSize = 0 
     }; 

     using (var bw = new BinaryWriter(memStream)) 
     { 
      //RIFF header 
      WriteString(memStream, "RIFF"); 
      bw.Write(dataLength + cbFormat + 4); //File size - 8 
      WriteString(memStream, "WAVE"); 
      WriteString(memStream, "fmt "); 
      bw.Write(cbFormat); 

      //WAVEFORMATEX 
      bw.Write(format.wFormatTag); 
      bw.Write(format.nChannels); 
      bw.Write(format.nSamplesPerSec); 
      bw.Write(format.nAvgBytesPerSec); 
      bw.Write(format.nBlockAlign); 
      bw.Write(format.wBitsPerSample); 
      bw.Write(format.cbSize); 

      //data header 
      WriteString(memStream, "data"); 
      bw.Write(dataLength); 
      memStream.WriteTo(stream); 
     } 
    } 
} 

WriteString也需要將其轉換成ASCII的護理。我引用上述演示文稿中引用的.zip文件(「下載Quickstart slides and samples」,KinectforWindowsSDKV1 \ 6.Audio \ AudioRecorder \ AudioRecorder \ MainWindow.xaml.cs)