Fix & Change a bit of AudioAnalyzer base functions

This commit is contained in:
Ralty
2025-07-27 18:29:24 +07:00
parent 23432cb596
commit c6c7cdd32b
+58 -46
View File
@@ -18,7 +18,11 @@ class AudioAnalyzer {
var __wordSize:Int;
var __bitUnsignedSize:Int;
var __bitSize:Int;
var __min:Array<Int> = [];
var __max:Array<Int> = [];
//var __ln10:Float = 2.3025850929940456;
//var __ln2:Float = 0.6931471805599453;
#if lime_vorbis
var __vorbis:VorbisFile;
#end
@@ -28,36 +32,47 @@ class AudioAnalyzer {
__check();
}
public function getLevels(levels:Array<Float>, barCount:Int, duration:Float):Float {
__check();
// TODO: implement FFT
return 0;
}
inline function __check() if (sound.buffer != buffer) {
__bitSize = 1 << ((buffer = sound.buffer).bitsPerSample - 1);
__bitUnsignedSize = 1 << buffer.bitsPerSample;
__toBits = buffer.sampleRate / 1000 * buffer.channels * (__wordSize = buffer.bitsPerSample >> 3);
__vorbis = null;
__min.resize(buffer.channels);
__max.resize(buffer.channels);
}
public function analyze(startPos:Float, endPos:Float, ?outSeperate:Array<Float>):Float {
/**
* Returns a peak of an attached FlxSound from startPos to endPos in milliseconds.
* @param startPos Start Position of the FlxSound in milliseconds.
* @param endPos End Position of the FlxSound in milliseconds.
* @param outMin The output minimum value from the analyzer, indices is in channels (0 to -0.5 -> 0 to 0.5) (Optional)
* @param outMax The output maximum value from the analyzer, indices is in channels (Optional)
* @return Output Amplitude value
**/
public function analyze(startPos:Float, endPos:Float, ?outMin:Array<Float>, ?outMax:Array<Float>):Float {
__check();
if (buffer.data != null) return __analyze(startPos, endPos, outSeperate);
if (buffer.data != null) return __analyze(startPos, endPos, outMin, outMax);
#if lime_vorbis
else if (__prepareVorbis()) return __analyzeVorbis(startPos, endPos, outSeperate);
else if (__prepareVorbis()) return __analyzeVorbis(startPos, endPos, outMin, outMax);
#end
return 0;
}
inline function __analyze(startPos:Float, endPos:Float, ?outSeperate:Array<Float>):Float {
var pos = Math.floor(startPos * __toBits), end = Math.floor(endPos * __toBits), buf = buffer.data #if !js .buffer #end;
var max = 0, b = 0, w = 0, c = 0, useSeperate = outSeperate != null;
if (useSeperate) {
while (c < outSeperate.length) outSeperate[c++] *= __bitSize;
while (c++ < buffer.channels) outSeperate.push(0);
c = Math.floor((pos % (__wordSize * buffer.channels)) / __wordSize);
}
function __analyze(startPos:Float, endPos:Float, ?outMin:Array<Float>, ?outMax:Array<Float>):Float {
__prepareAmplitude();
var pos = Math.floor(startPos * __toBits), end = Math.floor(endPos * __toBits), buf = buffer.data #if !js .buffer #end;
var c = Math.floor((pos % (__wordSize * buffer.channels)) / __wordSize), b = 0, w = 0;
pos -= pos % __wordSize;
end -= end % __wordSize;
while (pos < end) {
// 8-bit audio data is unsigned (0 is 128, 128 is 256, -128 is 0)
if (__wordSize == 1) b = #if js buf[pos++] #else buf.get(pos++) #end - __bitSize;
else {
while (w < buffer.bitsPerSample) {
@@ -67,15 +82,27 @@ class AudioAnalyzer {
}
if (b > __bitSize) b -= __bitUnsignedSize;
}
if (max < b) max = b;
if (useSeperate) {
if (outSeperate[c] < b) outSeperate[c] = b;
if (++c > buffer.channels) c = 0;
}
if (__max[c] < b) __max[c] = b; else if (__min[c] < (b = -b)) __min[c] = b;
if (++c > buffer.channels) c = 0;
w = b = 0;
}
return max / __bitSize;
return __getAmplitude(outMin, outMax);
}
inline function __prepareAmplitude() for (i in 0...buffer.channels) __min[i] = __max[i] = 0;
inline function __getAmplitude(?outMin:Array<Float>, ?outMax:Array<Float>):Float {
var min = 0, max = 0, useOutput = outMin != null && outMax != null, v:Float;
for (i in 0...buffer.channels) {
if (__min[i] > min) min = __min[i];
if (__max[i] > max) max = __max[i];
if (useOutput) {
if (outMin[i] < (v = __min[i] / __bitSize)) outMin[i] = v;
if (outMax[i] < (v = __max[i] / __bitSize)) outMax[i] = v;
}
}
return (max + min) / __bitSize;
}
#if lime_vorbis // As far i know, only native supports vorbis
@@ -91,17 +118,13 @@ class AudioAnalyzer {
return false;
}
inline function __analyzeVorbis(startPos:Float, endPos:Float, ?outSeperate:Array<Float>):Float @:privateAccess {
function __analyzeVorbis(startPos:Float, endPos:Float, ?outMin:Array<Float>, ?outMax:Array<Float>):Float @:privateAccess {
var n = Math.floor((endPos - startPos) * __toBits);
if ((n -= n % __wordSize) < __wordSize) return 0;
var pos = 0, max = 0, b = 0, w = 0, c = 0, useSeperate = outSeperate != null;
if (useSeperate) {
while (c < outSeperate.length) outSeperate[c++] *= __bitSize;
while (c++ < buffer.channels) outSeperate.push(0);
}
var pos = 0, c = 0, b = 0, w = 0;
__prepareAmplitude();
// IT SHOULD BE ALWAYS BACKEND.STREAMED IF THIS GETS CALLED.
var backend = sound._source != null ? sound._source.__backend : null;
if (backend != null && backend.streamTimer != null) {
var i = backend.bufferSizes.length - backend.queuedBuffers;
@@ -109,29 +132,21 @@ class AudioAnalyzer {
if (startPos >= time && startPos < backend.bufferTimes[backend.bufferSizes.length - 1] * 1000) {
var buf = backend.bufferDatas[i].buffer, size = backend.bufferSizes[i];
pos = Math.floor((startPos - time) * __toBits);
c = Math.floor((pos % (__wordSize * buffer.channels)) / __wordSize);
c = Math.floor(((pos = Math.floor((startPos - time) * __toBits)) % (__wordSize * buffer.channels)) / __wordSize);
pos -= pos % __wordSize;
while (n > 0) {
if (__wordSize == 1) {
b = buf.get(pos++) - __bitSize;
n--;
}
if (__wordSize == 1) b = buf.get(pos++) - __bitSize;
else {
while (w < buffer.bitsPerSample) {
b |= buf.get(pos) << w;
w += 8;
pos++;
n--;
}
if (b > __bitSize) b -= __bitUnsignedSize;
}
if (max < b) max = b;
if (useSeperate) {
if (outSeperate[c] < b) outSeperate[c] = b;
if (++c > buffer.channels) c = 0;
}
if (__max[c] < b) __max[c] = b; else if (__min[c] < (b = -b)) __min[c] = b;
if (++c > buffer.channels) c = 0;
if (pos >= size) {
if (++i >= backend.bufferDatas.length) break;
size = backend.bufferSizes[i];
@@ -139,20 +154,20 @@ class AudioAnalyzer {
pos = 0;
}
w = b = 0;
n -= __wordSize;
}
if (n == 0) return __getAmplitude(outMin, outMax);
startPos = pos / __toBits + time;
// Cannot inline a not final return
pos = c = 0;
}
}
if (n == 0) return max / __bitSize;
if (Math.abs(startPos - __vorbis.timeTell() * 1000) > 4) {
if (startPos < 1) __vorbis.rawSeek(0);
else __vorbis.timeSeek(startPos / 1000);
}
pos = c = 0;
var isBigEndian = lime.system.System.endianness == lime.system.Endian.BIG_ENDIAN, result;
while (n > 0) {
n -= (result = __vorbis.read(__buffer, 0, n < __bufferSize ? n : __bufferSize, isBigEndian, __wordSize, true));
@@ -170,17 +185,14 @@ class AudioAnalyzer {
}
if (b > __bitSize) b -= __bitUnsignedSize;
}
if (max < b) max = b;
if (useSeperate) {
if (outSeperate[c] < b) outSeperate[c] = b;
if (++c > buffer.channels) c = 0;
}
if (__max[c] < b) __max[c] = b; else if (__min[c] < (b = -b)) __min[c] = b;
if (++c > buffer.channels) c = 0;
w = b = 0;
}
pos = 0;
}
return max / __bitSize;
return __getAmplitude(outMin, outMax);
}
#end
}