fix SoundChannel peaks & give AudioAnalyzer desc

This commit is contained in:
Ralty
2025-07-27 18:49:23 +07:00
parent c6c7cdd32b
commit 92ee5a280a
2 changed files with 22 additions and 9 deletions
+14 -3
View File
@@ -9,8 +9,13 @@ import lime.media.vorbis.VorbisFile;
import lime.utils.ArrayBuffer;
#end
// ORIGINAL CODES FROM YOSH & LUNAR https://github.com/CodenameCrew/YoshiCrafterEngine/blob/main/source/WaveformSprite.hx
// REWRITTEN BY RALTYRO
/**
* 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.
**/
class AudioAnalyzer {
public var sound:FlxSound;
public var buffer:AudioBuffer;
@@ -27,14 +32,20 @@ class AudioAnalyzer {
var __vorbis:VorbisFile;
#end
/**
* Creates an analyzer for specified FlxSound
* @param sound An FlxSound to analyze.
**/
public function new(sound:FlxSound) {
this.sound = sound;
__check();
}
/**
* TODO: IMPLEMENT FFT
**/
public function getLevels(levels:Array<Float>, barCount:Int, duration:Float):Float {
__check();
// TODO: implement FFT
return 0;
}
+8 -6
View File
@@ -152,9 +152,7 @@ import lime.media.openal.AL;
@:noCompletion private function __updatePeaks(time:Float):Bool
{
if (Math.abs(time - __lastPeakTime) < 8) return false;
__lastPeakTime = time;
__leftPeak = __rightPeak = 0;
#if !macro
if (!__isValid) return false;
@@ -162,6 +160,7 @@ import lime.media.openal.AL;
var buffer = __source.buffer;
var wordSize = buffer.bitsPerSample >> 3, bitUnsignedSize = 1 << buffer.bitsPerSample, bitSize = 1 << (buffer.bitsPerSample - 1);
var pos = Math.floor(time * buffer.sampleRate / 1000 * buffer.channels * wordSize), size = 0, buf;
var leftMin = 0, leftMax = 0, rightMin = 0, rightMax = 0;
#if lime_cffi
var backend = __source.__backend, i = 0;
@@ -189,7 +188,10 @@ import lime.media.openal.AL;
}
if (b > bitSize) b -= bitUnsignedSize;
}
((c % 2 == 0) ? (if (__leftPeak < b) __leftPeak = b) : (if (__rightPeak < b) __rightPeak = b));
if (c % 2 == 0) {
if (leftMax < b) leftMax = b; else if (leftMin < (b = -b)) leftMin = b;
}
else if (rightMax < b) rightMax = b; else if (rightMin < (b = -b)) rightMin = b;
if (pos >= size) #if lime_cffi {
if (!backend.streamed || ++i >= backend.bufferDatas.length) break;
@@ -206,10 +208,10 @@ import lime.media.openal.AL;
w = b = 0;
}
if (buffer.channels == 1) __rightPeak = __leftPeak / bitSize;
if (buffer.channels == 1) __rightPeak = (__leftPeak = (leftMax + leftMin) / bitSize);
else {
__leftPeak = __leftPeak / bitSize;
__rightPeak = __rightPeak / bitSize;
__leftPeak = (leftMax + leftMin) / bitSize;
__rightPeak = (rightMax + rightMin) / bitSize;
}
#end