Skip to content
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

Resolves gh-34 and gh-35. Fixes implementation of AudioContext Clock. #36

Merged
merged 4 commits into from
Mar 7, 2023
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
},
"scripts": {
"prepare": "npx grunt",
"browser-test": "npx testem ci --file tests/testem.json",
"browser-core-test": "npx testem ci --file tests/testem.json",
"webaudio-test": "npx testem ci --file tests/testem-webaudio.json",
"node-test": "node tests/node-all-tests.js",
"test": "npm run node-test && npm run browser-test"
"test": "npm run node-test && npm run webaudio-test && npm run browser-core-test"
}
}
32 changes: 22 additions & 10 deletions src/js/audiocontext-clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var fluid = fluid || require("infusion"),
scriptNode: {
expander: {
funcName: "berg.clock.autoAudioContext.createScriptNode",
args: ["{that}.context", "{that}.options.blockSize", "{that}.tick"]
args: ["{that}.context", "{that}.options.blockSize"]
}
}
},
Expand All @@ -90,30 +90,42 @@ var fluid = fluid || require("infusion"),
"onStart.startAudioContext": {
priority: "after:updateState",
funcName: "berg.clock.autoAudioContext.start",
args: ["{that}.context", "{that}.scriptNode"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might get a second opinion, but in recent experience I've found the startup lifecycle to be one of the key places where you want to be stricter about your references. Seems like you could find a way to preserve the previous specificity, maybe just adding {that}.tick to the arguments and using that in the start function.

args: ["{that}.scriptNode", "{that}.context", "{that}.tick"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all I hoped for and I am totally happy with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, there was no debate from me about that, only the question of whether we wanted to work around the Web Audio API's lack of a way to determine if a node has already been connected to a given destination. If you're good with this all as-is, I'll merge and cut a new release and then whenever you get time you can see if it has any impact on Youme's demos?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy for this to be merged and to test/track any remaining work separately.

},

"onStop.stopAudioContext": {
priority: "after:updateState",
funcName: "berg.clock.autoAudioContext.stop",
args: ["{that}.context", "{that}.scriptNode"]
args: ["{that}.scriptNode", "{that}.context"]
}
}
});

berg.clock.autoAudioContext.createScriptNode = function (context, blockSize, tick) {
var sp = context.createScriptProcessor(blockSize, 1, 1);
sp.onaudioprocess = tick;
return sp;
berg.clock.autoAudioContext.createScriptNode = function (context,
blockSize) {
var scriptNode = context.createScriptProcessor(blockSize, 1, 1);
return scriptNode;
};

berg.clock.autoAudioContext.start = function (context, scriptNode) {
berg.clock.autoAudioContext.start = function (scriptNode, context, tickFn) {
scriptNode.connect(context.destination);
scriptNode.onaudioprocess = tickFn;
context.resume();
};

berg.clock.autoAudioContext.stop = function (context, scriptNode) {
scriptNode.disconnect(context.destination);
berg.clock.autoAudioContext.stop = function (scriptNode, context) {
try {
scriptNode.disconnect(context.destination);
} catch (e) {
// Only swallow the error if was thrown because
// the script node wasn't connected,
// which can occur if stop() is called before start().
if (e.name !== "InvalidAccessError") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only branch that wasn't hit in the coverage report I created, but it's fine, as error tests are often harder to simulate.

throw e;
}
}

scriptNode.onaudioprocess = undefined;
context.suspend();
};
})();
5 changes: 2 additions & 3 deletions tests/all-tests.html → tests/core-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>All Bergson Tests</title>
<title>Bergson Core Tests</title>

<link rel="stylesheet" href="../node_modules/infusion/tests/lib/qunit/css/qunit.css" type="text/css" media="screen">
<link rel="stylesheet" href="../node_modules/infusion/tests/lib/qunit/addons/composite/qunit-composite.css">
<script src="../node_modules/infusion/tests/lib/qunit/js/qunit.js"></script>
<script src="../node_modules/infusion/tests/lib/qunit/addons/composite/qunit-composite.js"></script>
<script src="/testem.js"></script>

