<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Measuring dB from digital audio</title>
		<link>http://www.allegro.cc/forums/view/566475</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 17 Feb 2006 03:31:14 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I thought of making a plotter for dB monitoring. dB seems to be a very complex thing, even if there are some formulas for that. But in the end there are lots of things that can vary. So far I&#39;ve come up with this:</p><p>The plotter would constantly take sound samples from the microphone, like 500 ms of sound data.</p><p>For each chunk of 500 ms, it would add the square of each sample.</p><p>Take the logarithm of the sum. This value would be linearly relative to the dB value of the sound. Calibrate it using a real dB meter.</p><p>The used mic has to be calibrated, too. Even if the formula above would be right, the used mic would distort it, if not calibrated. But after testing and calibrating, I should have a rather useable dB plotter. It would continuously sample 500 ms, calculate the dB value, plot it, then sample the next 500 ms. Of course computing time would make the updating time higher than 500 ms, but that&#39;s no problem, as long as it plots something. Or does the sampling happen in some interrupt mode while the computer does the calculating?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Mon, 13 Feb 2006 14:14:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Im not sure it&#39;s as easy as to take the logarithm of the sum; the math looks something like: 20 log (P2/P1) where one of the P&#39;s is input power and the other is a reference power (1 × 10-12 W/m²).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Mon, 13 Feb 2006 15:34:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, in my model the sum of the squares of the samples would equal P2/P1. But I&#39;m not sure if it is like that. A microphone has a diaphragm that covers an area, which would be the m² part and the mic chord transfers an AC signal, which would be the W part. So in the end I just want to compute how many effect units a signal is, analysing only the wave form, that is amplitudes and frequencies and such. If it were a simple sine wave, the amplitude would be enough (or would it?). But when it is a very complex wave form, I thought summing the squares of the samples would be a measure of the effect of the wave.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Mon, 13 Feb 2006 16:39:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you want the power of the signal, you want RMS (Root Mean Square). Take the square of each sample, sum them together, divide by number of samples and square-root. Transform into dB through logarithms and add or subtract a calibration bias. There.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Tue, 14 Feb 2006 01:22:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Great! Thanks, Tobias. I thought it would be something like that. The root thing I wouldn&#39;t have come up with. I&#39;m still working on the sound recording thing. So far I have this code:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>...</td></tr><tr><td class="number">2</td><td>size <span class="k3">=</span> <a href="http://www.allegro.cc/manual/start_sound_input" target="_blank"><span class="a">start_sound_input</span></a><span class="k2">(</span><span class="n">44100</span>, <span class="n">16</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td>buffer <span class="k3">=</span> <span class="k1">new</span> <span class="k1">unsigned</span> <span class="k1">int</span><span class="k2">(</span>size<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td><a href="http://www.allegro.cc/manual/textprintf_ex" target="_blank"><span class="a">textprintf_ex</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <span class="n">0xffffff</span>, <span class="n">0</span>, <span class="s">"%d    %d"</span>, size, buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td><a href="http://www.allegro.cc/manual/alert" target="_blank"><span class="a">alert</span></a><span class="k2">(</span><span class="s">"Start monitoring the mic"</span>, <span class="s">""</span>, <span class="s">""</span>, <span class="s">"Ok"</span>, NULL, <span class="k3">-</span><span class="n">1</span>, <span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">do</span></td></tr><tr><td class="number">8</td><td><span class="k2">{</span></td></tr><tr><td class="number">9</td><td>    <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/read_sound_input" target="_blank"><span class="a">read_sound_input</span></a><span class="k2">(</span>buffer<span class="k2">)</span> <span class="k3">!</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">10</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>        <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>        p <span class="k3">=</span> buffer<span class="k2">;</span></td></tr><tr><td class="number">13</td><td>        <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">1024</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td>        <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>              <a href="http://www.allegro.cc/manual/putpixel" target="_blank"><span class="a">putpixel</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, i, <span class="n">384</span> <span class="k3">-</span> <span class="k2">(</span><span class="k3">*</span>p<span class="k2">)</span> <span class="k3">/</span> <span class="n">256</span>, <span class="n">0xffffff</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>              p<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>        <span class="k2">}</span></td></tr><tr><td class="number">18</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr><tr><td class="number">20</td><td><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

I guess I do something wrong, it crashes in the middle of the read_sound_input() call.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Tue, 14 Feb 2006 04:04:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you wanted to simulate the classic VU meters, you could do 300ms averaging while applying a 10dB lag. I think that would be rather neat.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Damian Grove)</author>
		<pubDate>Tue, 14 Feb 2006 13:16:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I just turned to PortAudio and it works better than Allegro&#39;s sound recording. Cool. Next week I&#39;ll present my dB meter and as an extra bonus a guitar tuning program. Or something.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Fri, 17 Feb 2006 03:31:14 +0000</pubDate>
	</item>
</rss>
