Skip to content

Commit

Permalink
adding a test for router
Browse files Browse the repository at this point in the history
  • Loading branch information
hoch committed Aug 2, 2016
1 parent aab09c9 commit e987d59
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.DS_Store
bower_components/
node_modules/
temp/
8 changes: 6 additions & 2 deletions bower.json
Expand Up @@ -11,7 +11,7 @@
"ambisonic",
"VR audio",
"360 audio",
"spatial audio",
"spatial audio",
"Web Audio API"
],
"homepage": "https://github.com/GoogleChrome/omnitone",
Expand All @@ -21,5 +21,9 @@
"bower_components",
"test",
"tests"
]
],
"devDependencies": {
"mocha": "^3.0.0",
"chai": "^3.5.0"
}
}
2 changes: 1 addition & 1 deletion build/omnitone.min.js

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions src/foa-router.js
Expand Up @@ -13,30 +13,33 @@
* limitations under the License.
*/

'use strict';

/**
* @fileOverview An audio channel re-router to resolve different channel layouts
* between various platforms.
*/

'use strict';

var CHROME_CHANNEL_MAP = [0, 1, 2, 3];
var ISO_CHANNEL_MAP = [2, 0, 1, 3];
var DEFAULT_CHANNEL_MAP = [0, 1, 2, 3];

var IOS_CHANNEL_MAP = [2, 0, 1, 3];
var FUMA_2_ACN_CHANNEL_MAP = [0, 3, 1, 2];

/**
* @class A simple channel re-router.
* @param {AudioContext} context Associated AudioContext.
* @param {Array} channelMap Routing destination array.
* e.g.) Chrome: [0, 1, 2, 3],
* iOS: [1, 2, 0, 3]
* iOS: [2, 0, 1, 3]
*/
function FOARouter (context, channelMap) {
this._context = context;

this._splitter = this._context.createChannelSplitter(4);
this._merger = this._context.createChannelMerger(4);

this._channelMap = channelMap || CHROME_CHANNEL_MAP;
this._channelMap = channelMap || DEFAULT_CHANNEL_MAP;

this._splitter.connect(this._merger, 0, this._channelMap[0]);
this._splitter.connect(this._merger, 1, this._channelMap[1]);
Expand All @@ -48,7 +51,7 @@ function FOARouter (context, channelMap) {
this.output = this._merger;
}

FOARouter.prototype.setchannelMap = function (channelMap) {
FOARouter.prototype.setChannelMap = function (channelMap) {
if (!channelMap)
return;

Expand Down
4 changes: 2 additions & 2 deletions src/omnitone.js
Expand Up @@ -73,8 +73,8 @@ Omnitone.loadAudioBuffers = function (context, speakerData) {
* Router class.
* @return {Object}
*/
Omnitone.createFOARouter = function (context, options) {
return new FOARouter(context, options);
Omnitone.createFOARouter = function (context, channelMap) {
return new FOARouter(context, channelMap);
};

/**
Expand Down
20 changes: 20 additions & 0 deletions test/index.html
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Omnitone Unit Tests</title>
<link rel="stylesheet" href="../bower_components/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="../bower_components/chai/chai.js"></script>
<script src="../bower_components/mocha/mocha.js"></script>
<script src="../build/omnitone.min.js"></script>
<script src="test-setup.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test-router.js"></script>
<script>mocha.run();</script>
</body>
</html>
59 changes: 59 additions & 0 deletions test/test-router.js
@@ -0,0 +1,59 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


describe('FOARouter', function () {

it('Should route incoming channels according to the channel map correctly.',
function (done) {
var sampleRate = 48000;
var renderLength = 0.1 * sampleRate;
var context = new OfflineAudioContext(4, renderLength, sampleRate);

var testBuffer = context.createBuffer(4, renderLength, sampleRate);
for (var c = 0; c < testBuffer.numberOfChannels; c++) {
var channelData = testBuffer.getChannelData(c);
for (var i = 0; i < channelData.length; i++) {
channelData[i] = c;
}
}

var router = Omnitone.createFOARouter(context, [1, 2, 0, 3]);
var source = context.createBufferSource();
source.buffer = testBuffer;

source.connect(router.input);
router.output.connect(context.destination);
source.start();

// With the router configuration above, this is what must be rendered.
// Channel #0: value [0] => (to channel #1) => value [2]
// Channel #1: value [1] => (to channel #2) => value [0]
// Channel #2: value [2] => (to channel #0) => value [1]
// Channel #3: value [3] => (to channel #3) => value [3]
var expectedValues = [2, 0, 1, 3];
var passed = false;
context.startRendering().then(function (renderedBuffer) {
for (c = 0; c < renderedBuffer.numberOfChannels; c++) {
passed = isConstantValueOf(
renderedBuffer.getChannelData(c), expectedValues[c]);
expect(passed).to.equal(true);
}
done();
});
}
);

});
35 changes: 35 additions & 0 deletions test/test-setup.js
@@ -0,0 +1,35 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var expect = chai.expect;
var should = chai.should();


/**
* Check if the array is filled with the specified value only.
* @param {Float32Array} channelData The target array for testing.
* @param {Number} value A value for the testing.
* @return {Boolean}
*/
function isConstantValueOf(channelData, value) {
var mismatches = {};
for (var i = 0; i < channelData.length; i++) {
if (channelData[i] !== value)
mismatches[i] = channelData[i];
}

var numberOfmismatches = Object.keys(mismatches).length;
return numberOfmismatches === 0;
};

0 comments on commit e987d59

Please sign in to comment.