Sign in to follow this  
Followers 0
p0id

PHP dtmf generator

2 posts in this topic

http://aggemam.dk/code/dtmf

source:

http://aggemam.dk/scripts/dtmf.phps

Pretty cool...definitely works for me when I hold my phone up to my computer speakers aside from the fact that the last digit is not picked up and has to be manually dialed. I will play around with source code... Anyone have any other ideas as to what can be done with this(the color red comes to mind)?

Edited by p0id
0

Share this post


Link to post
Share on other sites

Please note this code is for my own personal education, I will not and cannot use it malevolently.

I dont know what Im doing when it comes to programming with sounds/frequencies as data but Im trying to learn a little bit.

I attempted to port a java app from the comments section at http://www.phonelosers.org/redbox/ to php. The app attempts to simulate an oldschool redbox. I used the dtmf generator originally posted as a starting point.

the original java code from http://www.phonelosers.org/redbox/:


package org.drg00n.util.dtmf;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;

/*Implement a DTMF dialer with javax’s DSP
* Tone tables are in tones.java
*/

public class DTMF {

public static void generateTones(float hz1, float hz2, int msecs, int volume)
throws Exception {
float frequency = 44100.0F;
int samplesize = 8;
int channels;
boolean signed = true;
boolean bigendian = false;
byte[] buf;
double ttpi = (2.0 * Math.PI);
AudioFormat format;
buf = new byte[2];
channels = 1;
format = new AudioFormat(frequency, samplesize, channels, signed,
bigendian);
SourceDataLine sdl = AudioSystem.getSourceDataLine(format);
sdl.open(format);
sdl.start();
for (int i = 0; i < msecs * frequency / 1000; i++) {

double angle = i / (frequency / hz1) * ttpi;
double angle2 = i / (frequency / hz2) * ttpi;
buf[0] = (byte) (((Math.sin(angle)) + (Math.sin(angle2))) * 10);
sdl.write(buf, 0, 1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
}

********************************************
then create a class for the coins using your data on the frequencies and times for each coin:
********************************************

package org.drg00n.util.dtmf;

public class payphone {

public static void qaurter() throws Exception {
DTMF.generateTones(1700, 2200, 33, 100);
DTMF.generateTones(0, 0, 33, 100);
DTMF.generateTones(1700, 2200, 33, 100);
DTMF.generateTones(0, 0, 33, 100);
DTMF.generateTones(1700, 2200, 33, 100);
DTMF.generateTones(0, 0, 33, 100);
DTMF.generateTones(1700, 2200, 33, 100);
DTMF.generateTones(0, 0, 33, 100);
DTMF.generateTones(1700, 2200, 33, 100);
DTMF.generateTones(0, 0, 33, 100);
}

public static void dime() throws Exception {
DTMF.generateTones(1700, 2200, 66, 100);
DTMF.generateTones(0, 0, 66, 0);
DTMF.generateTones(1700, 2200, 66, 100);
}

public static void nickle() throws Exception {
DTMF.generateTones(1700, 2200, 66, 100);
}
}

************************************************
then you call the coin you want and it beeps out of the sound card:

a main class to beep the coin tones:
************************************

package org.drg00n.util.dtmf;

public class dialer {

public static void main(String[] args) {

/*
* Dial some DTMF Tones:
*/

try {

// payphone.dime();
// payphone.nickle();
payphone.qaurter();

} catch (Exception e) {
e.printStackTrace();
}
}

}

credit goes to someone named drg00n for the java..

the PHP code:


<?


$output = '';

$signal_length = 33;
$pause_length = 33;
$frequency = 44100.0;
$lowHZ = 1700;
$hiHZ = 2200;
$sample_rate = 8000;

function generateDialTone ($signal_length, $pause_length, $frequency, $lowHZ, $hiHZ, $sample_rate)
{
$ttpi = 2.0 * pi();
GLOBAL $output;
$signal = 1;
for ($j = 0; $j < $signal_length * $frequency / 1000; $j++)
{
$angle1 = $j / ($frequency / $lowHZ) * $ttpi;
$angle2 = $j / ($frequency / $hiHZ) * $ttpi;
$buf = ((sin($angle1) + sin($angle2)) * 10);
$output .= chr($buf);


}


}
$output .= str_repeat("\0", $break_length / 1000 * $sample_rate);


//attempt to redbox a quarter
generateDialTone (33,33,$frequency,1700,2200, 8000);
generateDialTone ($signal_length,$pause_length,$frequency,$lowHZ,$hiHZ, $sample_rate);
generateDialTone ($signal_length,$pause_length,$frequency,$lowHZ,$hiHZ, $sample_rate);
generateDialTone ($signal_length,$pause_length,$frequency,$lowHZ,$hiHZ, $sample_rate);
generateDialTone ($signal_length,$pause_length,$frequency,$lowHZ,$hiHZ, $sample_rate);

//make sure that all output contains at least 1 byte excl. the header
if (strlen($output) == 0) {
$output = "\0";
}


function encode_int($n) {
$s = '';
for ($i = 3; $i >= 0; $i--) {
$j = pow(256, $i);
$s .= chr(floor($n / $j));
if ($n > $j) $n -= $j;
}
return $s;
}


//description of snd/au format available at http://www.wotsit.org/search.asp?s=music
$output = ".snd" . //"magic number"
"\0\0\0\x18" . //data offset
encode_int(strlen($output)) . //data size (0xffffffff = unknown)
"\0\0\0\2" . //encoding (2 = 8-bit linear PCM, 3 = 16-bit linear PCM)
encode_int($sample_rate) . //sample rate
"\0\0\0\2" . //channels
$output;

header('Content-Length: ' . strlen($output));
header('Content-Type: audio/basic');
header('Content-Disposition: filename="' . $HTTP_GET_VARS['n'] . '"');



print $output;
?>

this is a flash file containing all of the proper redbox tones http://www.phonelosers.org/zips/redbox.swf?phpMyAdmin=Am5GtwN9B1d5U07871O3b%2CHSiG8&phpMyAdmin=10c4a8582act2e83b170

according to phone losers the proper dial tone is 1700MHZ + 2200MHZ for US phones and a variation on the number of milliseconds of the duration of the tone and times the tone is beeped corresponding to whether a "nickel", "quarter", or "dime" was put in.

Attached to my post is the .snd output of the PHP.. judging by the difference between the real thing and the .snd file generated there seems to be something wrong with my code. Does anyone know if the original java version works properly? anyone care to try compiling it? If you find it works properly in java, any ideas on where I went wrong porting it to PHP?

I guess this is kinda pointless from a practical perspective, but any input or pointers in the right direction are appreciated.

testTone.zip

Edited by p0id
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0