nycki.net/static/abc/index.html
nycki 5e60b034b5
All checks were successful
/ build (push) Successful in 39s
now with sheet music
2025-07-06 18:26:33 -07:00

45 lines
1.1 KiB
HTML

<textarea id="f">
@i-e!ce!g+ce!dc-bgb+dgfe!de!
@ice!g+ce!dcdc-bagfe!d
@ie!ce!g+ce!dc-bgb+dgfe!d
@i+e!ce!g+ce!dcdc-bagfe!d
@i+e!c-ge!c++c-ge!a!--fa!+cfa!+ce!
@i+d-b!fd-b!++b!fdg--e!gb!+dgb!+d
@i+ge!dc-ge!dh.c
</textarea><br>
<button onclick="
A=new AudioContext();
// tempo
r=.4
// play note with pitch and duration
p=(h,t,u)=>{
a=A.createOscillator();
a.connect(A.destination);
a.frequency.value=h;
a.start(t*r);
a.stop(u*r);
};
// read text from input
f=f.value||window.location.search;
for(i=t=o=m=0,l=1;c=f[i++],d=f[i];){
// https://chriskallen.com/zzt/zztoop.php#sound
// look-ahead for accidentals
m=d=='#'?1:d=='!'?-1:0;
s=n=>()=>p(440*Math.pow(2,(n+m+o)/12),t,t+=l);
q=n=>()=>l=n;
x=()=>t+=l;
g=[
// c major scale
s(-9),s(-7),s(-5),s(-4),s(-2),s(0),s(2),
// octaves
()=>o-=12,()=>o+=12,
// change duration
q(4),q(2),q(1),q(.5),q(.25),q(.125),q(l/3),q(l*1.5),
// reset
()=>(o=0,l=.125),
// rest, sound effects not implemented
x,x,x,x,x,x,x,x,x,x,
]['cdefgab-+whqist3.@x012456789'.indexOf(c.toLowerCase())];
g&&g();
}
">#play</button>