| OLD | NEW |
| (Empty) |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <script src="../resources/js-test.js"></script> | |
| 5 <script src="resources/compatibility.js"></script> | |
| 6 <script src="resources/audit-util.js"></script> | |
| 7 <script src="resources/audio-testing.js"></script> | |
| 8 <title>Test Basic Functionality of AudioBuffer.copyFromChannel and AudioBuff
er.copyToChannel</title> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 description("Test Basic Functionality of copyFromChannel and AudioBuffer.c
opyToChannel"); | |
| 14 | |
| 15 window.jsTestIsAsync = true; | |
| 16 | |
| 17 // Define utility routines. | |
| 18 | |
| 19 // Initialize the AudioBuffer |buffer| with a ramp signal on each channel.
The ramp starts at | |
| 20 // channel number + 1. | |
| 21 function initializeAudioBufferRamp (buffer) { | |
| 22 for (var c = 0; c < buffer.numberOfChannels; ++c) { | |
| 23 var d = buffer.getChannelData(c); | |
| 24 for (var k = 0; k < d.length; ++k) { | |
| 25 d[k] = k + c + 1; | |
| 26 } | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Create a Float32Array of length |length| and initialize the array to -1
. | |
| 31 function createInitializedF32Array(length) { | |
| 32 var x = new Float32Array(length); | |
| 33 for (var k = 0; k < length; ++k) { | |
| 34 x[k] = -1; | |
| 35 } | |
| 36 return x; | |
| 37 } | |
| 38 | |
| 39 // Create a Float32Array of length |length| that is initialized to be a ra
mp starting at 1. | |
| 40 function createFloat32RampArray(length) { | |
| 41 var x = new Float32Array(length); | |
| 42 for (var k = 0; k < x.length; ++k) { | |
| 43 x[k] = k + 1; | |
| 44 } | |
| 45 | |
| 46 return x; | |
| 47 } | |
| 48 | |
| 49 // Test that the array |x| is a ramp starting at value |start| of length |
length|, starting at | |
| 50 // |startIndex| in the array. |startIndex| is optional and defaults to 0.
Any other values | |
| 51 // must be -1. | |
| 52 function shouldBeRamp(testName, x, startValue, length, startIndex) { | |
| 53 var k; | |
| 54 var startingIndex = startIndex || 0; | |
| 55 var expected = Array(x.length); | |
| 56 | |
| 57 // Fill the expected array with the correct results. | |
| 58 | |
| 59 // The initial part (if any) must be -1. | |
| 60 for (k = 0; k < startingIndex; ++k) { | |
| 61 expected[k] = -1; | |
| 62 } | |
| 63 | |
| 64 // The second part should be a ramp starting with |startValue| | |
| 65 for (; k < startingIndex + length; ++k) { | |
| 66 expected[k] = startValue + k - startingIndex; | |
| 67 } | |
| 68 | |
| 69 // The last part (if any) should be -1. | |
| 70 for (; k < x.length; ++k) { | |
| 71 expected[k] = -1; | |
| 72 } | |
| 73 | |
| 74 Should(testName, x, {numberOfArrayLog: 32}).beEqualToArray(expected); | |
| 75 } | |
| 76 | |
| 77 var audit = Audit.createTaskRunner(); | |
| 78 | |
| 79 var context = new AudioContext(); | |
| 80 // Temp array for testing exceptions for copyToChannel/copyFromChannel. T
he length is arbitrary. | |
| 81 var x = new Float32Array(8); | |
| 82 | |
| 83 // Number of frames in the AudioBuffer for testing. This is pretty arbitr
ary so choose a | |
| 84 // fairly small value. | |
| 85 var bufferLength = 16; | |
| 86 | |
| 87 // Number of channels in the AudioBuffer. Also arbitrary, but it should b
e greater than 1 for | |
| 88 // test coverage. | |
| 89 var numberOfChannels = 3; | |
| 90 | |
| 91 // AudioBuffer that will be used for testing copyFrom and copyTo. | |
| 92 var buffer = context.createBuffer(numberOfChannels, bufferLength, context.
sampleRate); | |
| 93 | |
| 94 var initialValues = Array(numberOfChannels); | |
| 95 | |
| 96 // Initialize things | |
| 97 audit.defineTask("initialize", function (done) { | |
| 98 // Initialize to -1. | |
| 99 for (var k = 0; k < numberOfChannels; ++k) { | |
| 100 initialValues[k] = -1; | |
| 101 } | |
| 102 | |
| 103 done(); | |
| 104 }); | |
| 105 | |
| 106 // Test that expected exceptions are signaled for copyFrom. | |
| 107 audit.defineTask("copyFrom-exceptions", function (done) { | |
| 108 shouldBeDefined("AudioBuffer.prototype.copyFromChannel"); | |
| 109 | |
| 110 Should("buffer = context.createBuffer(" + numberOfChannels + ", " + buff
erLength + ", context.sampleRate)", | |
| 111 function () { | |
| 112 buffer = context.createBuffer(numberOfChannels, bufferLength, contex
t.sampleRate); | |
| 113 }).notThrow(); | |
| 114 Should("buffer.copyFromChannel(null, 0)", function () { | |
| 115 buffer.copyFromChannel(null, 0); | |
| 116 }).throw("TypeError"); | |
| 117 Should("buffer.copyFromChannel(context, 0)", function () { | |
| 118 buffer.copyFromChannel(context, 0); | |
| 119 }).throw("TypeError"); | |
| 120 Should("buffer.copyFromChannel(x, -1)", function () { | |
| 121 buffer.copyFromChannel(x, -1); | |
| 122 }).throw("IndexSizeError"); | |
| 123 Should("buffer.copyFromChannel(x, " + numberOfChannels + ")", function (
) { | |
| 124 buffer.copyFromChannel(x, numberOfChannels); | |
| 125 }).throw("IndexSizeError");; | |
| 126 Should("buffer.copyFromChannel(x, 0, -1)", function () { | |
| 127 buffer.copyFromChannel(x, 0, -1); | |
| 128 }).throw("IndexSizeError"); | |
| 129 Should("buffer.copyFromChannel(x, 0, " + bufferLength + ")", function ()
{ | |
| 130 buffer.copyFromChannel(x, 0, bufferLength); | |
| 131 }).throw("IndexSizeError"); | |
| 132 | |
| 133 shouldThrow("buffer.copyFromChannel(x, 3)"); | |
| 134 | |
| 135 done(); | |
| 136 }); | |
| 137 | |
| 138 // Test that expected exceptions are signaled for copyTo. | |
| 139 audit.defineTask("copyTo-exceptions", function (done) { | |
| 140 shouldBeDefined("AudioBuffer.prototype.copyToChannel"); | |
| 141 Should("buffer.copyToChannel(null, 0)", function () { | |
| 142 buffer.copyToChannel(null, 0); | |
| 143 }).throw("TypeError"); | |
| 144 Should("buffer.copyToChannel(context, 0)", function () { | |
| 145 buffer.copyToChannel(context, 0); | |
| 146 }).throw("TypeError"); | |
| 147 Should("buffer.copyToChannel(x, -1)", function () { | |
| 148 buffer.copyToChannel(x, -1); | |
| 149 }).throw("IndexSizeError"); | |
| 150 Should("buffer.copyToChannel(x, " + numberOfChannels + ")", function ()
{ | |
| 151 buffer.copyToChannel(x, numberOfChannels); | |
| 152 }).throw("IndexSizeError"); | |
| 153 Should("buffer.copyToChannel(x, 0, -1)", function () { | |
| 154 buffer.copyToChannel(x, 0, -1); | |
| 155 }).throw("IndexSizeError"); | |
| 156 Should("buffer.copyToChannel(x, 0, " + bufferLength + ")", function () { | |
| 157 buffer.copyToChannel(x, 0, bufferLength); | |
| 158 }).throw("IndexSizeError"); | |
| 159 | |
| 160 shouldThrow("buffer.copyToChannel(x, 3)"); | |
| 161 | |
| 162 done(); | |
| 163 }); | |
| 164 | |
| 165 // Test copyFromChannel | |
| 166 audit.defineTask("copyFrom-validate", function (done) { | |
| 167 // Initialize the AudioBuffer to a ramp for testing copyFrom. | |
| 168 initializeAudioBufferRamp(buffer); | |
| 169 | |
| 170 // Test copyFrom operation with a short destination array, filling the d
estination completely. | |
| 171 for (var c = 0; c < numberOfChannels; ++c) { | |
| 172 var dst8 = createInitializedF32Array(8); | |
| 173 buffer.copyFromChannel(dst8, c); | |
| 174 shouldBeRamp("buffer.copyFromChannel(dst8, " + c + ")", dst8, c + 1, 8
) | |
| 175 } | |
| 176 | |
| 177 // Test copyFrom operation with a short destination array using a non-ze
ro start index that | |
| 178 // still fills the destination completely. | |
| 179 for (var c = 0; c < numberOfChannels; ++c) { | |
| 180 var dst8 = createInitializedF32Array(8); | |
| 181 buffer.copyFromChannel(dst8, c, 1); | |
| 182 shouldBeRamp("buffer.copyFromChannel(dst8, " + c + ", 1)", dst8, c + 2
, 8) | |
| 183 } | |
| 184 | |
| 185 // Test copyFrom operation with a short destination array using a non-ze
ro start index that | |
| 186 // does not fill the destinatiom completely. The extra elements should
be unchanged. | |
| 187 for (var c = 0; c < numberOfChannels; ++c) { | |
| 188 var dst8 = createInitializedF32Array(8); | |
| 189 var startInChannel = bufferLength - 5; | |
| 190 buffer.copyFromChannel(dst8, c, startInChannel); | |
| 191 shouldBeRamp("buffer.copyFromChannel(dst8, " + c + ", " + startInChann
el + ")", dst8, | |
| 192 c + 1 + startInChannel, bufferLength - startInChannel); | |
| 193 } | |
| 194 | |
| 195 // Copy operation with the destination longer than the buffer, leaving t
he trailing elements | |
| 196 // of the destination untouched. | |
| 197 for (var c = 0; c < numberOfChannels; ++c) { | |
| 198 var dst26 = createInitializedF32Array(bufferLength + 10); | |
| 199 buffer.copyFromChannel(dst26, c); | |
| 200 shouldBeRamp("buffer.copyFromChannel(dst26, " + c + ")", dst26, | |
| 201 c + 1, bufferLength); | |
| 202 } | |
| 203 | |
| 204 done(); | |
| 205 }); | |
| 206 | |
| 207 // Test copyTo | |
| 208 audit.defineTask("copyTo-validate", function (done) { | |
| 209 // Create a source consisting of a ramp starting at 1, longer than the A
udioBuffer | |
| 210 var src = createFloat32RampArray(bufferLength + 10); | |
| 211 | |
| 212 // Test copyTo with AudioBuffer shorter than Float32Array. The AudioBuff
er should be | |
| 213 // completely filled with the Float32Array. | |
| 214 Should("buffer = createConstantBuffer(context, " + bufferLength + ", ["
+ initialValues+ "])", | |
| 215 function () { | |
| 216 buffer = createConstantBuffer(context, bufferLength, initialValues); | |
| 217 }).notThrow();; | |
| 218 | |
| 219 for (var c = 0; c < numberOfChannels; ++c) { | |
| 220 buffer.copyToChannel(src, c); | |
| 221 shouldBeRamp("buffer.copyToChannel(src, " + c + ")", buffer.getChannel
Data(c), 1, bufferLength); | |
| 222 } | |
| 223 | |
| 224 // Test copyTo with AudioBuffer longer than the Float32Array. The tail
of the AudioBuffer | |
| 225 // should be unchanged. | |
| 226 buffer = createConstantBuffer(context, bufferLength, initialValues); | |
| 227 var src10 = createFloat32RampArray(10); | |
| 228 for (var c = 0; c < numberOfChannels; ++c) { | |
| 229 buffer.copyToChannel(src10, c); | |
| 230 shouldBeRamp("buffer.copyToChannel(src10, " + c + ")", buffer.getChann
elData(c), 1, 10); | |
| 231 } | |
| 232 | |
| 233 // Test copyTo with non-default startInChannel. Part of the AudioBuffer
should filled with | |
| 234 // the beginning and end sections untouched. | |
| 235 buffer = createConstantBuffer(context, bufferLength, initialValues); | |
| 236 for (var c = 0; c < numberOfChannels; ++c) { | |
| 237 var startInChannel = 5; | |
| 238 buffer.copyToChannel(src10, c, startInChannel); | |
| 239 | |
| 240 shouldBeRamp("buffer.copyToChannel(src10, " + c + ", " + startInChanne
l + ")", | |
| 241 buffer.getChannelData(c), 1, src10.length, startInChannel); | |
| 242 } | |
| 243 | |
| 244 done(); | |
| 245 }); | |
| 246 | |
| 247 audit.defineTask("finish", function (done) { | |
| 248 finishJSTest(); | |
| 249 done(); | |
| 250 }); | |
| 251 | |
| 252 audit.runTasks( | |
| 253 "initialize", | |
| 254 "copyFrom-exceptions", | |
| 255 "copyTo-exceptions", | |
| 256 "copyFrom-validate", | |
| 257 "copyTo-validate", | |
| 258 "finish" | |
| 259 ); | |
| 260 | |
| 261 successfullyParsed = true; | |
| 262 | |
| 263 </script> | |
| 264 | |
| 265 </body> | |
| 266 </html> | |
| OLD | NEW |