content/media/webaudio/test/test_waveDecoder.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <meta charset=utf-8>
michael@0 4 <head>
michael@0 5 <title>Test that we decode uint8 and sint16 wave files with correct conversion to float64</title>
michael@0 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body>
michael@0 10 <pre id="test">
michael@0 11 <script class="testbody" type="text/javascript">
michael@0 12 var testsDone = 0;
michael@0 13 var tests = ["UklGRjUrAABXQVZFZm10IBAAAAABAAEAESsAABErAAABAAgAZGF0YQMAAAD/AIA=",
michael@0 14 "UklGRkZWAABXQVZFZm10IBAAAAABAAEAESsAACJWAAACABAAZGF0YQYAAAD/fwCAAAA="];
michael@0 15
michael@0 16 SimpleTest.waitForExplicitFinish();
michael@0 17
michael@0 18 function base64ToUint8Buffer(b64) {
michael@0 19 var str = atob(b64)
michael@0 20 var u8 = new Uint8Array(str.length);
michael@0 21 for (var i = 0; i < str.length; ++i) {
michael@0 22 u8[i] = str.charCodeAt(i);
michael@0 23 }
michael@0 24 return u8;
michael@0 25 }
michael@0 26
michael@0 27 function fixupBufferSampleRate(u8, rate) {
michael@0 28 u8[24] = (rate & 0x000000ff) >> 0;
michael@0 29 u8[25] = (rate & 0x0000ff00) >> 8;
michael@0 30 u8[26] = (rate & 0x00ff0000) >> 16;
michael@0 31 u8[27] = (rate & 0xff000000) >> 24;
michael@0 32 }
michael@0 33
michael@0 34 function finishTest() {
michael@0 35 testsDone += 1;
michael@0 36 if (testsDone == tests.length) {
michael@0 37 SimpleTest.finish();
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 function decodeComplete(b) {
michael@0 42 ok(true, "Decoding succeeded.");
michael@0 43 is(b.numberOfChannels, 1, "Should have 1 channel.");
michael@0 44 is(b.length, 3, "Should have three samples.");
michael@0 45 var samples = b.getChannelData(0);
michael@0 46 ok(samples[0] > 0.99 && samples[0] < 1.01, "Check near 1.0. Got " + samples[0]);
michael@0 47 ok(samples[1] > -1.01 && samples[1] < -0.99, "Check near -1.0. Got " + samples[1]);
michael@0 48 ok(samples[2] > -0.01 && samples[2] < 0.01, "Check near 0.0. Got " + samples[2]);
michael@0 49 finishTest();
michael@0 50 }
michael@0 51
michael@0 52 function decodeFailed() {
michael@0 53 ok(false, "Decoding failed.");
michael@0 54 finishTest();
michael@0 55 }
michael@0 56
michael@0 57 addLoadEvent(function() {
michael@0 58 var context = new AudioContext();
michael@0 59
michael@0 60 for (var i = 0; i < tests.length; ++i) {
michael@0 61 var u8 = base64ToUint8Buffer(tests[i]);
michael@0 62 fixupBufferSampleRate(u8, context.sampleRate);
michael@0 63 context.decodeAudioData(u8.buffer, decodeComplete, decodeFailed);
michael@0 64 }
michael@0 65 });
michael@0 66 </script>
michael@0 67 </pre>
michael@0 68 </body>
michael@0 69 </html>

mercurial