Examples testtones js

From FreeSWITCH Wiki
Jump to: navigation, search
/** Original code by anthm, comments by pist, but it's mostly self explanatory :-) **/

use("TeleTone");  // Load the TeleTone module

session.answer();  // answer our session

console_log("I got the call! \n");  // log to console

var tts = new TeleTone(session);

// define some tones
tts.addTone("d", 350.0, 440.0, 0.0);
tts.addTone("r", 440.0, 480.0, 0.0);
tts.addTone("z", 1400.0, 2060.0, 2450.0, 2600.0, 0.0);
tts.addTone("u", 400.0, 450.0, 0.0);

var RESET = "v=2000;>=0;+=0;";
var BONG = RESET + "v=4000;>=0;+=2;#(60,0);v=2000;d(940,0)";
var ATTN = RESET + "L=4;z(100,100)";
var SIT = RESET + "%(274,0,913.8);%(274,0,1370.6);%(380,0,1776.7)";
var UK_RING = RESET + "L=2;u(400,200);u(400,2200)";
var US_RING = RESET + "r(2000,4000)";
var US_DIAL = RESET + "d(4000,0)";

// call back function that keeps check on length of digits
function mycb (session, type, data, arg) {

	if (type == "dtmf") {
		arg.digits += data.digit;
		if (arg.digits.length >= 3) {
			return false;
		}
	}
}

var dtmf = new Object();

while(session.ready()) {
	dtmf.digits = "";

    console_log("enter 3 digit code\n");
    mycb(dtmf);
	if (dtmf.digits.length < 3) {  // as long as caller has not entered enough digits, keep collecting (until timeout of 5000)
		session.collectInput(mycb, dtmf, 5000);
	}

// once we have 3 digits, match and run appropriate section
    if (dtmf.digits == "100") {
		console_log("means ringing sound\n");
		for (x = 0 ; x < 2 ; x++) {
			tts.generate(US_RING);
		}
    } else if (dtmf.digits == "101") {
		console_log("means dialtone\n");
		tts.generate(US_DIAL);
    } else if (dtmf.digits == "102") {
		console_log("means bong\n");
		tts.generate(BONG);
    } else if (dtmf.digits == "103") {
		console_log("means attention\n");
		for (x = 0 ; x < 4 ; x++) {
			tts.generate(ATTN);
		}
    } else if (dtmf.digits == "104") {
		console_log("means SIT\n");
		tts.generate(SIT);
    } else if (dtmf.digits == "105") {
		console_log("means UK Ring\n");
		for (x = 0 ; x < 2 ; x++) {
			tts.generate(UK_RING);
		}
    } else if (dtmf.digits == "200") {
		console_log("means bye bye\n");
		break;
    } else {
		console_log("invalid code, try again\n");
    }

 }

See Also