An utility that analyze FlxSounds, can be used to make waveform or real-time audio visualizer.
FlxSound.amplitude does work in CNE so if any case if your only checking for peak of current time, use that instead.
Static methods
staticgetByte(buffer:ArrayBuffer, position:Int, wordSize:Int):Int
Get bytes from an audio buffer with specified position and wordSize
Parameters:
buffer | The audio buffer to get byte from. |
---|---|
position | The specified position to get the byte from the audio buffer. |
wordSize | How many bytes to get with to one byte (Usually it's bitsPerSample / 8 or bitsPerSample >> 3). |
Returns:
Byte from the audio buffer with specified position.
staticgetFrequenciesFromSamples(samples:Array<Float>, fftN:Int = 2048, useWindowing:Bool = false, ?frequencies:Array<Float>):Array<Float>
Gets frequencies from the samples.
Parameters:
samples | The samples (can be from AudioAnalyzer.getSamples). |
---|---|
fftN | How much samples for the fft to get, Has to be power of two, or it won't work. |
useWindowing | Should fft related stuff use blackman windowing? (Web AnalyzerNode windowing), Most of the time it's not worth it. |
frequencies | The output for getting the frequencies, to avoid memory leaks (Optional). |
Returns:
Output of frequencies.
staticgetLevelsFromFrequencies(frequencies:Array<Float>, sampleRate:Int, barCount:Int, ?levels:Array<Float>, ratio:Float = 0.0, minDb:Float = -63.0, maxDb:Float = -10.0, minFreq:Float = 20.0, maxFreq:Float = 22000.0):Array<Float>
Gets levels from the frequencies with specified sample rate.
Parameters:
frequencies | Frequencies input. |
---|---|
sampleRate | Sample Rate input. |
barCount | How much bars to get. |
levels | The output for getting the values, to avoid memory leaks (Optional). |
ratio | How much ratio for smoothen the values from the previous levels values (Optional, use CoolUtil.getFPSRatio(1 - ratio) to simulate web AnalyserNode.smoothingTimeConstant, 0.35 of smoothingTime works most of the time). |
minDb | The minimum decibels to cap (Optional, default -63.0, -120 is pure silence). |
maxDb | The maximum decibels to cap (Optional, default -10.0, Above 0 is not recommended). |
minFreq | The minimum frequency to cap (Optional, default 20.0, Below 8.0 is not recommended). |
maxFreq | The maximum frequency to cap (Optional, default 22000.0, Above 23000.0 is not recommended). |
Returns:
Output of levels/bars that ranges from 0 to 1.
Constructor
new(sound:FlxSound, fftN:Int = 2048, useWindowingFFT:Bool = false)
Creates an analyzer for specified FlxSound
Parameters:
sound | An FlxSound to analyze. |
---|---|
fftN | How much samples for fft to get (Optional, default 2048, 4096 is recommended for highest quality). |
useWindowingFFT | Should fft related stuff use blackman windowing? (Web AnalyzerNode windowing). |
Variables
read onlybyteSize:Int
The current byteSize from buffer. Example the byteSize of 16 BitsPerSample is 32768 (1 << 16-1)
fftN:Int
How much samples for the fft to get. Usually for getting the levels or frequencies of the sound.
Has to be power of two, or it won't work.
useWindowingFFT:Bool
Should fft related stuff use blackman windowing? (Web AnalyzerNode windowing). Most of the time looks bad with this.
Methods
analyze(startPos:Float, endPos:Float, ?outOrOutMin:Array<Float>, ?outMax:Array<Float>):Float
Analyzes an attached FlxSound from startPos to endPos in milliseconds to get the amplitudes.
Parameters:
startPos | Start Position to get from sound in milliseconds. |
---|---|
endPos | End Position to get from sound in milliseconds. |
outOrOutMin | The output minimum value from the analyzer, indices is in channels (0 to -0.5 -> 0 to 0.5) (Optional, if outMax doesn't get passed in, it will be [min, max] with all channels combined instead). |
outMax | The output maximum value from the analyzer, indices is in channels (Optional). |
Returns:
Output of amplitude from given position.
getFrequencies(startPos:Float, ?volume:Float, ?frequencies:Array<Float>):Array<Float>
Gets frequencies from an attached FlxSound from startPos.
Parameters:
startPos | Start Position to get from sound in milliseconds. |
---|---|
volume | How much volume multiplier will it affect the output. (Optional, default 1.0). |
frequencies | The output for getting the frequencies, to avoid memory leaks (Optional). |
Returns:
Output of frequencies.
getLevels(startPos:Float, ?volume:Float, barCount:Int, ?levels:Array<Float>, ?ratio:Float, ?minDb:Float, ?maxDb:Float, ?minFreq:Float, ?maxFreq:Float):Array<Float>
Gets levels from an attached FlxSound from startPos, basically a minimized of frequencies.
Parameters:
startPos | Start Position to get from sound in milliseconds. |
---|---|
volume | How much volume multiplier will it affect the output. (Optional, default 1.0). |
barCount | How much bars to get. |
levels | The output for getting the values, to avoid memory leaks (Optional). |
ratio | How much ratio for smoothen the values from the previous levels values (Optional, use CoolUtil.getFPSRatio(1 - ratio) to simulate web AnalyserNode.smoothingTimeConstant, 0.35 of smoothingTime works most of the time). |
minDb | The minimum decibels to cap (Optional, default -63.0, -120 is pure silence). |
maxDb | The maximum decibels to cap (Optional, default -10.0, Above 0 is not recommended). |
minFreq | The minimum frequency to cap (Optional, default 20.0, Below 8.0 is not recommended). |
maxFreq | The maximum frequency to cap (Optional, default 22000.0, Above 23000.0 is not recommended). |
Returns:
Output of levels/bars that ranges from 0 to 1.
getSamples(startPos:Float, length:Int, mono:Bool = true, channel:Int = -1, volume:Float = 1.0, ?output:Array<Float>, outputMerge:Bool = false):Array<Float>
Gets samples from startPos with given length of samples.
Parameters:
startPos | Start Position to get from sound in milliseconds. |
---|---|
length | Length of Samples. |
mono | Merge all of the byte channels of samples in one channel instead (Optional). |
channel | What channels to get from? (-1 == All Channels, Optional, this will be ignored if mono is enabled). |
volume | How much volume multiplier will it affect the output. (Optional, default 1.0). |
output | An Output that gets passed into this function, usually for to avoid memory leaks (Optional). |
outputMerge | Merge with previous values (Optional, default false). |
Returns:
Output of samples.
read(startPos:Float, endPos:Float, callback:AudioAnalyzerCallback):Void
Read an attached FlxSound from startPos to endPos in milliseconds with a callback.
Parameters:
startPos | Start Position to get from sound in milliseconds. |
---|---|
endPos | End Position to get from sound in milliseconds. |
callback | Int->Int->Void Byte->Channels->Void Callback to get the byte of a sample. |