Click or drag to resize
DTMFGenerator Class
Represents a DTMF Generator class for 16-bit PCM audio streams.
Inheritance Hierarchy
SystemObject
  StreamCoders.SignalsDTMFGenerator

Namespace: StreamCoders.Signals
Assembly: MediaSuite (in MediaSuite.dll) Version: 2.0.5.0 (2.0.5.0)
Syntax
public class DTMFGenerator : IDisposable

The DTMFGenerator type exposes the following members.

Constructors
  NameDescription
Public methodDTMFGenerator
Default constructor.
Top
Properties
  NameDescription
Public propertyBitsPerSample
Bits per sample of the PCM stream. Currently this value has to be 16.
Public propertyChannels
Number of channels on the PCM stream. Currently this value has to be 1.
Public propertySampleFrequency
Gets or sets the sample frequency of the PCM stream. Allowed values are: 8000, 16000.
Top
Methods
  NameDescription
Public methodDispose
Destructor.
Public methodGenerate
Generates DTMF to a PCM stream.
Public methodInit
Initializes DTMF Generator.
Top
Extension Methods
  NameDescription
Public Extension MethodCopyOverloaded.
Creates a copy of the object.
(Defined by ObjectExtensions.)
Public Extension MethodCopy(Object)Overloaded.
Creates a deep copy of the object using the supplied object as a target for the copy operation.
(Defined by ObjectExtensions.)
Top
Examples
class Modulator
{
    public Modulator()
    {
        offset = 0;
        q = new Queue<byte[]>();
    }

    ~Modulator()
    {
        q.Clear();
    }

    public void Add(byte[] buffer)
    {
        lock (q) { q.Enqueue(buffer); }
    }

    public byte[] Modulate(byte[] buffer)
    {
        if (q.Count == 0)
            return buffer;
        int bufferOff = 0;
        byte[] modBuf = Peek();
        while (bufferOff < buffer.Length)
        {
            buffer[bufferOff++] += modBuf[offset++];
            if (offset >= modBuf.Length)
            {
                lock (q) { q.Dequeue(); }
                offset = 0;
                if (q.Count == 0)
                    break;
                modBuf = Peek();
            }
        }
        return buffer;
    }
    private byte[] Peek()
    {
        lock (q) { return q.Peek(); }
    }
    private int offset;
    Queue<byte[]> q;
}

class Program
{
    static void Main(string[] args)
    {
        Modulator mod = new Modulator();

        WaveOutput wout = new WaveOutput();
        wout.BitsPerSample = 16;
        wout.Channels = 1;
        wout.SampleRate = 8000;
        wout.Init();
        wout.OpenDevice();

        WaveInput win = new WaveInput();
        win.BitsPerSample = 16;
        win.Channels = 1;
        win.SampleRate = 8000;
        win.TransferBufferCount = 200;
        win.TransferBufferSize = 8000 * 2 / 50;
        win.Init();
        win.OpenDevice();
        win.Start();

        DTMFGenerator tg = new DTMFGenerator();
        tg.SampleFrequency = 8000;

        bool res = tg.Init();

        uint duration = 200;

        Console.WriteLine("Enter 0-9, #, * ");
        win.ClearBuffers();
        while (true)
        {
            if (win.SamplesAvailable)
            {
                byte[] inBuf = win.GetAllData();
                if (inBuf != null)
                {
                    inBuf = mod.Modulate(inBuf);
                    wout.Enqueue(inBuf);
                }

            }
            if (Console.KeyAvailable == false)
            {
                System.Threading.Thread.Sleep(10);
                continue;
            }

            var ki = Console.ReadKey(false);
            if (ki.Key == ConsoleKey.X || ki.Key == ConsoleKey.Escape)
                break;
            byte[] buf = tg.Generate((sbyte)char.ToUpper(ki.KeyChar), duration, 6);

            if (buf != null)
            {
                Console.WriteLine("Generated {0} for {1} ({2} bytes)", ki.Key.ToString(), duration, buf.Length);
                mod.Add(buf);
            }
            else
                Console.WriteLine("not a dtmf tone");

        }
    }
}
See Also