Skip to content

Commit e3fa1d1

Browse files
author
Gary Keeble
committed
Analyser startup should be more reliable
Make analyser more reliable… and a bit of cleanup… Still need to load the log file twice :-(
1 parent f2eb64b commit e3fa1d1

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

js/graph_spectrum.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ function FlightLogAnalyser(flightLog, graphConfig, canvas, craftCanvas, options)
44

55
var canvasCtx = canvas.getContext("2d");
66

7+
var // inefficient; copied from grapher.js
8+
9+
DEFAULT_FONT_FACE = "Verdana, Arial, sans-serif",
10+
11+
drawingParams = {
12+
fontSizeFrameLabel: null
13+
};
14+
715

816
var sampleRate = 8000;
917
var frameCount = sampleRate * 2;
@@ -17,10 +25,14 @@ var audioCtx = new AudioContext();
1725
var oscillator = audioCtx.createOscillator();
1826
oscillator.type = 'sine';
1927
oscillator.frequency.value = 2000; // value in hertz;
28+
oscillator.start();
2029

2130
var spectrumAnalyser = audioCtx.createAnalyser();
2231
spectrumAnalyser.fftSize = 256;
2332
spectrumAnalyser.smoothingTimeConstant = 0.8;
33+
spectrumAnalyser.minDecibels = -90;
34+
spectrumAnalyser.maxDecibels = -10;
35+
2436

2537
var myScriptProcessor = audioCtx.createScriptProcessor(2048,1,1);
2638

@@ -31,11 +43,20 @@ var initialised = false;
3143
oscillator.connect(myScriptProcessor);
3244
myScriptProcessor.connect(spectrumAnalyser);
3345

46+
47+
var audioIterations = 0; // variable to monitor spectrum processing
48+
3449
/* This event is triggered and re-loads the data from the current curve into the audiobuffer
3550
Definitely a place for some code optimisation... only update if the curve data changes perhaps? */
3651
myScriptProcessor.onaudioprocess = function(audioProcessingEvent) {
3752
var outputBuffer = audioProcessingEvent.outputBuffer;
3853
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();
3960
}
4061

4162
function dataLoad(chunks, startFrameIndex, fieldIndex, curve, buffer) {
@@ -102,9 +123,19 @@ function draw() {
102123

103124
x += barWidth;
104125
}
126+
drawAxisLabel('#' + leftPad(audioIterations, "0", 7), WIDTH - 8, HEIGHT - 10);
105127
canvasCtx.restore();
106128
}
107129

130+
131+
function drawAxisLabel(axisLabel, X, Y) {
132+
canvasCtx.font = drawingParams.fontSizeFrameLabel + "pt " + DEFAULT_FONT_FACE;
133+
canvasCtx.fillStyle = "rgba(255,255,255,0.9)";
134+
canvasCtx.textAlign = 'right';
135+
136+
canvasCtx.fillText(axisLabel, X, Y);
137+
}
138+
108139
/* This function is called from the canvas drawing routines within grapher.js
109140
It is only used to record the current curve positions and draw the
110141
analyser on screen; the actual data is collected within the
@@ -116,12 +147,7 @@ this.plotSpectrum = function (chunks, startFrameIndex, fieldIndex, curve) {
116147
bufferStartFrameIndex = startFrameIndex;
117148
bufferFieldIndex = fieldIndex;
118149
bufferCurve = curve;
119-
120-
// Get the audio context going.... Only once.....
121-
if (!initialised) {
122-
oscillator.start();
123-
initialised = true;
124-
}
150+
125151
draw(); // draw the analyser on the canvas....
126152
}
127153
}

js/grapher.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -821,22 +821,7 @@ function FlightLogGrapher(flightLog, graphConfig, canvas, craftCanvas, options)
821821
var graph = graphs[0]; // The first graph
822822
var field = graph.fields[0]; // and the top one in the list
823823
analyser.plotSpectrum(chunks, startFrameIndex, field.index, field.curve);
824-
825-
//for (i = 0; i < 1 /*graphs.length*/; i++) {
826-
// var
827-
// graph = graphs[i];
828-
//
829-
// canvasContext.save();
830-
// {
831-
//
832-
// for (j = 0; j < 1 /*graph.fields.length*/; j++) {
833-
// var field = graph.fields[j];
834-
// plotAnalyser(chunks, startFrameIndex, field.index, field.curve);
835-
// }
836-
// }
837-
// canvasContext.restore();
838-
//}
839-
824+
840825
}
841826
}
842827

0 commit comments

Comments
 (0)