1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-09-20 09:09:00 +00:00
Funkin/source/funkin/audio/visualize/SpectogramSprite.hx

280 lines
8 KiB
Haxe
Raw Normal View History

package funkin.audio.visualize;
2021-09-16 16:04:46 +00:00
import flixel.FlxSprite;
2021-09-16 18:50:02 +00:00
import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
2021-09-16 16:04:46 +00:00
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
2021-09-17 18:56:57 +00:00
import flixel.math.FlxVector;
import flixel.sound.FlxSound;
2021-09-16 16:04:46 +00:00
import flixel.util.FlxColor;
import funkin.audio.visualize.PolygonSpectogram.VISTYPE;
import funkin.audio.visualize.VisShit.CurAudioInfo;
import funkin.audio.visualize.dsp.FFT;
2022-08-26 20:46:48 +00:00
import lime.system.ThreadPool;
2021-09-16 16:04:46 +00:00
import lime.utils.Int16Array;
2021-09-28 02:30:38 +00:00
using Lambda;
2021-09-16 16:04:46 +00:00
using flixel.util.FlxSpriteUtil;
2021-09-16 18:50:02 +00:00
class SpectogramSprite extends FlxTypedSpriteGroup<FlxSprite>
2021-09-16 16:04:46 +00:00
{
var sampleRate:Int;
2021-09-16 19:28:29 +00:00
var lengthOfShit:Int = 500;
2021-09-16 19:28:29 +00:00
public var visType:VISTYPE = UPDATED;
2021-09-17 18:12:36 +00:00
public var col:Int = FlxColor.WHITE;
public var daHeight:Float = FlxG.height;
2021-09-17 18:12:36 +00:00
public var vis:VisShit;
public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE, ?height:Float = 720, ?amnt:Int = 500)
{
super();
2021-09-16 16:04:46 +00:00
vis = new VisShit(daSound);
this.col = col;
this.daHeight = height;
lengthOfShit = amnt;
2021-09-17 18:12:36 +00:00
regenLineShit();
2021-09-17 18:12:36 +00:00
// makeGraphic(200, 200, FlxColor.BLACK);
}
2021-09-16 18:50:02 +00:00
public function regenLineShit():Void
{
for (i in 0...lengthOfShit)
{
var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * daHeight).makeGraphic(1, 1, col);
lineShit.active = false;
lineShit.ID = i;
add(lineShit);
}
}
2021-09-16 16:04:46 +00:00
var setBuffer:Bool = false;
2021-09-28 02:30:38 +00:00
public var audioData:Int16Array;
2021-09-28 02:30:38 +00:00
var numSamples:Int = 0;
2021-09-16 19:28:29 +00:00
public var wavOptimiz:Int = 10;
2022-02-09 18:37:57 +00:00
override function update(elapsed:Float)
{
switch (visType)
{
case UPDATED:
updateVisulizer();
2021-09-28 02:30:38 +00:00
case FREQUENCIES:
updateFFT();
default:
}
2021-09-17 18:12:36 +00:00
forEach(spr -> {
spr.visible = spr.ID % wavOptimiz == 0;
});
2022-02-09 18:37:57 +00:00
// if visType is static, call updateVisulizer() manually whenever you want to update it!
2021-09-16 16:04:46 +00:00
super.update(elapsed);
}
2021-09-17 18:12:36 +00:00
/**
* @param start is the start in milliseconds?
*/
public function generateSection(start:Float = 0, seconds:Float = 1):Void
{
checkAndSetBuffer();
2021-09-17 18:12:36 +00:00
// vis.checkAndSetBuffer();
if (setBuffer)
{
var samplesToGen:Int = Std.int(sampleRate * seconds);
var startingSample:Int = Std.int(FlxMath.remapToRange(start, 0, vis.snd.length, 0, numSamples));
2021-09-17 18:12:36 +00:00
var prevLine:FlxPoint = new FlxPoint();
2021-09-17 18:56:57 +00:00
for (i in 0...group.members.length)
{
var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, startingSample, startingSample + samplesToGen));
var curAud:CurAudioInfo = VisShit.getCurAud(audioData, sampleApprox);
2021-09-17 18:12:36 +00:00
var swagheight:Int = 200;
2021-09-17 18:12:36 +00:00
group.members[i].x = prevLine.x;
group.members[i].y = prevLine.y;
2021-09-17 18:56:57 +00:00
prevLine.x = (curAud.balanced * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (i / group.members.length * daHeight) + y;
2021-09-17 18:12:36 +00:00
var line = FlxPoint.get(prevLine.x - group.members[i].x, prevLine.y - group.members[i].y);
2021-09-17 18:12:36 +00:00
group.members[i].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
group.members[i].angle = line.degrees;
}
2022-02-09 18:37:57 +00:00
wavOptimiz = 1; // hard set wavOptimiz to 1 so its a pure thing
}
}
2021-09-17 18:12:36 +00:00
public function checkAndSetBuffer()
{
vis.checkAndSetBuffer();
2021-09-17 18:12:36 +00:00
if (vis.setBuffer)
{
audioData = vis.audioData;
sampleRate = vis.sampleRate;
setBuffer = vis.setBuffer;
numSamples = Std.int(audioData.length / 2);
}
}
2021-09-17 18:12:36 +00:00
var doAnim:Bool = false;
var frameCounter:Int = 0;
public function updateFFT()
{
if (vis.snd != null)
{
var remappedShit:Int = 0;
2021-09-28 02:30:38 +00:00
checkAndSetBuffer();
2021-09-28 02:30:38 +00:00
if (!doAnim)
{
frameCounter++;
if (frameCounter >= 0)
{
frameCounter = 0;
doAnim = true;
}
}
if (setBuffer && doAnim)
{
doAnim = false;
if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, numSamples));
else
remappedShit = Std.int(FlxMath.remapToRange(Conductor.instance.songPosition, 0, vis.snd.length, 0, numSamples));
2021-09-28 02:30:38 +00:00
var fftSamples:Array<Float> = [];
var i = remappedShit;
for (sample in remappedShit...remappedShit + (Std.int((44100 * (1 / 144)))))
{
var curAud:CurAudioInfo = VisShit.getCurAud(audioData, i);
i += 2;
2021-09-28 02:30:38 +00:00
fftSamples.push(curAud.balanced);
}
2021-09-28 02:30:38 +00:00
var freqShit = vis.funnyFFT(fftSamples);
var prevLine:FlxPoint = new FlxPoint();
var swagheight:Int = 200;
2021-09-28 02:30:38 +00:00
for (i in 0...group.members.length)
{
// needs to be exponential growth / scaling
// still need to optmize the FFT to run better, gets only samples needed?
// not every frequency is built the same!
// 20hz to 40z is a LOT of subtle low ends, but somethin like 20,000hz to 20,020hz, the difference is NOT the same!
var powedShit:Float = FlxMath.remapToRange(i, 0, group.members.length, 0, 4);
// a value between 10hz and 100Khz
var hzPicker:Float = Math.pow(10, powedShit);
// var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, startingSample, startingSample + samplesToGen));
var remappedFreq:Int = Std.int(FlxMath.remapToRange(hzPicker, 0, 10000, 0, freqShit[0].length - 1));
2021-09-28 02:30:38 +00:00
group.members[i].x = prevLine.x;
group.members[i].y = prevLine.y;
2021-09-28 02:30:38 +00:00
var freqPower:Float = 0;
for (pow in 0...freqShit.length)
freqPower += freqShit[pow][remappedFreq];
freqPower /= freqShit.length;
var freqIDK:Float = FlxMath.remapToRange(freqPower, 0, 0.000005, 0, 50);
2021-09-28 02:30:38 +00:00
prevLine.x = (freqIDK * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (i / group.members.length * daHeight) + y;
2021-09-28 02:30:38 +00:00
var line = FlxPoint.get(prevLine.x - group.members[i].x, prevLine.y - group.members[i].y);
2021-09-28 02:30:38 +00:00
// dont draw a line until i figure out a nicer way to view da spikes and shit idk lol!
// group.members[i].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
// group.members[i].angle = line.degrees;
}
}
}
}
2021-09-28 02:30:38 +00:00
var curTime:Float = 0;
2022-02-09 18:37:57 +00:00
public function updateVisulizer():Void
{
if (vis.snd != null)
{
var remappedShit:Int = 0;
2021-09-17 18:12:36 +00:00
checkAndSetBuffer();
2021-09-16 16:04:46 +00:00
if (setBuffer)
{
if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, numSamples));
else
{
if (curTime == Conductor.instance.songPosition)
{
wavOptimiz = 3;
return; // already did shit, so finishes function early
}
2022-02-09 18:37:57 +00:00
curTime = Conductor.instance.songPosition;
2022-02-09 18:37:57 +00:00
remappedShit = Std.int(FlxMath.remapToRange(Conductor.instance.songPosition, 0, vis.snd.length, 0, numSamples));
}
2022-02-09 18:37:57 +00:00
wavOptimiz = 8;
2021-09-17 18:12:36 +00:00
var i = remappedShit;
var prevLine:FlxPoint = new FlxPoint();
2021-09-16 16:04:46 +00:00
var swagheight:Int = 200;
2021-09-16 18:50:02 +00:00
for (sample in remappedShit...remappedShit + lengthOfShit)
{
var curAud:CurAudioInfo = VisShit.getCurAud(audioData, i);
2021-09-16 19:28:29 +00:00
i += 2;
2021-09-16 16:04:46 +00:00
var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + lengthOfShit, 0, lengthOfShit - 1);
2021-09-16 18:50:02 +00:00
group.members[Std.int(remappedSample)].x = prevLine.x;
group.members[Std.int(remappedSample)].y = prevLine.y;
// group.members[0].y = prevLine.y;
2021-09-16 16:04:46 +00:00
// FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2);
prevLine.x = (curAud.balanced * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (Std.int(remappedSample) / lengthOfShit * daHeight) + y;
2021-09-17 18:56:57 +00:00
var line = FlxPoint.get(prevLine.x - group.members[Std.int(remappedSample)].x, prevLine.y - group.members[Std.int(remappedSample)].y);
2021-09-17 18:56:57 +00:00
group.members[Std.int(remappedSample)].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
group.members[Std.int(remappedSample)].angle = line.degrees;
}
}
}
}
2021-09-16 16:04:46 +00:00
}