Fundamentals 7 min read

Smart Audio Recorder Based on Volume Threshold – Implementation and Code

This article describes a C# program that continuously captures microphone audio, uses volume thresholds derived from Fourier analysis to automatically start and stop recording, and provides the full configuration and source code needed to build a smart recorder for capturing dream speech.

Architecture Digest
Architecture Digest
Architecture Digest
Smart Audio Recorder Based on Volume Threshold – Implementation and Code

The author explains his personal motivation to build a program that can silently record nighttime conversations by detecting loudness levels, aiming to capture and later analyze dream speech.

Principle and implementation: The application continuously captures audio frames from the microphone, applies a Fourier transform to determine the loudness of each frame, and starts recording when the volume exceeds a configurable opening threshold, stopping when the volume stays below a closing threshold for a specified number of consecutive low‑decibel frames.

Configuration file (XML) defines the thresholds and frame‑count parameters:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--开启录音的音量阈值-->
    <add key="DB2Open" value="30"/>
    <!--关闭录音的音量阈值-->
    <add key="DB2Close" value="30"/>
    <!--低分贝持续检测帧数-->
    <add key="checkCount" value="80"/>
  </appSettings>
</configuration>

The main form initializes the microphone capturer and the MP3 file maker:

public Form1()
{
    InitializeComponent();
    this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0); // start capture
    this.microphoneCapturer.AudioCaptured += new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured);
    this.microphoneCapturer.Start(); // begin capture
    // initialize recorder parameters
    this.audioFileMaker.Initialize("test.mp3", this.microphoneCapturer.SampleRate, this.microphoneCapturer.ChannelCount);
}

The microphoneCapturer_AudioCaptured handler processes each 20 ms audio chunk, updates the decibel display, and contains the core logic for automatic recording:

void microphoneCapturer_AudioCaptured(byte[] data)
{
    this.audioFileMaker.StartMakeFile(data);
    this.frameCounter.Start();
    this.decibelDisplayer1.DisplayAudioData(data);
    this.label_db.Text = this.decibelDisplayer1.Volume.ToString();
    this.label_RecordSign.Text = this.audioFileMaker.IsWorking ? "正在录音" : "未录音";
    this.label_RecordSign.ForeColor = this.audioFileMaker.IsWorking ? Color.Blue : Color.Red;

    // open recorder when volume exceeds threshold
    if (this.decibelDisplayer1.Volume > int.Parse(ConfigurationManager.AppSettings["DB2Open"]))
    {
        this.audioFileMaker.IsWorking = true;
    }

    // if low‑decibel frames exceed count, consider stopping
    if (this.lowDBFrameCounter.Count > int.Parse(ConfigurationManager.AppSettings["checkCount"]))
    {
        if (this.lowDBFrameCounter.Count == this.frameCounter.Count)
        {
            this.audioFileMaker.IsWorking = false;
        }
        this.frameCounter.IsWorking = false;
        this.lowDBFrameCounter.IsWorking = false;
        return;
    }

    // when volume falls below closing threshold, start low‑decibel counters
    if (this.decibelDisplayer1.Volume < int.Parse(ConfigurationManager.AppSettings["DB2Close"]))
    {
        this.frameCounter.IsWorking = true;
        this.lowDBFrameCounter.IsWorking = true;
        this.lowDBFrameCounter.Start();
    }
}

In the concluding remarks, the author notes that the program can be run nightly to collect valuable speech data, and suggests extending the system to video capture and image‑analysis for advanced security applications.

C++signal processingProgramming Tutorialaudio recordingmicrophonevolume threshold
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.