Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions wasmaudioworklet/e2e/video-export.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ test.describe('video export with sound (local repo)', () => {
test('one WebM carries a VP9 video track and an Opus audio track of the song', async ({ page }) => {
page.on('pageerror', (e) => console.log('[browser-error]', e.message));
await page.addInitScript(() => {
// what the export would ask the user for: a file to write to
window.showSaveFilePicker = async () => (await navigator.storage.getDirectory()).getFileHandle('export-test.webm', { create: true });
// what the export would ask the user for: a file to write to. Counted,
// because the real dialog only opens inside the click's gesture window:
// it must be asked for once, up front, before the long offline render.
window.__pickerCalls = 0;
window.showSaveFilePicker = async () => { window.__pickerCalls++; return (await navigator.storage.getDirectory()).getFileHandle('export-test.webm', { create: true }); };
});
await page.goto(`http://localhost:8080/?gitrepo=${REPO}`);
await waitForAppReady(page);
Expand Down Expand Up @@ -58,6 +61,7 @@ test.describe('video export with sound (local repo)', () => {
videoSeconds: video.duration, width: video.videoWidth, height: video.videoHeight };
});
console.log('export:', JSON.stringify(result));
expect(await page.evaluate(() => window.__pickerCalls)).toBe(1);
expect(result.size).toBeGreaterThan(50_000);
expect(result.channels).toBe(2);
expect(result.audioSeconds).toBeGreaterThan(3.9);
Expand Down
11 changes: 8 additions & 3 deletions wasmaudioworklet/editorcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { zipRepo, downloadBlob } from './wasmgit/repozip.js';
import { updateSong, updateSynth, exportToWav, renderSongOffline, confirmClipping } from './synth1/audioworklet/midisynthaudioworklet.js';
import { compileWebAssemblySynth } from './synth1/browsersynthcompiler.js';

import { exportVideo, setupWebGL } from './visualizer/fragmentshader.js';
import { exportVideo, pickVideoFile, setupWebGL } from './visualizer/fragmentshader.js';
import { hasScheduledText } from './visualizer/videoscheduler.js';
import { getVisualParamNames } from './visualizer/visualparams.js';
import { visualWarnings } from './visualizer/visualwarnings.js';
Expand Down Expand Up @@ -617,13 +617,18 @@ process = os.sawtooth(freq) * gain * en.adsr(0.01, 0.1, 0.7, 0.2, gate);
await exportVideo(shadersource, eventlist);
} else if (exportProject === 'videoaudio') {
// one file: the song rendered offline at 48 kHz (what Opus
// wants) becomes the WebM's audio track, then the frames
// wants) becomes the WebM's audio track, then the frames.
// The save dialog needs the click's user gesture, which the
// render (minutes for a long song) would use up: ask FIRST.
toggleSpinner(false);
const fileHandle = await pickVideoFile();
toggleSpinner(true);
const wasmBytes = await compileWebAssemblySynth(synthsource + '\n', undefined, 48000, false, faustSources);
const { renderedBuffer, clips } = await renderSongOffline(eventlist, wasmBytes, 48000);
if (clips.length) toggleSpinner(false); // the dialog must not sit under the spinner
if (await confirmClipping(clips)) {
toggleSpinner(true);
await exportVideo(shadersource, eventlist, { audioBuffer: renderedBuffer });
await exportVideo(shadersource, eventlist, { audioBuffer: renderedBuffer, fileHandle });
}
} else if (exportProject === EXPORT_MODE_MIDIPARTS_JSON) {
const songParts = getSongParts();
Expand Down
19 changes: 13 additions & 6 deletions wasmaudioworklet/visualizer/fragmentshader.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,18 +412,25 @@ export function createOpusAudioTrack(audioBuffer, onChunk, { bitrate = 192_000,
// Video export. With `audioBuffer` (the song rendered offline at 48 kHz, see
// renderSongOffline) the WebM gets an Opus audio track too — one file, in
// sync, since frames and audio both come from the same event list.
export async function exportVideo(source, eventlist, { audioBuffer = null, saveFilePicker = null } = {}) {
exporting = true;

const { Muxer, FileSystemWritableFileStreamTarget } = (await import('https://cdn.jsdelivr.net/npm/webm-muxer@3.0.3/+esm')).default;

let fileHandle = await (saveFilePicker || window.showSaveFilePicker)({
// The save dialog. The browser only shows it while the user's click is still
// "active" (a few seconds), so a caller that renders audio first — minutes for
// a long song — must ask for the file BEFORE rendering and pass the handle in.
export function pickVideoFile() {
return window.showSaveFilePicker({
suggestedName: `video.webm`,
types: [{
description: 'Video File',
accept: { 'video/webm': ['.webm'] }
}],
});
}

export async function exportVideo(source, eventlist, { audioBuffer = null, fileHandle = null } = {}) {
exporting = true;

const { Muxer, FileSystemWritableFileStreamTarget } = (await import('https://cdn.jsdelivr.net/npm/webm-muxer@3.0.3/+esm')).default;

if (!fileHandle) fileHandle = await pickVideoFile();
let fileStream = await fileHandle.createWritable();

const width = 1280, height = 720;
Expand Down
Loading