<!-- find . -name "*-tests.html" | awk '{print "\""$1"\","}' -->
<script>
QUnit.testSuites("Bergson Tests", [
QUnit.testSuites("Bergson Core Tests", [
"html/offline-clock-tests.html",
"html/realtime-clock-tests.html",
"html/raf-clock-tests.html",
Expand Down
43 changes: 43 additions & 0 deletions tests/html/audiocontext-clock-tests.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en" dir="ltr" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AudioContext Clock Tests</title>

<link rel="stylesheet" media="screen" href="../../node_modules/infusion/tests/lib/qunit/css/qunit.css" />

<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/jquery.standalone.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/Fluid.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/FluidPromises.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/FluidDebugging.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/FluidIoC.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/src/framework/core/js/DataBinding.js"></script>

<script type="text/javascript" src="../../node_modules/infusion/tests/lib/qunit/js/qunit.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/tests/test-core/jqUnit/js/jqUnit.js"></script>

<script type="text/javascript" src="../../src/js/clock.js"></script>
<script type="text/javascript" src="../../src/js/audiocontext-clock.js"></script>

<script type="text/javascript" src="../js/utils/clock-tester.js"></script>
<script type="text/javascript" src="../js/utils/realtime-tester.js"></script>
<script type="text/javascript" src="../js/utils/clock-test-utilities.js"></script>
<script type="text/javascript" src="../js/utils/audiocontext-tester.js"></script>

<script type="text/javascript" src="../js/audiocontext-clock-tests.js"></script>
<script src="/testem.js"></script>
</head>

<body id="body">
<h1 id="qunit-header">AudioContext Clock Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>

<!-- Test HTML -->
<div id="qunit-fixture" style="display: none;">

</div>
</body>
</html>
90 changes: 90 additions & 0 deletions tests/js/audiocontext-clock-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Bergson AudioContext Clock Tests
* http://github.com/colinbdclark/bergson
*
* Copyright 2023, Colin Clark
* Dual licensed under the MIT and GPL Version 2 licenses.
*/
/*global require*/
var fluid = fluid || require("infusion"),
berg = fluid.registerNamespace("berg");

(function () {
"use strict";

var QUnit = fluid.registerNamespace("QUnit");

fluid.registerNamespace("berg.test.AudioContextClock");

QUnit.module("AudioContext Clock Tests");

fluid.defaults("berg.test.clock.autoAudioContextClockTestSuite", {
gradeNames: ["berg.test.clock.testSuite"],

tests: [
{
name: "Initial state, default options",
initOnly: true,
tester: {
type: "berg.test.clock.tester.audioContext"
}
},

{
name: "tick() time update",
tester: {
type: "berg.test.clock.tester.audioContext"
}
}
]
});

var testSuite = berg.test.clock.autoAudioContextClockTestSuite();
testSuite.run();

QUnit.test("Instantiation", function () {
var clock = berg.clock.autoAudioContext();
QUnit.ok(clock, "Clock was successfully instantiated.");
});

QUnit.test("Start", function () {
var clock = berg.clock.autoAudioContext();

try {
clock.start();
QUnit.ok(clock.model.isPlaying, "Clock successfully started.");
} catch (e) {
QUnit.ok(false, "Clock failed to start successfully: " + e.message);
}
});

QUnit.test("Stop before start", function () {
var clock = berg.clock.autoAudioContext();

try {
clock.stop();
QUnit.ok(!clock.model.isPlaying, "Calling stop() before starting has no effect.");
} catch (e) {
QUnit.ok(false, "Calling stop() before starting failed: " + e.message);
}
});

QUnit.asyncTest("Start, stop, restart, stop sequence", function () {
var clock = berg.clock.autoAudioContext();

clock.start();
QUnit.ok(clock.model.isPlaying, "Calling start was successful.");
clock.stop();
QUnit.ok(!clock.model.isPlaying, "Calling stop was successful.");
clock.start();
QUnit.ok(clock.model.isPlaying,
"Calling start after stopping was successful.");

setInterval(function () {
clock.stop();
QUnit.ok(!clock.model.isPlaying,
"Calling stop again after waiting for a bit was successful.");
QUnit.start();
}, 100);
});
})();
53 changes: 29 additions & 24 deletions tests/js/utils/audiocontext-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@
});

berg.test.clock.testCase.audioContext.testInitial = function (clock, tester, maxJitter) {
QUnit.equal(clock.freq, tester.model.expectedFreq,
QUnit.equal(clock.freq, tester.options.expectedFreq,
"The clock should be initialized with a freq of " +
tester.model.expectedFreq + ".");
berg.test.assertTimeEqual(clock.time, tester.model.expectedTime, maxJitter,
tester.options.expectedFreq + ".");

berg.test.assertTimeEqual(clock.time,
tester.options.expectedInitialTime,
maxJitter,
"The clock should be initialized with the current time.");

QUnit.equal(clock.tickDuration, tester.model.expectedTickDuration,
QUnit.equal(clock.tickDuration,
tester.options.expectedTickDuration,
"The clock should have been initialized with a tick duration of " +
tester.model.expectedTickDuration + " seconds.");
tester.options.expectedTickDuration + " seconds.");
};

berg.test.clock.testCase.audioContext.testTick = function (clock, time, maxJitter, tester) {
var expectedTime = tester.model.expectedTime + tester.model.expectedTickDuration;
var expectedTime = tester.model.expectedTime +
tester.options.expectedTickDuration;

berg.test.assertTimeEqual(clock.time, expectedTime, maxJitter,
"The clock's time should reflect the current expected time.");
Expand All @@ -53,32 +58,32 @@

fluid.defaults("berg.test.clock.tester.audioContext", {
gradeNames: [
// TODO: The order of these two grades matters crucially. Why?
"berg.test.clock.tester.external",
"berg.test.clock.tester.realtime"
],

maxJitter: 0.05,
maxJitter: Number.EPSILON,

// TODO: These were moved into the model (instead of options)
// do to expansion issues. But all other testers expect to find
// these in the options. This should be normalized.
model: {
expectedTime: "{clock}.context.currentTime",
expectedFreq: {
expander: {
funcName: "berg.test.clock.tester.audioContext.calcFreq",
args: ["{clock}.context", "{clock}.options.blockSize"]
}
},
expectedTickDuration: {
expander: {
funcName: "berg.test.clock.tester.audioContext.calcTickDuration",
args: ["{clock}.context", "{clock}.options.blockSize"]
}
expectedInitialTime: "{clock}.context.currentTime",

expectedFreq: {
expander: {
funcName: "berg.test.clock.tester.audioContext.calcFreq",
args: ["{clock}.context", "{clock}.options.blockSize"]
}
},

expectedTickDuration: {
expander: {
funcName: "berg.test.clock.tester.audioContext.calcTickDuration",
args: ["{clock}.context", "{clock}.options.blockSize"]
}
},

model: {
expectedTime: 0
},

components: {
testCase: {
type: "berg.test.clock.testCase.audioContext"
Expand Down
2 changes: 1 addition & 1 deletion tests/js/utils/clock-test-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var fluid = fluid || require("infusion"),
" Expected time: " + expected +
", actual time was: " + actual +
" Tolerance is " + tolerance +
"; difference was: " + diff + "ms.");
"; difference was: " + diff);
};

berg.test.clock.manualTicker = function (numTicks, clock) {
Expand Down
10 changes: 10 additions & 0 deletions tests/testem-webaudio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"test_page": "tests/html/audiocontext-clock-tests.html",
"timeout": 300,
"launch": "Chrome",
"browser_args": {
"Chrome": [
"--autoplay-policy=no-user-gesture-required"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you decide to go headless by default, this should be "Headless Chrome".

]
}
}
4 changes: 2 additions & 2 deletions tests/testem.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"test_page": "tests/all-tests.html",
"test_page": "tests/core-tests.html",
"timeout": 300,
"skip": "PhantomJS,IE,Headless Chrome"
"skip": "Brave,Chrome Canary,Edge,Headless Brave,Headless Chrome,Headless Firefox,Headless Opera,IE,Opera"
}