TinyModem
Header-only software audio modem library
Loading...
Searching...
No Matches
DTMF.h
Go to the documentation of this file.
1#pragma once
2
3#include <ctype.h>
4#include <stdint.h>
5#include <string.h>
6
7#include "AudioTools.h"
8
9namespace tinymodem {
10
20struct DTMFConfig : public audio_tools::AudioInfo {
22 sample_rate = 48000;
23 channels = 1;
24 bits_per_sample = 16;
25 }
26
28 int tone_ms = 70;
30 int pause_ms = 70;
32 int comma_ms = 500;
34 float volume = 1.0f;
36 float min_amplitude = 0.01f;
39 float max_twist_db = 16.0f;
42 float group_ratio_db = 6.0f;
44 int rx_timeout_ms = 250;
46 bool rx_one = false;
48 uint8_t channel = 0;
50 float rx_noise = 0.0f;
51
54 bool setMode(const char* mode) { return isMode(mode); }
55
57 static bool isMode(const char* mode) {
58 if (mode == nullptr) return false;
59 const char* name = "dtmf";
60 for (int j = 0; j < 5; j++)
61 if (tolower(mode[j]) != name[j]) return false;
62 return true;
63 }
64
66 bool isValid() const {
67 if (tone_ms <= 0 || pause_ms <= 0 || comma_ms < 0 || rx_timeout_ms <= 0 ||
68 max_twist_db <= 0 || group_ratio_db <= 0) {
69 LOGE("DTMF: invalid durations");
70 return false;
71 }
72 if (sample_rate < 4000) {
73 LOGE("DTMF: the sample rate must be at least 4000 Hz");
74 return false;
75 }
76 return true;
77 }
78};
79
84class DTMFKeys {
85 public:
87 static const float* rows() {
88 static const float freqs[4] = {697.0f, 770.0f, 852.0f, 941.0f};
89 return freqs;
90 }
91
93 static const float* columns() {
94 static const float freqs[4] = {1209.0f, 1336.0f, 1477.0f, 1633.0f};
95 return freqs;
96 }
97
99 static char key(int row, int column) { return keys()[row * 4 + column]; }
100
103 static bool find(char ch, int& row, int& column) {
104 ch = toupper((unsigned char)ch);
105 const char* pos = ch == 0 ? nullptr : strchr(keys(), ch);
106 if (pos == nullptr) return false;
107 int idx = pos - keys();
108 row = idx / 4;
109 column = idx % 4;
110 return true;
111 }
112
113 protected:
114 static const char* keys() { return "123A456B789C*0#D"; }
115};
116
117} // namespace tinymodem
The DTMF keypad: the tones and the characters of the 16 keys.
Definition DTMF.h:84
static const char * keys()
Definition DTMF.h:114
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
static bool find(char ch, int &row, int &column)
Definition DTMF.h:103
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
float max_twist_db
Definition DTMF.h:39
int comma_ms
transmitter: duration of the pause for a ',' in ms
Definition DTMF.h:32
int rx_timeout_ms
receiver: the end of a transmission is reported after this silence
Definition DTMF.h:44
static bool isMode(const char *mode)
true if the mode name is "dtmf"
Definition DTMF.h:57
bool isValid() const
Checks the configuration: returns false (and logs the reason) if invalid.
Definition DTMF.h:66
float min_amplitude
receiver: minimum amplitude of each tone (1.0 = full scale)
Definition DTMF.h:36
uint8_t channel
receiver: input channel which is used for the decoding
Definition DTMF.h:48
bool rx_one
receiver: stop after the first transmission
Definition DTMF.h:46
float volume
transmit amplitude (0.0 - 1.0) of the sum of both tones
Definition DTMF.h:34
int pause_ms
transmitter: duration of the pause after a tone in ms
Definition DTMF.h:30
bool setMode(const char *mode)
Definition DTMF.h:54
int tone_ms
transmitter: duration of a tone in ms
Definition DTMF.h:28
float rx_noise
receiver: (testing only) amplitude of added uniform noise
Definition DTMF.h:50
float group_ratio_db
Definition DTMF.h:42
DTMFConfig()
Definition DTMF.h:21