41 if (info.sample_rate ==
cfg.sample_rate && info.channels ==
cfg.channels &&
42 info.bits_per_sample ==
cfg.bits_per_sample)
44 cfg.sample_rate = info.sample_rate;
45 cfg.channels = info.channels;
46 cfg.bits_per_sample = info.bits_per_sample;
52 using audio_tools::AudioDecoder::begin;
62 if (!
cfg.isValid())
return false;
67 for (
int t = 0; t <
TONES; t++) {
69 coeff[t] = 2.0f * cosf(2.0f * (
float)M_PI * freq /
cfg.sample_rate);
71 timeout = (uint32_t)
cfg.rx_timeout_ms *
cfg.sample_rate / 1000;
72 twist = powf(10.0f,
cfg.max_twist_db / 20.0f);
98 size_t write(
const uint8_t* data,
size_t len)
override {
100 input.write(data, len, [
this](
float sample) {
addSample(sample,
true); });
163 for (
int w = 0; w <
WINDOWS; w++) {
178 for (
int t = 0; t <
TONES; t++) w.
s1[t] = w.
s2[t] = 0;
186 return ((
noise_seed >> 8) & 0xFFFF) / 65535.0f - 0.5f;
191 if (add_noise &&
cfg.rx_noise != 0.0f) x +=
noise() *
cfg.rx_noise * 2.0f;
192 for (
int k = 0; k <
WINDOWS; k++) {
194 if (w.
count++ < 0)
continue;
195 for (
int t = 0; t <
TONES; t++) {
196 float s = x +
coeff[t] * w.
s1[t] - w.
s2[t];
213 for (
int t = 0; t <
TONES; t++) {
214 float p = w.
s1[t] * w.
s1[t] + w.
s2[t] * w.
s2[t] -
216 amp[t] = 2.0f * sqrtf(p > 0 ? p : 0) /
window;
220 float a_row = amp[row], a_col = amp[4 + column];
221 level = a_row + a_col;
222 if (a_row <
cfg.min_amplitude || a_col <
cfg.min_amplitude)
return -1;
223 if (a_row >
twist * a_col || a_col >
twist * a_row)
return -1;
224 for (
int t = 0; t < 4; t++) {
225 if (t != row && amp[t] * amp[t] *
group_ratio > a_row * a_row)
return -1;
226 if (t != column && amp[4 + t] * amp[4 + t] *
group_ratio > a_col * a_col)
230 float tone_power = (a_row * a_row + a_col * a_col) / 2.0f;
232 return row * 4 + column;
237 for (
int t = 1; t < 4; t++)
238 if (amp[t] > amp[result]) result = t;
262 if (p_print !=
nullptr) p_print->write((uint8_t)
currentKey());
277 status(
"### CARRIER dtmf ###\n");
void addSample(float x, bool add_noise)
Definition DTMFDecoder.h:189
int last_key
Definition DTMFDecoder.h:155
bool is_finished
Definition DTMFDecoder.h:138
uint32_t noise_seed
Definition DTMFDecoder.h:140
static constexpr int TONES
Definition DTMFDecoder.h:119
float twist
Definition DTMFDecoder.h:146
bool begin(DTMFConfig config)
Starts the processing with the indicated configuration.
Definition DTMFDecoder.h:55
void flush() override
Processes the remaining samples (e.g. at the end of the input).
Definition DTMFDecoder.h:87
float noise()
uniform random value in the range -0.5 to 0.5
Definition DTMFDecoder.h:184
audio_tools::AudioInfo audioInfo() override
Definition DTMFDecoder.h:50
static constexpr float WINDOW_SEC
Definition DTMFDecoder.h:121
static constexpr int MISSES
Definition DTMFDecoder.h:123
int candidate
Definition DTMFDecoder.h:150
int held
Definition DTMFDecoder.h:152
int window
Definition DTMFDecoder.h:141
void decide(int key)
Debouncing of the detected keys.
Definition DTMFDecoder.h:243
void resetWindow(Window &w)
Definition DTMFDecoder.h:177
bool is_active
Definition DTMFDecoder.h:137
int detect(Window &w)
Definition DTMFDecoder.h:211
size_t write(const uint8_t *data, size_t len) override
Provide PCM data to the decoder.
Definition DTMFDecoder.h:98
DTMFConfig & config()
The configuration which was provided in begin().
Definition DTMFDecoder.h:114
void resetState()
Definition DTMFDecoder.h:162
static int strongest(const float *amp)
Definition DTMFDecoder.h:235
bool begin() override
Definition DTMFDecoder.h:60
uint32_t timeout
Definition DTMFDecoder.h:145
void end() override
Processes the remaining samples and reports the end of the carrier.
Definition DTMFDecoder.h:80
char currentKey()
The key which is currently received or 0.
Definition DTMFDecoder.h:111
unsigned ndata
Definition DTMFDecoder.h:160
static constexpr float ECHO_LEVEL
the same key with a lower level (relative to the last one) is an echo
Definition DTMFDecoder.h:125
bool isCarrier() override
true from the first key until the end of the transmission
Definition DTMFDecoder.h:105
void startOfCarrier()
Definition DTMFDecoder.h:273
Window windows[WINDOWS]
Definition DTMFDecoder.h:144
int step
Definition DTMFDecoder.h:142
float group_ratio
Definition DTMFDecoder.h:147
static constexpr int HITS
Definition DTMFDecoder.h:122
PcmInput input
Definition DTMFDecoder.h:139
int misses
Definition DTMFDecoder.h:153
bool isFinished() override
true if the decoding was stopped (rx_one)
Definition DTMFDecoder.h:108
int hits
Definition DTMFDecoder.h:151
uint32_t idle_samples
Definition DTMFDecoder.h:159
static constexpr int WINDOWS
Definition DTMFDecoder.h:120
void setAudioInfo(audio_tools::AudioInfo info) override
Defines the format of the input audio data.
Definition DTMFDecoder.h:40
bool carrier
Definition DTMFDecoder.h:158
DTMFConfig defaultConfig()
Provides a default configuration.
Definition DTMFDecoder.h:37
DTMFConfig cfg
Definition DTMFDecoder.h:136
float level
Definition DTMFDecoder.h:157
DTMFDecoder(Print &out)
Definition DTMFDecoder.h:34
float last_level
Definition DTMFDecoder.h:156
static constexpr float ENERGY_RATIO
part of the signal energy which must be in the two tones
Definition DTMFDecoder.h:127
void endOfCarrier()
Definition DTMFDecoder.h:280
float coeff[TONES]
Definition DTMFDecoder.h:143
static const float * rows()
row frequencies in Hz
Definition DTMF.h:87
static char key(int row, int column)
Character of the key in the indicated row and column.
Definition DTMF.h:99
static const float * columns()
column frequencies in Hz
Definition DTMF.h:93
Common API of the demodulators (FSKDecoder, MFSKDecoder): PCM audio data is written to the decoder an...
Definition ModemBase.h:344
void status(const char *fmt,...)
Definition ModemBase.h:367
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
Configuration of the DTMF (touch tone) modem (DTMFEncoder, DTMFDecoder): each of the characters 0-9,...
Definition DTMF.h:20
Goertzel filters of one window.
Definition DTMFDecoder.h:130
float s2[TONES]
Definition DTMFDecoder.h:131
float s1[TONES]
Definition DTMFDecoder.h:131
int count
Definition DTMFDecoder.h:133
float power
Definition DTMFDecoder.h:132