Desktop Code AudioCapture - shibotsu/obs-clone GitHub Wiki

Desktop Code: AudioCapture Module

This page documents the AudioCapture class used in the Desktop application, which handles input and output audio capture using the Windows Core Audio API (WASAPI)

๐Ÿงฑ Purpose

The AudioCapture module provides low-level access to system audio input and output streams on Windows using the WASAPI interface. This allows capturing real-time audio data from both the microphone and system output (loopback), allowing for the data to be visualized and or used for processing (encoding for recording/streaming).

๐Ÿ”ง Core Responsibilities

  • Capture audio from the default input device (microphone).
  • Capture audio from the default output device (system loopback).
  • Extract volume levels in decibels (dB) from both input and output.
  • Proper resource management and COM lifecycle handling.
  • Thread-safe start/stop for capture.

๐Ÿ› ๏ธ Key Methods

Constructor

AudioCapture::AudioCapture();
  • Initializes the COM library and sets up the device enumerator for audio device management.

Destructor

AudioCapture::~AudioCapture();

Stops any ongoing capture, releases resources and uninitializes COM.


๐Ÿ”Œ Device Setup

Initialize Input Device

bool AudioCapture::initializeInput();
  • Activates the default microphone
  • Prepares an event-driven audio client
  • Retrieves the mix format and capture client

Initialize Output Device (Loopback)

bool AudioCapture::initializeOutput();
  • Activates the default output device (speakers)
  • Initializes in loopback mode for capturing output audio.
  • Retrieve mix format and capture client.

โ–ถ๏ธ Control

Start Capturing

void AudioCapture::startCapture();

Stars capturing from the initialized devices (if available).

Stop Capturing

void AudioCapture::stopCapture();

Stops the audio stream and resets capture flags.

๐Ÿ“Š Volume Extraction

Get output volume

float AudioCapture::getOutputVolume();
  • Returns volume in dB (float) from the output stream.
  • uses RMS (Root Mean Square) calculation.

Get input volume

float AudioCapture::getInputVolume();
  • Returns volume in dB (float) from the microphone stream.
  • Uses the same RMS volume calculation method.

๐Ÿ“ Volume Calculations

float calculateRMSVolume(BYTE* pData, UINT32 numFrames, WAVEFORMATEX* pwfx);
float convertToDecibels(float rmsVolume);

These convert raw PCM data into a normalized volume level, and then to decibels.

๐Ÿ›‘ Error Handling

All errors are logged using qDebug() from the Qt framework. Make sure

๐Ÿงช Example Usage

AudioCapture audio;
audio.initializeInput();
audio.initializeOutput();
audio.startCapture();

float micVolume = audio.getInputVolume();
float sysVolume = audio.getOutputVolume();

audio.stopCapture();

๐Ÿ“‚ Related files

  • AudioCapture.h
  • AudioCapture.cpp

๐Ÿงฑ Dependencies

  • Qt (for qDebug())
  • Windows Core Audio APIs (WASAPI)