OLD | NEW |
1 <!doctype html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
| 4 <title> |
| 5 Test Multiple Calls to getFloatFrequencyData |
| 6 </title> |
4 <script src="../../resources/testharness.js"></script> | 7 <script src="../../resources/testharness.js"></script> |
5 <script src="../../resources/testharnessreport.js"></script> | 8 <script src="../../resources/testharnessreport.js"></script> |
6 <script src="../resources/audit-util.js"></script> | 9 <script src="../resources/audit-util.js"></script> |
7 <script src="../resources/audit.js"></script> | 10 <script src="../resources/audit.js"></script> |
8 <title>Test Multiple Calls to getFloatFrequencyData</title> | |
9 </head> | 11 </head> |
| 12 <body> |
| 13 <script id="layout-test-code"> |
| 14 let sampleRate = 48000; |
| 15 // Render enough data to run the test. |
| 16 let renderFrames = 2 * 1024; |
| 17 let renderDuration = renderFrames / sampleRate; |
10 | 18 |
11 <body> | 19 let audit = Audit.createTaskRunner(); |
12 <script> | |
13 var sampleRate = 48000; | |
14 // Render enough data to run the test. | |
15 var renderFrames = 2*1024; | |
16 var renderDuration = renderFrames / sampleRate; | |
17 | 20 |
18 var audit = Audit.createTaskRunner(); | 21 audit.define('test', (task, should) => { |
19 | 22 |
20 audit.define("test", (task, should) => { | 23 let context = new OfflineAudioContext(1, renderFrames, sampleRate); |
21 | 24 |
22 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 25 // Use sawtooth oscillator as the source because it has quite a bit of |
| 26 // harmonic content. Otherwise, the type doesn't really matter. |
| 27 let osc = context.createOscillator(); |
| 28 osc.type = 'sawtooth'; |
23 | 29 |
24 // Use sawtooth oscillator as the source because it has quite a bit of h
armonic content. | 30 // Create an analyser with 256-point FFT. The FFT size doesn't really |
25 // Otherwise, the type doesn't really matter. | 31 // matter much. |
26 var osc = context.createOscillator(); | 32 let analyser = context.createAnalyser(); |
27 osc.type = "sawtooth"; | |
28 | |
29 // Create an analyser with 256-point FFT. The FFT size doesn't really m
atter much. | |
30 var analyser = context.createAnalyser(); | |
31 analyser.fftSize = 256; | 33 analyser.fftSize = 256; |
32 | 34 |
33 osc.connect(analyser); | 35 osc.connect(analyser); |
34 analyser.connect(context.destination); | 36 analyser.connect(context.destination); |
35 | 37 |
36 var success = true; | 38 let success = true; |
37 | 39 |
38 // Suspend after getting a full analyser frame. (Not really necessary, b
ut it's nice that | 40 // Suspend after getting a full analyser frame. (Not really necessary, |
39 // the frame doesn't include any initial zeroes. | 41 // but it's nice that the frame doesn't include any initial zeroes. |
40 var suspendFrame = analyser.fftSize; | 42 let suspendFrame = analyser.fftSize; |
41 context.suspend(suspendFrame / sampleRate).then(function () { | 43 context.suspend(suspendFrame / sampleRate) |
42 // Test successive calls to getFloatFrequencyData in the same renderin
g quantum. | 44 .then(function() { |
43 let f1 = new Float32Array(analyser.frequencyBinCount); | 45 // Test successive calls to getFloatFrequencyData in the same |
44 let f2 = new Float32Array(analyser.frequencyBinCount); | 46 // rendering quantum. |
| 47 let f1 = new Float32Array(analyser.frequencyBinCount); |
| 48 let f2 = new Float32Array(analyser.frequencyBinCount); |
45 | 49 |
46 analyser.getFloatFrequencyData(f1); | 50 analyser.getFloatFrequencyData(f1); |
47 analyser.getFloatFrequencyData(f2); | 51 analyser.getFloatFrequencyData(f2); |
48 should(f2, "Second call to getFloatFrequencyData") | 52 should(f2, 'Second call to getFloatFrequencyData') |
49 .beEqualToArray(f1); | 53 .beEqualToArray(f1); |
50 }).then(context.resume.bind(context)); | 54 }) |
| 55 .then(context.resume.bind(context)); |
51 | 56 |
52 suspendFrame += 128; | 57 suspendFrame += 128; |
53 context.suspend(suspendFrame / sampleRate).then(function () { | 58 context.suspend(suspendFrame / sampleRate) |
54 // Test successive calls to getByteFrequencyData in the same rendering
quantum. | 59 .then(function() { |
55 let f1 = new Uint8Array(analyser.frequencyBinCount); | 60 // Test successive calls to getByteFrequencyData in the same |
56 let f2 = new Uint8Array(analyser.frequencyBinCount); | 61 // rendering quantum. |
| 62 let f1 = new Uint8Array(analyser.frequencyBinCount); |
| 63 let f2 = new Uint8Array(analyser.frequencyBinCount); |
57 | 64 |
58 analyser.getByteFrequencyData(f1); | 65 analyser.getByteFrequencyData(f1); |
59 analyser.getByteFrequencyData(f2); | 66 analyser.getByteFrequencyData(f2); |
60 | 67 |
61 should(f2, "Second call to getByteFrequencyData") | 68 should(f2, 'Second call to getByteFrequencyData') |
62 .beEqualToArray(f1); | 69 .beEqualToArray(f1); |
63 }).then(context.resume.bind(context)); | 70 }) |
| 71 .then(context.resume.bind(context)); |
64 | 72 |
65 suspendFrame += 128; | 73 suspendFrame += 128; |
66 context.suspend(suspendFrame / sampleRate).then(function () { | 74 context.suspend(suspendFrame / sampleRate) |
67 // Test calls to getFloatFrequencyData followed by getByteFrequencyDat
a. The float data, | 75 .then(function() { |
68 // when converted to byte values should be identical to the result fro
m | 76 // Test calls to getFloatFrequencyData followed by |
69 // getByteFrequencyData. | 77 // getByteFrequencyData. The float data, when converted to byte |
70 let f1 = new Float32Array(analyser.frequencyBinCount); | 78 // values should be identical to the result from |
71 let f2 = new Uint8Array(analyser.frequencyBinCount); | 79 // getByteFrequencyData. |
| 80 let f1 = new Float32Array(analyser.frequencyBinCount); |
| 81 let f2 = new Uint8Array(analyser.frequencyBinCount); |
72 | 82 |
73 analyser.getFloatFrequencyData(f1); | 83 analyser.getFloatFrequencyData(f1); |
74 analyser.getByteFrequencyData(f2); | 84 analyser.getByteFrequencyData(f2); |
75 | 85 |
76 var byteValuesFromFloat = convertFloatToByte(f1, analyser.minDecibels,
analyser.maxDecibels); | 86 let byteValuesFromFloat = convertFloatToByte( |
77 should(byteValuesFromFloat, "Output of getByteFrequencyData after getF
loatFrequencyData") | 87 f1, analyser.minDecibels, analyser.maxDecibels); |
78 .beEqualToArray(f2); | 88 should( |
79 }).then(context.resume.bind(context)); | 89 byteValuesFromFloat, |
| 90 'Output of getByteFrequencyData after getFloatFrequencyData') |
| 91 .beEqualToArray(f2); |
| 92 }) |
| 93 .then(context.resume.bind(context)); |
80 | 94 |
81 suspendFrame += 128; | 95 suspendFrame += 128; |
82 context.suspend(suspendFrame / sampleRate).then(function () { | 96 context.suspend(suspendFrame / sampleRate) |
83 // Test calls to getByteFrequencyData followed by getFloatFrequencyDat
a. The float data, | 97 .then(function() { |
84 // when converted to byte values should be identical to the result fro
m | 98 // Test calls to getByteFrequencyData followed by |
85 // getByteFrequencyData. | 99 // getFloatFrequencyData. The float data, when converted to byte |
86 var f1 = new Uint8Array(analyser.frequencyBinCount); | 100 // values should be identical to the result from |
87 var f2 = new Float32Array(analyser.frequencyBinCount); | 101 // getByteFrequencyData. |
| 102 let f1 = new Uint8Array(analyser.frequencyBinCount); |
| 103 let f2 = new Float32Array(analyser.frequencyBinCount); |
88 | 104 |
89 analyser.getByteFrequencyData(f1); | 105 analyser.getByteFrequencyData(f1); |
90 analyser.getFloatFrequencyData(f2); | 106 analyser.getFloatFrequencyData(f2); |
91 | 107 |
92 var byteValuesFromFloat = convertFloatToByte(f2, analyser.minDecibels,
analyser.maxDecibels); | 108 let byteValuesFromFloat = convertFloatToByte( |
93 should(f1, | 109 f2, analyser.minDecibels, analyser.maxDecibels); |
94 "Output of getFloatFrequenycData (converted to byte) after getByte
FrequencyData") | 110 should( |
95 .beEqualToArray(byteValuesFromFloat); | 111 f1, |
96 }).then(context.resume.bind(context)); | 112 'Output of getFloatFrequenycData (converted to byte) after get
ByteFrequencyData') |
| 113 .beEqualToArray(byteValuesFromFloat); |
| 114 }) |
| 115 .then(context.resume.bind(context)); |
97 | 116 |
98 osc.start(); | 117 osc.start(); |
99 context.startRendering().then(() => task.done()); | 118 context.startRendering().then(() => task.done()); |
100 }); | 119 }); |
101 | 120 |
102 audit.run(); | 121 audit.run(); |
103 | 122 |
104 // Convert the float frequency data (in dB), |floatFreqData|, to byte valu
es using the dB | 123 // Convert the float frequency data (in dB), |floatFreqData|, to byte |
105 // limits |minDecibels| and |maxDecibels|. The new byte array is returned
. | 124 // values using the dB limits |minDecibels| and |maxDecibels|. The new |
| 125 // byte array is returned. |
106 function convertFloatToByte(floatFreqData, minDecibels, maxDecibels) { | 126 function convertFloatToByte(floatFreqData, minDecibels, maxDecibels) { |
107 var scale = 255 / (maxDecibels - minDecibels); | 127 let scale = 255 / (maxDecibels - minDecibels); |
108 | 128 |
109 return floatFreqData.map(function (x) { | 129 return floatFreqData.map(function(x) { |
110 var value = Math.floor(scale * (x - minDecibels)); | 130 let value = Math.floor(scale * (x - minDecibels)); |
111 return Math.min(255, Math.max(0, value)); | 131 return Math.min(255, Math.max(0, value)); |
112 }); | 132 }); |
113 } | 133 } |
114 </script> | 134 </script> |
115 </body> | 135 </body> |
116 </html> | 136 </html> |
OLD | NEW |