none
Recording a continuous Kinect AudioStream using C#.Net RRS feed

  • Question

  • Dear,

    I am implementing a WPF application in C#.Net using the Microsoft Kinect Sensor (v1) for speech and gesture recognition. I also need to continu record the audio from the Kinect Sensor by saving it into a .WAV file.

    I did find the following example, which does that, but it requires the total length of the .WAV file before creating one.

    (source: http://channel9.msdn.com/Series/KinectSDKQuickstarts/Understanding-Kinect-Hardware )

    private void AudioReadingThread()
            {
    
                if (sensor == null)
                {
                    return;
                }
    
                while (this.reading)
                {
    
    
                    int _amountOfTimeToRecord = mySeconds;
                    int recordingLength = _amountOfTimeToRecord * 2 * 16000;
                    byte[] buffer = new byte[1024];
    
    
                    using (FileStream _fileStream = new FileStream(_lastRecordedFileName, FileMode.Create))
                    {
                        WriteWavHeader(_fileStream, recordingLength);
    
                        //Start capturing audio                               
                        using (Stream audioStream = sensor.AudioSource.Start())
                        {
                            //Simply copy the data from the stream down to the file
                            int count, totalCount = 0;
                            while ((count = audioStream.Read(buffer, 0, buffer.Length)) > 0 && totalCount < recordingLength)
                            {
                                _fileStream.Write(buffer, 0, count);
                                totalCount += count;
                            }
                        }
                    }
                }
            }
    
            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);
                    }
                }
            }
    
            static void WriteString(Stream stream, string s)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(s);
                stream.Write(bytes, 0, bytes.Length);
            }
    
            struct WAVEFORMATEX
            {
                public ushort wFormatTag;
                public ushort nChannels;
                public uint nSamplesPerSec;
                public uint nAvgBytesPerSec;
                public ushort nBlockAlign;
                public ushort wBitsPerSample;
                public ushort cbSize;
            }

    Nevertheless, this code is only applicable if you know the length of the recording in seconds. In my case, I would like to record the AudioStream until the user press the Stop button available in the WPF application.

    Is there any possible way to do this ?

     Thanks!

    Wednesday, February 17, 2016 6:33 PM