// Morse alphabet from
// https://stackoverflow.com/questions/26059170/using-javascript-to-encode-morsecode
var alphabet = {
'a': '.-', 'b': '-...', 'c': '-.-.', 'd': '-..',
'e': '.', 'f': '..-.', 'g': '--.', 'h': '....',
'i': '..', 'j': '.---', 'k': '-.-', 'l': '.-..',
'm': '--', 'n': '-.', 'o': '---', 'p': '.--.',
'q': '--.-', 'r': '.-.', 's': '...', 't': '-',
'u': '..-', 'v': '...-', 'w': '.--', 'x': '-..-',
'y': '-.--', 'z': '--..', ' ': '/',
'1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..',
'9': '----.', '0': '-----',
}
ditty.bpm = 60*20;
var playtime = {'.': 1, '-': 3, '/': 0, ' ': 0};
var sleeptime = {'.': 2, '-': 4, '/': 6, ' ': 2};
var frequency = 750;
var str = "Dittytoy: Create your generative music online using a simple Javascript API";
//var str = "The quick brown fox jumped over the lazy dog";
str.split('') // Transform the string into an array: ['T', 'h', 'i', 's'...
.map(function(e){ // Replace each character with a morse "letter"
return alphabet[e.toLowerCase()] || ''; // Lowercase only, ignore unknown characters.
})
.join(' ') // Recombine these into a single array by adding spaces
.split('')
.map(function(c){
if(playtime[c]) {
sine.play(hz_to_midi(frequency), {duration:playtime[c], attack:0.001, release:0.001, amp:0.3});
}
sleep(sleeptime[c]);
});