Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created March 31, 2017 02:42
Show Gist options
  • Save gjyoung1974/83dabb9cdd49d42ab1e6b39b2996f6d6 to your computer and use it in GitHub Desktop.
Save gjyoung1974/83dabb9cdd49d42ab1e6b39b2996f6d6 to your computer and use it in GitHub Desktop.
//Java using the Javax JSP stuff:
//************************************************
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment