1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2025-09-04 04:38:09 +00:00
Funkin/source/funkin/audio/visualize/ABotVis.hx

152 lines
4 KiB
Haxe
Raw Permalink Normal View History

package funkin.audio.visualize;
import funkin.graphics.FunkinSprite;
import flixel.FlxSprite;
2021-10-08 01:01:46 +00:00
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
import flixel.sound.FlxSound;
import funkin.vis.dsp.SpectralAnalyzer;
2021-10-08 01:01:46 +00:00
using Lambda;
@:nullSafety
class ABotVis extends FlxTypedSpriteGroup<FlxSprite>
{
2024-02-23 09:00:31 +00:00
// public var vis:VisShit;
var analyzer:Null<SpectralAnalyzer> = null;
2021-10-08 01:01:46 +00:00
var volumes:Array<Float> = [];
2021-10-08 01:01:46 +00:00
public var snd:Null<FlxSound> = null;
static final BAR_COUNT:Int = 7;
2024-02-23 09:00:31 +00:00
// TODO: Make the sprites easier to soft code.
public function new(snd:FlxSound, pixel:Bool)
{
super();
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
this.snd = snd;
var visCount = pixel ? (BAR_COUNT + 1) : (BAR_COUNT + 1);
var visScale = pixel ? 6 : 1;
2021-10-08 01:01:46 +00:00
var visFrms:FlxAtlasFrames = Paths.getSparrowAtlas(pixel ? 'characters/abotPixel/aBotVizPixel' : 'characters/abot/aBotViz');
2021-10-08 01:01:46 +00:00
2024-02-23 07:03:34 +00:00
// these are the differences in X position, from left to right
2025-02-06 05:51:59 +00:00
var positionX:Array<Float> = pixel ? [0, 7 * 6, 8 * 6, 9 * 6, 10 * 6, 6 * 6, 7 * 6] : [0, 59, 56, 66, 54, 52, 51];
var positionY:Array<Float> = pixel ? [0, -2 * 6, -1 * 6, 0, 0, 1 * 6, 2 * 6] : [0, -8, -3.5, -0.4, 0.5, 4.7, 7];
2024-02-23 07:03:34 +00:00
for (index in 1...visCount)
{
// pushes initial value
volumes.push(0.0);
// Sum the offsets up to the current index
2024-02-23 07:18:50 +00:00
var sum = function(num:Float, total:Float) return total += num;
var posX:Float = positionX.slice(0, index).fold(sum, 0);
var posY:Float = positionY.slice(0, index).fold(sum, 0);
2021-10-08 01:01:46 +00:00
var viz:FunkinSprite = new FunkinSprite(posX, posY);
viz.frames = visFrms;
viz.antialiasing = pixel ? false : true;
viz.scale.set(visScale, visScale);
add(viz);
2021-10-08 01:01:46 +00:00
2024-02-22 06:50:55 +00:00
var visStr = 'viz';
viz.animation.addByPrefix('VIZ', '$visStr${index}0', 0);
2025-02-06 05:51:59 +00:00
viz.animation.play('VIZ', false, false, 1);
}
}
2021-10-08 01:01:46 +00:00
public function initAnalyzer():Void
2024-02-23 09:00:31 +00:00
{
if (snd == null) return;
2024-02-23 09:00:31 +00:00
@:privateAccess
analyzer = new SpectralAnalyzer(snd._channel.__audioSource, BAR_COUNT, 0.1, 40);
// A-Bot tuning...
analyzer.minDb = -65;
analyzer.maxDb = -25;
analyzer.maxFreq = 22000;
// we use a very low minFreq since some songs use low low subbass like a boss
analyzer.minFreq = 10;
2024-06-07 00:46:12 +00:00
#if sys
// On native it uses FFT stuff that isn't as optimized as the direct browser stuff we use on HTML5
2024-06-07 00:46:12 +00:00
// So we want to manually change it!
2024-06-08 02:36:25 +00:00
analyzer.fftN = 256;
2024-06-07 00:46:12 +00:00
#end
2024-05-24 20:38:35 +00:00
// analyzer.maxDb = -35;
// analyzer.fftN = 2048;
2024-02-23 09:00:31 +00:00
}
public function dumpSound():Void
{
snd = null;
analyzer = null;
}
2024-03-07 19:19:26 +00:00
var visTimer:Float = -1;
var visTimeMax:Float = 1 / 30;
override function update(elapsed:Float)
{
super.update(elapsed);
}
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
static inline function min(x:Int, y:Int):Int
{
2024-02-23 09:00:31 +00:00
return x > y ? y : x;
}
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
override function draw()
{
2024-03-01 05:15:51 +00:00
super.draw();
drawFFT();
2024-03-01 05:15:51 +00:00
}
/**
* TJW funkin.vis based visualizer! updateFFT() is the old nasty shit that dont worky!
2024-03-01 05:15:51 +00:00
*/
function drawFFT():Void
{
var levels = (analyzer != null) ? analyzer.getLevels() : getDefaultLevels();
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
for (i in 0...min(group.members.length, levels.length))
{
2025-06-25 05:24:37 +00:00
var animFrame:Int = (FlxG.sound.volume == 0 || FlxG.sound.muted) ? 0 : Math.round(levels[i].value * 6);
// don't display if we're at 0 volume from the level
group.members[i].visible = animFrame > 0;
// decrement our animFrame, so we can get a value from 0-5 for animation frames
animFrame -= 1;
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
animFrame = Math.floor(Math.min(5, animFrame));
animFrame = Math.floor(Math.max(0, animFrame));
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
animFrame = Std.int(Math.abs(animFrame - 5)); // shitty dumbass flip, cuz dave got da shit backwards lol!
2021-10-08 01:01:46 +00:00
2024-02-23 09:00:31 +00:00
group.members[i].animation.curAnim.curFrame = animFrame;
}
}
2024-02-23 09:00:31 +00:00
/**
* Explicitly define the default levels to draw when the analyzer is not available.
* @return Array<Bar>
*/
static function getDefaultLevels():Array<Bar>
{
var result:Array<Bar> = [];
for (i in 0...BAR_COUNT)
{
result.push({value: 0, peak: 0.0});
}
return result;
}
}