Funkin/source/SpectogramSprite.hx

378 lines
10 KiB
Haxe
Raw Normal View History

2021-09-16 12:04:46 -04:00
package;
2021-09-27 22:30:38 -04:00
import dsp.FFT;
2021-09-16 12:04:46 -04:00
import flixel.FlxSprite;
2021-09-16 14:50:02 -04:00
import flixel.group.FlxGroup;
import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
2021-09-16 12:04:46 -04:00
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
2021-09-17 14:56:57 -04:00
import flixel.math.FlxVector;
2021-09-16 15:28:29 -04:00
import flixel.system.FlxSound;
2021-09-16 12:04:46 -04:00
import flixel.util.FlxColor;
import lime.utils.Int16Array;
2021-09-27 22:30:38 -04:00
using Lambda;
2021-09-16 12:04:46 -04:00
using flixel.util.FlxSpriteUtil;
2021-09-16 14:50:02 -04:00
class SpectogramSprite extends FlxTypedSpriteGroup<FlxSprite>
2021-09-16 12:04:46 -04:00
{
2021-09-17 14:12:36 -04:00
var sampleRate:Int;
2021-09-16 15:28:29 -04:00
2021-09-17 14:12:36 -04:00
var lengthOfShit:Int = 500;
2021-09-16 15:28:29 -04:00
2021-09-17 14:12:36 -04:00
public var visType:VISTYPE = UPDATED;
public var col:Int = FlxColor.WHITE;
public var daHeight:Float = FlxG.height;
public var vis:VisShit;
2021-09-17 14:12:36 -04:00
public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE, ?height:Float = 720)
2021-09-16 12:04:46 -04:00
{
super();
vis = new VisShit(daSound);
2021-09-17 14:12:36 -04:00
this.col = col;
this.daHeight = height;
regenLineShit();
// makeGraphic(200, 200, FlxColor.BLACK);
}
2021-09-16 14:50:02 -04:00
2021-09-17 14:12:36 -04:00
public function regenLineShit():Void
{
2021-09-16 15:28:29 -04:00
for (i in 0...lengthOfShit)
{
2021-09-17 14:12:36 -04:00
var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * daHeight).makeGraphic(1, 1, col);
2021-09-16 15:28:29 -04:00
lineShit.active = false;
2021-09-16 14:50:02 -04:00
add(lineShit);
}
2021-09-16 12:04:46 -04:00
}
2021-09-16 15:28:29 -04:00
var setBuffer:Bool = false;
2021-09-27 22:30:38 -04:00
public var audioData:Int16Array;
2021-09-16 15:28:29 -04:00
var numSamples:Int = 0;
2021-09-16 12:04:46 -04:00
override function update(elapsed:Float)
{
2021-09-27 22:30:38 -04:00
switch (visType)
2021-09-16 12:04:46 -04:00
{
2021-09-27 22:30:38 -04:00
case UPDATED:
updateVisulizer();
case FREQUENCIES:
updateFFT();
default:
2021-09-17 14:12:36 -04:00
}
// if visType is static, call updateVisulizer() manually whenever you want to update it!
2021-09-16 12:04:46 -04:00
2021-09-17 14:12:36 -04:00
super.update(elapsed);
}
/**
* @param start is the start in milliseconds?
*/
public function generateSection(start:Float = 0, seconds:Float = 1):Void
{
checkAndSetBuffer();
// vis.checkAndSetBuffer();
2021-09-17 14:12:36 -04:00
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 14:12:36 -04:00
2021-09-17 14:56:57 -04:00
var prevLine:FlxPoint = new FlxPoint();
2021-09-17 14:12:36 -04:00
for (i in 0...group.members.length)
2021-09-16 15:28:29 -04:00
{
2021-09-17 14:12:36 -04:00
var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, startingSample, startingSample + samplesToGen));
var left = audioData[sampleApprox] / 32767;
var right = audioData[sampleApprox + 1] / 32767;
var swagheight:Int = 200;
var balanced = (left + right) / 2;
2021-09-17 14:56:57 -04:00
group.members[i].x = prevLine.x;
group.members[i].y = prevLine.y;
prevLine.x = (balanced * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (i / group.members.length * daHeight) + y;
2021-09-17 14:12:36 -04:00
2021-09-17 14:56:57 -04:00
var line = FlxVector.get(prevLine.x - group.members[i].x, prevLine.y - group.members[i].y);
2021-09-17 14:12:36 -04:00
2021-09-17 14:56:57 -04:00
group.members[i].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
group.members[i].angle = line.degrees;
2021-09-16 15:28:29 -04:00
}
2021-09-17 14:12:36 -04:00
}
}
public function checkAndSetBuffer()
{
vis.checkAndSetBuffer();
2021-09-17 14:12:36 -04:00
if (vis.setBuffer)
{
audioData = vis.audioData;
sampleRate = vis.sampleRate;
setBuffer = vis.setBuffer;
numSamples = Std.int(audioData.length / 2);
2021-09-17 14:12:36 -04:00
}
}
var doAnim:Bool = false;
var frameCounter:Int = 0;
2021-09-27 22:30:38 -04:00
public function updateFFT()
{
if (vis.snd != null)
2021-09-27 22:30:38 -04:00
{
var remappedShit:Int = 0;
checkAndSetBuffer();
if (!doAnim)
{
frameCounter++;
if (frameCounter >= 3)
{
frameCounter = 0;
doAnim = true;
}
}
if (setBuffer && doAnim)
2021-09-27 22:30:38 -04:00
{
doAnim = false;
if (vis.snd.playing)
remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, numSamples));
2021-09-27 22:30:38 -04:00
else
remappedShit = Std.int(FlxMath.remapToRange(Conductor.songPosition, 0, vis.snd.length, 0, numSamples));
2021-09-27 22:30:38 -04:00
var i = remappedShit;
var prevLine:FlxPoint = new FlxPoint();
var swagheight:Int = 200;
var fftSamples:Array<Float> = [];
// var array:Array<Float> = cast audioData.subarray(remappedShit, remappedShit + lengthOfShit);
if (FlxG.keys.justPressed.M)
{
trace('POOP LOL');
var funnyAud = audioData.subarray(remappedShit, remappedShit + lengthOfShit);
for (poop in funnyAud)
{
// trace("actual audio: " + poop);
trace("win: " + poop);
}
// trace(audioData.subarray(remappedShit, remappedShit + lengthOfShit).buffer);
}
for (sample in remappedShit...remappedShit + (Std.int((44100 * (10 / 60)) / 4)))
2021-09-27 22:30:38 -04:00
{
var left = audioData[i] / 32767;
var right = audioData[i + 1] / 32767;
var balanced = (left + right) / 2;
i += 8;
2021-09-27 22:30:38 -04:00
// var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + lengthOfShit, 0, lengthOfShit - 1);
fftSamples.push(balanced);
}
var freqShit = funnyFFT(fftSamples, 4);
2021-09-27 22:30:38 -04: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);
2021-09-27 22:30:38 -04:00
// var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, startingSample, startingSample + samplesToGen));
var remappedFreq:Int = Std.int(FlxMath.remapToRange(hzPicker + 50, 0, 10000, 0, freqShit[0].length - 1));
2021-09-27 22:30:38 -04:00
group.members[i].x = prevLine.x;
group.members[i].y = prevLine.y;
var freqPower:Float = 0;
for (pow in 0...freqShit.length)
freqPower += freqShit[pow][remappedFreq];
freqPower /= freqShit.length;
// freqShit[remappedFreq]
var freqIDK:Float = FlxMath.remapToRange(freqPower, 0, 0.000005, 0, 50);
2021-09-27 22:30:38 -04:00
prevLine.x = (freqIDK * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (i / group.members.length * daHeight) + y;
2021-09-28 18:12:32 -04:00
var line = FlxVector.get(prevLine.x - group.members[i].x, prevLine.y - group.members[i].y);
2021-09-27 22:30:38 -04:00
// group.members[i].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
// group.members[i].angle = line.degrees;
}
/*
for (freq in 0...freqShit.length)
{
var remappedFreq:Float = FlxMath.remapToRange(freq, 0, freqShit.length, 0, lengthOfShit - 1);
group.members[Std.int(remappedFreq)].x = prevLine.x;
group.members[Std.int(remappedFreq)].y = prevLine.y;
var freqShit:Float = FlxMath.remapToRange(freqShit[freq], 0, 0.002, 0, 20);
prevLine.x = (freqShit * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (Math.ceil(remappedFreq) / lengthOfShit * daHeight) + y;
}*/
}
}
}
2021-09-17 14:12:36 -04:00
public function updateVisulizer():Void
{
if (vis.snd != null)
2021-09-17 14:12:36 -04:00
{
var remappedShit:Int = 0;
checkAndSetBuffer();
2021-09-16 12:04:46 -04:00
2021-09-16 15:28:29 -04:00
if (setBuffer)
{
if (vis.snd.playing)
remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, numSamples));
2021-09-17 14:12:36 -04:00
else
remappedShit = Std.int(FlxMath.remapToRange(Conductor.songPosition, 0, vis.snd.length, 0, numSamples));
2021-09-17 14:12:36 -04:00
2021-09-16 12:04:46 -04:00
var i = remappedShit;
var prevLine:FlxPoint = new FlxPoint();
2021-09-16 14:50:02 -04:00
var swagheight:Int = 200;
2021-09-16 15:28:29 -04:00
for (sample in remappedShit...remappedShit + lengthOfShit)
2021-09-16 12:04:46 -04:00
{
var left = audioData[i] / 32767;
2021-09-16 15:28:29 -04:00
var right = audioData[i + 1] / 32767;
var balanced = (left + right) / 2;
2021-09-16 12:04:46 -04:00
i += 2;
2021-09-16 15:28:29 -04:00
var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + lengthOfShit, 0, lengthOfShit - 1);
2021-09-16 14:50:02 -04:00
group.members[Std.int(remappedSample)].x = prevLine.x;
2021-09-17 14:12:36 -04:00
group.members[Std.int(remappedSample)].y = prevLine.y;
2021-09-16 14:50:02 -04:00
// group.members[0].y = prevLine.y;
2021-09-16 12:04:46 -04:00
2021-09-16 14:50:02 -04:00
// FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2);
2021-09-17 14:12:36 -04:00
prevLine.x = (balanced * swagheight / 2 + swagheight / 2) + x;
prevLine.y = (Std.int(remappedSample) / lengthOfShit * daHeight) + y;
2021-09-17 14:56:57 -04:00
var line = FlxVector.get(prevLine.x - group.members[Std.int(remappedSample)].x, prevLine.y - group.members[Std.int(remappedSample)].y);
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 12:04:46 -04:00
}
}
}
}
2021-09-27 22:30:38 -04:00
function funnyFFT(samples:Array<Float>, ?skipped:Int = 1):Array<Array<Float>>
2021-09-27 22:30:38 -04:00
{
// nab multiple samples at once in while / for loops?
var fs:Float = 44100 / skipped; // sample rate shit?
2021-09-27 22:30:38 -04:00
final fftN = 2048;
final halfN = Std.int(fftN / 2);
final overlap = 0.5;
final hop = Std.int(fftN * (1 - overlap));
// window function to compensate for overlapping
final a0 = 0.5; // => Hann(ing) window
2021-09-27 22:30:38 -04:00
final window = (n:Int) -> a0 - (1 - a0) * Math.cos(2 * Math.PI * n / fftN);
// helpers, note that spectrum indexes suppose non-negative frequencies
final binSize = fs / fftN;
final indexToFreq = (k:Int) -> 1.0 * k * binSize; // we need the `1.0` to avoid overflows
// "melodic" band-pass filter
2021-09-28 18:12:32 -04:00
final minFreq = 20.70;
final maxFreq = 4000.01;
2021-09-27 22:30:38 -04:00
final melodicBandPass = function(k:Int, s:Float)
{
// final freq = indexToFreq(k);
// final filter = freq > minFreq - binSize && freq < maxFreq + binSize ? 1 : 0;
return s;
2021-09-27 22:30:38 -04:00
};
var freqOutput:Array<Array<Float>> = [];
2021-09-27 22:30:38 -04:00
var c = 0; // index where each chunk begins
var indexOfArray:Int = 0;
2021-09-27 22:30:38 -04:00
while (c < samples.length)
{
// take a chunk (zero-padded if needed) and apply the window
final chunk = [
for (n in 0...fftN)
(c + n < samples.length ? samples[c + n] : 0.0) * window(n)
];
// compute positive spectrum with sampling correction and BP filter
final freqs = FFT.rfft(chunk).map(z -> z.scale(1 / fs).magnitude).mapi(melodicBandPass);
freqOutput.push([]);
2021-09-27 22:30:38 -04:00
// find spectral peaks and their instantaneous frequencies
for (k => s in freqs)
{
final time = c / fs;
final freq = indexToFreq(k);
2021-09-28 18:12:32 -04:00
final power = s * s;
2021-09-27 22:30:38 -04:00
if (FlxG.keys.justPressed.N)
{
haxe.Log.trace('${time};${freq};${power}', null);
}
2021-09-28 18:12:32 -04:00
if (freq < maxFreq)
freqOutput[indexOfArray].push(power);
2021-09-27 22:30:38 -04:00
//
}
// haxe.Log.trace("", null);
indexOfArray++;
2021-09-27 22:30:38 -04:00
// move to next (overlapping) chunk
c += hop;
}
if (FlxG.keys.justPressed.C)
trace(freqOutput.length);
2021-09-27 22:30:38 -04:00
return freqOutput;
}
2021-09-16 12:04:46 -04:00
}
2021-09-17 14:12:36 -04:00
enum VISTYPE
{
STATIC;
UPDATED;
2021-09-27 22:30:38 -04:00
FREQUENCIES;
2021-09-17 14:12:36 -04:00
}