Skip to content

Commit 90a42b4

Browse files
author
Gary Keeble
committed
Updated Analyser Generation
OK; used a different processing technique to generate analyser data (on drawing refresh rather than by audio event); thus the analyser loads correctly first time.
1 parent e3fa1d1 commit 90a42b4

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

js/graph_spectrum.js

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,31 @@ var // inefficient; copied from grapher.js
1313
};
1414

1515

16-
var sampleRate = 8000;
17-
var frameCount = sampleRate * 2;
16+
var frameCount = 2048;
1817

1918
var AudioContext = window.AudioContext || window.webkitAudioContext;
2019
var audioCtx = new AudioContext();
2120

22-
/* The oscillator node is used as a trigger for the .onaudioprocess event, the actual
23-
values the oscillator generates are discarded in the scriptProcessing node and replaced
24-
by values from the currently plotted curve */
25-
var oscillator = audioCtx.createOscillator();
26-
oscillator.type = 'sine';
27-
oscillator.frequency.value = 2000; // value in hertz;
28-
oscillator.start();
21+
var audioBuffer = audioCtx.createBuffer(1, frameCount, audioCtx.sampleRate);
22+
var source = audioCtx.createBufferSource();
23+
source.buffer = audioBuffer;
24+
source.loop = true;
25+
source.start();
2926

3027
var spectrumAnalyser = audioCtx.createAnalyser();
3128
spectrumAnalyser.fftSize = 256;
3229
spectrumAnalyser.smoothingTimeConstant = 0.8;
3330
spectrumAnalyser.minDecibels = -90;
34-
spectrumAnalyser.maxDecibels = -10;
35-
31+
spectrumAnalyser.maxDecibels = -10;
3632

37-
var myScriptProcessor = audioCtx.createScriptProcessor(2048,1,1);
38-
3933
var bufferChunks, bufferStartFrameIndex, bufferFieldIndex, bufferCurve;
4034
var initialised = false;
4135

4236
// Setup the audio path
43-
oscillator.connect(myScriptProcessor);
44-
myScriptProcessor.connect(spectrumAnalyser);
45-
37+
source.connect(spectrumAnalyser);
4638

4739
var audioIterations = 0; // variable to monitor spectrum processing
4840

49-
/* This event is triggered and re-loads the data from the current curve into the audiobuffer
50-
Definitely a place for some code optimisation... only update if the curve data changes perhaps? */
51-
myScriptProcessor.onaudioprocess = function(audioProcessingEvent) {
52-
var outputBuffer = audioProcessingEvent.outputBuffer;
53-
dataLoad(bufferChunks, bufferStartFrameIndex, bufferFieldIndex, bufferCurve, outputBuffer);
54-
audioIterations++;
55-
}
56-
57-
oscillator.onended = function() {
58-
/* function added to catch stopping of the oscillator */
59-
oscillator.start();
60-
}
61-
6241
function dataLoad(chunks, startFrameIndex, fieldIndex, curve, buffer) {
6342

6443
var chunkIndex, frameIndex;
@@ -81,6 +60,7 @@ function dataLoad(chunks, startFrameIndex, fieldIndex, curve, buffer) {
8160
}
8261
frameIndex = 0;
8362
}
63+
audioIterations++;
8464
}
8565

8666
/* Function to actually draw the spectrum analyser overlay
@@ -147,7 +127,9 @@ this.plotSpectrum = function (chunks, startFrameIndex, fieldIndex, curve) {
147127
bufferStartFrameIndex = startFrameIndex;
148128
bufferFieldIndex = fieldIndex;
149129
bufferCurve = curve;
150-
130+
if (audioBuffer) {
131+
dataLoad(bufferChunks, bufferStartFrameIndex, bufferFieldIndex, bufferCurve, audioBuffer);
132+
}
151133
draw(); // draw the analyser on the canvas....
152134
}
153135
}

js/grapher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ function FlightLogGrapher(flightLog, graphConfig, canvas, craftCanvas, options)
748748
canvasContext.translate(0, canvas.height * graph.y);
749749

750750
drawAxisLine();
751-
drawAxisBackground(canvas.height * graph.height);
751+
if(graphs.length > 1) // only draw the background if more than one graph set.
752+
drawAxisBackground(canvas.height * graph.height);
752753

753754
for (j = 0; j < graph.fields.length; j++) {
754755
var field = graph.fields[j];

0 commit comments

Comments
 (0)