<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: W Lab</title>
    <description>The latest articles on DEV Community by W Lab (wlab).</description>
    <link>https://dev.to/wlab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F16073%2Faf544ebe-7f3d-4f5d-ada3-beee3be22835.png</url>
      <title>DEV Community: W Lab</title>
      <link>https://dev.to/wlab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wlab"/>
    <language>en</language>
    <item>
      <title>Why red ink vanishes under a red sheet, and how I made an iPhone do it</title>
      <dc:creator>Johnny Nguyen</dc:creator>
      <pubDate>Sat, 14 Nov 2026 08:07:32 +0000</pubDate>
      <link>https://dev.to/wlab/why-red-ink-vanishes-under-a-red-sheet-and-how-i-made-an-iphone-do-it-2haa</link>
      <guid>https://dev.to/wlab/why-red-ink-vanishes-under-a-red-sheet-and-how-i-made-an-iphone-do-it-2haa</guid>
      <description>&lt;p&gt;I work on Red Sheet at W Lab. Students in Japan write answers in red or orange pen, lay a sheet of red plastic over the page, and the answers disappear while the black questions stay. Slide the sheet down and you check yourself one line at a time. Red Sheet is an iPhone and iPad app that does this to a photo of your own notebook. This post is about the engine that finds the ink and hides it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the paper trick works, and why a filter isn't enough
&lt;/h2&gt;

&lt;p&gt;Red plastic passes red light and blocks the rest. Through it, white paper looks red and red ink looks red, so the ink has nothing to stand out against. Black ink reflects no red either way, so it stays dark. Green highlighter is the opposite case: it reflects almost no red, so through the sheet it turns nearly black and blocks the word under it.&lt;/p&gt;

&lt;p&gt;The obvious way to copy that on a phone is to take the photo's red channel and tint it. I kept that version in the code as a comparison, and it fails on real photos. A phone photo of a notebook has a shadow across it, warm indoor light, greyish recycled paper. Red ink on a shadowed page is not the same value as the paper next to it, so it shows through as a faint ghost. A plastic sheet gets away with this because your eye adapts. An image doesn't.&lt;/p&gt;

&lt;p&gt;So the app does not filter. It works out which pixels are answer ink, and guarantees those are gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: make the paper white
&lt;/h2&gt;

&lt;p&gt;Before any colour decision, the lighting has to come out. The engine estimates the paper level at every point of the page: a morphological closing (a max filter, then a min filter) at quarter scale wipes out the writing and leaves the paper, and a box blur smooths it. Dividing each colour channel by that map sends paper to white under any light, and because the three channels are divided separately, yellowish paper comes out white too.&lt;/p&gt;

&lt;p&gt;Pictures in a textbook would wreck that estimate, so coloured figures are found first, as blocks of cells that are saturated and span several hues, and filled in from their borders. Dark photos and panels count as figures too. That rule came from a bug: flattening turned a near-black panel into something that looked like marker.&lt;/p&gt;

&lt;p&gt;A PDF page skips this step. It has no shadow to undo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: decide what is ink, in Lab
&lt;/h2&gt;

&lt;p&gt;Colour decisions are made in CIELAB, where distances roughly match what people see. Converting every pixel is slow, so the conversion is a lookup table with 64 levels per channel, built once.&lt;/p&gt;

&lt;p&gt;A pixel is ink if it is far enough from white. Ink with little chroma is black if it is dark, and paper if it is light, which is how the grey anti-aliased edge of a black letter is kept out. The rest is coloured ink.&lt;/p&gt;

&lt;p&gt;Fixed hue thresholds don't survive real pens, so the coloured pixels of each page are clustered with k-means, six clusters with a fixed seed so the result is repeatable. The page tells the engine what its own red looks like. Pale pixels on the rim of a stroke take the label of the saturated pixel beside them, so an orange stroke's faint edge belongs to the stroke and not to some pale marker of a similar hue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: pen or highlighter is a question of shape
&lt;/h2&gt;

