Skip to content

Commit

Permalink
Headless Web Audio
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Apr 10, 2024
1 parent eb2d892 commit 5a7fa01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ var path = require('path');
var JZZ = require('jzz');
require('jzz-midi-gm')(JZZ);
require('jzz-midi-smf')(JZZ);
var WA;

function tryWA() {
if (WA) return;
try{
WA = require('node-web-audio-api');
if (global && !global.window) global.window = {};
if (!global.window.AudioContext) global.window.AudioContext = WA.AudioContext;
require('jzz-synth-tiny')(JZZ);
JZZ.synth.Tiny.register('Web Audio');
}
catch (e) {
WA = true;
}
}

function _error(s) { throw new Error(s); }
var SMF2CLIP = 'SMF2CLIP';
Expand Down Expand Up @@ -204,6 +219,7 @@ else {
var data;
var smf;
if (process.argv.length < 3) {
tryWA();
usage(process.argv[0], process.argv[1]);
process.exit(0);
}
Expand Down Expand Up @@ -276,6 +292,7 @@ function play(smf, out) {
console.log(JZZ.lib.toBase64(smf.dump()));
return;
}
tryWA();
var player = smf.player();
JZZ().or(function() {
console.error('Cannot start MIDI engine!');
Expand All @@ -296,7 +313,7 @@ function play(smf, out) {
m2m1.connect(this);
player.connect(m2m1);
player.connect(log);
player.onEnd = function() { console.log('\ndone!'); };
player.onEnd = function() { console.log('\ndone!'); process.exit(0); };
player.play();
});
});
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
"author": "jazz-soft (https://jazz-soft.net)",
"dependencies": {
"jzz": "^1.8.1",
"jzz-gui-player": "^1.7.2",
"jzz-midi-gear": "^1.3.2",
"jzz-midi-gm": "^1.3.5",
"jzz-midi-smf": "^1.9.0",
"jzz-synth-tiny": "^1.3.8"
"jzz-gui-player": "^1.7.3",
"jzz-midi-gear": "^1.3.3",
"jzz-midi-gm": "^1.3.6",
"jzz-midi-smf": "^1.9.1",
"jzz-synth-tiny": "^1.3.9"
},
"devDependencies": {
"eslint": "^8.57.0",
"eslint": "^9.0.0",
"grunt": "^1.6.1",
"mocha": "^10.3.0",
"mocha": "^10.4.0",
"node-web-audio-api": "^0.18.0",
"nyc": "^15.1.0"
},
"runkitExampleFilename": "runkit.js",
Expand Down

0 comments on commit 5a7fa01

Please sign in to comment.