Skip to content

Use AudioContext lookahead scheduler for MIDI playback#1

Open
jeromepotter wants to merge 1 commit intomainfrom
codex/implement-lookahead-scheduler-using-web-audio-clock
Open

Use AudioContext lookahead scheduler for MIDI playback#1
jeromepotter wants to merge 1 commit intomainfrom
codex/implement-lookahead-scheduler-using-web-audio-clock

Conversation

@jeromepotter
Copy link
Copy Markdown
Owner

Motivation

  • Replace brittle per-note setTimeout scheduling with the Web Audio clock to get microsecond-precise timing.
  • Make MIDI playback robust when the window is dragged, CPU is overloaded, or the tab is backgrounded by using the AudioContext clock.

Description

  • Replaced per-note setTimeout timers with an AudioContext-backed lookahead scheduler that uses audioContext.currentTime and performance.now() when calling midiOutput.send(message, timestamp).
  • Accumulate and sort note on/off entries into an events array and schedule them in a short lookahead loop using LOOKAHEAD_MS and SCHEDULE_AHEAD_TIME.
  • Added audioContext initialization/resume logic and new timer handles schedulerTimerId and finishTimeoutId, and updated stopPlayback to cancel those timers and send silence CCs.
  • Made playSequence asynchronous and awaited it from generateMusic to ensure scheduler setup completes before signaling success.

Testing

  • No automated tests were executed for this change.

Codex Task

@netlify
Copy link
Copy Markdown

netlify Bot commented Jan 5, 2026

Deploy Preview for text-to-midi ready!

Name Link
🔨 Latest commit 1c62555
🔍 Latest deploy log https://app.netlify.com/projects/text-to-midi/deploys/695c0bf846c1ad0008656c47
😎 Deploy Preview https://deploy-preview-1--text-to-midi.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant