Class DtmfStream

java.lang.Object
com.tino1b2be.dtmf.DtmfStream
All Implemented Interfaces:
AutoCloseable, Iterator<DtmfTone>

public final class DtmfStream extends Object implements Iterator<DtmfTone>, AutoCloseable
Pull-based DTMF iteration: expose a DtmfDetector as an Iterator over emitted DtmfTone values.

Callers wire a DtmfStream.SampleSource to an in-memory buffer, file reader, or any other source of normalised double PCM samples, and iterate. hasNext() pulls samples from the source until a tone becomes available or the source signals end-of-stream; next() dequeues the head of the buffered emissions.

Relationship to DtmfDetector (Requirement 7.5): DtmfStream.fromSamples(samples, cfg) iterated to exhaustion produces the same tones as a fresh DtmfDetector(cfg) fed samples followed by DtmfDetector.flush(). The stream is a re-packaging of the push API, not a separate detection implementation.

close() flushes the underlying detector and releases references to the DtmfStream.SampleSource; it is idempotent and safe to call multiple times, including from a try-with-resources block.

Instances are not thread-safe. One stream per consumer.

Since:
2.0.0
  • Method Details

    • fromSource

      public static DtmfStream fromSource(DtmfStream.SampleSource source, DtmfConfig config)
      Create a DtmfStream that pulls samples from source.
      Parameters:
      source - source of PCM samples; non-null
      config - detection configuration; non-null
      Returns:
      a new stream
      Throws:
      NullPointerException - if either argument is null
    • fromSamples

      public static DtmfStream fromSamples(double[] samples, DtmfConfig config)
      Create a DtmfStream over a pre-existing double[] sample buffer.

      The buffer is read sequentially exactly once; subsequent iteration past the end of the buffer triggers DtmfStream.SampleSource EOS.

      Parameters:
      samples - sample buffer; non-null
      config - detection configuration; non-null
      Returns:
      a new stream
      Throws:
      NullPointerException - if either argument is null
    • hasNext

      public boolean hasNext()

      Pulls samples from the source, one read at a time, into the internal buffer; each read feeds the detector and any emitted tones are queued. Returns true as soon as the queue becomes non-empty; returns false after the source signals EOS, DtmfDetector.flush() has been called, and the queue is empty.

      Specified by:
      hasNext in interface Iterator<DtmfTone>
    • next

      public DtmfTone next()
      Specified by:
      next in interface Iterator<DtmfTone>
      Throws:
      NoSuchElementException - if no further tones are available
    • close

      public void close()
      Flush the underlying detector and release references to the DtmfStream.SampleSource. Idempotent: safe to call multiple times, safe to call after the iterator is already exhausted.
      Specified by:
      close in interface AutoCloseable