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

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

mercurial