Skip to content

Instantly share code, notes, and snippets.

@Korilakkuma
Last active February 24, 2022 15:04
Show Gist options
  • Save Korilakkuma/e7c8af01eadfd673242b2ce7ecfd2054 to your computer and use it in GitHub Desktop.
Save Korilakkuma/e7c8af01eadfd673242b2ce7ecfd2054 to your computer and use it in GitHub Desktop.
Web Audio API Library (https://github.com/Korilakkuma/XSound) on Headless Chrome
'use strict';
const chrome = require('chrome-remote-interface');
const ChromeLauncher = require('chrome-launcher').Launcher;
function launchChrome() {
const launcher = new ChromeLauncher();
return Promise.resolve(launcher);
}
function onPageLoad(Runtime) {
// https://github.com/Korilakkuma/XSound
const js = `
X('oscillator').setup(true);
X('oscillator').get(0).param('type', 'sawtooth');
X('oscillator').ready().start(440);
`;
return Promise.resolve(Runtime.evaluate({ expression : js }));
}
launchChrome().then(launcher => {
chrome(protocol => {
const { Page, Runtime } = protocol;
Page.enable();
Page.navigate({ url : 'https://korilakkuma.github.io/XSound/' });
onPageLoad(Runtime).then(() => {
protocol.close();
launcher.kill();
});
}).on('error', error => {
throw Error(error);
});
});
@Korilakkuma
Copy link
Author

$ npm init -y
$ npm install --save chrome-remote-interface chrome-launcher
$ alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" # mac
$ chrome --headless --disable-gpu --remote-debugging-port=9222 &
$ node headless-webaudio.js

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