This article outlines the theoretical background of RMS and provides a practical guide to calculating and visualizing RMS from real-time audio input using openFrameworks. The techniques are applicable to field recording, sound visualization, and audio-reactive projects.

  • The full sample code for this article is available for download on Patreon after following.
    (It uses openFrameworks for the implementation.)
    Support my Work:) / Coffee Supplier on Patreon

1. What is RMS (Root Mean Square)?

RMS, or Root Mean Square, is a statistical measure that expresses the “average magnitude (or energy)” of a varying signal. It is commonly used in physics, acoustics, electrical engineering, and signal processing.

For discrete data such as digital audio, RMS is defined by the following formula:

Here, xi represents each sample value, and N is the total number of samples. RMS provides a smoother and more perceptually accurate measurement than peak values or the average of absolute values, making it widely used in audio analysis.

A simple average would approach zero for waveforms that fluctuate between positive and negative values. Therefore, RMS is used to express the effective amplitude.

Steps to calculate RMS:

  1. Square each value.
  2. Take the mean of the squared values.
  3. Take the square root of that mean.

2. Understanding RMS Through Programming (openFrameworks)

In openFrameworks, RMS can be calculated using real-time audio input (e.g., microphone). The key components used are:

  • ofSoundStream
  • ofSoundBuffer
  • audioIn()

Calculating RMS from Real-Time Audio Input

When computing RMS from a microphone or other audio input, use the following:

  • ofSoundStream
    Manages audio I/O. Set sampling rate, buffer size, input channels, etc.
  • ofSoundBuffer
    Stores and manages audio data in a buffer. Used here to compute RMS from collected data.
  • audioIn(ofSoundBuffer & input)
    A callback function that receives incoming audio in real time. It’s a virtual function defined in ofBaseApp, where the ofSoundBuffer passed as an argument is analyzed.

▸ Processing Flow

  1. Set up input via ofSoundStream (e.g., buffer size, number of channels).
  2. Collect audio samples into ofSoundBuffer.
  3. Use audioIn() to compute RMS using the buffer.

Code Example 1: Calculating RMS in audioIn()

▸ Code Explanation

1. Initialization


float sum = 0.0;
int numCounted = 0;
  • sum: Initialized to accumulate squared sample values.
  • numCounted: Initialized to count processed samples.

2. Processing All Samples


for (size_t i = 0; i < input.getNumFrames(); i++) {
    float sample = input[i];
    sum += sample * sample;
    numCounted++;
}
  • Loops through all samples in the input buffer.
  • input.getNumFrames() gets the number of frames in the current audio input.
  • Each sample is squared and added to sum.

Note: In this context, "frames" typically refer to individual samples. The number may not exactly match the buffer size set in ofSoundStream, so it’s important to use getNumFrames().

3. Compute RMS


sum = sqrt(sum / (float)numCounted);
  • Divides the total squared sum by the number of samples and takes the square root.

4. Smoothing the RMS Value


smoothedRms *= 0.93;
smoothedRms += 0.07 * sum;
  • Applies exponential smoothing.
  • smoothedRms retains 93% of its previous value and adds 7% of the current RMS.
  • This reduces abrupt volume changes and produces a smoother transition.

This is a type of Exponential Moving Average (EMA).

  • smoothedRms is the previously smoothed RMS value.
  • sum is the current RMS.
  • Together, they create a smooth-following value.
  • smoothedRms *= 0.93; → retains 93% of the past value
  • smoothedRms += 0.07 * sum; → adds 7% of the new value
  • The total is 100% (0.93 + 0.07 = 1.0)

Adjusting the 0.93/0.07 ratio changes the smoothing responsiveness. A larger 0.07 makes it respond faster, while a smaller 0.07 results in slower, smoother changes.

Code Example 2: Calculating RMS for Multiple Channels in audioIn()

▸ Explanation

For stereo or multi-channel input, use getNumChannels() to loop through each channel:


float sample = input[i * numChannels + ch];

ofSoundBuffer stores audio data in interleaved format:
[Left] [Right] [Left] [Right] ...

Indexing with i * numChannels + ch accesses each channel properly:

  • Frame 0 → index 0, 1
  • Frame 1 → index 2, 3
  • etc.

3. Visualizing the RMS Value

Use smoothedRms inside the draw() function to visualize the audio strength in real time. For example, you can draw a circle whose size changes with RMS:


ofDrawCircle(x, y, smoothedRms * scaleFactor);

4. Visualization Methods Using RMS

Beyond displaying RMS as a raw number, you can visualize it in various ways:

  • Volume bars
  • Line graphs over time
  • Overlays on heatmaps or waveforms
  • Interactive particle systems

By extending RMS analysis, you can explore:

  • Stereo or surround channel comparisons
  • Envelope curves derived from time-varying RMS
  • Combinations with peak levels or spectrum analysis
  • Trigger detection and automatic environment evaluation

Combining openFrameworks with tools like Max/MSP or Pure Data enables even more complex audio interactions.

5. Summary: RMS (Root Mean Square)

RMS is a fundamental and important metric to express the "strength" of sound. This article covered the theoretical background of RMS, how to implement it using openFrameworks, and ways to visualize it.

Combining RMS with other acoustic metrics such as peak value, loudness, or FFT can lead to richer understanding and expressive visualizations of sound.

Reference: openFrameworks Official Website

Recommended Book / Generative art for beginner

Generative Art: A Practical Guide Using Processing – Matt Pearson

Generative Art presents both the techniques and the beauty of algorithmic art. In it, you’ll find dozens of high-quality examples of generative art, along with the specific steps the author followed to create each unique piece using the Processing programming language. The book includes concise tutorials for each of the technical components required to create the book’s images, and it offers countless suggestions for how you can combine and reuse the various techniques to create your own works.

Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book.
—–
► Generative Art: A Practical Guide Using Processing – Matt Pearson
Publication date: 2011. July

🌐 Support my Website
By using our affiliate links, you’re helping my content and allows me to keep creating valuable articles. I appreciate it so much:)

BGD_SOUNDS (barbe_generative_diary SOUNDS)

barbe_generative_diary SOUNDS will start sharing and selling a variety of field recordings collected for use in my artwork “Sound Visualization” experiments. All sounds are royalty-free.

Link / BGD_SOUNDS on bandcamp