nycki.net/static/abc/index.html
nycki 968ec59529
All checks were successful
/ build (push) Successful in 35s
remove clicking
2025-07-06 19:31:24 -07:00

50 lines
1.3 KiB
HTML

<meta name="viewport" content="width=device-width">
<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="
const A=new AudioContext();
// tempo
r=.4;
e='exponentialRampToValueAtTime';
// play note with pitch and duration
p=(h,t,u)=>{
a=A.createOscillator();
const b=A.createGain();
a.connect(b);
b.connect(A.destination);
a.frequency.value=h;
a.start(t*r);
b.gain[e](1,t*r+.1);
b.gain[e](0.001,u*r);
};
// read text from input
v=window.location.search.slice(1)||f.value;f.value=v;
for(i=t=o=m=0,l=1;c=v[i++],d=v[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>