@@ -4,6 +4,14 @@ function FlightLogAnalyser(flightLog, graphConfig, canvas, craftCanvas, options)
44
55var 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
816var sampleRate = 8000 ;
917var frameCount = sampleRate * 2 ;
@@ -17,10 +25,14 @@ var audioCtx = new AudioContext();
1725var oscillator = audioCtx . createOscillator ( ) ;
1826oscillator . type = 'sine' ;
1927oscillator . frequency . value = 2000 ; // value in hertz;
28+ oscillator . start ( ) ;
2029
2130var spectrumAnalyser = audioCtx . createAnalyser ( ) ;
2231 spectrumAnalyser . fftSize = 256 ;
2332 spectrumAnalyser . smoothingTimeConstant = 0.8 ;
33+ spectrumAnalyser . minDecibels = - 90 ;
34+ spectrumAnalyser . maxDecibels = - 10 ;
35+
2436
2537var myScriptProcessor = audioCtx . createScriptProcessor ( 2048 , 1 , 1 ) ;
2638
@@ -31,11 +43,20 @@ var initialised = false;
3143oscillator . connect ( myScriptProcessor ) ;
3244myScriptProcessor . 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? */
3651myScriptProcessor . 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
4162function 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}
0 commit comments