content/media/webaudio/test/test_waveDecoder.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/test/test_waveDecoder.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<meta charset=utf-8>
     1.7 +<head>
     1.8 +  <title>Test that we decode uint8 and sint16 wave files with correct conversion to float64</title>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +<pre id="test">
    1.14 +<script class="testbody" type="text/javascript">
    1.15 +var testsDone = 0;
    1.16 +var tests = ["UklGRjUrAABXQVZFZm10IBAAAAABAAEAESsAABErAAABAAgAZGF0YQMAAAD/AIA=",
    1.17 +             "UklGRkZWAABXQVZFZm10IBAAAAABAAEAESsAACJWAAACABAAZGF0YQYAAAD/fwCAAAA="];
    1.18 +
    1.19 +SimpleTest.waitForExplicitFinish();
    1.20 +
    1.21 +function base64ToUint8Buffer(b64) {
    1.22 +  var str = atob(b64)
    1.23 +  var u8 = new Uint8Array(str.length);
    1.24 +  for (var i = 0; i < str.length; ++i) {
    1.25 +    u8[i] = str.charCodeAt(i);
    1.26 +  }
    1.27 +  return u8;
    1.28 +}
    1.29 +
    1.30 +function fixupBufferSampleRate(u8, rate) {
    1.31 +  u8[24] = (rate & 0x000000ff) >> 0;
    1.32 +  u8[25] = (rate & 0x0000ff00) >> 8;
    1.33 +  u8[26] = (rate & 0x00ff0000) >> 16;
    1.34 +  u8[27] = (rate & 0xff000000) >> 24;
    1.35 +}
    1.36 +
    1.37 +function finishTest() {
    1.38 +  testsDone += 1;
    1.39 +  if (testsDone == tests.length) {
    1.40 +    SimpleTest.finish();
    1.41 +  }
    1.42 +}
    1.43 +
    1.44 +function decodeComplete(b) {
    1.45 +  ok(true, "Decoding succeeded.");
    1.46 +  is(b.numberOfChannels, 1, "Should have 1 channel.");
    1.47 +  is(b.length, 3, "Should have three samples.");
    1.48 +  var samples = b.getChannelData(0);
    1.49 +  ok(samples[0] >  0.99 && samples[0] <  1.01, "Check near  1.0. Got " + samples[0]);
    1.50 +  ok(samples[1] > -1.01 && samples[1] < -0.99, "Check near -1.0. Got " + samples[1]);
    1.51 +  ok(samples[2] > -0.01 && samples[2] <  0.01, "Check near  0.0. Got " + samples[2]);
    1.52 +  finishTest();
    1.53 +}
    1.54 +
    1.55 +function decodeFailed() {
    1.56 +  ok(false, "Decoding failed.");
    1.57 +  finishTest();
    1.58 +}
    1.59 +
    1.60 +addLoadEvent(function() {
    1.61 +  var context = new AudioContext();
    1.62 +
    1.63 +  for (var i = 0; i < tests.length; ++i) {
    1.64 +    var u8 = base64ToUint8Buffer(tests[i]);
    1.65 +    fixupBufferSampleRate(u8, context.sampleRate);
    1.66 +    context.decodeAudioData(u8.buffer, decodeComplete, decodeFailed);
    1.67 +  }
    1.68 +});
    1.69 +</script>
    1.70 +</pre>
    1.71 +</body>
    1.72 +</html>

mercurial