OLD | NEW |
1 <!doctype html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Test ConstantSourceNode Output</title> | 4 <title> |
| 5 Test ConstantSourceNode Output |
| 6 </title> |
5 <script src="../../resources/testharness.js"></script> | 7 <script src="../../resources/testharness.js"></script> |
6 <script src="../../resources/testharnessreport.js"></script> | 8 <script src="../../resources/testharnessreport.js"></script> |
7 <script src="../resources/audit-util.js"></script> | 9 <script src="../resources/audit-util.js"></script> |
8 <script src="../resources/audit.js"></script> | 10 <script src="../resources/audit.js"></script> |
9 <script src="../resources/audioparam-testing.js"></script> | 11 <script src="../resources/audioparam-testing.js"></script> |
10 </head> | 12 </head> |
| 13 <body> |
| 14 <script id="layout-test-code"> |
| 15 let sampleRate = 48000; |
| 16 let renderDuration = 0.125; |
| 17 let renderFrames = sampleRate * renderDuration; |
11 | 18 |
12 <body> | 19 let audit = Audit.createTaskRunner(); |
13 <script> | |
14 var sampleRate = 48000; | |
15 var renderDuration = 0.125; | |
16 var renderFrames = sampleRate * renderDuration; | |
17 | 20 |
18 var audit = Audit.createTaskRunner(); | 21 audit.define('constant source', (task, should) => { |
19 | |
20 audit.define("constant source", (task, should) => { | |
21 // Verify a constant source outputs the correct (fixed) constant. | 22 // Verify a constant source outputs the correct (fixed) constant. |
22 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 23 let context = new OfflineAudioContext(1, renderFrames, sampleRate); |
23 var node = new ConstantSourceNode(context, { | 24 let node = new ConstantSourceNode(context, {offset: 0.5}); |
24 offset: 0.5 | |
25 }); | |
26 node.connect(context.destination); | 25 node.connect(context.destination); |
27 node.start(); | 26 node.start(); |
28 | 27 |
29 context.startRendering().then(function (buffer) { | 28 context.startRendering() |
30 var actual = buffer.getChannelData(0); | 29 .then(function(buffer) { |
31 var expected = new Float32Array(actual.length); | 30 let actual = buffer.getChannelData(0); |
32 expected.fill(node.offset.value); | 31 let expected = new Float32Array(actual.length); |
| 32 expected.fill(node.offset.value); |
33 | 33 |
34 should(actual, "Basic: ConstantSourceNode({offset: 0.5})") | 34 should(actual, 'Basic: ConstantSourceNode({offset: 0.5})') |
35 .beEqualToArray(expected); | 35 .beEqualToArray(expected); |
36 }).then(() => task.done()); | 36 }) |
| 37 .then(() => task.done()); |
37 }); | 38 }); |
38 | 39 |
39 audit.define("start/stop", (task, should) => { | 40 audit.define('start/stop', (task, should) => { |
40 // Verify a constant source starts and stops at the correct time and has | 41 // Verify a constant source starts and stops at the correct time and has |
41 // the correct (fixed) value. | 42 // the correct (fixed) value. |
42 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 43 let context = new OfflineAudioContext(1, renderFrames, sampleRate); |
43 var node = new ConstantSourceNode(context, { | 44 let node = new ConstantSourceNode(context, {offset: 1}); |
44 offset: 1 | |
45 }); | |
46 node.connect(context.destination); | 45 node.connect(context.destination); |
47 | 46 |
48 var startFrame = 10; | 47 let startFrame = 10; |
49 var stopFrame = 300; | 48 let stopFrame = 300; |
50 | 49 |
51 node.start(startFrame / context.sampleRate); | 50 node.start(startFrame / context.sampleRate); |
52 node.stop(stopFrame / context.sampleRate); | 51 node.stop(stopFrame / context.sampleRate); |
53 | 52 |
54 context.startRendering().then(function (buffer) { | 53 context.startRendering() |
55 var actual = buffer.getChannelData(0); | 54 .then(function(buffer) { |
56 var expected = new Float32Array(actual.length); | 55 let actual = buffer.getChannelData(0); |
57 // The expected output is all 1s from start to stop time. | 56 let expected = new Float32Array(actual.length); |
58 expected.fill(0); | 57 // The expected output is all 1s from start to stop time. |
| 58 expected.fill(0); |
59 | 59 |
60 for (var k = startFrame; k < stopFrame; ++k) { | 60 for (let k = startFrame; k < stopFrame; ++k) { |
61 expected[k] = node.offset.value; | 61 expected[k] = node.offset.value; |
62 } | 62 } |
63 | 63 |
64 var prefix = "start/stop: "; | 64 let prefix = 'start/stop: '; |
65 should(actual.slice(0, startFrame, | 65 should(actual.slice( |
66 prefix + "ConstantSourceNode frames [0, " + | 66 0, startFrame, |
67 startFrame + ")" | 67 prefix + 'ConstantSourceNode frames [0, ' + |
68 )) | 68 startFrame + ')')) |
69 .beConstantValueOf(0); | 69 .beConstantValueOf(0); |
70 | 70 |
71 should(actual.slice(startFrame, stopFrame, | 71 should(actual.slice( |
72 prefix + "ConstantSourceNode frames [" + startFrame + | 72 startFrame, stopFrame, |
73 ", " + | 73 prefix + 'ConstantSourceNode frames [' + startFrame + |
74 stopFrame + ")")) | 74 ', ' + stopFrame + ')')) |
75 .beConstantValueOf(1); | 75 .beConstantValueOf(1); |
76 | 76 |
77 should(actual.slice(stopFrame), | 77 should( |
78 prefix + "ConstantSourceNode frames [" + stopFrame + ", " + | 78 actual.slice(stopFrame), |
79 renderFrames + ")") | 79 prefix + 'ConstantSourceNode frames [' + stopFrame + ', ' + |
80 .beConstantValueOf(0); | 80 renderFrames + ')') |
81 }).then(() => task.done()); | 81 .beConstantValueOf(0); |
82 | 82 }) |
| 83 .then(() => task.done()); |
| 84 |
83 }); | 85 }); |
84 | 86 |
85 audit.define("basic automation", (task, should) => { | 87 audit.define('basic automation', (task, should) => { |
86 // Verify that automation works as expected. | 88 // Verify that automation works as expected. |
87 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 89 let context = new OfflineAudioContext(1, renderFrames, sampleRate); |
88 var source = context.createConstantSource(); | 90 let source = context.createConstantSource(); |
89 source.connect(context.destination); | 91 source.connect(context.destination); |
90 | 92 |
91 var rampEndTime = renderDuration / 2; | 93 let rampEndTime = renderDuration / 2; |
92 source.offset.setValueAtTime(0.5, 0); | 94 source.offset.setValueAtTime(0.5, 0); |
93 source.offset.linearRampToValueAtTime(1, rampEndTime); | 95 source.offset.linearRampToValueAtTime(1, rampEndTime); |
94 | 96 |
95 source.start(); | 97 source.start(); |
96 | 98 |
97 context.startRendering() | 99 context.startRendering() |
98 .then(function (buffer) { | 100 .then(function(buffer) { |
99 var actual = buffer.getChannelData(0); | 101 let actual = buffer.getChannelData(0); |
100 var expected = createLinearRampArray(0, rampEndTime, 0.5, 1, | 102 let expected = createLinearRampArray( |
101 context.sampleRate); | 103 0, rampEndTime, 0.5, 1, context.sampleRate); |
102 | 104 |
103 var rampEndFrame = Math.ceil(rampEndTime * context.sampleRate); | 105 let rampEndFrame = Math.ceil(rampEndTime * context.sampleRate); |
104 var prefix = "Automation: "; | 106 let prefix = 'Automation: '; |
105 | 107 |
106 should(actual.slice(0, rampEndFrame, | 108 should(actual.slice( |
107 prefix + "ConstantSourceNode.linearRamp(1, 0.5)")) | 109 0, rampEndFrame, |
108 .beCloseToArray(expected, { | 110 prefix + 'ConstantSourceNode.linearRamp(1, 0.5)')) |
109 // Experimentally determined threshold.. | 111 .beCloseToArray(expected, { |
110 relativeThreshold: 7.1610e-7 | 112 // Experimentally determined threshold.. |
111 }); | 113 relativeThreshold: 7.1610e-7 |
| 114 }); |
112 | 115 |
113 should(actual.slice(rampEndFrame), | 116 should( |
114 prefix + "ConstantSourceNode after ramp") | 117 actual.slice(rampEndFrame), |
115 .beConstantValueOf(1); | 118 prefix + 'ConstantSourceNode after ramp') |
116 }) | 119 .beConstantValueOf(1); |
117 .then(() => task.done()); | 120 }) |
| 121 .then(() => task.done()); |
118 }); | 122 }); |
119 | 123 |
120 audit.define("connected audioparam", (task, should) => { | 124 audit.define('connected audioparam', (task, should) => { |
121 // Verify the constant source output with connected AudioParam produces | 125 // Verify the constant source output with connected AudioParam produces |
122 // the correct output. | 126 // the correct output. |
123 var context = new OfflineAudioContext(2, renderFrames, sampleRate) | 127 let context = new OfflineAudioContext(2, renderFrames, sampleRate) |
124 context.destination.channelInterpretation = "discrete"; | 128 context.destination.channelInterpretation = 'discrete'; |
125 var source = new ConstantSourceNode(context, { | 129 let source = new ConstantSourceNode(context, {offset: 1}); |
126 offset: 1 | 130 let osc = context.createOscillator(); |
127 }); | 131 let merger = context.createChannelMerger(2); |
128 var osc = context.createOscillator(); | |
129 var merger = context.createChannelMerger(2); | |
130 merger.connect(context.destination); | 132 merger.connect(context.destination); |
131 | 133 |
132 source.connect(merger, 0, 0); | 134 source.connect(merger, 0, 0); |
133 osc.connect(merger, 0, 1); | 135 osc.connect(merger, 0, 1); |
134 osc.connect(source.offset); | 136 osc.connect(source.offset); |
135 | 137 |
136 osc.start(); | 138 osc.start(); |
137 var sourceStartFrame = 10; | 139 let sourceStartFrame = 10; |
138 source.start(sourceStartFrame / context.sampleRate); | 140 source.start(sourceStartFrame / context.sampleRate); |
139 | 141 |
140 context.startRendering() | 142 context.startRendering() |
141 .then(function (buffer) { | 143 .then(function(buffer) { |
142 // Channel 0 and 1 should be identical, except channel 0 (the | 144 // Channel 0 and 1 should be identical, except channel 0 (the |
143 // source) is silent at the beginning. | 145 // source) is silent at the beginning. |
144 var actual = buffer.getChannelData(0); | 146 let actual = buffer.getChannelData(0); |
145 var expected = buffer.getChannelData(1); | 147 let expected = buffer.getChannelData(1); |
146 // The expected output should be oscillator + 1 because offset | 148 // The expected output should be oscillator + 1 because offset |
147 // is 1. | 149 // is 1. |
148 expected = expected.map(x => 1 + x); | 150 expected = expected.map(x => 1 + x); |
149 var prefix = "Connected param: "; | 151 let prefix = 'Connected param: '; |
150 | 152 |
151 // The initial part of the output should be silent because the | 153 // The initial part of the output should be silent because the |
152 // source node hasn't started yet. | 154 // source node hasn't started yet. |
153 should(actual.slice(0, sourceStartFrame), | 155 should( |
154 prefix + "ConstantSourceNode frames [0, " + | 156 actual.slice(0, sourceStartFrame), |
155 sourceStartFrame + ")") | 157 prefix + 'ConstantSourceNode frames [0, ' + sourceStartFrame + |
156 .beConstantValueOf(0); | 158 ')') |
157 // The rest of the output should be the same as the oscillator (in | 159 .beConstantValueOf(0); |
158 // channel 1) | 160 // The rest of the output should be the same as the oscillator (in |
159 should(actual.slice(sourceStartFrame), | 161 // channel 1) |
160 prefix + "ConstantSourceNode frames [" + | 162 should( |
161 sourceStartFrame + ", " + renderFrames + ")") | 163 actual.slice(sourceStartFrame), |
162 .beCloseToArray(expected.slice(sourceStartFrame), 0); | 164 prefix + 'ConstantSourceNode frames [' + sourceStartFrame + |
| 165 ', ' + renderFrames + ')') |
| 166 .beCloseToArray(expected.slice(sourceStartFrame), 0); |
163 | 167 |
164 }) | 168 }) |
165 .then(() => task.done()); | 169 .then(() => task.done()); |
166 }); | 170 }); |
167 | 171 |
168 audit.run(); | 172 audit.run(); |
169 </script> | 173 </script> |
170 </body> | 174 </body> |
171 </html> | 175 </html> |
OLD | NEW |