OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Test Convolver Channel Outputs for Response with 2 channels</title> |
| 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/audit-util.js"></script> |
| 8 <script src="../resources/audit.js"></script> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 // Test various convolver configurations when the convolver response has |
| 14 // a stereo response. |
| 15 |
| 16 // Fairly arbitrary sample rate, except that we want the rate to be a |
| 17 // power of two so that 1/sampleRate is exactly respresentable as a |
| 18 // single-precision float. |
| 19 let sampleRate = 8192; |
| 20 |
| 21 // A fairly arbitrary number of frames, except the number of frames should |
| 22 // be more than a few render quanta. |
| 23 let renderFrames = 10 * 128; |
| 24 |
| 25 let audit = Audit.createTaskRunner(); |
| 26 |
| 27 // Convolver response |
| 28 let response; |
| 29 |
| 30 audit.define( |
| 31 { |
| 32 label: 'initialize', |
| 33 description: 'Convolver response with one channel' |
| 34 }, |
| 35 (task, should) => { |
| 36 // Convolver response |
| 37 should( |
| 38 () => { |
| 39 response = new AudioBuffer( |
| 40 {numberOfChannels: 2, length: 4, sampleRate: sampleRate}); |
| 41 // Each channel of the response is a simple impulse (with |
| 42 // different delay) so that we can use a DelayNode to simulate |
| 43 // the convolver output. Channel k is delayed by k+1 frames. |
| 44 for (let k = 0; k < response.numberOfChannels; ++k) { |
| 45 response.getChannelData(k)[k + 1] = 1; |
| 46 } |
| 47 }, |
| 48 'new AudioBuffer({numberOfChannels: 2, length: 4, sampleRate: '
+ |
| 49 sampleRate + '})') |
| 50 .notThrow(); |
| 51 |
| 52 task.done(); |
| 53 }); |
| 54 |
| 55 audit.define( |
| 56 {label: '1-channel input', description: 'produces 2-channel output'}, |
| 57 (task, should) => { |
| 58 stereoResponseTest({numberOfInputs: 1, prefix: '1'}, should) |
| 59 .then(() => task.done()); |
| 60 }); |
| 61 |
| 62 audit.define( |
| 63 {label: '2-channel input', description: 'produces 2-channel output'}, |
| 64 (task, should) => { |
| 65 stereoResponseTest({numberOfInputes: 2, prefix: '2'}, should) |
| 66 .then(() => task.done()); |
| 67 }); |
| 68 |
| 69 audit.define( |
| 70 { |
| 71 label: '3-channel input', |
| 72 description: '3->2 downmix producing 2-channel output' |
| 73 }, |
| 74 (task, should) => { |
| 75 stereoResponseTest({numberOfInputs: 3, prefix: '3'}, should) |
| 76 .then(() => task.done()); |
| 77 }); |
| 78 |
| 79 audit.define( |
| 80 { |
| 81 label: '4-channel input', |
| 82 description: '4->2 downmix producing 2-channel output' |
| 83 }, |
| 84 (task, should) => { |
| 85 stereoResponseTest({numberOfInputs: 4, prefix: '4'}, should) |
| 86 .then(() => task.done()); |
| 87 }); |
| 88 |
| 89 audit.define( |
| 90 { |
| 91 label: '5.1-channel input', |
| 92 description: '5.1->2 downmix producing 2-channel output' |
| 93 }, |
| 94 (task, should) => { |
| 95 stereoResponseTest({numberOfInputs: 6, prefix: '5.1'}, should) |
| 96 .then(() => task.done()); |
| 97 }); |
| 98 |
| 99 function stereoResponseTest(options, should) { |
| 100 // Create an 4-channel offline context. The first two channels are for |
| 101 // the stereo output of the convolver and the next two channels are for |
| 102 // the reference stereo signal. |
| 103 let context = new OfflineAudioContext(4, renderFrames, sampleRate); |
| 104 context.destination.channelInterpretation = 'discrete'; |
| 105 |
| 106 // Create oscillators for use as the input. The type and frequency is |
| 107 // arbitrary except that oscillators must be different. |
| 108 let src = new Array(options.numberOfInputs); |
| 109 for (let k = 0; k < src.length; ++k) { |
| 110 src[k] = new OscillatorNode( |
| 111 context, {type: 'square', frequency: 440 + 220 * k}); |
| 112 } |
| 113 |
| 114 // Merger to combine the oscillators into one output stream. |
| 115 let srcMerger = |
| 116 new ChannelMergerNode(context, {numberOfInputs: src.length}); |
| 117 |
| 118 for (let k = 0; k < src.length; ++k) { |
| 119 src[k].connect(srcMerger, 0, k); |
| 120 } |
| 121 |
| 122 // Convolver under test. |
| 123 let conv = new ConvolverNode( |
| 124 context, {disableNormalization: true, buffer: response}); |
| 125 srcMerger.connect(conv); |
| 126 |
| 127 // Splitter to get individual channels of the convolver output so we can |
| 128 // feed them (eventually) to the context in the right set of channels. |
| 129 let splitter = new ChannelSplitterNode(context, {numberOfOutputs: 2}); |
| 130 conv.connect(splitter); |
| 131 |
| 132 // Reference graph consists of a delays node to simulate the response of |
| 133 // the convolver. (The convolver response is designed this way.) |
| 134 let delay = new Array(2); |
| 135 for (let k = 0; k < delay.length; ++k) { |
| 136 delay[k] = new DelayNode(context, { |
| 137 delayTime: (k + 1) / context.sampleRate, |
| 138 channelCount: 1, |
| 139 channelCountMode: 'explicit' |
| 140 }); |
| 141 } |
| 142 |
| 143 // Gain node to mix the sources to stereo in the desired way. (Could be |
| 144 // done in the delay node, but let's keep the mixing separated from the |
| 145 // functionality.) |
| 146 let gainMixer = new GainNode( |
| 147 context, {channelCount: 2, channelCountMode: 'explicit'}); |
| 148 srcMerger.connect(gainMixer); |
| 149 |
| 150 // Splitter to extract the channels of the reference signal. |
| 151 let refSplitter = |
| 152 new ChannelSplitterNode(context, {numberOfOutputs: 2}); |
| 153 gainMixer.connect(refSplitter); |
| 154 |
| 155 // Connect each channel to the delay nodes |
| 156 for (let k = 0; k < delay.length; ++k) { |
| 157 refSplitter.connect(delay[k], k); |
| 158 } |
| 159 |
| 160 // Final merger to bring back the individual channels from the convolver |
| 161 // and the reference in the right order for the destination. |
| 162 let finalMerger = new ChannelMergerNode( |
| 163 context, {numberOfInputs: context.destination.channelCount}); |
| 164 |
| 165 // First two channels are for the convolver output, and the next two are |
| 166 // for the reference. |
| 167 splitter.connect(finalMerger, 0, 0); |
| 168 splitter.connect(finalMerger, 1, 1); |
| 169 delay[0].connect(finalMerger, 0, 2); |
| 170 delay[1].connect(finalMerger, 0, 3); |
| 171 |
| 172 finalMerger.connect(context.destination); |
| 173 |
| 174 // Start the sources at last. |
| 175 for (let k = 0; k < src.length; ++k) { |
| 176 src[k].start(); |
| 177 } |
| 178 |
| 179 return context.startRendering().then(audioBuffer => { |
| 180 // Extract the various channels out |
| 181 let actual0 = audioBuffer.getChannelData(0); |
| 182 let actual1 = audioBuffer.getChannelData(1); |
| 183 let expected0 = audioBuffer.getChannelData(2); |
| 184 let expected1 = audioBuffer.getChannelData(3); |
| 185 |
| 186 // Verify that each output channel of the convolver matches |
| 187 // the delayed signal from the reference |
| 188 should(actual0, options.prefix + ': Channel 0') |
| 189 .beEqualToArray(expected0); |
| 190 should(actual1, options.prefix + ': Channel 1') |
| 191 .beEqualToArray(expected1); |
| 192 }); |
| 193 } |
| 194 |
| 195 audit.run(); |
| 196 </script> |
| 197 </body> |
| 198 </html> |
OLD | NEW |