diff --git a/source/funkin/backend/utils/AudioAnalyzer.hx b/source/funkin/backend/utils/AudioAnalyzer.hx index 48683859..b05ad285 100644 --- a/source/funkin/backend/utils/AudioAnalyzer.hx +++ b/source/funkin/backend/utils/AudioAnalyzer.hx @@ -10,6 +10,8 @@ import lime.media.vorbis.Vorbis; import lime.media.vorbis.VorbisFile; #end +typedef AudioAnalyzerCallback = Int->Int->Void; + /** * An utility that analyze FlxSounds, * can be used to make waveform or real-time audio visualizer. @@ -19,21 +21,21 @@ import lime.media.vorbis.VorbisFile; **/ class AudioAnalyzer { public static function getByte(buf:ArrayBuffer, pos:Int, wordSize:Int):Int { - if (wordSize == 2) return ArrayBufferIO.getInt16(buf, pos); + if (wordSize == 2) return inline ArrayBufferIO.getInt16(buf, pos); else if (wordSize == 3) { - var b = ArrayBufferIO.getUint16(buf, pos) | (buf.get(pos + 2) << 16); + var b = inline ArrayBufferIO.getUint16(buf, pos) | (buf.get(pos + 2) << 16); if (b > 8388608) return b - 16777216; else return b; } - else if (wordSize == 4) return ArrayBufferIO.getInt32(buf, pos); - else return ArrayBufferIO.getUint8(buf, pos) - 128; + else if (wordSize == 4) return inline ArrayBufferIO.getInt32(buf, pos); + else return inline ArrayBufferIO.getUint8(buf, pos) - 128; } public var sound:FlxSound; - public var buffer:AudioBuffer; - public var fftSize:Int; + public var buffer(default, null):AudioBuffer; + public var fftSamples(default, set):Int; - public var byteSize:Int; + public var byteSize(default, null):Int; var __toBits:Float; var __wordSize:Int; @@ -51,66 +53,178 @@ class AudioAnalyzer { var __minByte:Int; var __maxByte:Int; + // samples + var __sampleIndex:Int; + var __sampleOutputLength:Int; + var __sampleOutput:Array; + + // fft + var __logN:Int; + var __freqSamples:Array; + var __reverseIndices:Array = []; + var __rootReals:Array = []; + var __rootImags:Array = []; + var __freqReals:Array = []; + var __freqImags:Array = []; + + // levels + var __frequencies:Array; + /** * Creates an analyzer for specified FlxSound * @param sound An FlxSound to analyze. **/ - public function new(sound:FlxSound, fftSize = 1024) { + public function new(sound:FlxSound, fftSamples = 512) { this.sound = sound; - this.fftSize = fftSize; + this.fftSamples = fftSamples; __check(); } function __check() if (sound.buffer != buffer) { byteSize = 1 << ((buffer = sound.buffer).bitsPerSample - 1); + #if (lime_cffi && lime_vorbis) __vorbis = null; #end + __toBits = buffer.sampleRate / 1000 * (__sampleSize = buffer.channels * (__wordSize = buffer.bitsPerSample >> 3)); __min.resize(buffer.channels); __max.resize(buffer.channels); + } - #if (lime_cffi && lime_vorbis) __vorbis = null; #end + inline function set_fftSamples(v:Int):Int { + if (fftSamples == (fftSamples = v)) return v; + + var half = v >> 1, a, c; + __logN = Math.floor(Math.log(v) / Math.log(2)); + __reverseIndices.resize(half); + __rootReals.resize(half); + __rootImags.resize(half); + __freqReals.resize(half); + __freqImags.resize(half); + for (i in 0...half) { + __reverseIndices[i] = __bitReverse(i); + __rootReals[i] = Math.cos(a = 2 * Math.PI * i / v); + __rootImags[i] = Math.sin(a); + } + + return v; + } + + inline function __bitReverse(x:Int):Int { + var y = 0, i = __logN; + while (i > 0) { + y = (y << 1) | (x & 1); + x >>= 1; + i--; + } + return y; + } + + public function getLevels(startPos:Float, barCount:Int, ?levels:Array, delta = 0.0, minDb = -70.0, maxDb = -20.0):Array { + __frequencies = getFrequencies(startPos, __frequencies); + + if (levels == null) levels = []; + levels.resize(barCount); + + var x1 = 0.5, i1 = 1, n = fftSamples >> 1; + var x2, i2, v; + for (i in 0...barCount) { + i2 = Math.floor(x2 = Math.pow(n, (i + 1) / barCount) - 0.5); + + if (i2 < i1) v = __frequencies[i2] * (x2 - x1); + else { + v = __frequencies[i1 - 1] * (i1 - x1); + while (i1 < i2) v += __frequencies[i1++]; + if (i2 < n) v += __frequencies[i2] * (x2 - i2); + } + + i1 = Math.ceil(x1 = x2); + + v = 20 * Math.log(v * barCount / 12) / 2.302585092994046/*log(10)*/ / (maxDb - minDb); + if (delta > 0 && levels[i] > v) levels[i] -= (levels[i] - v) * delta; + else levels[i] = v; + } + + return levels; } /** - * TODO: IMPLEMENT FFT + * Gets frequencies from an attached FlxSound from startPos with samples + * @return Output of frequencies ranging from 1 to fftSamples/2. **/ - public function getLevels(levels:Array, barCount:Int, duration:Float):Float { - return 0; + public function getFrequencies(startPos:Float, ?frequencies:Array):Array { + __freqSamples = getSamples(startPos, fftSamples, true, __freqSamples); + + var inv = fftSamples >> 1; + for (i in 0...inv) { + __freqReals[__reverseIndices[i]] = __freqSamples[i]; + __freqImags[i] = 0; + } + + var h = 1, g = 0, b = 0, r = 0; + var tr, ti, i1, i2; + while (inv > 0) { + h <<= 1; + while (g < fftSamples) { + while (b < h) { + i2 = (i1 = g + b) + h; + tr = __rootReals[r] * __freqReals[i2] - __rootImags[r] * __freqImags[i2]; + ti = __rootReals[r] * __freqImags[i2] + __rootImags[r] * __freqReals[i2]; + + __rootReals[i2] = __freqReals[i1] - tr; + __rootImags[i2] = __freqImags[i1] - ti; + __freqReals[i1] += tr; + __freqImags[i1] += ti; + + b++; + r += inv; + } + b = r = 0; + g += h; + } + g = 0; + + inv >>= 1; + } + + if (frequencies == null) frequencies = []; + frequencies.resize(inv = fftSamples >> 1); + + var mag; + for (i in 0...inv) frequencies[i] = (mag = Math.sqrt(__freqReals[i] * __freqReals[i] + __freqImags[i] * __freqImags[i])) < 0.001 ? 0 : mag; + + return frequencies; } /** - * Returns a peak of an attached FlxSound from startPos to endPos in milliseconds. + * Analyzes an attached FlxSound from startPos to endPos in milliseconds to get the amplitudes. * @param startPos Start Position of the FlxSound in milliseconds. * @param endPos End Position of the FlxSound in milliseconds. - * @param 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) - * @param outMax The output maximum value from the analyzer, indices is in channels (Optional) - * @return Output Amplitude value + * @param 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). + * @param outMax The output maximum value from the analyzer, indices is in channels (Optional). + * @return Output of amplitude from given position. **/ public function analyze(startPos:Float, endPos:Float, ?outOrOutMin:Array, ?outMax:Array):Float { + var hasOut = outOrOutMin != null; + var hasTwoOut = hasOut && outMax != null; + + if (hasTwoOut) for (i in 0...buffer.channels) __min[i] = __max[i] = 0; __minByte = __maxByte = 0; - if (outOrOutMin != null) { + __check(); + __read(startPos, endPos, hasTwoOut ? __analyzeCallback : __analyzeCallbackSimple); + + if (hasOut) { var f:Float; - if (outMax != null) { - for (i in 0...buffer.channels) __min[i] = __max[i] = 0; - - read(startPos, endPos, __analyzeCallback); - - for (i in 0...buffer.channels) { - if (outOrOutMin[i] < (f = __min[i] / byteSize)) outOrOutMin[i] = f; - if (outMax[i] < (f = __max[i] / byteSize)) outMax[i] = f; - } + if (hasTwoOut) for (i in 0...buffer.channels) { + if (outOrOutMin[i] < (f = __min[i] / byteSize)) outOrOutMin[i] = f; + if (outMax[i] < (f = __max[i] / byteSize)) outMax[i] = f; } else { - read(startPos, endPos, __analyzeCallbackSimple); - outOrOutMin.resize(2); if (outOrOutMin[0] < (f = __minByte / byteSize)) outOrOutMin[0] = f; if (outOrOutMin[1] < (f = __maxByte / byteSize)) outOrOutMin[1] = f; } } - else - read(startPos, endPos, __analyzeCallbackSimple); return (__maxByte + __minByte) / byteSize; } @@ -121,9 +235,54 @@ class AudioAnalyzer { function __analyzeCallbackSimple(b:Int, c:Int):Void ((b > __maxByte) ? (__maxByte = b) : (if (-b > __minByte) (__minByte = -b))); + /** + * Gets samples from startPos with given length of samples. + * @param startPos Start Position of the FlxSound in milliseconds. + * @param length Length of Samples. + * @param mono Merge all of the byte channels of samples in one channel instead (Optional). + * @param Output that gets passed into this function (Optional). + * @return Output of + **/ + public function getSamples(startPos:Float, length:Int, mono = true, ?output:Array):Array { + ((output == null) ? (__sampleOutput = output = []) : (__sampleOutput = output)).resize((__sampleOutputLength = length) * (mono ? 1 : buffer.channels)); + __sampleIndex = 0; + + __check(); + __read(startPos, startPos + (length / __toBits), mono ? __getSamplesCallbackMerge : __getSamplesCallback); + + __sampleOutput = null; + return output; + } + + // TODO: check later if this formula is correct. + function __getSamplesCallbackMerge(b:Int, c:Int):Void if (__sampleIndex < __sampleOutputLength) { + if (c == 0) __sampleOutput[__sampleIndex] = b / buffer.channels / byteSize; + else if (c == buffer.channels) { + __sampleOutput[__sampleIndex] += b / buffer.channels / byteSize; + __sampleIndex++; + } + else + __sampleOutput[__sampleIndex] += b / buffer.channels / byteSize; + } + + function __getSamplesCallback(b:Int, c:Int):Void if (__sampleIndex < __sampleOutputLength) { + __sampleOutput[__sampleIndex] = b / byteSize; + __sampleIndex++; + } + + /** + * Read an attached FlxSound from startPos to endPos in milliseconds with a callback. + * @param startPos Start Position of the FlxSound in milliseconds. + * @param endPos End Position of the FlxSound in milliseconds. + * @param callback Int->Int->Void Byte->Channels->Void Callback to get the byte of a sample. + **/ public function read(startPos:Float, endPos:Float, callback:AudioAnalyzerCallback) { __check(); - if (buffer.data != null) __read(startPos, endPos, callback); + __read(startPos, endPos, callback); + } + + inline function __read(startPos:Float, endPos:Float, callback:AudioAnalyzerCallback) { + if (buffer.data != null) __readData(startPos, endPos, callback); #if lime_cffi else if (__canReadStream() && (startPos += __readStream(startPos, endPos, callback)) >= endPos) return; #if lime_vorbis @@ -132,7 +291,7 @@ class AudioAnalyzer { #end } - inline function __read(startPos:Float, endPos:Float, callback:AudioAnalyzerCallback) { + inline function __readData(startPos:Float, endPos:Float, callback:AudioAnalyzerCallback) { var pos = Math.floor(startPos * __toBits), end = Math.floor(endPos * __toBits), c = 0; pos -= pos % __sampleSize; end -= end % __sampleSize; @@ -212,44 +371,4 @@ class AudioAnalyzer { } #end #end -} - -private typedef AudioAnalyzerCallback = Int->Int->Void; - -/* -abstract Complex(Array) { - public var real(get, never):Float; - inline function get_real() return this[0]; - - public var imag(get, never):Float; - inline function get_imag() return this[1]; - - public inline function new(real:Float, imag:Float) this = [real, imag]; - public static inline function fromReal(real:Float):Complex return new Complex(real, 0); - public static inline function exp(w:Float):Complex return new Complex(Math.cos(w), Math.sin(w)); - - public var angle(get, never):Float; - inline function get_angle() return Math.atan2(imag, real); - - public var magnitude(get, never):Float; - inline function get_magnitude():Float return Math.sqrt(real*real + imag*imag); - - @:op(A + B) - public inline function add(rhs:Complex):Complex return new Complex(real + rhs.real, imag + rhs.imag); - - @:op(A - B) - public inline function sub(rhs:Complex):Complex return new Complex(real - rhs.real, imag - rhs.imag); - - @:op(A * B) - public inline function mult(rhs:Complex):Complex - return new Complex(real*rhs.real - imag*rhs.imag, real*rhs.imag + imag*rhs.real); - - @:op(A / B) - public inline function div(rhs:Complex):Complex { - var m = rhs.magnitude; - return new Complex((real*rhs.real + imag*rhs.imag) / m, (imag*rhs.real - real*rhs.imag) / m); - } - - public inline function conj():Complex return new Complex(real, -imag); -} -*/ \ No newline at end of file +} \ No newline at end of file