&lt;p&gt;Hue alone can't separate a pink pen from a pink highlighter. Shape can. A highlighter is a solid band: its bounding box, counting the black text inside it, is mostly filled and about as thick as a character. A pen stroke is thin and its box is mostly empty.&lt;/p&gt;

&lt;p&gt;That gives the classes the sheets care about. Red, orange and pink pen vanish under the red sheet. Green highlighter goes dark under it and blocks the word. Under the green sheet it is pink highlighter that blocks, and under the blue sheet a blue pen vanishes. A thin green stroke or a wide blue one is neither, and is never hidden.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: from pixels to answers you can tap
&lt;/h2&gt;

&lt;p&gt;Hidden ink is not yet an answer. A handwritten answer is several strokes and often several words, and the app needs one box per answer to reveal on a tap.&lt;/p&gt;

&lt;p&gt;The ink mask is dilated a little more along the writing direction than across it, and the blobs are labelled with a two-pass union-find. Then come the rules that make a box match what a person would call one answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A blob taller than a line and a half is cut into lines at the valleys of its ink profile.&lt;/li&gt;
&lt;li&gt;Pieces on the same line, close together, with no black ink between them are one answer. The "no black ink between" part stops two answers in neighbouring table cells from merging.&lt;/li&gt;
&lt;li&gt;Specks are dropped. Long thin strokes are underlines: they get hidden, but they are not answers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Writing direction is measured, not assumed, because Japanese notebooks are often vertical. If tall blobs clearly outweigh wide ones, the page is vertical, and the dilation and the sliding band turn with it.&lt;/p&gt;

&lt;p&gt;Each answer gets a key from its ink class and its position on a coarse grid. When detection runs again, answers are matched by key, so anything the user changed by hand stays attached to the right one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: the guarantee
&lt;/h2&gt;

&lt;p&gt;Rendering is where "mostly hidden" becomes "hidden". For ink that should vanish, the mask is dilated by two pixels at full strength and one more at half strength, and those pixels are replaced with an estimate of the paper under them: the mean colour of nearby non-ink pixels, with the paper's own grain added back so the patch doesn't look painted. Only then is the sheet's tint applied. Black text and figures are never touched.&lt;/p&gt;

&lt;p&gt;The sheet is computed once per page as a whole image. Moving the band only changes a clip rectangle, so dragging stays smooth. Revealing an answer draws the original photo's pixels back inside its box. Until the first render is ready, the page shows blurred under the sheet colour, so an answer is never on screen before it is hidden.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping it fast and testable
&lt;/h2&gt;

&lt;p&gt;The engine is a pure Swift package with plain loops over a pixel buffer, on a working copy whose long edge is 1200 pixels. The min and max filters are the van Herk / Gil-Werman kind, constant time per pixel whatever the radius. Analysis runs off the main thread, a few pages at a time, since each one holds tens of megabytes of per-pixel planes.&lt;/p&gt;

&lt;p&gt;I wrote the engine first as a JavaScript mockup in a browser, where tuning a threshold and seeing the result takes a second, then ported it to Swift function by function. The Swift tests run twenty fixture pages, four kinds of page under five lighting conditions including shadow, warm light and recycled paper, and must match the mockup's output and find the same number of answers.&lt;/p&gt;

&lt;p&gt;All of this runs on the device. The app's own code makes no network requests, and the camera permission text says what is true: images are never sent to us.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it still gets wrong
&lt;/h2&gt;

&lt;p&gt;A heading printed in red is hidden just like red pen. A real red sheet hides it too, so I left that alone and gave the user an eraser: tap a wrong hit and it shows for good, even after the page is detected again. For the opposite case, a word printed in black, the user drags a box or runs a marker over it and it hides under every sheet.&lt;/p&gt;

