Audio Event Detection in Video with PANNs: Window Tags and Event Times
Run the PANNs CNN14 audio tagger on a video's soundtrack and you get, for each stretch of audio you give it, 527 independent scores saying which sound classes appear somewhere in that stretch. You do not get the moment a sound started or stopped. This article shows how the model works, what its scores mean, how to run it on a soundtrack, and how far a window label is from an event time.
One thing first. AI-Powered Video Analyzer, a video analyzer that runs locally, has an audio event stage built on PANNs, but in version 1.1.0 with panns-inference 0.1.1 that stage returns no sounds at all, as checked on 27 November 2026 with the real checkpoint. Every score below comes from calling panns-inference directly. None of it is analyzer output.
Transcription, tagging and detection
| Task | Question it answers | Output | Example model family |
|---|---|---|---|
| Speech transcription | What words were said? | Text, possibly with segment times | Whisper |
| Audio tagging | Which sounds are present in this clip? | Scores per class for the whole clip | PANNs CNN14 |
| Sound event detection | Which sounds happen, from when to when? | Scores per class over time | Frame-wise models, such as PANNs decision-level variants |
CNN14 used through AudioTagging does the middle task. It can say that a recording contains speech and music; it cannot say that the music began at 12.4 seconds.
What the analyzer's stage does today
The intended procedure is simple: load the extracted audio at 32 kHz mono, peak-normalise it, cut it into 5 second windows that do not overlap, keep only the single highest-scoring label of each window if it reaches 0.1, and record the window's start time.
The failure is in handling the library's return value. AudioTagging.inference() returns a pair of arrays, the class scores with shape (1, 527) and an embedding with shape (1, 2048). The analyzer converts the pair as a whole into one array, which raises an error. The run still exits successfully, and the report contains:
"audio_events": {"error": []}
The Markdown report prints this as an entry named error, the report's limitations list does not mention it, and the summary stage receives the word as if it were a sound. Without a checkpoint file the entry is model_not_found instead. Until this is fixed, pass --no-audio-events and tag the audio yourself as shown below. Even after a repair, the procedure above would give at most one label per 5 second window.
What CNN14 receives
The model takes a mono waveform at 32 kHz. The number of samples is sampling rate times duration, so a 5 second window is 32000 * 5 = 160,000 samples.
Level of the 9 second speech clip and its two analysis windows
Signal level over nine seconds as a line. It stays high with small dips while the synthetic voice speaks, drops to zero after about five and a half seconds and remains there. Brackets mark a five second first window and a four second second window.
level, values at or above the threshold: 22 of 36 (0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.25)
| window | covers | at or above threshold at |
|---|---|---|
| window 1 | 0 to 4.75 | 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75 |
| window 2 | 5 to 8.75 | 5, 5.25 |
| time (s) | level | at or above threshold |
|---|---|---|
| 0 | 0.056 | level |
| 0.25 | 0.275 | level |
| 0.5 | 0.241 | level |
| 0.75 | 0.334 | level |
| 1 | 0.262 | level |
| 1.25 | 0.302 | level |
| 1.5 | 0.237 | level |
| 1.75 | 0.237 | level |
| 2 | 0.217 | level |
| 2.25 | 0.101 | level |
| 2.5 | 0.203 | level |
| 2.75 | 0.145 | level |
| 3 | 0.231 | level |
| 3.25 | 0.202 | level |
| 3.5 | 0.138 | level |
| 3.75 | 0.259 | level |
| 4 | 0.303 | level |
| 4.25 | 0.299 | level |
| 4.5 | 0.148 | level |
| 4.75 | 0.186 | level |
| 5 | 0.201 | level |
| 5.25 | 0.155 | level |
| 5.5 | 0 | none |
| 5.75 | 0 | none |
| 6 | 0 | none |
| 6.25 | 0 | none |
| 6.5 | 0 | none |
| 6.75 | 0 | none |
| 7 | 0 | none |
| 7.25 | 0 | none |
| 7.5 | 0 | none |
| 7.75 | 0 | none |
| 8 | 0 | none |
| 8.25 | 0 | none |
| 8.5 | 0 | none |
| 8.75 | 0 | none |
Threshold 0.01. level is at or above it at 22 of 36 (0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.25).
From waveform to log-Mel spectrogram
The front end cuts the signal into overlapping frames, takes a Fourier transform of each, pools the power into Mel-spaced bands and converts to decibels:
X(m, k) = sum over n of x[n + m*H] * w[n] * exp(-2*pi*i*k*n / N)
L(m, b) = 10 * log10( max( sum over k of M_b(k) * |X(m, k)|^2 , 1e-10 ) )
x is the waveform, m the frame, H = 320 samples the step (10 ms), N = 1024 the window length (32 ms), w a Hann window, k the frequency bin and M_b the b-th of 64 Mel filters spanning 50 to 14,000 Hz. With the centred framing used by the implementation, a 5 second window becomes 501 frames by 64 bands. These settings are those of panns-inference 0.1.1; the paper describes the same sizes and frequency range with a Hamming window.
Log-Mel spectrogram of the speech test clip, CNN14 settings
A coarse spectrogram with 32 rows of Mel band pairs over nine seconds. Most bands carry energy while the synthetic voice speaks, up to about five and a half seconds, with only a faint constant level in the highest rows. From 5.75 seconds on, every cell sits at the floor value.
log-Mel, dB (cells below the threshold are dimmed)
-100 (dark)15 (light)
Left to right: time (s) 0 to 8.75. Bottom to top: lowest Mel band in row 0 to 62.
- selected cell
- lowest Mel band in row 0, time (s) 0
- log-Mel, dB there
- -28
- log-Mel, dB: cells counted
- 1152 of 1152
| row | 0 | 0.25 | 0.5 | 0.75 | 1 | 1.25 | 1.5 | 1.75 | 2 | 2.25 | 2.5 | 2.75 | 3 | 3.25 | 3.5 | 3.75 | 4 | 4.25 | 4.5 | 4.75 | 5 | 5.25 | 5.5 | 5.75 | 6 | 6.25 | 6.5 | 6.75 | 7 | 7.25 | 7.5 | 7.75 | 8 | 8.25 | 8.5 | 8.75 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 62 | -85 | -85 | -85 | -85 | -85 | -85 | -86 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -94 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 60 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -94 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 58 | -85 | -85 | -84 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -85 | -94 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 56 | -84 | -60 | -60 | -79 | -83 | -84 | -69 | -82 | -77 | -72 | -82 | -79 | -83 | -81 | -81 | -83 | -79 | -84 | -81 | -77 | -77 | -85 | -94 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 54 | -82 | -62 | -63 | -78 | -82 | -83 | -70 | -82 | -76 | -73 | -81 | -77 | -82 | -81 | -81 | -81 | -79 | -82 | -81 | -78 | -77 | -84 | -93 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 52 | -65 | -29 | -30 | -51 | -59 | -61 | -40 | -61 | -51 | -46 | -57 | -53 | -59 | -57 | -56 | -63 | -53 | -60 | -56 | -49 | -49 | -68 | -88 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 50 | -56 | -16 | -15 | -34 | -44 | -44 | -26 | -48 | -39 | -33 | -47 | -40 | -43 | -41 | -39 | -46 | -36 | -45 | -43 | -35 | -33 | -53 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 48 | -57 | -19 | -19 | -33 | -40 | -38 | -26 | -47 | -39 | -35 | -47 | -41 | -39 | -38 | -36 | -42 | -33 | -33 | -39 | -32 | -28 | -45 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 46 | -55 | -24 | -22 | -34 | -38 | -25 | -21 | -46 | -28 | -32 | -34 | -34 | -40 | -34 | -35 | -42 | -30 | -29 | -34 | -26 | -27 | -48 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 44 | -56 | -29 | -28 | -34 | -34 | -28 | -20 | -44 | -32 | -37 | -34 | -36 | -39 | -35 | -37 | -39 | -32 | -33 | -39 | -33 | -32 | -49 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 42 | -54 | -29 | -27 | -31 | -32 | -21 | -14 | -43 | -29 | -35 | -32 | -32 | -35 | -31 | -31 | -37 | -26 | -23 | -35 | -30 | -26 | -41 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 40 | -57 | -36 | -34 | -35 | -36 | -17 | -15 | -46 | -28 | -38 | -27 | -31 | -38 | -33 | -33 | -39 | -23 | -25 | -36 | -30 | -27 | -45 | -83 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 38 | -55 | -29 | -27 | -28 | -33 | -24 | -17 | -40 | -32 | -38 | -30 | -30 | -33 | -29 | -29 | -34 | -20 | -23 | -34 | -30 | -23 | -37 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 36 | -54 | -28 | -26 | -24 | -27 | -19 | -11 | -38 | -31 | -39 | -30 | -30 | -31 | -27 | -26 | -32 | -19 | -21 | -31 | -27 | -18 | -32 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 34 | -55 | -33 | -33 | -26 | -27 | -15 | -14 | -41 | -31 | -41 | -26 | -30 | -33 | -27 | -28 | -35 | -21 | -27 | -33 | -27 | -21 | -35 | -85 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 32 | -57 | -37 | -36 | -31 | -31 | -6 | -21 | -45 | -28 | -45 | -22 | -30 | -35 | -31 | -33 | -38 | -23 | -29 | -35 | -32 | -25 | -40 | -85 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 30 | -55 | -31 | -30 | -26 | -28 | -2 | -22 | -42 | -20 | -46 | -21 | -28 | -31 | -30 | -30 | -35 | -22 | -30 | -35 | -30 | -27 | -48 | -85 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 28 | -58 | -33 | -31 | -29 | -31 | -2 | -28 | -42 | -17 | -47 | -20 | -29 | -31 | -31 | -31 | -38 | -23 | -33 | -32 | -27 | -29 | -54 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 26 | -61 | -38 | -34 | -33 | -34 | -7 | -35 | -44 | -20 | -49 | -18 | -32 | -35 | -34 | -32 | -43 | -23 | -32 | -31 | -24 | -29 | -56 | -88 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 24 | -63 | -42 | -37 | -37 | -38 | -11 | -39 | -48 | -21 | -50 | -16 | -37 | -39 | -38 | -35 | -46 | -24 | -31 | -31 | -23 | -29 | -57 | -89 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 22 | -62 | -42 | -39 | -37 | -39 | -14 | -39 | -50 | -24 | -50 | -13 | -39 | -40 | -40 | -36 | -46 | -23 | -30 | -33 | -22 | -28 | -61 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 20 | -64 | -42 | -38 | -34 | -35 | -12 | -36 | -48 | -26 | -51 | -10 | -41 | -37 | -40 | -35 | -43 | -21 | -28 | -34 | -25 | -29 | -58 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 18 | -62 | -38 | -35 | -28 | -33 | -11 | -34 | -45 | -22 | -49 | -6 | -39 | -33 | -34 | -32 | -40 | -20 | -27 | -34 | -23 | -27 | -49 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 16 | -63 | -37 | -35 | -31 | -33 | -8 | -32 | -43 | -25 | -48 | -13 | -36 | -33 | -34 | -30 | -39 | -20 | -23 | -33 | -24 | -24 | -46 | -86 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 14 | -64 | -38 | -36 | -28 | -31 | -2 | -32 | -48 | -24 | -51 | -12 | -38 | -32 | -34 | -30 | -38 | -16 | -24 | -30 | -24 | -28 | -47 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 12 | -61 | -31 | -29 | -25 | -27 | -1 | -29 | -45 | -19 | -49 | -5 | -36 | -25 | -29 | -25 | -37 | -15 | -21 | -29 | -24 | -22 | -44 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 10 | -59 | -28 | -26 | -19 | -18 | 11 | -20 | -37 | -11 | -42 | -1 | -26 | -22 | -22 | -25 | -29 | -9 | -7 | -25 | -19 | -17 | -42 | -87 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 8 | -54 | -16 | -17 | -16 | -11 | 4 | -14 | -38 | -10 | -38 | -1 | -22 | -21 | -19 | -23 | -31 | -11 | -3 | -24 | -20 | -16 | -39 | -86 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 6 | -51 | -15 | -18 | -16 | -10 | 5 | -11 | -33 | -4 | -32 | 4 | -17 | -18 | -13 | -22 | -26 | -11 | 2 | -22 | -17 | -9 | -34 | -84 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 4 | -46 | -7 | -15 | -6 | -1 | 10 | -9 | -22 | -5 | -30 | 2 | -13 | -14 | -8 | -15 | -15 | -2 | 8 | -18 | -13 | -7 | -21 | -82 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 2 | -34 | 1 | -9 | 0 | 3 | 9 | -6 | -13 | -3 | -23 | 4 | -10 | -9 | -4 | -9 | -7 | 1 | 9 | -11 | -7 | -2 | -12 | -75 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
| 0 | -28 | 2 | -7 | 4 | 8 | 15 | -1 | -8 | 4 | -18 | 9 | -5 | -3 | 3 | -7 | -1 | 5 | 15 | -9 | -1 | 4 | -10 | -70 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 | -100 |
Cell lowest Mel band in row 0, time (s) 0: log-Mel, dB -28 (at or above the threshold). Cells at or above -100: log-Mel, dB 1152 of 1152.
CNN14 in brief
CNN14 treats the spectrogram as an image. Six blocks of two 3 x 3 convolutions each raise the channel count from 64 to 2048, and the first five halve both time and frequency.
CNN14 from one 5 second window to 527 scores
Five stages in order: a waveform of 160000 samples becomes a 501 by 64 log-Mel spectrogram, six convolution blocks reduce it to 2048 channels over 15 time steps, global pooling removes the time and frequency axes, and a final layer with a sigmoid gives 527 class scores.
| stage | input | output | what happens |
|---|---|---|---|
| 1. Log-Mel front end | waveform, 160000 samples | 1 x 501 x 64 | STFT with a 1024 sample window and 320 sample step, 64 Mel bands, decibels. |
| 2. Convolution blocks 1 to 3 | 1 x 501 x 64 | 256 x 62 x 8 | Each block has two convolutions and average pooling by 2 in time and frequency. |
| 3. Convolution blocks 4 to 6 | 256 x 62 x 8 | 2048 x 15 x 2 | Blocks 4 and 5 halve both axes; block 6 does not pool. |
| 4. Global pooling | 2048 x 15 x 2 | 2048 numbers | Mean over frequency, then maximum plus mean over time. The position within the window is gone. |
| 5. Classifier | 2048 numbers | 527 scores in 0..1 | A 2048 unit layer, then one output per class through a sigmoid. |
Stage 1 of 5, Log-Mel front end. Input: waveform, 160000 samples. Output: 1 x 501 x 64.
The global pooling step is the reason a tag carries no time: after the maximum and mean over time, nothing records where in the window the evidence was.
The authors trained on AudioSet, about two million 10 second clips from YouTube whose labels state which classes are present in a clip, not when. They report a mean average precision (mAP) of 0.431 for CNN14 on AudioSet tagging; their larger Wavegram-Logmel-CNN reaches 0.439. Average precision is computed per class from the precision-recall curve and then averaged over classes. The CNN14 instantiated here has about 80.8 million parameters.
Reading multi-label scores
For class k the network produces a number z_k and squashes it on its own:
p_k = 1 / (1 + exp(-z_k))
Each class is a separate yes-or-no question. The 527 values do not sum to 1, several can be high together (speech over music), and all can be low. Training uses binary cross-entropy per class. A score is not a calibrated probability for your audio unless you have checked that.
The class list follows AudioSet. A few representative entries, with their exact names:
| Group | Class names |
|---|---|
| Voice | Speech, Speech synthesizer, Narration, monologue, Laughter, Crying, sobbing, Cough |
| Animals | Dog, Bark, Meow |
| Music and applause | Music, Applause |
| Weather and water | Rain, Thunder, Wind, Water |
| Vehicles and alarms | Car, Vehicle horn, car horn, honking, Siren, Fire alarm, Smoke detector, smoke alarm |
| Impacts | Glass, Shatter, Gunshot, gunfire, Explosion |
| Signals and background | Telephone bell ringing, Dial tone, Busy signal, Sine wave, Silence, Inside, small room |
Run PANNs directly on a soundtrack
Extract the soundtrack:
ffmpeg -i video.mp4 -vn -ac 1 -ar 32000 -c:a pcm_s16le speech.wav
Install panns-inference, librosa and PyTorch:
python -m pip install panns-inference librosa torch
Download Cnn14_mAP=0.431.pth (327,428,481 bytes) from the authors' Zenodo record, which is licensed CC BY 4.0. The record gives its MD5 as 541141fa2ee191a88f24a3219fff024e; check it.
Two behaviours of the library affect where your machine connects. Importing panns_inference downloads the class label CSV with wget, over plain HTTP, into a panns_data folder in your home directory if the file is missing. And AudioTagging downloads the checkpoint again if the file you point to is smaller than 300 million bytes. Both need wget.
import librosa
import numpy as np
from panns_inference import AudioTagging, labels
SR = 32000
WINDOW = 5 * SR
wave, _ = librosa.load("speech.wav", sr=SR, mono=True)
peak = np.abs(wave).max()
if peak > 0:
wave = wave / peak # the analyzer's normalisation
model = AudioTagging(checkpoint_path="Cnn14_mAP=0.431.pth") # GPU if present, else CPU
for start in range(0, len(wave), WINDOW):
chunk = wave[start:start + WINDOW]
if len(chunk) < SR: # pad a very short tail
chunk = np.pad(chunk, (0, SR - len(chunk)))
clipwise, embedding = model.inference(chunk[None, :]) # a tuple of two arrays
scores = clipwise[0] # 527 independent scores
top = np.argsort(scores)[::-1][:3]
print(f"{start / SR:4.1f} s", [(labels[k], round(float(scores[k]), 3)) for k in top])
Pad short tails: in this version, an input of 0.3 seconds or less raised an error because the pooling layers run out of time steps.
The test clip is 9 seconds of synthetic speech (FFmpeg's flite voice) followed by silence. The script, run on the soundtrack as the analyzer had extracted it (panns-inference 0.1.1, librosa 0.11.0, PyTorch 2.15.0), printed the same result lines on GPU and CPU, after the library's own start-up messages:
0.0 s [('Speech synthesizer', 0.745), ('Speech', 0.685), ('Narration, monologue', 0.108)]
5.0 s [('Speech', 0.627), ('Silence', 0.241), ('Music', 0.113)]
Read these carefully. The voice really is synthetic, and speech really is present. There is no music in the file, so Music at 0.113 is a false positive. And the second window is tagged Speech at 0.627 although speech occupies only its first 0.4 seconds or so.
For comparison, a pure 440 Hz tone was tagged Sine wave (0.60 to 0.71 across three windows) with Dial tone next (0.06 to 0.12).
Choosing a threshold
A threshold turns scores into labels. The code below applies several to the recorded scores above (the two lists hold the top five of each window).
# recorded top scores (panns-inference called directly, NOT analyzer output), speech clip
windows = {
"0-5 s": {"Speech synthesizer": 0.745, "Speech": 0.685, "Narration, monologue": 0.108,
"Music": 0.051, "Male speech, man speaking": 0.028},
"5-9 s": {"Speech": 0.627, "Silence": 0.241, "Music": 0.113,
"Vehicle": 0.068, "Inside, small room": 0.037},
}
for threshold in (0.05, 0.1, 0.2, 0.5):
for name, scores in windows.items():
kept = [label for label, score in scores.items() if score >= threshold]
print(f"threshold {threshold:<4} {name}: {kept}")
threshold 0.05 0-5 s: ['Speech synthesizer', 'Speech', 'Narration, monologue', 'Music']
threshold 0.05 5-9 s: ['Speech', 'Silence', 'Music', 'Vehicle']
threshold 0.1 0-5 s: ['Speech synthesizer', 'Speech', 'Narration, monologue']
threshold 0.1 5-9 s: ['Speech', 'Silence', 'Music']
threshold 0.2 0-5 s: ['Speech synthesizer', 'Speech']
threshold 0.2 5-9 s: ['Speech', 'Silence']
threshold 0.5 0-5 s: ['Speech synthesizer', 'Speech']
threshold 0.5 5-9 s: ['Speech']
The analyzer's intended rule, top label only, would have kept Speech synthesizer in the first window and dropped Speech, although both are correct.
There is no universal threshold. To choose one, label some audio of the kind you will process and, for each class you care about, count at several thresholds:
- precision, the share of reported windows that really contain the sound,
TP / (TP + FP); - recall, the share of windows containing the sound that were reported,
TP / (TP + FN).
The numbers below are invented to show the mechanics.
# made-up validation data for one class: (model score, 1 if the sound really is in the window)
labelled = [(0.92, 1), (0.81, 1), (0.64, 1), (0.55, 0), (0.41, 1), (0.33, 0),
(0.27, 1), (0.18, 0), (0.12, 0), (0.09, 1), (0.05, 0), (0.02, 0)]
for threshold in (0.5, 0.3, 0.1):
tp = sum(1 for score, truth in labelled if score >= threshold and truth)
fp = sum(1 for score, truth in labelled if score >= threshold and not truth)
fn = sum(1 for score, truth in labelled if score < threshold and truth)
print(f"threshold {threshold}: precision {tp / (tp + fp):.2f} recall {tp / (tp + fn):.2f}")
threshold 0.5: precision 0.75 recall 0.50
threshold 0.3: precision 0.67 recall 0.67
threshold 0.1: precision 0.56 recall 0.83
Lowering the threshold finds more of the sound and raises more false alarms. Which trade is right depends on what happens next: a search tool that a person checks can accept false alarms, while an automatic alert usually cannot. Choose on one labelled set and report performance on another. Classes differ, and a threshold tuned on 5 second windows need not hold for 1 second windows.
A window tag is not an event time
Timing comes only from which windows you feed in. With a duration T, window length W and step D between window starts, there are n = floor((T - W) / D) + 1 full windows, and shorter steps cost proportionally more model calls. Shorter windows localise better but give the model less context than the 10 second clips it was trained on.
To see how window length affects timing, a test signal was built with a known answer: 10 seconds of very faint noise with a 440 Hz tone added from 6.2 to 7.2 seconds. It was tagged with the same model, first in two 5 second windows and then in 1 second windows starting every 0.5 seconds (19 of them).
With 5 second windows, the first scored Silence highest (0.142) and the second Busy signal (0.696). That locates the beep only "between 5 and 10 seconds", and under a name you might not expect for a tone: Sine wave scored 0.026.
Scores of 1 second windows around a 1 second tone
Two lines over window start times from 0 to 9 seconds. Busy signal is zero except in the four windows that overlap the tone, where it ranges from 0.55 to 0.87 and does not follow how much tone each window holds. White noise stays near 0.1 elsewhere.
Busy signal (circles)White noise (squares, dashed)
Busy signal, values at or above the threshold: 4 of 19 (5.5, 6, 6.5, 7)
White noise, values at or above the threshold: 0 of 19
| window | covers | at or above threshold at |
|---|---|---|
| overlap tone | 5.5 to 7 | 5.5, 6, 6.5, 7 |
| window start (s) | Busy signal | White noise | at or above threshold |
|---|---|---|---|
| 0 | 0 | 0.111 | none |
| 0.5 | 0 | 0.094 | none |
| 1 | 0 | 0.073 | none |
| 1.5 | 0 | 0.091 | none |
| 2 | 0 | 0.114 | none |
| 2.5 | 0 | 0.095 | none |
| 3 | 0 | 0.099 | none |
| 3.5 | 0 | 0.089 | none |
| 4 | 0 | 0.073 | none |
| 4.5 | 0 | 0.06 | none |
| 5 | 0 | 0.08 | none |
| 5.5 | 0.865 | 0.024 | Busy signal |
| 6 | 0.553 | 0.005 | Busy signal |
| 6.5 | 0.748 | 0.001 | Busy signal |
| 7 | 0.611 | 0.012 | Busy signal |
| 7.5 | 0 | 0.115 | none |
| 8 | 0 | 0.095 | none |
| 8.5 | 0 | 0.061 | none |
| 9 | 0 | 0.091 | none |
Threshold 0.5. Busy signal is at or above it at 4 of 19 (5.5, 6, 6.5, 7). White noise is at or above it at 0 of 19.
All four windows that overlap the tone fire, including the one holding only 0.2 seconds of it, and the window holding the most tone has the lowest of the four scores. A score is not the fraction of the window a sound fills. Turning window hits into intervals therefore depends strongly on the threshold:
# recorded "Busy signal" scores of 1 s windows starting every 0.5 s; the tone runs from 6.2 to 7.2 s
busy = {5.0: 0.000, 5.5: 0.865, 6.0: 0.553, 6.5: 0.748, 7.0: 0.611, 7.5: 0.000}
def intervals(scores, threshold, length=1.0):
"""Merge the windows at or above the threshold that touch or overlap."""
merged = []
for start in sorted(s for s, p in scores.items() if p >= threshold):
if merged and start <= merged[-1][1]:
merged[-1][1] = start + length
else:
merged.append([start, start + length])
return merged
for threshold in (0.5, 0.7, 0.8, 0.9):
print(f"threshold {threshold}: {intervals(busy, threshold)}")
threshold 0.5: [[5.5, 8.0]]
threshold 0.7: [[5.5, 7.5]]
threshold 0.8: [[5.5, 6.5]]
threshold 0.9: []
One tone and three different answers about when it happened
A ten second timeline. The true tone lasts from 6.2 to 7.2 seconds. Five second windows only place it somewhere in the second half. Merged one second windows report 5.5 to 8 seconds at threshold 0.5 and 5.5 to 6.5 seconds at threshold 0.8.
| lane | holds |
|---|---|
| True tone | nothing |
| 5 s window, Busy signal | nothing |
| 1 s windows, 0.5 or more | nothing |
| 1 s windows, 0.8 or more | nothing |
| event | lane | time |
|---|---|---|
| 440 Hz | True tone | 6.2 to 7.2 s |
| score 0.70 | 5 s window, Busy signal | 5 to 10 s |
| 5.5 to 8.0 s | 1 s windows, 0.5 or more | 5.5 to 8 s |
| 5.5 to 6.5 s | 1 s windows, 0.8 or more | 5.5 to 6.5 s |
At 0 s. True tone: nothing. 5 s window, Busy signal: nothing. 1 s windows, 0.5 or more: nothing. 1 s windows, 0.8 or more: nothing.
Report such results as "a window overlapping this range scored high", keep the window length and threshold with them, and do not present the interval's ends as onset and offset. To compare with video frames, also check whether the audio stream starts later than the video; ffprobe shows each stream's start_time, and a WAV extracted on its own does not keep that offset.
A trap with synthetic test signals
The first version of this test used exact digital silence and a pure tone. On those inputs GPU and CPU disagreed badly: for the window starting at 6.5 s the GPU's top class was Busy signal (0.703) and the CPU's was Sine wave (0.577). The same silent signal also gave Music as the top class (0.142) for 1 second windows of pure zeros. Adding the faint noise floor brought GPU and CPU to within 0.003 of each other. A likely reason is that logarithms of near-zero energy depend on floating-point rounding, though that was not isolated. Test with realistic audio, and do not assume digital silence is scored as Silence.
Models that output times
panns-inference also provides SoundEventDetection, which uses a decision-level CNN14 checkpoint and returns scores for each frame. It was not run for this article. Its training labels are also clip-level, so check its times against hand-marked events before relying on them.
False positives and false negatives
| Situation | Risk | What to do |
|---|---|---|
| A brief sound in a long window | Missed or diluted | Shorter or overlapping windows, then validate |
| Digital silence or synthetic tones | Odd labels, hardware-dependent scores | Gate by signal level; test with real recordings |
| Several sounds at once | Quieter class falls below threshold | Keep all classes above a per-class threshold |
| Acoustically similar sounds | Confusion, such as a beep read as a phone signal | Group related classes; listen to hits |
| A class your task needs but the list lacks | Mapped to a neighbour | Train or adapt a model for that class |
| Recording conditions unlike YouTube clips | Scores shift | Re-measure thresholds on your own audio |
Store each result with its window start and end, label, score, model file, window length and threshold. A label without those cannot be reviewed later.
For the words in the soundtrack, see offline video transcription with Whisper.
How captions, detections and speech are combined into a summary, and checked, is covered in local video summarization with Ollama.
References
- Q. Kong, Y. Cao, T. Iqbal, Y. Wang, W. Wang, M. D. Plumbley. PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition. arXiv:1912.10211
- Q. Kong.
panns_inferencerepository. github.com/qiuqiangkong/panns_inference - PANNs pretrained models, Zenodo record. zenodo.org/records/3987831
- J. F. Gemmeke, D. P. W. Ellis, D. Freedman, A. Jansen, W. Lawrence, R. C. Moore, M. Plakal, M. Ritter. Audio Set: An ontology and human-labeled dataset for audio events. ICASSP 2017. Google Research
- AudioSet ontology and dataset site. research.google.com/audioset
- A. Sajjadi. AI-Powered Video Analyzer, source repository, version 1.1.0. GitHub