OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 | |
3 <!-- | 2 <!-- |
4 Tests that an audio-rate signal (AudioNode output) can be connected to an | 3 Tests that an audio-rate signal (AudioNode output) can be connected to an |
5 AudioParam. Specifically, this tests that an audio-rate signal coming from an | 4 AudioParam. Specifically, this tests that an audio-rate signal coming from an |
6 AudioBufferSourceNode playing an AudioBuffer containing a specific curve can be | 5 AudioBufferSourceNode playing an AudioBuffer containing a specific curve can be |
7 connected to an AudioGainNode's .gain attribute (an AudioParam). Another | 6 connected to an AudioGainNode's .gain attribute (an AudioParam). Another |
8 AudioBufferSourceNode will be the audio source having its gain changed. We load | 7 AudioBufferSourceNode will be the audio source having its gain changed. We load |
9 this one with an AudioBuffer containing a constant value of 1. Thus it's easy | 8 this one with an AudioBuffer containing a constant value of 1. Thus it's easy |
10 to check that the resultant signal should be equal to the gain-scaling curve. | 9 to check that the resultant signal should be equal to the gain-scaling curve. |
11 --> | 10 --> |
| 11 <html> |
| 12 <head> |
| 13 <title> |
| 14 audioparam-connect-audioratesignal.html |
| 15 </title> |
| 16 <script src="../../resources/testharness.js"></script> |
| 17 <script src="../../resources/testharnessreport.js"></script> |
| 18 <script src="../resources/audit-util.js"></script> |
| 19 <script src="../resources/audit.js"></script> |
| 20 </head> |
| 21 <body> |
| 22 <script id="layout-test-code"> |
| 23 let audit = Audit.createTaskRunner(); |
12 | 24 |
13 <html> | 25 let sampleRate = 44100.0; |
14 <head> | 26 let lengthInSeconds = 1; |
15 <script src="../../resources/testharness.js"></script> | |
16 <script src="../../resources/testharnessreport.js"></script> | |
17 <script src="../resources/audit-util.js"></script> | |
18 <script src="../resources/audit.js"></script> | |
19 | 27 |
20 </head> | 28 let context = 0; |
21 <body> | 29 let constantOneBuffer = 0; |
| 30 let linearRampBuffer = 0; |
22 | 31 |
23 <script> | 32 function checkResult(renderedBuffer, should) { |
24 let audit = Audit.createTaskRunner(); | 33 let renderedData = renderedBuffer.getChannelData(0); |
| 34 let expectedData = linearRampBuffer.getChannelData(0); |
| 35 let n = renderedBuffer.length; |
25 | 36 |
26 let sampleRate = 44100.0; | 37 should(n, 'Rendered signal length').beEqualTo(linearRampBuffer.length); |
27 let lengthInSeconds = 1; | |
28 | 38 |
29 let context = 0; | 39 // Check that the rendered result exactly matches the buffer used to |
30 let constantOneBuffer = 0; | 40 // control gain. This is because we're changing the gain of a signal |
31 let linearRampBuffer = 0; | 41 // having constant value 1. |
| 42 let success = true; |
| 43 for (let i = 0; i < n; ++i) { |
| 44 if (renderedData[i] != expectedData[i]) { |
| 45 success = false; |
| 46 break; |
| 47 } |
| 48 } |
32 | 49 |
33 function checkResult(renderedBuffer, should) { | 50 should( |
34 let renderedData = renderedBuffer.getChannelData(0); | 51 success, |
35 let expectedData = linearRampBuffer.getChannelData(0); | 52 'Rendered signal exactly matches the audio-rate gain changing signal
') |
36 let n = renderedBuffer.length; | 53 .beTrue(); |
| 54 } |
37 | 55 |
38 should(n, 'Rendered signal length').beEqualTo(linearRampBuffer.length); | 56 audit.define('test', function(task, should) { |
| 57 let sampleFrameLength = sampleRate * lengthInSeconds; |
39 | 58 |
40 // Check that the rendered result exactly matches the buffer used to control | 59 // Create offline audio context. |
41 // gain. This is because we're changing the gain of a signal having constant | 60 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate); |
42 // value 1. | |
43 let success = true; | |
44 for (let i = 0; i < n; ++i) { | |
45 if (renderedData[i] != expectedData[i]) { | |
46 success = false; | |
47 break; | |
48 } | |
49 } | |
50 | 61 |
51 should( | 62 // Create buffer used by the source which will have its gain controlled. |
52 success, | 63 constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1); |
53 'Rendered signal exactly matches the audio-rate gain changing signal') | |
54 .beTrue(); | |
55 } | |
56 | 64 |
57 audit.define('test', function(task, should) { | 65 // Create buffer used to control gain. |
58 let sampleFrameLength = sampleRate * lengthInSeconds; | 66 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength); |
59 | 67 |
60 // Create offline audio context. | 68 // Create the two sources. |
61 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate); | |
62 | 69 |
63 // Create buffer used by the source which will have its gain controlled. | 70 let constantSource = context.createBufferSource(); |
64 constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1); | 71 constantSource.buffer = constantOneBuffer; |
65 | 72 |
66 // Create buffer used to control gain. | 73 let gainChangingSource = context.createBufferSource(); |
67 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength); | 74 gainChangingSource.buffer = linearRampBuffer; |
68 | 75 |
69 // Create the two sources. | 76 // Create a gain node controlling the gain of constantSource and make |
| 77 // the connections. |
| 78 let gainNode = context.createGain(); |
70 | 79 |
71 let constantSource = context.createBufferSource(); | 80 // Intrinsic baseline gain of zero. |
72 constantSource.buffer = constantOneBuffer; | 81 gainNode.gain.value = 0; |
73 | 82 |
74 let gainChangingSource = context.createBufferSource(); | 83 constantSource.connect(gainNode); |
75 gainChangingSource.buffer = linearRampBuffer; | 84 gainNode.connect(context.destination); |
76 | 85 |
77 // Create a gain node controlling the gain of constantSource and make the | 86 // Connect an audio-rate signal to control the .gain AudioParam. |
78 // connections. | 87 // This is the heart of what is being tested. |
79 let gainNode = context.createGain(); | 88 gainChangingSource.connect(gainNode.gain); |
80 | 89 |
81 // Intrinsic baseline gain of zero. | 90 // Start both sources at time 0. |
82 gainNode.gain.value = 0; | 91 constantSource.start(0); |
| 92 gainChangingSource.start(0); |
83 | 93 |
84 constantSource.connect(gainNode); | 94 context.startRendering().then(buffer => { |
85 gainNode.connect(context.destination); | 95 checkResult(buffer, should); |
| 96 task.done(); |
| 97 }); |
| 98 }); |
86 | 99 |
87 // Connect an audio-rate signal to control the .gain AudioParam. | 100 audit.run(); |
88 // This is the heart of what is being tested. | 101 </script> |
89 gainChangingSource.connect(gainNode.gain); | 102 </body> |
90 | |
91 // Start both sources at time 0. | |
92 constantSource.start(0); | |
93 gainChangingSource.start(0); | |
94 | |
95 context.startRendering().then(buffer => { | |
96 checkResult(buffer, should); | |
97 task.done(); | |
98 }); | |
99 }); | |
100 | |
101 audit.run(); | |
102 | |
103 </script> | |
104 | |
105 </body> | |
106 </html> | 103 </html> |
OLD | NEW |