&lt;p&gt;Red Sheet is on the App Store for iPhone and iPad as "Red Sheet - Photo &amp;amp; PDF Study": &lt;a href="https://w-lab.app/apps/redsheet/" rel="noopener noreferrer"&gt;https://w-lab.app/apps/redsheet/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>imageprocessing</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Building an offline audio editor in Flutter: what the platforms won't encode for you</title>
      <dc:creator>Johnny Nguyen</dc:creator>
      <pubDate>Sat, 14 Nov 2026 07:58:06 +0000</pubDate>
      <link>https://dev.to/wlab/building-an-offline-audio-editor-in-flutter-what-the-platforms-wont-encode-for-you-30fc</link>
      <guid>https://dev.to/wlab/building-an-offline-audio-editor-in-flutter-what-the-platforms-wont-encode-for-you-30fc</guid>
      <description>&lt;p&gt;I work on AuCut at W Lab. It is an audio editor for iPhone and Android: trim a recording, pull the audio out of a video, convert between MP3, M4A and WAV, change the volume or the speed. Everything is processed on the phone and no file is uploaded. That one rule shaped most of the architecture, and this post is about where it got awkward.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Flutter UI, two native engines, and FFmpeg
&lt;/h2&gt;

&lt;p&gt;The UI is Flutter with Riverpod: one controller and one screen per tool under &lt;code&gt;lib/features&lt;/code&gt;, shared services under &lt;code&gt;lib/core&lt;/code&gt;. Dart doesn't touch audio samples. It talks to native code over one &lt;code&gt;MethodChannel&lt;/code&gt; for commands (trim, extract, convert, get duration, cancel) and one &lt;code&gt;EventChannel&lt;/code&gt; for progress.&lt;/p&gt;

&lt;p&gt;Behind that channel are two different engines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On iOS, AVFoundation, mostly &lt;code&gt;AVAssetExportSession&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On Android, Media3 &lt;code&gt;Transformer&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And beside both, FFmpeg, through the LGPL audio-only build of FFmpegKit. The platform APIs alone turned out not to be enough, for the reasons below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Neither platform will write an MP3
&lt;/h2&gt;

&lt;p&gt;Both phones play MP3 all day. Neither ships an MP3 encoder you can call. AVFoundation has none, and neither does Android's stock codec set. So "save as MP3" is a two-step job: the native side exports M4A, then FFmpeg re-encodes it with libmp3lame. The progress bar is weighted so the two halves look like one job.&lt;/p&gt;

&lt;p&gt;WAV is the opposite surprise. It is the simplest format there is, and neither export API will produce it. &lt;code&gt;AVAssetExportSession&lt;/code&gt; has no WAV preset, and &lt;code&gt;Transformer&lt;/code&gt; can't write one. Both are done by hand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;iOS: read with &lt;code&gt;AVAudioFile&lt;/code&gt;, convert with &lt;code&gt;AVAudioConverter&lt;/code&gt;, write 16-bit PCM.&lt;/li&gt;
&lt;li&gt;Android: decode to raw PCM with &lt;code&gt;MediaExtractor&lt;/code&gt; and &lt;code&gt;MediaCodec&lt;/code&gt;, then put a 44-byte WAV header in front of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rule I ended up with: the platform does what it does well (cutting, pulling a track out of a video, AAC), and FFmpeg does what the platform can't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trimming without re-encoding when possible
&lt;/h2&gt;

&lt;p&gt;A trim is a time range on an export. On iOS that is a &lt;code&gt;CMTimeRange&lt;/code&gt; on the export session. The session uses the passthrough preset when the source codec can be copied straight into an M4A container, which is fast and loses nothing, and falls back to an AAC re-encode when it can't. On Android it is a clipping configuration on the media item.&lt;/p&gt;

&lt;p&gt;If the user also asked for MP3 output, a volume change or a fade, a second FFmpeg pass follows the native cut.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cutting by ear, to a tenth of a second
&lt;/h2&gt;

