Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record Audio With Kinect

I tried to adjust this Wav Recording sample : http://channel9.msdn.com/Series/KinectQuickstart/Audio-Fundamentals

To the new SDK (Ver 1.6) and for some reason - the resulting .wav file is invalid

In the 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);

The thread:

    private void AudioReadingThread()
    {

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

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

Wav Header:

    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);
            }
        }
    }
like image 915
Uri Goren Avatar asked Aug 16 '26 19:08

Uri Goren


1 Answers

You forgot to add the code to add the "RIFF Header" to the file. The code is as simple as:

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

You also forgot to modify memStream at the "data header", you need the line:

WriteString(memStream, "data");
like image 106
Kinected Avatar answered Aug 18 '26 08:08

Kinected



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!