2022-10-07 00:37:21 -04:00
|
|
|
package funkin.audio.visualize;
|
2022-02-10 14:23:45 -05:00
|
|
|
|
|
|
|
import flixel.math.FlxMath;
|
|
|
|
import flixel.math.FlxPoint;
|
|
|
|
import flixel.system.FlxSound;
|
|
|
|
import flixel.util.FlxColor;
|
2022-10-07 00:37:21 -04:00
|
|
|
import funkin.audiovis.VisShit;
|
|
|
|
import funkin.graphics.rendering.MeshRender;
|
2022-02-10 14:23:45 -05:00
|
|
|
import lime.utils.Int16Array;
|
|
|
|
|
|
|
|
class PolygonSpectogram extends MeshRender
|
|
|
|
{
|
|
|
|
var sampleRate:Int;
|
|
|
|
|
|
|
|
public var vis:VisShit;
|
2022-02-11 14:21:42 -05:00
|
|
|
public var visType:VISTYPE = UPDATED;
|
2022-02-10 14:23:45 -05:00
|
|
|
public var daHeight:Float = FlxG.height;
|
2022-02-18 16:29:29 -05:00
|
|
|
public var realtimeVisLenght:Float = 0.2;
|
2022-02-10 14:23:45 -05:00
|
|
|
|
|
|
|
var numSamples:Int = 0;
|
|
|
|
var setBuffer:Bool = false;
|
|
|
|
|
|
|
|
public var audioData:Int16Array;
|
|
|
|
public var detail:Float = 1;
|
|
|
|
|
2022-02-18 16:29:29 -05:00
|
|
|
public var thickness:Float = 2;
|
|
|
|
public var waveAmplitude:Int = 100;
|
|
|
|
|
2022-02-10 14:23:45 -05:00
|
|
|
public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE, ?height:Float = 720, ?detail:Float = 1)
|
|
|
|
{
|
2022-02-11 14:21:42 -05:00
|
|
|
super(0, 0, col);
|
2022-02-10 14:23:45 -05:00
|
|
|
|
2022-10-07 00:37:21 -04:00
|
|
|
setSound(daSound);
|
2022-02-10 14:23:45 -05:00
|
|
|
|
|
|
|
if (height != null)
|
|
|
|
this.daHeight = height;
|
|
|
|
|
|
|
|
this.detail = detail;
|
|
|
|
|
|
|
|
// col not in yet
|
|
|
|
}
|
|
|
|
|
2022-10-07 00:37:21 -04:00
|
|
|
public function setSound(daSound:FlxSound)
|
|
|
|
{
|
|
|
|
vis = new VisShit(daSound);
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:21:42 -05:00
|
|
|
override function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
super.update(elapsed);
|
|
|
|
|
|
|
|
switch (visType)
|
|
|
|
{
|
|
|
|
case UPDATED:
|
|
|
|
realtimeVis();
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevAudioData:Int16Array;
|
|
|
|
|
2022-02-10 14:23:45 -05:00
|
|
|
/**
|
|
|
|
* Generates and draws a section of the audio data to a visual waveform
|
|
|
|
* @param start start of the song in milliseconds
|
|
|
|
* @param seconds how long to generate (also in milliseconds)
|
|
|
|
*/
|
|
|
|
public function generateSection(start:Float = 0, seconds:Float = 1):Void
|
|
|
|
{
|
|
|
|
checkAndSetBuffer();
|
|
|
|
|
|
|
|
if (setBuffer)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2022-02-16 16:31:20 -05:00
|
|
|
start = Math.max(start, 0);
|
|
|
|
|
2022-02-10 14:23:45 -05:00
|
|
|
var samplesToGen:Int = Std.int(sampleRate * seconds);
|
|
|
|
// gets which sample to start at
|
|
|
|
var startSample:Int = Std.int(FlxMath.remapToRange(start, 0, vis.snd.length, 0, numSamples));
|
|
|
|
|
|
|
|
var prevPoint:FlxPoint = new FlxPoint();
|
|
|
|
|
2022-02-11 14:21:42 -05:00
|
|
|
var funnyPixels:Int = Std.int(daHeight); // sorta redundant but just need it for different var...
|
|
|
|
|
|
|
|
if (prevAudioData == audioData.subarray(startSample, startSample + samplesToGen))
|
|
|
|
return; // optimize / finish funciton here, no need to re-render
|
|
|
|
|
|
|
|
prevAudioData = audioData.subarray(startSample, samplesToGen);
|
|
|
|
|
|
|
|
for (i in 0...funnyPixels)
|
2022-02-10 14:23:45 -05:00
|
|
|
{
|
2022-02-11 14:21:42 -05:00
|
|
|
var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, funnyPixels, startSample, startSample + samplesToGen));
|
2022-02-10 14:23:45 -05:00
|
|
|
var curAud:CurAudioInfo = VisShit.getCurAud(audioData, sampleApprox);
|
|
|
|
|
|
|
|
var coolPoint:FlxPoint = new FlxPoint();
|
2022-02-18 16:29:29 -05:00
|
|
|
coolPoint.x = (curAud.balanced * waveAmplitude);
|
2022-02-11 14:21:42 -05:00
|
|
|
coolPoint.y = (i / funnyPixels * daHeight);
|
2022-02-10 14:23:45 -05:00
|
|
|
|
2022-02-18 16:29:29 -05:00
|
|
|
add_quad(prevPoint.x, prevPoint.y, prevPoint.x
|
|
|
|
+ thickness, prevPoint.y, coolPoint.x, coolPoint.y, coolPoint.x
|
|
|
|
+ thickness,
|
|
|
|
coolPoint.y
|
|
|
|
+ thickness);
|
2022-02-10 14:23:45 -05:00
|
|
|
|
|
|
|
prevPoint.x = coolPoint.x;
|
|
|
|
prevPoint.y = coolPoint.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:21:42 -05:00
|
|
|
var curTime:Float = 0;
|
|
|
|
|
|
|
|
function realtimeVis():Void
|
|
|
|
{
|
|
|
|
if (vis.snd != null)
|
|
|
|
{
|
|
|
|
if (curTime != vis.snd.time)
|
|
|
|
{
|
2022-02-18 16:29:29 -05:00
|
|
|
// trace("DOIN SHIT" + FlxG.random.int(0, 200));
|
2022-02-11 14:21:42 -05:00
|
|
|
|
|
|
|
if (vis.snd.playing)
|
|
|
|
curTime = vis.snd.time;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Math.abs(curTime - vis.snd.time) > 10)
|
|
|
|
curTime = FlxMath.lerp(curTime, vis.snd.time, 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
curTime = vis.snd.time;
|
|
|
|
|
2022-02-18 16:29:29 -05:00
|
|
|
generateSection(vis.snd.time, realtimeVisLenght);
|
2022-02-11 14:21:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 14:23:45 -05:00
|
|
|
public function checkAndSetBuffer()
|
|
|
|
{
|
|
|
|
vis.checkAndSetBuffer();
|
|
|
|
|
|
|
|
if (vis.setBuffer)
|
|
|
|
{
|
|
|
|
audioData = vis.audioData;
|
|
|
|
sampleRate = vis.sampleRate;
|
|
|
|
setBuffer = vis.setBuffer;
|
|
|
|
numSamples = Std.int(audioData.length / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 14:21:42 -05:00
|
|
|
|
|
|
|
enum VISTYPE
|
|
|
|
{
|
|
|
|
STATIC;
|
|
|
|
UPDATED;
|
|
|
|
FREQUENCIES;
|
|
|
|
}
|