&lt;p&gt;A waveform is too coarse to cut speech by eye. You can't see where a word ends. So the editor is built around listening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dragging a handle on the waveform is continuous.&lt;/li&gt;
&lt;li&gt;Two arrow buttons under each handle move it 0.1 s per tap. Hold one and it repeats, then speeds up to 1 s steps.&lt;/li&gt;
&lt;li&gt;A jog bar under each handle gives fine control over ±5 seconds from where you touch it.&lt;/li&gt;
&lt;li&gt;Every time a cut point moves, the app plays two seconds around it: the first two seconds of what will be kept after a start point, the last two before an end point. You hear exactly what the cut will sound like.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The waveform is 100 bars of RMS, extracted natively by the audio_waveforms plugin. I paint the bars myself with a &lt;code&gt;CustomPainter&lt;/code&gt; behind a &lt;code&gt;RepaintBoundary&lt;/code&gt;, because the plugin's own widget repainted every bar on each drag frame. Seeks while scrubbing are throttled to one per 60 ms. A seek is async, and queueing one per pointer event just piles them up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speed without pitch, pitch without speed
&lt;/h2&gt;

&lt;p&gt;Both are FFmpeg filters. &lt;code&gt;atempo&lt;/code&gt; changes tempo and keeps pitch. &lt;code&gt;asetrate&lt;/code&gt; changes both together. So a pitch shift is &lt;code&gt;asetrate&lt;/code&gt; by the pitch ratio, then an &lt;code&gt;atempo&lt;/code&gt; that cancels the tempo change it caused:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="s1"&gt;'aresample=$rate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'asetrate=${(rate * k).round()}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'aresample=$rate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_atempoChain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// k = 2^(semitones / 12)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Older FFmpeg builds only accept 0.5 to 2.0 per &lt;code&gt;atempo&lt;/code&gt; instance, so larger changes are chained.&lt;/p&gt;

&lt;p&gt;Normalising volume uses FFmpeg's &lt;code&gt;loudnorm&lt;/code&gt;, which measures loudness the EBU R128 way, not by peak. One trap: &lt;code&gt;loudnorm&lt;/code&gt; upsamples to 192 kHz internally, so the filter chain has to resample back down or the output file balloons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Files that arrive from other apps
&lt;/h2&gt;

&lt;p&gt;Much of the real use starts in another app: a voice memo, a video in Photos, an audio message from a chat. On iOS the app declares itself a viewer for audio and movie types. On Android it has intent filters for send and view. Either way the native side copies the file into a temp directory, queues its path, and Dart drains the queue and asks which tool to open it in.&lt;/p&gt;

&lt;p&gt;Two details mattered there. On a cold launch, the file can arrive before Flutter is listening, so the Dart side drains the queue once at start-up and the iOS side sweeps its inbox when asked. And chat apps hand over OPUS, OGG and FLAC, which the native trim path won't take, so anything that isn't M4A, MP3 or WAV is re-encoded to M4A by FFmpeg on the way in.&lt;/p&gt;

&lt;p&gt;Temp files carry a timestamp in their name and are cleaned up after 24 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small things that cost real time
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Reading a file's duration does file I/O and can stall on a large or cold file, so it runs off the main thread on both platforms. An unreadable asset reports a NaN duration, and converting that to an &lt;code&gt;Int&lt;/code&gt; in Swift traps.&lt;/li&gt;
&lt;li&gt;Android's &lt;code&gt;EventSink&lt;/code&gt; may only be called on the UI thread. A decode loop on a background thread has to post its progress.&lt;/li&gt;
&lt;li&gt;A stray back swipe used to throw away a long export without a word. Now a guard asks first, and leaving a tool cancels its job. Riverpod providers outlive their screen, so without that a job kept running where nobody could see it.&lt;/li&gt;
&lt;li&gt;The strings are in eight languages, and a test fails if any language is missing a key or has an extra one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd do the same way again
&lt;/h2&gt;

&lt;p&gt;Keep one narrow channel between Dart and native, with every operation reporting progress and accepting a cancel. Let each platform's own exporter do the cutting. And accept early that an audio app needs FFmpeg for the formats and filters the platforms leave out.&lt;/p&gt;

