File: buffer-resampling.html

package info (click to toggle)
thunderbird 1%3A115.16.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,476,252 kB
  • sloc: cpp: 6,972,150; javascript: 5,209,211; ansic: 3,507,222; python: 1,137,609; asm: 432,531; xml: 205,149; java: 175,761; sh: 116,485; makefile: 22,152; perl: 13,971; objc: 12,561; yacc: 4,583; pascal: 2,840; lex: 1,720; ruby: 1,075; exp: 762; sql: 666; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (101 lines) | stat: -rw-r--r-- 3,726 bytes parent folder | download | duplicates (27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!doctype html>
<html>
  <head>
    <title>Test Extrapolation at end of AudibBuffer in an AudioBufferSourceNode</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script>
      let audit = Audit.createTaskRunner();

      const sampleRate = 48000;

      // For testing we only need a few render quanta.
      const renderSamples = 512

      // Sample rate for our buffers.  This is the lowest sample rate that is
      // required to be supported.
      const bufferRate = 8000;

      // Number of samples in each AudioBuffer; this is fairly arbitrary but
      // should be less than a render quantum.
      const bufferLength = 30;

      // Frequency of the sine wave for testing.
      const frequency = 440;

      audit.define(
          {
            label: 'interpolate',
            description: 'Interpolation of AudioBuffers to context sample rate'
          },
          (task, should) => {
            // The first channel is for the interpolated signal, and the second
            // channel is for the reference signal from an oscillator.
            let context = new OfflineAudioContext({
              numberOfChannels: 2,
              length: renderSamples,
              sampleRate: sampleRate
            });

            let merger = new ChannelMergerNode(
                context, {numberOfChannels: context.destination.channelCount});
            merger.connect(context.destination);

            // Create a set of AudioBuffers which are samples from a pure sine
            // wave with frequency |frequency|.
            const nBuffers = Math.floor(context.length / bufferLength);
            const omega = 2 * Math.PI * frequency / bufferRate;

            let frameNumber = 0;
            let startTime = 0;

            for (let k = 0; k < nBuffers; ++k) {
              //          let buffer = context.createBuffer(1, bufferLength,
              //          bufferRate);
              let buffer = new AudioBuffer(
                  {length: bufferLength, sampleRate: bufferRate});
              let data = buffer.getChannelData(0);
              for (let n = 0; n < bufferLength; ++n) {
                data[n] = Math.sin(omega * frameNumber);
                ++frameNumber;
              }
              // Create a source using this buffer and start it at the end of
              // the previous buffer.
              let src = new AudioBufferSourceNode(context, {buffer: buffer});

              src.connect(merger, 0, 0);
              src.start(startTime);
              startTime += buffer.duration;
            }

            // Create the reference sine signal using an oscillator.
            let osc = new OscillatorNode(
                context, {type: 'sine', frequency: frequency});
            osc.connect(merger, 0, 1);
            osc.start(0);

            context.startRendering()
                .then(audioBuffer => {
                  let actual = audioBuffer.getChannelData(0);
                  let expected = audioBuffer.getChannelData(1);

                  should(actual, 'Interpolated sine wave')
                      .beCloseToArray(expected, {absoluteThreshold: 9.0348e-2});

                  // Compute SNR between them.
                  let snr = 10 * Math.log10(computeSNR(actual, expected));

                  should(snr, `SNR (${snr.toPrecision(4)} dB)`)
                      .beGreaterThanOrEqualTo(37.17);
                })
                .then(() => task.done());
          });

      audit.run();
    </script>
  </body>
</html>