| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE html> | |
| 2 | |
| 3 <html> | |
| 4 <head> | |
| 5 <script src="../resources/js-test.js"></script> | |
| 6 <script src="resources/compatibility.js"></script> | |
| 7 <script src="resources/audit-util.js"></script> | |
| 8 <script src="resources/audio-testing.js"></script> | |
| 9 <script src="resources/mixing-rules.js"></script> | |
| 10 </head> | |
| 11 | |
| 12 <body> | |
| 13 | |
| 14 <script> | |
| 15 description("Channel mixing rules for AudioNodes."); | |
| 16 | |
| 17 var context = 0; | |
| 18 var sampleRate = 44100; | |
| 19 var renderNumberOfChannels = 8; | |
| 20 var singleTestFrameLength = 8; | |
| 21 var testBuffers; | |
| 22 | |
| 23 // A list of connections to an AudioNode input, each of which is to be used in o
ne or more specific test cases. | |
| 24 // Each element in the list is a string, with the number of connections correspo
nding to the length of the string, | |
| 25 // and each character in the string is from '1' to '8' representing a 1 to 8 cha
nnel connection (from an AudioNode output). | |
| 26 // For example, the string "128" means 3 connections, having 1, 2, and 8 channel
s respectively. | |
| 27 var connectionsList = ["1", "2", "3", "4", "5", "6", "7", "8", "11", "12", "14",
"18", "111", "122", "123", "124", "128"]; | |
| 28 | |
| 29 // A list of mixing rules, each of which will be tested against all of the conne
ctions in connectionsList. | |
| 30 var mixingRulesList = [ | |
| 31 {channelCount: 2, channelCountMode: "max", channelInterpretation: "speakers"
}, | |
| 32 {channelCount: 4, channelCountMode: "clamped-max", channelInterpretation: "s
peakers"}, | |
| 33 | |
| 34 // Test up-down-mix to some explicit speaker layouts. | |
| 35 {channelCount: 1, channelCountMode: "explicit", channelInterpretation: "spea
kers"}, | |
| 36 {channelCount: 2, channelCountMode: "explicit", channelInterpretation: "spea
kers"}, | |
| 37 {channelCount: 4, channelCountMode: "explicit", channelInterpretation: "spea
kers"}, | |
| 38 {channelCount: 6, channelCountMode: "explicit", channelInterpretation: "spea
kers"}, | |
| 39 | |
| 40 {channelCount: 2, channelCountMode: "max", channelInterpretation: "discrete"
}, | |
| 41 {channelCount: 4, channelCountMode: "clamped-max", channelInterpretation: "d
iscrete"}, | |
| 42 {channelCount: 4, channelCountMode: "explicit", channelInterpretation: "disc
rete"}, | |
| 43 {channelCount: 8, channelCountMode: "explicit", channelInterpretation: "disc
rete"}, | |
| 44 ]; | |
| 45 | |
| 46 var numberOfTests = mixingRulesList.length * connectionsList.length; | |
| 47 | |
| 48 // Print out the information for an individual test case. | |
| 49 function printTestInformation(testNumber, actualBuffer, expectedBuffer, frameLen
gth, frameOffset) { | |
| 50 var actual = stringifyBuffer(actualBuffer, frameLength); | |
| 51 var expected = stringifyBuffer(expectedBuffer, frameLength, frameOffset); | |
| 52 debug('TEST CASE #' + testNumber + '\n'); | |
| 53 debug('actual channels:\n' + actual); | |
| 54 debug('expected channels:\n' + expected); | |
| 55 } | |
| 56 | |
| 57 function scheduleTest(testNumber, connections, channelCount, channelCountMode, c
hannelInterpretation) { | |
| 58 var mixNode = context.createGain(); | |
| 59 mixNode.channelCount = channelCount; | |
| 60 mixNode.channelCountMode = channelCountMode; | |
| 61 mixNode.channelInterpretation = channelInterpretation; | |
| 62 mixNode.connect(context.destination); | |
| 63 | |
| 64 for (var i = 0; i < connections.length; ++i) { | |
| 65 var connectionNumberOfChannels = connections.charCodeAt(i) - "0".charCod
eAt(0); | |
| 66 | |
| 67 var source = context.createBufferSource(); | |
| 68 // Get a buffer with the right number of channels, converting from 1-bas
ed to 0-based index. | |
| 69 var buffer = testBuffers[connectionNumberOfChannels - 1]; | |
| 70 source.buffer = buffer; | |
| 71 source.connect(mixNode); | |
| 72 | |
| 73 // Start at the right offset. | |
| 74 var sampleFrameOffset = testNumber * singleTestFrameLength; | |
| 75 var time = sampleFrameOffset / sampleRate; | |
| 76 source.start(time); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 function checkTestResult(renderedBuffer, testNumber, connections, channelCount,
channelCountMode, channelInterpretation) { | |
| 81 var s = "connections: " + connections + ", " + channelCountMode; | |
| 82 | |
| 83 // channelCount is ignored in "max" mode. | |
| 84 if (channelCountMode == "clamped-max" || channelCountMode == "explicit") { | |
| 85 s += "(" + channelCount + ")"; | |
| 86 } | |
| 87 | |
| 88 s += ", " + channelInterpretation; | |
| 89 | |
| 90 var computedNumberOfChannels = computeNumberOfChannels(connections, channelC
ount, channelCountMode); | |
| 91 | |
| 92 // Create a zero-initialized silent AudioBuffer with computedNumberOfChannel
s. | |
| 93 var destBuffer = context.createBuffer(computedNumberOfChannels, singleTestFr
ameLength, context.sampleRate); | |
| 94 | |
| 95 // Mix all of the connections into the destination buffer. | |
| 96 for (var i = 0; i < connections.length; ++i) { | |
| 97 var connectionNumberOfChannels = connections.charCodeAt(i) - "0".charCod
eAt(0); | |
| 98 var sourceBuffer = testBuffers[connectionNumberOfChannels - 1]; // conve
rt from 1-based to 0-based index | |
| 99 | |
| 100 if (channelInterpretation == "speakers") { | |
| 101 speakersSum(sourceBuffer, destBuffer); | |
| 102 } else if (channelInterpretation == "discrete") { | |
| 103 discreteSum(sourceBuffer, destBuffer); | |
| 104 } else { | |
| 105 alert("Invalid channel interpretation!"); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Use this when debugging mixing rules. | |
| 110 // printTestInformation(testNumber, renderedBuffer, destBuffer, singleTestFr
ameLength, sampleFrameOffset); | |
| 111 | |
| 112 // Validate that destBuffer matches the rendered output. | |
| 113 // We need to check the rendered output at a specific sample-frame-offset co
rresponding | |
| 114 // to the specific test case we're checking for based on testNumber. | |
| 115 | |
| 116 var sampleFrameOffset = testNumber * singleTestFrameLength; | |
| 117 for (var c = 0; c < renderNumberOfChannels; ++c) { | |
| 118 var renderedData = renderedBuffer.getChannelData(c); | |
| 119 for (var frame = 0; frame < singleTestFrameLength; ++frame) { | |
| 120 var renderedValue = renderedData[frame + sampleFrameOffset]; | |
| 121 | |
| 122 var expectedValue = 0; | |
| 123 if (c < destBuffer.numberOfChannels) { | |
| 124 var expectedData = destBuffer.getChannelData(c); | |
| 125 expectedValue = expectedData[frame]; | |
| 126 } | |
| 127 | |
| 128 // We may need to add an epsilon in the comparison if we add more te
st vectors. | |
| 129 if (renderedValue != expectedValue) { | |
| 130 var message = s + "rendered: " + renderedValue + " expected: " +
expectedValue + " channel: " + c + " frame: " + frame; | |
| 131 testFailed(s); | |
| 132 return; | |
| 133 } | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 testPassed(s); | |
| 138 } | |
| 139 | |
| 140 function checkResult(event) { | |
| 141 var buffer = event.renderedBuffer; | |
| 142 | |
| 143 // Sanity check result. | |
| 144 if (buffer.length != numberOfTests * singleTestFrameLength || buffer.numberO
fChannels != renderNumberOfChannels) { | |
| 145 testFailed("OfflineAudioContext result not of expected size!"); | |
| 146 finishJSTest(); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 // Check all the tests. | |
| 151 var testNumber = 0; | |
| 152 for (var m = 0; m < mixingRulesList.length; ++m) { | |
| 153 var mixingRules = mixingRulesList[m]; | |
| 154 for (var i = 0; i < connectionsList.length; ++i, ++testNumber) { | |
| 155 checkTestResult(buffer, testNumber, connectionsList[i], mixingRules.
channelCount, mixingRules.channelCountMode, mixingRules.channelInterpretation); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 finishJSTest(); | |
| 160 } | |
| 161 | |
| 162 function runTest() { | |
| 163 if (window.testRunner) { | |
| 164 testRunner.dumpAsText(); | |
| 165 testRunner.waitUntilDone(); | |
| 166 } | |
| 167 | |
| 168 window.jsTestIsAsync = true; | |
| 169 | |
| 170 // Create 8-channel offline audio context. | |
| 171 // Each test will render 8 sample-frames starting at sample-frame position t
estNumber * 8. | |
| 172 var totalFrameLength = numberOfTests * singleTestFrameLength; | |
| 173 context = new OfflineAudioContext(renderNumberOfChannels, totalFrameLength,
sampleRate); | |
| 174 | |
| 175 // Set destination to discrete mixing. | |
| 176 context.destination.channelCount = renderNumberOfChannels; | |
| 177 context.destination.channelCountMode = "explicit"; | |
| 178 context.destination.channelInterpretation = "discrete"; | |
| 179 | |
| 180 // Create test buffers from 1 to 8 channels. | |
| 181 testBuffers = new Array(); | |
| 182 for (var i = 0; i < renderNumberOfChannels; ++i) { | |
| 183 testBuffers[i] = createShiftedImpulseBuffer(context, i + 1, singleTestFr
ameLength); | |
| 184 } | |
| 185 | |
| 186 // Schedule all the tests. | |
| 187 var testNumber = 0; | |
| 188 for (var m = 0; m < mixingRulesList.length; ++m) { | |
| 189 var mixingRules = mixingRulesList[m]; | |
| 190 for (var i = 0; i < connectionsList.length; ++i, ++testNumber) { | |
| 191 scheduleTest(testNumber, connectionsList[i], mixingRules.channelCoun
t, mixingRules.channelCountMode, mixingRules.channelInterpretation); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 // Render then check results. | |
| 196 context.oncomplete = checkResult; | |
| 197 context.startRendering(); | |
| 198 } | |
| 199 | |
| 200 runTest(); | |
| 201 | |
| 202 </script> | |
| 203 | |
| 204 </body> | |
| 205 </html> | |
| OLD | NEW |