&lt;p&gt;AuCut is on the App Store as "AuCut: Audio Cutter &amp;amp; Editor" and on Google Play as "adTrim: Offline Audio Tools": &lt;a href="https://w-lab.app/apps/aucut/" rel="noopener noreferrer"&gt;https://w-lab.app/apps/aucut/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>audio</category>
      <category>showdev</category>
    </item>
    <item>
      <title>What it takes to turn an iPhone microphone into a decibel meter</title>
      <dc:creator>Johnny Nguyen</dc:creator>
      <pubDate>Fri, 13 Nov 2026 03:41:17 +0000</pubDate>
      <link>https://dev.to/wlab/what-it-takes-to-turn-an-iphone-microphone-into-a-decibel-meter-1j4p</link>
      <guid>https://dev.to/wlab/what-it-takes-to-turn-an-iphone-microphone-into-a-decibel-meter-1j4p</guid>
      <description>&lt;p&gt;I work on Noise Meter at W Lab. It is an iPhone app that shows the sound level in decibels and logs a whole night of it, so someone with a noisy neighbour can show when it was loud. This post is about the measuring part: what happens between the microphone and the number on screen, and the places where I had to be honest about what a phone can't do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting clean samples
&lt;/h2&gt;

&lt;p&gt;iOS processes microphone input by default: automatic gain, noise suppression, EQ. All of that is exactly what a meter must not have, because a quiet room would be boosted and a loud one pulled down. The audio session mode that turns it off is &lt;code&gt;.measurement&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;measurement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPreferredSampleRate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;48_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From there it is an &lt;code&gt;AVAudioEngine&lt;/code&gt; input tap with a 4800-frame buffer, which is 100 ms at 48 kHz. I ask for 48 kHz but build the analyzer for whatever rate the hardware returns. I also select the built-in microphone's bottom data source with an omnidirectional pattern, and re-select it on every route change, because a route change can move the input.&lt;/p&gt;

&lt;p&gt;One Swift 6 trap is worth knowing about. A closure created inside a &lt;code&gt;@MainActor&lt;/code&gt; method inherits that isolation, and the audio thread calling it is a runtime trap. The tap block has to be built in a nonisolated context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Weighting: A, C and Z
&lt;/h2&gt;

&lt;p&gt;A raw level over-reports low rumble compared with what people hear, so sound level meters apply a frequency weighting. A-weighting follows the ear, C-weighting keeps much more bass, and Z is flat.&lt;/p&gt;

&lt;p&gt;The standard, IEC 61672-1, defines these curves by their analog poles (20.6 Hz, 107.7 Hz, 737.9 Hz and 12194 Hz). I bilinear-transform those poles at runtime for the actual sample rate into up to three biquad sections, and normalise the gain to 0 dB at 1 kHz. A-weighting is a high-pass, a middle section and a low-pass; C drops the middle section; Z is a passthrough. The filter runs per sample in plain Swift &lt;code&gt;Double&lt;/code&gt;s, with no FFT.&lt;/p&gt;

&lt;p&gt;The bilinear transform warps the top of the spectrum, so I wrote down how far off it is: at 48 kHz it stays inside the standard's class 2 tolerance from 31.5 Hz to 8 kHz, and is 2.6 dB low at 12.5 kHz. The tests feed sines through the filters at 48 and 44.1 kHz and compare the result with the standard's design values.&lt;/p&gt;

&lt;h2&gt;
  
  
  From samples to a level
&lt;/h2&gt;

&lt;p&gt;Each weighted sample is squared. Everything after that is an average of those squares:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number on screen is an exponential average with a time constant of 125 ms (Fast) or 1 s (Slow), the two responses a hardware meter offers.&lt;/li&gt;
&lt;li&gt;Every second, the plain mean of that second's squares becomes one stored row: the second's Leq, plus the highest and lowest Fast/Slow level inside it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Leq, the equivalent continuous level, is the number noise standards are written in. It is an average of energy, not of decibels, so the Leq of a recording is &lt;code&gt;10·log10&lt;/code&gt; of the mean of the per-second energies. A test pins this down: one second at 60 dB and one at 70 dB give 67.4 dB, not 65.&lt;/p&gt;

