tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cancel-scheduled-values.html (3996B)


      1 <!doctype html>
      2 <html>
      3  <head>
      4    <title>
      5      cancelScheduledValues
      6    </title>
      7    <script src="/resources/testharness.js"></script>
      8    <script src="/resources/testharnessreport.js"></script>
      9    <script src="/webaudio/resources/audit-util.js"></script>
     10  </head>
     11  <body>
     12    <script>
     13 
     14      const sampleRate = 8000;
     15      const renderFrames = 8000;
     16 
     17 
     18      test(t => {
     19        const context = new OfflineAudioContext({
     20          numberOfChannels: 1,
     21          length: renderFrames,
     22          sampleRate: sampleRate
     23        });
     24 
     25        const source = new ConstantSourceNode(context);
     26        source.connect(context.destination);
     27 
     28        assert_throws_js(RangeError,
     29          () => source.offset.cancelScheduledValues(-1),
     30          'cancelScheduledValues(-1)');
     31 
     32        // |cancelTime| is a double, so NaN and Infinity must throw TypeError.
     33        assert_throws_js(TypeError,
     34          () => source.offset.cancelScheduledValues(NaN),
     35          'cancelScheduledValues(NaN)');
     36 
     37        assert_throws_js(TypeError,
     38          () => source.offset.cancelScheduledValues(Infinity),
     39          'cancelScheduledValues(Infinity)');
     40      }, 'cancel‑time: handle cancelTime values');
     41 
     42      promise_test(async t => {
     43        const context = new OfflineAudioContext({
     44          numberOfChannels: 1,
     45          length: renderFrames,
     46          sampleRate: sampleRate
     47        });
     48 
     49        const source = new ConstantSourceNode(context);
     50        const gain = new GainNode(context);
     51        source.connect(gain).connect(context.destination);
     52 
     53        // Initial time and value for first automation (setValue)
     54        const time0 = 0;
     55        const value0 = 0.5;
     56 
     57        // Time and duration of the setValueCurve. We'll also schedule a
     58        // setValue at the same time.
     59        const value1 = 1.5;
     60        const curveStartTime = 0.25;
     61        const curveDuration = 0.25;
     62 
     63        // Time at which to cancel events
     64        const cancelTime = 0.3;
     65 
     66        // Time and value for event added after cancelScheduledValues has
     67        // been called.
     68        const time2 = curveStartTime + curveDuration / 2;
     69        const value2 = 3;
     70 
     71        // Self‑consistency checks for the test.
     72        assert_greater_than(cancelTime, curveStartTime,
     73                            'cancelTime is after curve start');
     74        assert_less_than(cancelTime, curveStartTime + curveDuration,
     75                         'cancelTime is before curve ends');
     76 
     77        // These assertions are just to show what's happening
     78        gain.gain.setValueAtTime(value0, time0);
     79        // setValue at the same time as the curve, to test that this event
     80        // wasn't removed.
     81        gain.gain.setValueAtTime(value1, curveStartTime);
     82 
     83        gain.gain.setValueCurveAtTime([1, -1], curveStartTime, curveDuration);
     84 
     85        // An event after the curve to verify this is removed.
     86        gain.gain.setValueAtTime(99, curveStartTime + curveDuration);
     87 
     88        // Cancel events now.
     89        gain.gain.cancelScheduledValues(cancelTime);
     90 
     91        // Simple check that the setValueCurve is gone, by scheduling
     92        // something in the middle of the (now deleted) event
     93        gain.gain.setValueAtTime(value2, time2);
     94 
     95        source.start();
     96        const buffer = await context.startRendering();
     97        const audio = buffer.getChannelData(0);
     98 
     99        const curveFrame = curveStartTime * context.sampleRate;
    100        const time2Frame = time2 * context.sampleRate;
    101 
    102        assert_constant_value(audio.slice(0, curveFrame),
    103                              value0,
    104                              `output[0:${curveFrame - 1}]`);
    105 
    106        assert_constant_value(audio.slice(curveFrame, time2Frame),
    107                              value1,
    108                              `output[${curveFrame}:${time2Frame - 1}]`);
    109 
    110        assert_constant_value(audio.slice(time2Frame),
    111                              value2,
    112                              `output[${time2Frame}:]`);
    113      }, 'cancel1: cancel setValueCurve');
    114    </script>
    115  </body>
    116 </html>