2013-01-07 55 views
3

我試圖調整這一WAV記錄樣本: http://channel9.msdn.com/Series/KinectQuickstart/Audio-Fundamentals錄音隨着Kinect的

到了新的SDK(版本1.6),由於某種原因 - 生成的.wav文件是無效的

在init:

 this.audioStream = this.sensor.AudioSource.Start(); 

     // Use a separate thread for capturing audio because audio stream read operations 
     // will block, and we don't want to block main UI thread. 
     this.readingThread = new Thread(AudioReadingThread); 
     this.readingThread.Start(); 
     fileStream = new FileStream(@"d:\temp\temp.wav", FileMode.Create); 

     int rec_time = (int) 20 * 2 * 16000;//20 sec 
     WriteWavHeader(fileStream, rec_time); 

線程:

private void AudioReadingThread() 
    { 

     while (this.reading) 
     { 
       int readCount = audioStream.Read(audioBuffer, 0, audioBuffer.Length); 

       fileStream.Write(audioBuffer, 0, readCount); 
     } 
    } 

的Wav部首:

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)) 
      { 
       bw.Write(dataLength + cbFormat + 4); //File size - 8 
       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 
       bw.Write(dataLength); 
       memStream.WriteTo(stream); 
      } 
     } 
    } 
+0

是'this.audioStream'其他有效?你如何設置你的傳感器? –

+0

代碼無例外運行,傳感器已設置,並且wav文件不爲空。只是格式可能歪 –

+0

你可以給我你的結果文件?爲了達到相同的目標,我在設置此代碼時遇到了一些麻煩。 WAVEFORMATEX和WriteString類來自哪些名稱空間?我到底在哪裏放入了riff頭文件和修改了代碼中數據頭的memstream? – Ivan

回答

2

您忘了添加代碼以將「RIFF標題」添加到文件。代碼很簡單,只要:

//RIFF header 
WriteString(memStream, "RIFF"); 
bw.Write(dataLength + cbFormat + 4); //File size - 8 
WriteString(memStream, "WAVE"); 
WriteString(memStream, "fmt "); 
bw.Write(cbFormat); 

你也忘了在「數據頭」修改memStream,則需要行:

WriteString(memStream, "data");