&lt;p&gt;That distinction matters for the app's main use. Footsteps upstairs are short and loud. They barely move a night's Leq, but they show up in the per-second maximum, which is why the app keeps both.&lt;/p&gt;

&lt;p&gt;The first half second after starting is thrown away. The filter needs to settle, and without that the start-up ramp would be recorded as the minimum of every session.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hard part: dBFS is not dB
&lt;/h2&gt;

&lt;p&gt;Everything above produces dBFS, a level relative to the largest number the converter can represent. A full-scale sine reads −3.01. A sound level meter shows dB SPL, relative to 20 micropascals. The two differ by a constant that depends on the microphone and the hardware around it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;level&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;dBFS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dBFS&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// total = model + user&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That constant is the whole accuracy story. The app ships with a default offset measured on one iPhone, in a table keyed by the model identifier from &lt;code&gt;uname&lt;/code&gt;, and I do not claim a margin of error for it. What it offers instead is calibration: put the phone next to a real sound level meter set to A-weighting, play a steady noise, type in the meter's reading, and the app stores the difference as a user offset in 0.1 dB steps. Every recording keeps the offset and calibration state it was measured with.&lt;/p&gt;

&lt;p&gt;Two quick checks come before that, for people without a meter: a quiet-room check (does the level drop when the room is quiet?) and a response check (does the reading move at least 15 dB when you turn the TV up and down?). They can't make the phone accurate. They catch a blocked or broken microphone, which is the more common failure.&lt;/p&gt;

&lt;p&gt;A calibration offset also can't fix physics. The microphone has a self-noise floor, so a very quiet room never reads as quiet as it is, and a loud enough sound clips the converter. No offset corrects either, and the app tells people its readings are a reference, not a certified measurement.&lt;/p&gt;

&lt;h2&gt;
  
  
  A whole night without recording audio
&lt;/h2&gt;

&lt;p&gt;The app never writes audio to disk. People put this next to their bed, and a level meter has no reason to keep what was said in the room. What is stored per second is three numbers, Leq, max and min, as 16-bit hundredths of a decibel. That is 6 bytes a second, so twelve hours is about 259 KB.&lt;/p&gt;

&lt;p&gt;Rows are flushed to storage every ten seconds, so a crash or a dead battery loses almost nothing, and a session left open by a kill is recovered at the next launch.&lt;/p&gt;

&lt;p&gt;Phone calls interrupt the audio session. The recording pauses, and the paused seconds are stored as empty slots, not skipped, so every later second keeps its true clock time. That matters when the point of the recording is to say "it was loud at 2:14".&lt;/p&gt;

&lt;h2&gt;
  
  
  Making the number mean something
&lt;/h2&gt;

&lt;p&gt;A bare "52 dB" tells most people nothing. Two things on screen give it meaning.&lt;/p&gt;

&lt;p&gt;Under every reading, the app names a place about as loud: a library, a subway car, a coffee shop. The fifteen examples and their levels come from a noise level guide published on the website of Japan's Ministry of the Environment, and the app picks the nearest one.&lt;/p&gt;

&lt;p&gt;The chart also draws Japan's environmental noise standards for the chosen area type, for example 55 dB by day and 45 dB at night in residential areas. Those standards are assessed outdoors over the whole day or night, so the app labels the comparison as a reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell someone starting the same thing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;.measurement&lt;/code&gt; mode and check which microphone you actually got.&lt;/li&gt;
&lt;li&gt;Implement the weighting from the standard's poles, then test it against the standard's table. It is less code than it sounds.&lt;/li&gt;
&lt;li&gt;Average energy, never decibels.&lt;/li&gt;
&lt;li&gt;Be plain about calibration. An uncalibrated phone gives a useful relative reading and an unknown absolute one, and users deserve to know which is which.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Noise Meter is on the App Store for iPhone as "Noise Meter - Decibel Level": &lt;a href="https://w-lab.app/apps/noise-meter/" rel="noopener noreferrer"&gt;https://w-lab.app/apps/noise-meter/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>audio</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
