Class DtmfGenerator

java.lang.Object
com.tino1b2be.dtmf.DtmfGenerator

public final class DtmfGenerator extends Object
DTMF tone generation: turn a key sequence into normalised double PCM samples suitable for feeding back through DtmfDecoder.decode(double[], DtmfConfig) or playing through an audio output.

Per Requirements 11.1–11.5, the generator:

  • accepts the sixteen DTMF keys 0-9, A-D, *, # plus lowercase a-d (normalised to uppercase);
  • produces each tone as 0.5 * (sin(2π·lowHz·n/Fs) + sin(2π·highHz·n/Fs)) of length N = round(minimumToneDuration.toSeconds() * sampleRate);
  • inserts M = round(minimumGapDuration.toSeconds() * sampleRate) samples of silence between consecutive tones, none after the final tone;
  • rejects any character outside the accepted set with IllegalArgumentException naming the offending character and its index.

The 0.5 amplitude keeps the combined peak at 0.5, leaving 6 dB of headroom so callers can apply gain without clipping.

Total output length for a sequence of |s| characters is |s| * N + max(0, |s| - 1) * M samples. An empty sequence produces an empty double[].

The class is final with a private constructor; all entry points are static.

Since:
2.0.0
  • Method Details

    • generate

      public static double[] generate(String sequence, DtmfConfig config)
      Generate a fresh double[] holding the PCM samples for sequence.
      Parameters:
      sequence - key sequence; non-null. May be empty
      config - configuration supplying sample rate, minimum tone duration, and minimum gap duration; non-null
      Returns:
      a newly allocated double[] of length |sequence| * N + max(0, |sequence| - 1) * M
      Throws:
      NullPointerException - if either argument is null
      IllegalArgumentException - if sequence contains any character outside the accepted set
    • generateInto

      public static int generateInto(String sequence, DtmfConfig config, double[] out, int offset)
      Generate PCM samples for sequence into a caller-supplied buffer starting at offset. Returns the number of samples written.

      The caller is responsible for sizing out to hold the full output: offset + |sequence| * N + max(0, |sequence| - 1) * M.

      Parameters:
      sequence - key sequence; non-null. May be empty
      config - configuration supplying sample rate, minimum tone duration, and minimum gap duration; non-null
      out - destination buffer; non-null
      offset - starting index into out; must be non-negative and leave enough room for the full output
      Returns:
      the number of samples written (equal to |sequence| * N + max(0, |sequence| - 1) * M)
      Throws:
      NullPointerException - if any argument is null
      IllegalArgumentException - if sequence contains any character outside the accepted set, or if offset < 0
      IndexOutOfBoundsException - if out is too small for the generated samples at the given offset