@@ -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
1918var AudioContext = window . AudioContext || window . webkitAudioContext ;
2019var 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
3027var 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-
3933var bufferChunks , bufferStartFrameIndex , bufferFieldIndex , bufferCurve ;
4034var initialised = false ;
4135
4236// Setup the audio path
43- oscillator . connect ( myScriptProcessor ) ;
44- myScriptProcessor . connect ( spectrumAnalyser ) ;
45-
37+ source . connect ( spectrumAnalyser ) ;
4638
4739var 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-
6241function 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}
0 commit comments