Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Created April 4, 2024 02:30
Show Gist options
  • Save bitmvr/dd7dba2c0a5230ed271ab60cb45b157c to your computer and use it in GitHub Desktop.
Save bitmvr/dd7dba2c0a5230ed271ab60cb45b157c to your computer and use it in GitHub Desktop.
Javascript Oscillation / DTMF Songs
#!/usr/bin/env node
var dtmf = {
"0":{ "hf": "1336", "lf": "941" },
"1":{ "hf": "1209", "lf": "697" },
"2":{ "hf": "1336", "lf": "697" },
"3":{ "hf": "1477", "lf": "697" },
"4":{ "hf": "1209", "lf": "770" },
"5":{ "hf": "1336", "lf": "770" },
"6":{ "hf": "1477", "lf": "770" },
"7":{ "hf": "1209", "lf": "852" },
"8":{ "hf": "1336", "lf": "852" },
"9":{ "hf": "1477", "lf": "852" },
"*":{ "hf": "1209", "lf": "941" },
"#":{ "hf": "1477", "lf": "941" },
"A":{ "hf": "1633", "lf": "697" },
"B":{ "hf": "1633", "lf": "770" },
"C":{ "hf": "1633", "lf": "852" },
"D":{ "hf": "1633", "lf": "941" }
}
async function playDTMF(key, duration){
highFrequency = parseInt(dtmf[key].hf);
lowFrequency = parseInt(dtmf[key].lf);
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillatorHigh = audioCtx.createOscillator();
var oscillatorLow = audioCtx.createOscillator();
oscillatorHigh.frequency.setValueAtTime(highFrequency, audioCtx.currentTime);
oscillatorLow.frequency.setValueAtTime(lowFrequency, audioCtx.currentTime);
oscillatorHigh.connect(audioCtx.destination);
oscillatorLow.connect(audioCtx.destination);
oscillatorHigh.start();
oscillatorLow.start();
var stopTime = audioCtx.currentTime + duration;
oscillatorHigh.stop(stopTime);
oscillatorLow.stop(stopTime);
await new Promise((resolve) => setTimeout(resolve, duration * 1500));
}
function rest(duration) {
return new Promise(resolve => setTimeout(resolve, duration * 1000));
}
short = 0.2
long = 0.4
/*
------------------------------------------------
Mary Had A Little Lamb
------------------------------------------------
await playDTMF("3", short);
await playDTMF("2", short);
await playDTMF("1", short);
await playDTMF("2", short);
await playDTMF("3", short);
await playDTMF("3", short);
await playDTMF("3", long);
await playDTMF("2", short);
await playDTMF("2", short);
await playDTMF("2", long);
await playDTMF("3", short);
await playDTMF("9", short);
await playDTMF("9", long);
await playDTMF("3", short);
await playDTMF("2", short);
await playDTMF("1", short);
await playDTMF("2", short);
await playDTMF("3", short);
await playDTMF("3", short);
await playDTMF("3", short);
await playDTMF("3", short);
await playDTMF("2", short);
await playDTMF("2", short);
await playDTMF("3", short);
await playDTMF("2", short);
await playDTMF("1", long);
*/
/*
------------------------------------------------
Frere Jacques
------------------------------------------------
*/
await playDTMF("1", short);
await playDTMF("2", short);
await playDTMF("3", short);
await playDTMF("1", short);
await playDTMF("1", short);
await playDTMF("2", short);
await playDTMF("3", short);
await playDTMF("1", short);
await playDTMF("3", short);
await playDTMF("6", short);
await playDTMF("9", short);
await rest(0.4);
await playDTMF("3", short);
await playDTMF("6", short);
await playDTMF("9", short);
await rest(0.4);
await playDTMF("9", short);
await playDTMF("#", short);
await playDTMF("9", short);
await playDTMF("6", short);
await playDTMF("3", short);
await playDTMF("1", short);
await playDTMF("9", short);
await playDTMF("#", short);
await playDTMF("9", short);
await playDTMF("6", short);
await playDTMF("3", short);
await playDTMF("1", short);
await playDTMF("7", short);
await playDTMF("1", short);
await playDTMF("7", short);
await rest(0.4);
await playDTMF("7", short);
await playDTMF("1", short);
await playDTMF("7", short);
await rest(0.2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment