xander
← back

Starting Gun Detector

Apr to May 2026
github ↗

An Android app that hears the starting gun and timestamps it to the millisecond, shares detections across phones in real time, and feeds them to a FinishLynx timing system through a Windows companion app. Six releases in four weeks, built for Happy Pace Timing.

  • Android
  • Kotlin
  • Audio
  • Firebase
  • C#
  • Race Timing

An Android app that listens for the starting gun and records the moment it fires, to the millisecond. Phones at the start and the finish join a shared session, so every detection shows up on every device as it happens, and a Windows companion app forwards those start times into FinishLynx, the photo-finish system the races are timed on. I built it for Happy Pace Timing, a family-owned race timing company, and shipped six versions in four weeks as the crew used it and told me what broke.

The app listening at sensitivity 10, with two wooden blocks struck together standing in for the gun, and wind and people talking in the background. Listen for the mic switching to its raw source when listening starts. Each detection turns its meter bar red and lands in the history.
This is a work in progress. The app is in use at races; the tightened server security rules are written and ship with v1.6, and go live once every phone in use has updated.
App
Kotlin and Jetpack Compose, Android 8 and up, one activity with a foreground service so it keeps listening with the screen off.
Audio
AudioRecord at 44.1 kHz mono, 16-bit, from the raw unprocessed mic source.
Sync
Firebase Firestore for shared sessions, anonymous Firebase Auth for device identity.
Desktop
A .NET 8 WinForms app that talks to FinishLynx over its TCP remote port and reads its .lif result files.

The first field test failed

The first version worked at my desk and did nothing outdoors. Recording the test audio and opening it in a DAW showed why: the gunshot and the wind gusts had nearly the same envelope. Android's automatic gain control was squashing the shot down to the level of the wind before my code ever saw it. I had picked the voice-recognition mic source thinking it skipped that processing; it doesn't.

Two changes fixed it, shipped the same day as v1.1:

  • Use the unprocessed mic source, which asks the phone for raw samples with no gain control or noise suppression. The shot is far louder than wind in raw terms, and the raw source keeps that difference.
  • Confirm that the spike is short. A gunshot drops back toward the background level within a few milliseconds; a gust stays loud. On a candidate trigger the detector reads the next three buffers and only accepts it if the level falls back. The timestamp is taken at the spike, so the check adds no delay to the reported time.

How a detection works

The detector computes the RMS level of each audio buffer and keeps a rolling average of the last second as the background. It triggers when a buffer is several times louder than that background (the sensitivity slider sets how many times, from 4 to 20) and also above a fixed floor, so a tiny sound in a silent room can't trigger it. It then finds the loudest sample inside the buffer and works back from there to the exact millisecond, rather than stamping the whole buffer with one time. A 2.5 s cooldown stops echoes from triggering it twice.

That in-buffer offset had a bug of its own: I measured it from the start of the buffer instead of the end, so every time came out early by up to one buffer, about 40 ms. Fixing that was v1.2.

Getting two phones to agree on the time

A start time is only useful against a finish time, and those come from different devices. v1.3 added a finish-line camera to the app: record the finish, scrub to the frame where a runner crosses, and the app works out the split. Each phone's own clock can be tens of milliseconds off from the other's, which goes straight into every split.

So neither phone uses its own clock. Both convert their times to the Firestore server's clock, measuring their offset from it with five round trips and keeping the one with the shortest delay. A false start is handled the same way: every detection is saved, and the official start is picked after the race, so nobody has to tap anything with the gun in the air.

Before shipping that, I wrote a timing audit that traced the full chain from the gun firing to the split on screen and listed every source of error. Two were large. The gun time was being taken when the server received the write, so network lag made every split short. The video's start time was taken before the camera actually started, so every split came out long. Fixing those and the smaller ones brought the estimated worst-case split error from about 550 ms down to about 130 ms.

Feeding FinishLynx

The phones give a start time; the race results live in FinishLynx. The detector-to-lynx Windows app closes that gap. It reads the session's detections from Firestore and sends them to FinishLynx over its TCP remote interface.

It also calibrates itself. Each phone has its own fixed input delay between the sound reaching the mic and the samples reaching the app. The desktop app watches the folder where FinishLynx writes its result files, reads each race's start time, and pairs it with the nearest detection within a 10 s window. The mean difference across the matched pairs becomes the correction, and the leftover per-race differences show whether that correction can be trusted. The matching and the file parsing are covered by unit tests.

Polling Firestore through a meet can hit its rate limits, so the app backs off when it's told to slow down, starting at one minute and doubling up to fifteen.

Built from what the crew asked for

Most of the six releases came from watching the app get used:

VersionWhat changed
1.0Detection, history, and shared sessions across phones
1.1Raw mic source and the short-spike check, after the outdoor test failed
1.2The 40 ms offset fix, a live loudness meter, a manual latency offset, and names on detections
1.3Finish-line video capture with server-clock splits, and the timing audit fixes
1.4Saved races (meet, event, race) with every candidate start time kept for picking later
1.5Listening with the screen locked, and latency calibration against a reference clock
1.6Device sign-in and server security rules, a readable session code, and a remembered sensitivity

Some of the smallest changes mattered most at the track. Session codes get read aloud across a stadium, so v1.6 builds them only from letters and digits that can't be confused by eye or by ear: no O and 0, no I, 1 and L, no B and 8, no S and 5, no Z and 2. The app now shows the code in the top bar in a monospace font with a slashed zero, instead of hiding it in a menu.

What's next

  • Deploy the security rules once every phone in use is on v1.6, since older versions can't write under them.
  • Presence by heartbeat. A phone that's swiped away or killed can still show as listening, because it never gets the chance to say it stopped. Each phone will write a timestamp every few seconds instead, and anything stale counts as offline.
  • Measure each phone's input delay automatically from Android's audio timestamp API, instead of calibrating against a reference.