JSFiddle - React, Tailwind, and code Playground

by talyian

JavaScript

context = new AudioContext;

// immediately starts a note
function playNote(frequency, duration) {
  var oscillator = context.createOscillator();
  oscillator.frequency.value = frequency;
  oscillator.connect(context.destination);
  oscillator.start(0);
  setTimeout(function() {oscillator.stop();}, duration);
}

function playNoteSequence(notes, time = 0) {
  firstNote = notes.splice(0, 1);
  if (firstNote) {
    playNote(firstNote[0].f, firstNote[0].d);
    setTimeout(function() { playNoteSequence(notes); }, firstNote[0].d);
  }
}

playNoteSequence([
  {f: 250, d: 400},
  {f: 220, d: 400}, 
  {f: 200, d: 400}, 
  {f: 220, d: 400}, 
  {f: 250, d: 400},
  {f: 250, d: 400},
  {f: 250, d: 1400},
])