-
Notifications
You must be signed in to change notification settings - Fork 28
add save-to-WAV button using OfflineAudioContext #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||
| import { soundFiles } from '../constants/config' | ||||||||||||
|
|
||||||||||||
| export async function renderSequenceToWav({ sequence, BPM, totalSteps, totalBeats }) { | ||||||||||||
| const sampleRate = 44100 | ||||||||||||
| const numChannels = 2 | ||||||||||||
| const tailSeconds = 1 | ||||||||||||
| const durationSeconds = (60 / BPM) * totalBeats + tailSeconds | ||||||||||||
| const secondsPerStep = ((60 / BPM) * totalBeats) / totalSteps | ||||||||||||
|
|
||||||||||||
| const offlineCtx = new OfflineAudioContext( | ||||||||||||
| numChannels, | ||||||||||||
| Math.ceil(durationSeconds * sampleRate), | ||||||||||||
| sampleRate | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| const uniquePaths = [...new Set(sequence.trackList.map(t => soundFiles[t.soundFile]))] | ||||||||||||
| const decoded = {} | ||||||||||||
| await Promise.all( | ||||||||||||
| uniquePaths.map(async (path) => { | ||||||||||||
| const res = await fetch(path) | ||||||||||||
|
||||||||||||
| const res = await fetch(path) | |
| const res = await fetch(path) | |
| if (!res.ok) { | |
| throw new Error(`Failed to fetch audio file "${path}": ${res.status} ${res.statusText}`) | |
| } |
Copilot
AI
Apr 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses the promise-returning form of decodeAudioData, but elsewhere in the codebase audio decoding is wrapped with the callback form (likely for Safari/WebKit compatibility). To avoid export breaking on browsers where decodeAudioData doesn't return a promise, use a small wrapper (or reuse the existing pattern) that supports the callback signature.
Copilot
AI
Apr 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataView#setInt16 will coerce the float value here, which can truncate inconsistently across engines. Convert to a proper int (e.g., Math.round(...)) before writing so PCM values are stable and match expected WAV encoding.
| view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true) | |
| const pcmSample = Math.round(s < 0 ? s * 0x8000 : s * 0x7fff) | |
| view.setInt16(offset, pcmSample, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OfflineAudioContext is referenced as a global constructor here, but the codebase already uses vendor-prefixed AudioContext constructors for Safari compatibility. Consider selecting the constructor via
window.OfflineAudioContext || window.webkitOfflineAudioContext(and failing with a clear error if unavailable) so export works in the same set of supported browsers.