Skip to main content

A zero-heap, no_std, const-first DTMF keypad frequency table with runtime tolerance helpers.

Project description

DTMF Table

A zero-heap, no_std friendly, const-first implementation of the standard DTMF (Dual-Tone Multi-Frequency) keypad used in telephony systems.
This crate provides compile-time safe mappings between keypad keys and their canonical low/high frequencies, along with runtime helpers for practical audio processing.


Features

  • Const-evaluated forward and reverse mappings between DTMF keys and frequencies
  • Closed enum for keys — invalid keys are unrepresentable
  • Zero allocations, works in no_std environments
  • Runtime helpers:
    • Tolerance-based reverse lookup (e.g., from FFT peaks)
    • Nearest snapping for noisy frequency estimates
    • Iteration over all tones and keys

Installation

cargo add dtmf_tones

This crate is no_std by default and does not pull in any dependencies.


Quick Example

use dtmf_table::{DtmfTable, DtmfKey};

fn main() {
    // Construct a zero-sized table instance
    let table = DtmfTable::new();

    // Forward lookup from key to canonical frequencies
    let (low, high) = DtmfTable::lookup_key(DtmfKey::K8);
    assert_eq!((low, high), (852, 1336));

    // Reverse lookup with tolerance (e.g., from FFT bin centres)
    let key = table.from_pair_tol_f64(770.2, 1335.6, 6.0).unwrap();
    assert_eq!(key.to_char(), '5');

    // Nearest snapping for noisy estimates
    let (k, snapped_low, snapped_high) = table.nearest_u32(768, 1342);
    assert_eq!(k.to_char(), '5');
    assert_eq!((snapped_low, snapped_high), (770, 1336));
}

Why Const-First?

Most DTMF tone mappings are fixed, known at compile time, and tiny (4×4 keypad). By making the mapping fully const, you can:

  • Use it inside const fn, static initialisers, or const generic contexts
  • Catch invalid keys at compile time
  • Eliminate runtime table lookups entirely

API Overview

Function Description const
DtmfKey::from_char Convert a char to a key (fallible)
DtmfKey::from_char_or_panic Convert a char to a key, panics at compile time if invalid
DtmfKey::to_char Convert key to char
DtmfTable::lookup_key Forward lookup: key → (low, high)
DtmfTable::from_pair_exact Reverse lookup: exact pair → key
DtmfTable::from_pair_normalised Reverse lookup: order-insensitive
DtmfTable::from_pair_tol_f64 Reverse lookup with tolerance
DtmfTable::nearest_u32 Snap noisy frequencies to nearest canonical pair
DtmfTable::iter_tones Iterate over all tones

Integration Example

This crate pairs naturally with audio analysis pipelines. For example:

  • Take an audio segment
  • Compute FFT magnitude
  • Pick two frequency peaks
  • Use from_pair_tol_f64 or nearest_f64 to resolve the DTMF key
// freq1 and freq2 are the peak frequencies extracted from your FFT
let key = table.from_pair_tol_f64(freq1, freq2, 5.0);
if let Some(k) = key {
    println!("Detected key: {}", k.to_char());
}

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dtmf_table-1.0.2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dtmf_table-1.0.2-cp313-cp313-manylinux_2_34_x86_64.whl (227.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

File details

Details for the file dtmf_table-1.0.2.tar.gz.

File metadata

  • Download URL: dtmf_table-1.0.2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.0

File hashes

Hashes for dtmf_table-1.0.2.tar.gz
Algorithm Hash digest
SHA256 2fb6b8a53b3581c9c8943ac908ebac3a0a56c3cbc3cc274b8beb96c547b4bf6e
MD5 df98218804e404776bd3cb162c1ccd49
BLAKE2b-256 72641e1e3f4955ecae8cf1d67cb2f1e86ed6304a7fdcab02e2339b58bfeb8f05

See more details on using hashes here.

File details

Details for the file dtmf_table-1.0.2-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for dtmf_table-1.0.2-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4f86ea56163d9009135aa541454aa576af108548387aa8c3106a4699f17a073e
MD5 1ce2cffb270537b44eaf1974a84c222c
BLAKE2b-256 a4e2139f6452c3bf33af3c2375064753a6e58dbce0a3ae814dd83b7710e689b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page