Deluge Firmware 1.4.0
Build date: 2026.11.30
Loading...
Searching...
No Matches
thump_detector.h
1/*
2 * Copyright © 2026 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <array>
21#include <cstdint>
22#include <optional>
23
31
32constexpr int32_t kInputRate = 44100;
33constexpr int32_t kDecimation = 8;
34constexpr float kRate = static_cast<float>(kInputRate) / kDecimation;
35
37using Frame = uint32_t;
38
41class Decimator {
42public:
44 std::optional<float> push(int32_t sample);
45 void reset();
46
47private:
48 std::array<int32_t, kDecimation> first_{};
49 std::array<int64_t, kDecimation> second_{};
50 int64_t sumFirst_ = 0;
51 int64_t sumSecond_ = 0;
52 int32_t index_ = 0;
53};
54
56struct Biquad {
57 float b0 = 1, b1 = 0, b2 = 0, a1 = 0, a2 = 0;
58 float z1 = 0, z2 = 0;
59
60 float process(float x) {
61 float const y = b0 * x + z1;
62 z1 = b1 * x - a1 * y + z2;
63 z2 = b2 * x - a2 * y;
64 return y;
65 }
66 void reset() { z1 = z2 = 0; }
67
68 static Biquad highPass1(float cutoff, float rate);
69 static Biquad lowPass2(float cutoff, float rate);
70 static Biquad lowPass1(float cutoff, float rate);
71};
72
74struct Thump {
75 Frame onset = 0;
76 float level = 0;
77 bool settled = false;
78};
79
80class ThumpDetector {
81public:
83 static constexpr Frame kSettleFrames = kInputRate * 4 / 1000;
85 static constexpr Frame kMinGapFrames = kInputRate * 15 / 1000;
87 static constexpr float kOnsetOverFloor = 4.f;
89 static constexpr float kMinOnsetLevel = 3.2e-5f;
90 static constexpr int32_t kNumThumps = 16;
91
92 ThumpDetector();
93 void reset();
94
96 void process(float sample, Frame frame);
97
99 [[nodiscard]] float noiseFloor() const { return floor_; }
101 [[nodiscard]] float onsetLevel() const;
102
104 [[nodiscard]] Thump const* recent(int32_t index) const;
105
107 [[nodiscard]] bool rising() const { return inThump_ && !thumps_[newest_].settled; }
109 [[nodiscard]] bool inThump() const { return inThump_; }
110
111private:
112 Biquad highPass_;
113 Biquad lowPass_;
114
115 std::array<Thump, kNumThumps> thumps_{};
116 int32_t newest_ = 0;
117 int32_t count_ = 0;
118 bool inThump_ = false;
119 float thumpPeak_ = 0; // Over the whole thump, for telling when it has died away
120 // After a thump, its peak decaying: a new thump has to beat it, so the pad's bounce and ringing (bursts 15-20 ms
121 // apart, 10-15 dB down) don't start their own
122 float holdOff_ = 0;
123
124 // Noise floor, from 10 ms blocks that aren't in a thump
125 float floor_ = 0;
126 float blockMax_ = 0;
127 int32_t blockSamples_ = 0;
128 bool blockHadThump_ = false;
129 bool floorStarted_ = false;
130 int32_t sustainedBlocks_ = 0;
131};
132
133} // namespace deluge::dsp::mic_velocity
Definition thump_detector.h:41
std::optional< float > push(int32_t sample)
Returns a decimated sample every kDecimation inputs.
Definition thump_detector.cpp:54
static constexpr float kOnsetOverFloor
Rising this far over the noise floor starts a thump (12 dB).
Definition thump_detector.h:87
bool inThump() const
Whether a thump is going on (from its onset until it has died away).
Definition thump_detector.h:109
bool rising() const
Whether a thump is in progress (started, not yet settled).
Definition thump_detector.h:107
float noiseFloor() const
The noise floor: a typical 10 ms peak of the band-limited mic with no thump.
Definition thump_detector.h:99
void process(float sample, Frame frame)
One decimated sample of the mic (after any echo cancelling); frame is the input frame it ends at.
Definition thump_detector.cpp:139
static constexpr float kMinOnsetLevel
Never start a thump below this, however quiet the noise (-90 dBFS).
Definition thump_detector.h:89
static constexpr Frame kSettleFrames
How long after onset the level is measured for.
Definition thump_detector.h:83
Thump const * recent(int32_t index) const
The most recent thumps, newest first (0: the newest). nullptr past the ones there are.
Definition thump_detector.cpp:132
float onsetLevel() const
The level a thump has to reach to be found.
Definition thump_detector.cpp:128
static constexpr Frame kMinGapFrames
A thump lasts at least this long.
Definition thump_detector.h:85
Definition delay_finder.cpp:21
constexpr float kRate
5512.5 Hz
Definition thump_detector.h:34
uint32_t Frame
Input frames (at 44.1 kHz) on the mic reader's clock.
Definition thump_detector.h:37
A second-order section, direct form II transposed.
Definition thump_detector.h:56
static Biquad highPass1(float cutoff, float rate)
First order (b2 = a2 = 0).
Definition thump_detector.cpp:73
static Biquad lowPass2(float cutoff, float rate)
Butterworth.
Definition thump_detector.cpp:95
static Biquad lowPass1(float cutoff, float rate)
First order.
Definition thump_detector.cpp:84
A thump: where it rose out of the noise, and how loud it got in the 4 ms after.
Definition thump_detector.h:74
bool settled
Its 4 ms have passed, so level won't change.
Definition thump_detector.h:77
float level
Peak of the band-limited mic, full scale 1.
Definition thump_detector.h:76