|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test ScriptProcessorNode</title> |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <script type="text/javascript" src="webaudio.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
8 </head> |
|
9 <body> |
|
10 <pre id="test"> |
|
11 <script class="testbody" type="text/javascript"> |
|
12 |
|
13 // We do not use our generic graph test framework here because |
|
14 // the testing logic here is sort of complicated, and would |
|
15 // not be easy to map to OfflineAudioContext, as ScriptProcessorNodes |
|
16 // can experience delays. |
|
17 |
|
18 SimpleTest.waitForExplicitFinish(); |
|
19 addLoadEvent(function() { |
|
20 var context = new AudioContext(); |
|
21 var buffer = null; |
|
22 |
|
23 var sourceSP = context.createScriptProcessor(2048); |
|
24 sourceSP.addEventListener("audioprocess", function(e) { |
|
25 // generate the audio |
|
26 for (var i = 0; i < 2048; ++i) { |
|
27 // Make sure our first sample won't be zero |
|
28 e.outputBuffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * (i + 1) / context.sampleRate); |
|
29 e.outputBuffer.getChannelData(0)[i] = Math.sin(880 * 2 * Math.PI * (i + 1) / context.sampleRate); |
|
30 } |
|
31 // Remember our generated audio |
|
32 buffer = e.outputBuffer; |
|
33 |
|
34 sourceSP.removeEventListener("audioprocess", arguments.callee); |
|
35 }, false); |
|
36 |
|
37 expectException(function() { |
|
38 context.createScriptProcessor(1); |
|
39 }, DOMException.INDEX_SIZE_ERR); |
|
40 expectException(function() { |
|
41 context.createScriptProcessor(2); |
|
42 }, DOMException.INDEX_SIZE_ERR); |
|
43 expectException(function() { |
|
44 context.createScriptProcessor(128); |
|
45 }, DOMException.INDEX_SIZE_ERR); |
|
46 expectException(function() { |
|
47 context.createScriptProcessor(255); |
|
48 }, DOMException.INDEX_SIZE_ERR); |
|
49 |
|
50 is(sourceSP.channelCount, 2, "script processor node has 2 input channels by default"); |
|
51 is(sourceSP.channelCountMode, "explicit", "Correct channelCountMode for the script processor node"); |
|
52 is(sourceSP.channelInterpretation, "speakers", "Correct channelCountInterpretation for the script processor node"); |
|
53 |
|
54 function findFirstNonZeroSample(buffer) { |
|
55 for (var i = 0; i < buffer.length; ++i) { |
|
56 if (buffer.getChannelData(0)[i] != 0) { |
|
57 return i; |
|
58 } |
|
59 } |
|
60 return buffer.length; |
|
61 } |
|
62 |
|
63 var sp = context.createScriptProcessor(2048); |
|
64 sourceSP.connect(sp); |
|
65 sp.connect(context.destination); |
|
66 var lastPlaybackTime = 0; |
|
67 sp.onaudioprocess = function(e) { |
|
68 isnot(buffer, null, "The audioprocess handler for sourceSP must be run at this point"); |
|
69 is(e.target, sp, "Correct event target"); |
|
70 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set"); |
|
71 lastPlaybackTime = e.playbackTime; |
|
72 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer"); |
|
73 is(e.inputBuffer.length, 2048, "Correct length for the input buffer"); |
|
74 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer"); |
|
75 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer"); |
|
76 is(e.outputBuffer.length, 2048, "Correct length for the output buffer"); |
|
77 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer"); |
|
78 |
|
79 // Because of the initial latency added by the second script processor node, |
|
80 // we will never see any generated audio frames in the first callback. |
|
81 var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate); |
|
82 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0)); |
|
83 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0)); |
|
84 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0)); |
|
85 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0)); |
|
86 |
|
87 sp.onaudioprocess = function(e) { |
|
88 is(e.target, sp, "Correct event target"); |
|
89 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set"); |
|
90 lastPlaybackTime = e.playbackTime; |
|
91 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer"); |
|
92 is(e.inputBuffer.length, 2048, "Correct length for the input buffer"); |
|
93 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer"); |
|
94 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer"); |
|
95 is(e.outputBuffer.length, 2048, "Correct length for the output buffer"); |
|
96 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer"); |
|
97 |
|
98 var firstNonZero = findFirstNonZeroSample(e.inputBuffer); |
|
99 ok(firstNonZero <= 2048, "First non-zero sample within range"); |
|
100 |
|
101 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero); |
|
102 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero); |
|
103 compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 2048 - firstNonZero, firstNonZero, 0); |
|
104 compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 2048 - firstNonZero, firstNonZero, 0); |
|
105 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0)); |
|
106 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0)); |
|
107 |
|
108 if (firstNonZero == 0) { |
|
109 // If we did not experience any delays, the test is done! |
|
110 sp.onaudioprocess = null; |
|
111 |
|
112 SimpleTest.finish(); |
|
113 } else if (firstNonZero != 2048) { |
|
114 // In case we just saw a zero buffer this time, wait one more round |
|
115 sp.onaudioprocess = function(e) { |
|
116 is(e.target, sp, "Correct event target"); |
|
117 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set"); |
|
118 lastPlaybackTime = e.playbackTime; |
|
119 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer"); |
|
120 is(e.inputBuffer.length, 2048, "Correct length for the input buffer"); |
|
121 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer"); |
|
122 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer"); |
|
123 is(e.outputBuffer.length, 2048, "Correct length for the output buffer"); |
|
124 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer"); |
|
125 |
|
126 compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), firstNonZero, 0, 2048 - firstNonZero); |
|
127 compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), firstNonZero, 0, 2048 - firstNonZero); |
|
128 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), undefined, firstNonZero); |
|
129 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), undefined, firstNonZero); |
|
130 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0)); |
|
131 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0)); |
|
132 |
|
133 sp.onaudioprocess = null; |
|
134 |
|
135 SimpleTest.finish(); |
|
136 }; |
|
137 } |
|
138 }; |
|
139 }; |
|
140 }); |
|
141 |
|
142 </script> |
|
143 </pre> |
|
144 </body> |
|
145 </html> |