Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form class="synth" id="midi" style="--synth-bgc: hsl(216, 69%, 27%);--_h:216;">
  <h2>MIDI KEYS-49</h2>
  <div id="kb49" class="kb kb--49"><button aria-label="C3" data-freq="130.8127826502993" name="midi_48" "="" style="--gcs:1" type="button>"></button>
<button aria-label="C#3" data-freq="138.59131548843604" name="midi_49" "="" style="--gcs:3" type="button>"></button>
<button aria-label="D3" data-freq="146.8323839587038" name="midi_50" "="" style="--gcs:4" type="button>"></button>
<button aria-label="D#3" data-freq="155.56349186104043" name="midi_51" "="" style="--gcs:6" type="button>"></button>
<button aria-label="E3" data-freq="164.81377845643496" name="midi_52" "="" style="--gcs:7" type="button>"></button>
<button aria-label="F3" data-freq="174.61411571650194" name="midi_53" "="" style="--gcs:10" type="button>"></button>
<button aria-label="F#3" data-freq="184.99721135581723" name="midi_54" "="" style="--gcs:12" type="button>"></button>
<button aria-label="G3" data-freq="195.99771799087463" name="midi_55" "="" style="--gcs:13" type="button>"></button>
<button aria-label="G#3" data-freq="207.65234878997256" name="midi_56" "="" style="--gcs:15" type="button>"></button>
<button aria-label="A3" data-freq="220" name="midi_57" "="" style="--gcs:16" type="button>"></button>
<button aria-label="A#3" data-freq="233.08188075904496" name="midi_58" "="" style="--gcs:18" type="button>"></button>
<button aria-label="B3" data-freq="246.94165062806206" name="midi_59" "="" style="--gcs:19" type="button>"></button>
<button aria-label="C4" data-freq="261.6255653005986" name="midi_60" "="" style="--gcs:22" type="button>"></button>
<button aria-label="C#4" data-freq="277.1826309768721" name="midi_61" "="" style="--gcs:24" type="button>"></button>
<button aria-label="D4" data-freq="293.6647679174076" name="midi_62" "="" style="--gcs:25" type="button>"></button>
<button aria-label="D#4" data-freq="311.12698372208087" name="midi_63" "="" style="--gcs:27" type="button>"></button>
<button aria-label="E4" data-freq="329.6275569128699" name="midi_64" "="" style="--gcs:28" type="button>"></button>
<button aria-label="F4" data-freq="349.2282314330039" name="midi_65" "="" style="--gcs:31" type="button>"></button>
<button aria-label="F#4" data-freq="369.99442271163446" name="midi_66" "="" style="--gcs:33" type="button>"></button>
<button aria-label="G4" data-freq="391.99543598174927" name="midi_67" "="" style="--gcs:34" type="button>"></button>
<button aria-label="G#4" data-freq="415.3046975799451" name="midi_68" "="" style="--gcs:36" type="button>"></button>
<button aria-label="A4" data-freq="440" name="midi_69" "="" style="--gcs:37" type="button>"></button>
<button aria-label="A#4" data-freq="466.1637615180899" name="midi_70" "="" style="--gcs:39" type="button>"></button>
<button aria-label="B4" data-freq="493.8833012561241" name="midi_71" "="" style="--gcs:40" type="button>"></button>
<button aria-label="C5" data-freq="523.2511306011972" name="midi_72" "="" style="--gcs:43" type="button>"></button>
<button aria-label="C#5" data-freq="554.3652619537442" name="midi_73" "="" style="--gcs:45" type="button>"></button>
<button aria-label="D5" data-freq="587.3295358348151" name="midi_74" "="" style="--gcs:46" type="button>"></button>
<button aria-label="D#5" data-freq="622.2539674441618" name="midi_75" "="" style="--gcs:48" type="button>"></button>
<button aria-label="E5" data-freq="659.2551138257398" name="midi_76" "="" style="--gcs:49" type="button>"></button>
<button aria-label="F5" data-freq="698.4564628660078" name="midi_77" "="" style="--gcs:52" type="button>"></button>
<button aria-label="F#5" data-freq="739.9888454232689" name="midi_78" "="" style="--gcs:54" type="button>"></button>
<button aria-label="G5" data-freq="783.9908719634986" name="midi_79" "="" style="--gcs:55" type="button>"></button>
<button aria-label="G#5" data-freq="830.6093951598903" name="midi_80" "="" style="--gcs:57" type="button>"></button>
<button aria-label="A5" data-freq="880" name="midi_81" "="" style="--gcs:58" type="button>"></button>
<button aria-label="A#5" data-freq="932.3275230361799" name="midi_82" "="" style="--gcs:60" type="button>"></button>
<button aria-label="B5" data-freq="987.7666025122483" name="midi_83" "="" style="--gcs:61" type="button>"></button>
<button aria-label="C6" data-freq="1046.5022612023945" name="midi_84" "="" style="--gcs:64" type="button>"></button>
<button aria-label="C#6" data-freq="1108.7305239074883" name="midi_85" "="" style="--gcs:66" type="button>"></button>
<button aria-label="D6" data-freq="1174.6590716696303" name="midi_86" "="" style="--gcs:67" type="button>"></button>
<button aria-label="D#6" data-freq="1244.5079348883235" name="midi_87" "="" style="--gcs:69" type="button>"></button>
<button aria-label="E6" data-freq="1318.5102276514797" name="midi_88" "="" style="--gcs:70" type="button>"></button>
<button aria-label="F6" data-freq="1396.9129257320155" name="midi_89" "="" style="--gcs:73" type="button>"></button>
<button aria-label="F#6" data-freq="1479.9776908465378" name="midi_90" "="" style="--gcs:75" type="button>"></button>
<button aria-label="G6" data-freq="1567.981743926997" name="midi_91" "="" style="--gcs:76" type="button>"></button>
<button aria-label="G#6" data-freq="1661.2187903197805" name="midi_92" "="" style="--gcs:78" type="button>"></button>
<button aria-label="A6" data-freq="1760" name="midi_93" "="" style="--gcs:79" type="button>"></button>
<button aria-label="A#6" data-freq="1864.6550460723597" name="midi_94" "="" style="--gcs:81" type="button>"></button>
<button aria-label="B6" data-freq="1975.533205024496" name="midi_95" "="" style="--gcs:82" type="button>"></button>
<button aria-label="C7" data-freq="2093.004522404789" name="midi_96" "="" style="--gcs:85" type="button>"></button></div>
</form>
<p><small>Web MIDI API Demo. Attach a MIDI-keyboard and refresh. <strong>Not supported in Safari.</strong></small></p>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Russo+One&display=swap');
.kb {
  block-size: var(--_bs, 8em);
  display: grid;
  grid-column-gap: 1px;
  grid-template-columns: repeat(var(--_r, 156), 1fr);
  grid-template-rows: repeat(5, 1fr);
}
.kb [aria-label] {
  --_focus-bxsh: inset 0 0 50px #fff, inset 20px 0 80px hsl(var(--_h,0), 70%, 35%), inset -20px 0 80px hsl(var(--_h,0), 100%, 50%);
  --_hover-bg: linear-gradient(to bottom, #FFF 0%, hsl(0, 0%, 90%) 100%);
  background-color: var(--_bgc, #FFF);
  border: 0;
  border-radius: 0 0 3px 3px;
  grid-column: var(--gcs) / span var(--_csp, 3);
  grid-row: 1 / span var(--_rsp, 5);
  position: relative;
}
.kb [aria-label*="#"] { 
  --_csp: 2;
  --_focus-bxsh: inset 0 0 50px hsl(var(--_h,0), 25%, 35%), inset 20px 0 80px hsl(var(--_h,0), 70%, 35%), inset -20px 0 80px hsl(var(--_h,0), 100%, 50%);
  --_hover-bg: linear-gradient(to top, #000 0%, hsl(0, 0%, 25%) 100%);
  --_rsp: 3;
  background-color: var(--_bgcs, #000);
  z-index: 1;
}
.kb [aria-label]:not(.keydown):hover { background: var(--_hover-bg); }
.kb [aria-label]:not(.keydown):focus-visible { box-shadow: var(--_focus-bxsh); outline: none; }
.synth {
  background: var(--synth-bgc, linear-gradient(215deg, hsl(0, 0%, 25%), hsl(0, 0%, 15%), hsl(0, 0%, 5%)));
  border-radius: .33em .33em .75em .75em;
  color: var(--synth-c, #FFF);
  font-family: var(--synth-ff, 'Russo One', sans-serif);
  inline-size: max-content;
  padding: .5em 1em 1em 1em;
}
.synth h2 {
  font-size: var(--synth-h-fz, 1.33em);
  font-weight: var(--synth-h-fw, 700);
  margin: 0 0 .5em 0;
}

/* modifiers */
.kb--61 { --_r: 108; }
.kb--49 { --_r: 87; }
.kb--32 { --_bs: 6em; --_r: 57;}
.kb--25 { --_bs: 6em; --_r: 45; }

.keydown {
  --_l: calc((var(--v, 1) * 0.394%));
  --_bgc: hsla(var(--_h), 50%, calc(95% - var(--_l)));
  --_bgcs: var(--_bgc);
}
.keydown::after {
  background-color: #000;
  block-size: .5em;
  border-radius: 50%;;
  content: "";
  display: block;
  inline-size: .5em;
  inset-block-end: 1em;
  inset-inline-start: 50%;
  position: absolute;
  transform: translateX(-50%);
}
small { font-family: ui-sans-serif, system-ui, sans-serif; }
              
            
!

JS

              
                /* midi.js */
function enableMIDI(element) {
	const onMIDISuccess = (midiAccess) => {
		for (var input of midiAccess.inputs.values()) input.onmidimessage = getMIDIMessage;
	}
	const getMIDIMessage = message => {
		const [command, note, velocity] = message.data;
		switch (command) {
			case 144: // on
				if (velocity > 0) {
					const event = new CustomEvent('noteon', { detail: { note, velocity }});
					element.dispatchEvent(event)
				}
			break;
			case 128: // off
				const event = new CustomEvent('noteoff', { detail: { note }});
				element.dispatchEvent(event)
			break;
		}
	}
	if (navigator.requestMIDIAccess) {
		navigator.requestMIDIAccess().then(onMIDISuccess, () => console.log('Could not access your MIDI devices.'));
	}
}

/* audio.js */
const audioCtx = new (window.AudioContext || window.webkitAudioContext)
const gainNode = audioCtx.createGain()
const notemap = new Map();
gainNode.connect(audioCtx.destination);

function createOscillator(freq) {
  const oscNode = audioCtx.createOscillator()
  oscNode.type = 'triangle'
  oscNode.frequency.value = freq
  oscNode.connect(gainNode)
  return oscNode
}

function noteon(key) {
  if (!key.classList.contains('keydown')) {
    gainNode.gain.value = 0.33
    notemap.set(key.name, createOscillator(key.dataset.freq))
    notemap.get(key.name).start(0)
    key.classList.add('keydown')
  }
}

function noteoff(key) {
  key.classList.remove('keydown')
  const oscNode = notemap.get(key.name)
  if (oscNode) oscNode.stop(0)
  notemap.delete(key.name)
}


/* init */
enableMIDI(midi);

midi.addEventListener('noteon', (event) => {
  const note = midi.elements[`midi_${event.detail.note}`]
  note.style.setProperty('--v', event.detail.velocity)
  noteon(note, [{freq: note.dataset.freq}])
})

midi.addEventListener('noteoff', (event) => {
  const note = midi.elements[`midi_${event.detail.note}`]
  noteoff(note)
})

midi.addEventListener('submit', event => event.preventDefault())

const keys = midi.querySelectorAll('button');
keys.forEach(key => {
  key.addEventListener('pointerdown', event => {
    noteon(event.target, [{freq: event.target.dataset.freq}])
  })
  key.addEventListener('pointerup', event => { noteoff(event.target) })
  key.addEventListener('pointerleave', event => { noteoff(event.target) })
})
              
            
!
999px

Console