Create a musical score. Use select options to add musical notes. "_" means a silence. "END" will stop the song, even if there are notes remaining. Use Play and Stop buttons...
_='$@n){turn d.getEmGtById(nN{n"`=1;for(Y0;e<V.ngth;e++)n=`\'< ~_f[e]+\'">~V[e]+">";n=\'< ~n`\'" changeN">~`" >"Gd"PN(n){YWcaJOscillator;^squa",fquGcy.=n,cnect(WdeKa),Kart,K(WcurntTime+nsPP{Y@ni);fJxt,-1!=f&&ni<nn?(_p.push(n),PN(f)i+=1):(,@"F")_p.jo()P{Z,=!0i=1,II=setPPms{moveAttribuJ(""),carII)}d=documGt,II;b\'<Ky>*{ft-size:2X;height:4X} {width:8X}Ky%S,S=@"S");V=["_DoRéURéMiUMiFaSolUSolLaULaSiUSiDoEND"],_f=[QQ262,277,294,311,33Q349,37Q392,415,44Q466,494,523,-1],Z;nms=400s=nms/1e3n=0=\'PP"/>SSt"/%Fbeg"),N;aC=new(#||#webkit);)}$",",S.sertAdjacGtHTML("befo=s[ edIndex]." ^butt" idtivar sectvaluePStAudioCJxt.nerHTML=" click,n()inInJrval(">div>\'le="@"PP").onree.Playdisabd<putPop#wdow.$func %><div@g(GenJteKstQ0,UbV_nFWaC.X0pxYe=Z_p=[]^type`n+~\'+</';for(Y in $='~`^ZYXWVUQKJG@%$# ')with(_.split($[Y]))_=join(pop());eval(_)
Xz0nJEBuKXsadHVybiBkLmdldEUWbUd0QnlJZChuAU4Se24XIhFgPTE7Zm9yKFkwO2U8Vi4Wbmd0aDtlKyspbj1gXCc8HwcgCxd+X2ZbZV0rXCciPn5WW2VdKyJ/Hwc+IjtuPVwnPAkGfm5gXCciIBljaGFuZ2UXThIiPn5gIn8JPiIDR2QiEQFQTihuKXtZV2MaYUpPc2NpbGxhdG9yEjsbXnNxdWEaIiwbZhpxdUdjeS4LPW4sG2MZbmVjdChXZGVLE2EHKSwbS2FydBIsG0sfKFdjdXIabnRUaW1lK25zAVBQEntZQG5pKTtmBAsRBEp4dCwtMSE9ZiYmbmk8bm4/KF9wLnB1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IN SCORE" by Patrice CHASSAING
* @description Create a musical score. Use select options to add musical notes. "_" means a silence. "END" will stop the song, even if there are notes remaining. Use Play and Stop buttons...
* @file javascript into shim.html
* @version 2018-02-25-11-55-00
* @date 2018/02/25
* @author Patrice CHASSAING
* @running Firefox ESR 52.6.0, running on Debian 8.10
* @running Chromium 57.0.2987.98 Built on 8.7, running on Debian 8.10
* @copyright Me
* @brief JS1k 2018 demo contest
* @compressedsize 1021 Bytes
*/
//Declaration
var d = document
, II;//Timer variables
// getElementById function
function g(U) { return d.getElementById(U) }
/* MAIN */
//Set up the scene
b.innerHTML = '<style>*{font-size:20px;height:40px}select{width:80px}</style><div id="S"></div>';
S=g('S');
//Here a good page that associates notes and frequencies : http://jeanjacques.dialo.free.fr/frequenc.htm
var _nF = ['', '_', 'Do', 'Réb', 'Ré', 'Mib', 'Mi', 'Fa', 'Solb', 'Sol', 'Lab', 'La', 'Sib', 'Si', 'Do', 'END']//Notes - Could be ABC notation ...
, _f = [ 0, 0, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, -1]//Notes frequencies Hz
, _p = [];//Musical score
nms = 400; //play note milliseconds
ns = nms/1000; //play note seconds
nn = 0;//Notes n°
function N(selectedIndex){
n='';
nn+=1
for (var i = 0; i < _nF.length; i++) {
//if (selectedIndex==_nE[i]) {sel='selected'} else {sel=''};//'+sel+'
n=n+'<option value="'+_f[i]+'">'+_nF[i]+'</option>';//+' '+_nE[i]
}
n='<select id="'+nn+'" onchange="N()">'+n+'</select>';
S.insertAdjacentHTML('beforeend', n);
}
n='<input id="PP" type="button" value="Play" onclick="PPlay()"/><input id="PS" type="button" value="Stop" onclick="PStop()"/><div id="F"></div>';
S.insertAdjacentHTML('beforebegin', n);
N();
// create web audio api context
var aC = new (window.AudioContext || window.webkitAudioContext)();
function PN(f){
// create Oscillator node
var o = aC.createOscillator();
o.type = 'square';
o.frequency.value=f;
o.connect(aC.destination);
o.start();
o.stop(aC.currentTime+ns);
}
function PP(){
var e = g(ni);
f = e.options[e.selectedIndex].value; // value in hertz
n = e.options[e.selectedIndex].text; // note value
if (f!=-1 && ni<nn){
_p.push(n);
PN(f);
ni=ni+1;
}
else {PStop();g('F').innerHTML= _p.join(',')}
}
function PPlay(){
_p = [];
g('PP').disabled=true;
ni=1;
II = setInterval(PP, nms);
}
function PStop(){g('PP').removeAttribute("disabled");clearInterval(II);}
//That's all...