{"id":762,"date":"2025-06-20T10:02:07","date_gmt":"2025-06-20T01:02:07","guid":{"rendered":"https:\/\/barbegenerativediary.com\/en\/?p=762"},"modified":"2025-06-20T10:16:28","modified_gmt":"2025-06-20T01:16:28","slug":"rms-root-mean-square-openframeworks","status":"publish","type":"post","link":"https:\/\/barbegenerativediary.com\/en\/sounds\/rms-root-mean-square-openframeworks\/","title":{"rendered":"RMS (Root Mean Square) | openFrameworks"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The full sample code for this article is available for download on Patreon after following.<\/strong><br>(It uses openFrameworks for the implementation.)<br>\u2615 <strong>Support my Work:) \/  <a href=\"https:\/\/www.patreon.com\/barbe_generative_diary\/membership\" target=\"_blank\" rel=\"noopener\" title=\"\">Coffee Supplier on Patreon<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is RMS (Root Mean Square)?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RMS, or Root Mean Square, is a statistical measure that expresses the &#8220;average magnitude (or energy)&#8221; of a varying signal. It is commonly used in physics, acoustics, electrical engineering, and signal processing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For discrete data such as digital audio, RMS is defined by the following formula:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"275\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/RMS-openFrameworks-01.webp\" alt=\"\" class=\"wp-image-763 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/RMS-openFrameworks-01.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/RMS-openFrameworks-01-300x81.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/RMS-openFrameworks-01-768x206.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/275;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <strong>xi<\/strong> represents each sample value, and <strong>N<\/strong> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A simple average would approach zero for waveforms that fluctuate between positive and negative values. Therefore, RMS is used to express the <em>effective amplitude<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to calculate RMS:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Square each value.<\/li>\n\n\n\n<li>Take the mean of the squared values.<\/li>\n\n\n\n<li>Take the square root of that mean.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Understanding RMS Through Programming (openFrameworks)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In openFrameworks, RMS can be calculated using real-time audio input (e.g., microphone). The key components used are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ofSoundStream<\/code><\/li>\n\n\n\n<li><code>ofSoundBuffer<\/code><\/li>\n\n\n\n<li><code>audioIn()<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating RMS from Real-Time Audio Input<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When computing RMS from a microphone or other audio input, use the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ofSoundStream<\/strong><br>Manages audio I\/O. Set sampling rate, buffer size, input channels, etc.<\/li>\n\n\n\n<li><strong>ofSoundBuffer<\/strong><br>Stores and manages audio data in a buffer. Used here to compute RMS from collected data.<\/li>\n\n\n\n<li><strong>audioIn(ofSoundBuffer &amp; input)<\/strong><br>A callback function that receives incoming audio in real time. It&#8217;s a virtual function defined in <strong>ofBaseApp<\/strong>, where the <strong>ofSoundBuffer<\/strong> passed as an argument is analyzed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u25b8 Processing Flow<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Set up input via <strong>ofSoundStream<\/strong> (e.g., buffer size, number of channels).<\/li>\n\n\n\n<li>Collect audio samples into <strong>ofSoundBuffer<\/strong>.<\/li>\n\n\n\n<li>Use <strong>audioIn()<\/strong> to compute RMS using the buffer.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Code Example 1: Calculating RMS in <code>audioIn()<\/code><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u25b8 Code Explanation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Initialization<\/strong><\/p>\n\n\n\n<pre><code class=\"language-cpp\">\nfloat sum = 0.0;\nint numCounted = 0;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sum<\/strong>: Initialized to accumulate squared sample values.<\/li>\n\n\n\n<li><strong>numCounted<\/strong>: Initialized to count processed samples.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. <strong>Processing All Samples<\/strong><\/strong><\/p>\n\n\n\n<pre><code class=\"language-cpp\">\nfor (size_t i = 0; i < input.getNumFrames(); i++) {\n    float sample = input[i];\n    sum += sample * sample;\n    numCounted++;\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loops through all samples in the input buffer.<\/li>\n\n\n\n<li><strong>input.getNumFrames()<\/strong> gets the number of frames in the current audio input.<\/li>\n\n\n\n<li>Each sample is squared and added to <strong>sum<\/strong>.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Note: In this context, \"frames\" typically refer to individual samples. The number may not exactly match the buffer size set in <strong>ofSoundStream<\/strong>, so it\u2019s important to use <strong>getNumFrames()<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. <strong><strong>Compute RMS<\/strong><\/strong><\/strong><\/p>\n<\/blockquote>\n\n\n\n<pre><code class=\"language-cpp\">\nsum = sqrt(sum \/ (float)numCounted);\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Divides the total squared sum by the number of samples and takes the square root.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. <strong><strong><strong>Smoothing the RMS Value<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre><code class=\"language-cpp\">\nsmoothedRms *= 0.93;\nsmoothedRms += 0.07 * sum;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Applies exponential smoothing.<\/li>\n\n\n\n<li><strong>smoothedRms<\/strong> retains 93% of its previous value and adds 7% of the current RMS.<\/li>\n\n\n\n<li>This reduces abrupt volume changes and produces a smoother transition.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is a type of <strong>Exponential Moving Average (EMA)<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>smoothedRms<\/strong> is the previously smoothed RMS value.<\/li>\n\n\n\n<li><strong>sum<\/strong> is the current RMS.<\/li>\n\n\n\n<li>Together, they create a smooth-following value.<\/li>\n\n\n\n<li><strong>smoothedRms *= 0.93;<\/strong> \u2192 retains 93% of the past value<\/li>\n\n\n\n<li><strong>smoothedRms += 0.07 * sum;<\/strong> \u2192 adds 7% of the new value<\/li>\n\n\n\n<li>The total is 100% (0.93 + 0.07 = 1.0)<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">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.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Code Example 2: Calculating RMS for Multiple Channels in audioIn()<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u25b8 Explanation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">For stereo or multi-channel input, use <strong>getNumChannels() <\/strong>to loop through each channel:<\/p>\n\n\n\n<pre><code class=\"language-cpp\">\nfloat sample = input[i * numChannels + ch];\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ofSoundBuffer<\/strong> stores audio data in <strong>interleaved format<\/strong>:<br><strong>[Left] [Right] [Left] [Right] ...<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Indexing with<strong> i * numChannels + ch<\/strong> accesses each channel properly:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Frame 0 \u2192 index 0, 1<\/li>\n\n\n\n<li>Frame 1 \u2192 index 2, 3<\/li>\n\n\n\n<li>etc.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Visualizing the RMS Value<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use <strong>smoothedRms<\/strong> inside the <strong>draw()<\/strong> function to visualize the audio strength in real time. For example, you can draw a circle whose size changes with RMS:<\/p>\n\n\n\n<pre><code class=\"language-cpp\">\nofDrawCircle(x, y, smoothedRms * scaleFactor);\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"576\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/Root-Mean-Square-01.webp\" alt=\"\" class=\"wp-image-764 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/Root-Mean-Square-01.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/Root-Mean-Square-01-300x169.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2025\/06\/Root-Mean-Square-01-768x432.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/576;\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Visualization Methods Using RMS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Beyond displaying RMS as a raw number, you can visualize it in various ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Volume bars<\/li>\n\n\n\n<li>Line graphs over time<\/li>\n\n\n\n<li>Overlays on heatmaps or waveforms<\/li>\n\n\n\n<li>Interactive particle systems<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By extending RMS analysis, you can explore:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stereo or surround channel comparisons<\/li>\n\n\n\n<li>Envelope curves derived from time-varying RMS<\/li>\n\n\n\n<li>Combinations with peak levels or spectrum analysis<\/li>\n\n\n\n<li>Trigger detection and automatic environment evaluation<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Combining openFrameworks with tools like Max\/MSP or Pure Data enables even more complex audio interactions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Summary: RMS (Root Mean Square)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combining RMS with other acoustic metrics such as peak value, loudness, or FFT can lead to richer understanding and expressive visualizations of sound.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Reference: <a class=\"\" href=\"https:\/\/openframeworks.cc\">openFrameworks Official Website<\/a><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"MinkowskiDistance-05\"><em>Recommended Book \/ Generative art for beginner<\/em><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"576\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/09\/GenerativeArtProcessing.webp\" alt=\"\" class=\"wp-image-327 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/09\/GenerativeArtProcessing.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/09\/GenerativeArtProcessing-300x169.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2023\/09\/GenerativeArtProcessing-768x432.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/576;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/amzn.to\/3rBeaOZ\" target=\"_blank\" rel=\"noreferrer noopener\">Generative Art: A Practical Guide Using Processing<\/a><a href=\"https:\/\/amzn.to\/45Xzptl\" target=\"_blank\" rel=\"noreferrer noopener\">&nbsp;<\/a>\u2013 Matt Pearson<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generative Art presents both the techniques and the beauty of algorithmic art. In it, you\u2019ll 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\u2019s images, and it offers countless suggestions for how you can combine and reuse the various techniques to create your own works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<br>\u2014\u2013<br>\u25ba&nbsp;<strong><a href=\"https:\/\/amzn.to\/3rBeaOZ\" target=\"_blank\" rel=\"noreferrer noopener\">Generative Art: A Practical Guide Using Processing<\/a><\/strong>&nbsp;\u2013 Matt Pearson<br>Publication date: 2011. July<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83c\udf10 <strong>Support my Website<\/strong><br>By using our affiliate links, you\u2019re helping my content and allows me to keep creating valuable articles. I appreciate it so much:)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"BGD_SOUNDS\"><em>BGD_SOUNDS (barbe_generative_diary SOUNDS)<\/em><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">barbe_generative_diary SOUNDS will start sharing and selling a variety of field recordings collected for use in my artwork \u201cSound Visualization\u201d experiments. All sounds are royalty-free.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/barbegenerativediary.bandcamp.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Link \/ BGD_SOUNDS on bandcamp<\/strong><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"731\" data-src=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS.webp\" alt=\"\" class=\"wp-image-554 lazyload\" data-srcset=\"https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS.webp 1024w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS-300x214.webp 300w, https:\/\/barbegenerativediary.com\/en\/wp-content\/uploads\/2024\/07\/240801_bandcamp-top-BGD_SOUNDS-768x548.webp 768w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/731;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In sound visualization and acoustic analysis, RMS (Root Mean Square) is a highly important metric. Unlike instantaneous peak values, RMS takes the average of amplitudes over a certain time window (buffer size), which enables a more natural and perceptual representation of dynamics, loudness, and visualizations of audio signals.<\/p>\n","protected":false},"author":1,"featured_media":765,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,9,13],"tags":[],"class_list":["post-762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-openframeworks-tutorials","category-sounds","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/comments?post=762"}],"version-history":[{"count":13,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":778,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/posts\/762\/revisions\/778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/media\/765"}],"wp:attachment":[{"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/media?parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/categories?post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barbegenerativediary.com\/en\/wp-json\/wp\/v2\/tags?post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}