OLD | NEW |
1 function createGraph(options) { | 1 function createGraph(options) { |
2 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 2 var context = new OfflineAudioContext(1, renderFrames, sampleRate); |
3 | 3 |
4 // Use a default sawtooth wave for the test signal. We want something is a | 4 // Use a default sawtooth wave for the test signal. We want something is a |
5 // bit more harmonic content than a sine wave, but otherwise it doesn't | 5 // bit more harmonic content than a sine wave, but otherwise it doesn't |
6 // really matter. | 6 // really matter. |
7 var src = context.createOscillator(); | 7 var src = context.createOscillator(); |
8 src.type = "sawtooth"; | 8 src.type = "sawtooth"; |
9 | 9 |
10 var analyser = context.createAnalyser(); | 10 var analyser = context.createAnalyser(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 return 20 * Math.log10(x); | 70 return 20 * Math.log10(x); |
71 } | 71 } |
72 | 72 |
73 // Clip the FFT magnitude so that values below |limit| are set to |limit|. The | 73 // Clip the FFT magnitude so that values below |limit| are set to |limit|. The |
74 // FFT must be in dB. The input array is clipped in place. | 74 // FFT must be in dB. The input array is clipped in place. |
75 function clipMagnitude(limit, x) { | 75 function clipMagnitude(limit, x) { |
76 for (var k = 0; k < x.length; ++k) | 76 for (var k = 0; k < x.length; ++k) |
77 x[k] = Math.max(limit, x[k]) | 77 x[k] = Math.max(limit, x[k]) |
78 } | 78 } |
79 | 79 |
80 // Compare the float frequency data in dB, |freqData|, against the expected | 80 // Compare the float frequency data in dB, |freqData|, against the expected valu
e, |
81 // value, |expectedFreq|. |options| is a dictionary with the property | 81 // |expectedFreq|. |options| is a dictionary with the property |floatRelError| f
or setting the |
82 // |floatRelError| for setting the comparison threshold. | 82 // comparison threshold and |precision| for setting the printed precision. Sett
ing |precision| to |
| 83 // |undefined| means printing all digits. If |options.precision} doesn't exist,
use a default |
| 84 // precision. |
83 function compareFloatFreq(message, freqData, expectedFreq, options) { | 85 function compareFloatFreq(message, freqData, expectedFreq, options) { |
84 // Any dB values below -100 is pretty much in the noise due to round-off in | 86 // Any dB values below -100 is pretty much in the noise due to round-off in |
85 // the (single-precisiion) FFT, so just clip those values to -100. | 87 // the (single-precisiion) FFT, so just clip those values to -100. |
86 var lowerLimit = -100; | 88 var lowerLimit = -100; |
87 clipMagnitude(lowerLimit, expectedFreq); | 89 clipMagnitude(lowerLimit, expectedFreq); |
88 | 90 |
89 var actual = freqData; | 91 var actual = freqData; |
90 clipMagnitude(lowerLimit, actual); | 92 clipMagnitude(lowerLimit, actual); |
91 | 93 |
| 94 // Default precision for printing the FFT data. Distinguish between options
.precision existing |
| 95 // or not. If it does, use whatever value is there, including undefined. |
| 96 var defaultPrecision = options.hasOwnProperty("precision") ? options.precisi
on : 3; |
| 97 |
92 var success = Should(message, actual, { | 98 var success = Should(message, actual, { |
93 verbose: true, | 99 verbose: true, |
94 precision: 3 | 100 precision: defaultPrecision |
95 }) | 101 }) |
96 .beCloseToArray(expectedFreq, { | 102 .beCloseToArray(expectedFreq, { |
97 relativeThreshold: options.floatRelError || 0, | 103 relativeThreshold: options.floatRelError || 0, |
98 }); | 104 }); |
99 | 105 |
100 return { | 106 return { |
101 success: success, | 107 success: success, |
102 expected: expectedFreq | 108 expected: expectedFreq |
103 }; | 109 }; |
104 } | 110 } |
(...skipping 11 matching lines...) Expand all Loading... |
116 // Convert the float frequency data, |floatFreqData|, to byte values using the | 122 // Convert the float frequency data, |floatFreqData|, to byte values using the |
117 // dB limits |minDecibels| and |maxDecibels|. The new byte array is returned. | 123 // dB limits |minDecibels| and |maxDecibels|. The new byte array is returned. |
118 function convertFloatToByte(floatFreqData, minDecibels, maxDecibels) { | 124 function convertFloatToByte(floatFreqData, minDecibels, maxDecibels) { |
119 var scale = 255 / (maxDecibels - minDecibels); | 125 var scale = 255 / (maxDecibels - minDecibels); |
120 | 126 |
121 return floatFreqData.map(function (x) { | 127 return floatFreqData.map(function (x) { |
122 var value = Math.floor(scale * (x - minDecibels)); | 128 var value = Math.floor(scale * (x - minDecibels)); |
123 return Math.min(255, Math.max(0, value)); | 129 return Math.min(255, Math.max(0, value)); |
124 }); | 130 }); |
125 } | 131 } |
OLD | NEW |