Skip to content
Sign upLog in
← Back to Community

A geeky way to say “Happy Birthday”

Profile icon
dtinth

It’s my friend’s birthday. I plan to send him some JS code that when pasted into DevTools plays the happy b’day song. This is the result. (Solo project.)

  • I used Web Audio API to create a simple music box.
  • Developed this in repl.it by creating a “Preview” button to let me test the code to copy.
  • Melody and chords are encoded into CJK characters to “disguise” the Happy Birthday song. The first 47 characters are the melody, the rest are the (broken) chords.
  • To not pollute the global scope, while being concise, local variables are declared as default parameters. e.g. ((paramX, paramY, localA=1, localB=2) => …).
  • The t(f, b) function plays a single note of frequency f at time b.
  • Each character is processed in forEach loop. The index is used to determine when to play the note.

HBD

@phoomparin
!

Voters
Profile icon
Ericka-JaneJane
Profile icon
IlhemBensofta
Profile icon
AdrianHall
Profile icon
EC_CeezNY
Profile icon
maciejmatu
Profile icon
RahulKumar26
Profile icon
phoomparin
Profile icon
dtinth
Comments
hotnewtop
Profile icon
AdrianHall

Oh wow, that's so cool! You can just send them an email telling them to paste

((c=new AudioContext(),n=[...'畋畋畍甯畋甯畐甯畏甯甯甯畋畋畍甯畋甯畒甯畐甯甯甯畋畋畗甯畔甯畐畐畏甯畍甯甯甯畕畕畔甯畐甯畒甯畐甯甸甼甿町甽甿町甽甿甸甼甿甸甿畂甸甽畁畄町甿畃甼甿畄'].map(a=>a.charCodeAt(0)-30000),z=c.currentTime,t=(f,b,o=c.createOscillator(),g=c.createGain(),l=0.5)=>(o.frequency.value=f,o.connect(g),g.connect(c.destination),g.gain.setValueAtTime(0.5,z+b),g.gain.linearRampToValueAtTime(0,z+b+l),o.start(z+b),o.stop(z+b+l)))=>n.forEach((c,i)=>~c&&t(220*2**((c)/12),(i>=47?(i-47)*2:i+(i%2)/6)/4)))()

into the console after 'inspect element' and they'll get a happy birthday! Awesome job!

Profile icon
phoomparin

What a pleasant surprise! Love this <3