Capturing Audio Data in C#

Kinect for Windows 1.5, 1.6, 1.7, 1.8


This How-To topic describes user tasks from the AudioBasics-WPF C# sample, which includes a visualization of sound wave intensity, audio beam direction, sound source direction, and sound source direction confidence.

Code It

This example assumes you know how to enumerate the Kinect sensors on your computer and return one. An example of doing this is described in Enumerate Kinect sensors.

Start Streaming Audio Data

  private Stream audioStream;

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

Stream of audio being captured by the Kinect sensor.

Add Events

  this.sensor.AudioSource.BeamAngleChanged += this.AudioSourceBeamChanged;
  this.sensor.AudioSource.SoundSourceAngleChanged += this.AudioSourceSoundSourceAngleChanged;

Implement a Beam Angle Event Handler

  private void AudioSourceBeamChanged(object sender, BeamAngleChangedEventArgs e)
    beamRotation.Angle = -e.Angle;

The sender object is the KinectSensor that fired the event.

Implement a Sound Source Angle Event Handler

  private void AudioSourceSoundSourceAngleChanged(object sender, SoundSourceAngleChangedEventArgs e)
    // Rotate gradient to match angle
    sourceRotation.Angle = -e.Angle;

Create a Separate Thread for Capturing Audio

  private bool reading;

  private Thread readingThread;

  this.reading = true;
  this.readingThread = new Thread(AudioReadingThread);

The readingThread is a thread that is reading audio from a Kinect stream. Use a separate thread for capturing audio so that the audio stream read operations don't block the main UI thread.

The reading member is true if audio is being read from the Kinect audio stream, and false otherwise.


There is a known issue regarding the audio not being captured if the skeleton stream is enabled after starting audio capture.

Poll the Audio Thread for Audio Data

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



Add drawing code for updating the display in the while loop.

Shut Down the Audio

  // Tell the audio reading thread to stop and wait for it to finish.
  this.reading = false;
  if (null != readingThread)

  if (null != this.sensor)
    this.sensor.AudioSource.BeamAngleChanged -= this.AudioSourceBeamChanged;
    this.sensor.AudioSource.SoundSourceAngleChanged -= this.AudioSourceSoundSourceAngleChanged;

    this.sensor = null;