This uses Audio::Wav to create a WAV file of DTMF tones. If you are running Windows, you have the option of playing the file after creating it (just add a second command line parameter of 1). I found the required calculations here and modified the example script listed in the Audio::Wav docs.

Usage: $0 [DTMF tones] [play]
i.e. dtmf.pl 18005551212 1

#!/usr/bin/perl -w use strict; use Audio::Wav; my @tones; my %dtmf = ( '1' => [ 697, 1209 ], '2' => [ 697, 1336 ], '3' => [ 697, 1477 ], 'A' => [ 697, 1633 ], '4' => [ 770, 1209 ], '5' => [ 770, 1336 ], '6' => [ 770, 1477 ], 'B' => [ 770, 1633 ], '7' => [ 852, 1209 ], '8' => [ 852, 1336 ], '9' => [ 852, 1477 ], 'C' => [ 852, 1633 ], '*' => [ 941, 1209 ], '0' => [ 941, 1336 ], '#' => [ 941, 1477 ], 'D' => [ 941, 1633 ], ); if (@ARGV > 0) { @tones = split'',$ARGV[0]; } else { @tones = sort keys %dtmf; } my $play = $ARGV[1] || 0; my $sample_rate = 8000; my $bits_sample = 8; my $num_channels = 1; my $pi = 4 * atan2 1, 1; my $duration = 0.5 * $sample_rate; my $wav = new Audio::Wav; my $details = { 'bits_sample' => $bits_sample, 'sample_rate' => $sample_rate, 'channels' => $num_channels, }; my $write = $wav -> write( 'dtmf.wav', $details ); for my $tone (@tones) { my @hz = map { 2 * $pi * $_ } @{$dtmf{$tone}}; add_tone(@hz); } $write -> finish(); Win32::Sound::Play('dtmf.wav') if $play && $main::can_play; sub add_tone { my (@hz) = @_; for my $pos ( 0 .. $duration ) { my $time = $pos / $sample_rate; my $val = 63 * sin($time * $hz[0]) + 63 * sin($time * $hz[1]); $write -> write( $val ); } } BEGIN { if ($^O =~ /MSWin32/) { require Win32::Sound; our $can_play = 1; } }

Update: Moved some of the calculations outside of the subroutine. Adjusted the calculation of pi.

Updated much, much later: I really don't know if the tones produced are indeed at the right frequencies... It sounds right to me but I'm no expert.

Third update: Fixed the formula as pointed out in arhuman's reply.


In reply to DTMF Tone Generator by Mr. Muskrat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.