Skip to content

Commit cefee05

Browse files
Gary KeebleGary Keeble
authored andcommitted
Tweaks to Analyser
Changes some of the analyser setup parameters
1 parent 5144146 commit cefee05

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

js/graph_spectrum.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ try {
3636
source.start();
3737

3838
var spectrumAnalyser = audioCtx.createAnalyser();
39-
spectrumAnalyser.fftSize = 256;
39+
spectrumAnalyser.fftSize = 512;
4040
spectrumAnalyser.smoothingTimeConstant = 0.8;
41-
spectrumAnalyser.minDecibels = -120;
42-
spectrumAnalyser.maxDecibels = -20;
41+
spectrumAnalyser.minDecibels = -100;
42+
spectrumAnalyser.maxDecibels = -30;
4343

4444
var dataBuffer = {
4545
chunks: 0,
@@ -162,23 +162,32 @@ try {
162162

163163
/* only plot the lower half of the FFT, as the top half
164164
never seems to have any values in it - too high frequency perhaps. */
165-
var PLOTTED_BUFFER_LENGTH = bufferLength; // / 2;
165+
var PLOTTED_BUFFER_LENGTH = bufferLength / 2;
166166

167167
canvasCtx.translate(LEFT, TOP);
168168

169169
spectrumAnalyser.getByteFrequencyData(dataArray);
170170

171-
canvasCtx.fillStyle = 'rgba(255, 255, 255, .25)'; /* white */
171+
var gradient = canvasCtx.createLinearGradient(0,0,0,(HEIGHT));
172+
gradient.addColorStop(1, 'rgba(255,255,255,0.25)');
173+
gradient.addColorStop(0, 'rgba(255,255,255,0)');
174+
canvasCtx.fillStyle = gradient; //'rgba(255, 255, 255, .25)'; /* white */
172175
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
173176

174177
var barWidth = (WIDTH / PLOTTED_BUFFER_LENGTH) - 1;// * 2.5;
175178
var barHeight;
176179
var x = 0;
177180

181+
var gradient = canvasCtx.createLinearGradient(0,HEIGHT,0,0);
182+
gradient.addColorStop(0, 'rgba(0,255,0,0.2)');
183+
gradient.addColorStop(0.15, 'rgba(128,255,0,0.2)');
184+
gradient.addColorStop(0.45, 'rgba(255,0,0,0.5)');
185+
gradient.addColorStop(1, 'rgba(255,128,128,1.0)');
186+
178187
for(var i = 0; i < (PLOTTED_BUFFER_LENGTH); i++) {
179-
barHeight = (dataArray[i]/255 * (HEIGHT));
188+
barHeight = (dataArray[i]/255 * (HEIGHT));
180189

181-
canvasCtx.fillStyle = 'rgba(0,255,0,0.3)'; /* green */
190+
canvasCtx.fillStyle = gradient; //'rgba(0,255,0,0.3)'; /* green */
182191
canvasCtx.fillRect(x,(HEIGHT)-barHeight,barWidth,barHeight);
183192

184193
x += barWidth + 1;
@@ -249,4 +258,6 @@ try {
249258
console.log('Failed to create analyser');
250259
};
251260

261+
// execute the resize function once so that the canvas gets a valid setup
262+
252263
}

0 commit comments

Comments
 (0)