@@ -20,8 +20,7 @@ typedef AudioAnalyzerCallback = Int->Int->Void;
2020 * An utility that analyze FlxSounds,
2121 * can be used to make waveform or real-time audio visualizer.
2222 *
23- * FlxSound.amplitude does work in CNE so if any case if your only checking for peak of current
24- * time, use that instead.
23+ * FlxSound.amplitude works so if any case if your only checking for peak of current time, use that instead.
2524 */
2625final class AudioAnalyzer {
2726 /**
@@ -296,7 +295,7 @@ final class AudioAnalyzer {
296295 __check ();
297296 }
298297
299- function __check () if (sound .buffer != buffer ) {
298+ function __check () if (sound != null && sound .buffer != buffer ) {
300299 byteSize = 1 << ((buffer = sound .buffer ).bitsPerSample - 1 );
301300
302301 #if (lime_cffi && lime_vorbis)
@@ -324,7 +323,7 @@ final class AudioAnalyzer {
324323 * @param maxFreq The maximum frequency to cap (Optional, default 22000.0, Above 23000.0 is not recommended).
325324 * @return Output of levels/bars that ranges from 0 to 1.
326325 */
327- public function getLevels (startPos : Float , ? volume : Float , barCount : Int , ? levels : Array <Float >, ? ratio : Float , ? minDb : Float , ? maxDb : Float , ? minFreq : Float , ? maxFreq : Float ): Array <Float >
326+ public function getLevels (? startPos : Float , ? volume : Float , barCount : Int , ? levels : Array <Float >, ? ratio : Float , ? minDb : Float , ? maxDb : Float , ? minFreq : Float , ? maxFreq : Float ): Array <Float >
328327 return inline getLevelsFromFrequencies (__frequencies = getFrequencies (startPos , volume , __frequencies ), buffer .sampleRate , barCount , levels , ratio , minDb , maxDb , minFreq , maxFreq );
329328
330329 /**
@@ -334,8 +333,8 @@ final class AudioAnalyzer {
334333 * @param frequencies The output for getting the frequencies, to avoid memory leaks (Optional).
335334 * @return Output of frequencies.
336335 */
337- public function getFrequencies (startPos : Float , ? volume : Float , ? frequencies : Array <Float >): Array <Float >
338- return inline getFrequenciesFromSamples (__freqSamples = getSamples (startPos , fftN , true , - 1 , volume , __freqSamples ), fftN , useWindowingFFT , frequencies );
336+ public function getFrequencies (? startPos : Float , ? volume : Float , ? frequencies : Array <Float >): Array <Float >
337+ return inline getFrequenciesFromSamples (__freqSamples = getSamples (startPos != null ? startPos : sound . time , fftN , true , - 1 , volume , __freqSamples ), fftN , useWindowingFFT , frequencies );
339338
340339 /**
341340 * Analyzes an attached FlxSound from startPos to endPos in milliseconds to get the amplitudes.
@@ -467,36 +466,37 @@ final class AudioAnalyzer {
467466 @:privateAccess return sound ._source != null && sound ._source .__backend != null && sound ._source .__backend .playing ;
468467
469468 inline function __readStream (startPos : Float , endPos : Float , callback : AudioAnalyzerCallback ): Float @:privateAccess {
470- var backend = sound ._source .__backend ;
471- var i = backend .bufferSizes .length - backend .queuedBuffers ;
472- var time = backend .bufferTimes [i ] * 1000 ;
469+ final backend = sound ._source .__backend ;
473470
471+ // TODO: Wrap it with try until i figured it out an effective way to do this...
472+ // So... sometimes it just uses the decoder even if it looks good?? please help
474473 var n = Math .floor ((endPos - startPos ) * __toBits );
475- if (startPos >= time && startPos < backend .bufferTimes [backend .bufferSizes .length - 1 ] * 1000 ) {
476- var pos = Math .floor ((startPos - time ) * __toBits ), buf = backend .bufferDatas [i ].buffer , size = backend .bufferSizes [i ], c = 0 ;
474+ var i = backend .bufferLengths .length - backend .queuedBuffers - 1 , time : Float ;
475+ while (++ i < backend .bufferLengths .length ) if (startPos >= (time = backend .bufferTimes [i ] * 1000 )) {
476+ var pos = Math .floor ((startPos - time ) * __toBits ), buf = backend .bufferDatas [i ].buffer , size = backend .bufferLengths [i ], c = 0 ;
477477 while (pos >= size ) {
478- if (++ i >= backend .bufferSizes .length ) {
479- n = 0 ;
480- break ;
481- }
478+ if (++ i >= backend .bufferLengths .length ) break ;
482479 pos - = size ;
483480 buf = backend .bufferDatas [i ].buffer ;
484- size = backend .bufferSizes [i ];
481+ size = backend .bufferLengths [i ];
485482 }
486- pos - = pos % __sampleSize ;
483+ if (i >= backend .bufferLengths .length ) break ;
484+ if ((pos - = pos % __sampleSize ) < 0 ) pos = 0 ;
487485 n - = pos % __sampleSize ;
488486
489487 while (n > 0 ) {
490488 callback (getByte (buf , pos , __wordSize ), c );
491489 if (++ c > buffer .channels ) c = 0 ;
492490 if ((pos + = __wordSize ) >= size ) {
493- if (++ i >= backend .bufferSizes .length ) break ;
491+ if (++ i >= backend .bufferLengths .length ) break ;
494492 pos = 0 ;
495493 buf = backend .bufferDatas [i ].buffer ;
496- size = backend .bufferSizes [i ];
494+ size = backend .bufferLengths [i ];
497495 }
498496 n - = __wordSize ;
499497 }
498+
499+ break ;
500500 }
501501
502502 return endPos - (n / __toBits );
0 commit comments