2021-09-16 16:04:46 +00:00
|
|
|
package;
|
|
|
|
|
|
|
|
import flixel.FlxSprite;
|
2021-09-16 18:50:02 +00:00
|
|
|
import flixel.group.FlxGroup;
|
|
|
|
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;
|
2021-09-16 19:28:29 +00:00
|
|
|
import flixel.system.FlxSound;
|
2021-09-16 16:04:46 +00:00
|
|
|
import flixel.util.FlxColor;
|
|
|
|
import lime.utils.Int16Array;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
var sampleRate:Int;
|
2021-09-16 19:28:29 +00:00
|
|
|
|
2021-09-17 18:12:36 +00:00
|
|
|
var lengthOfShit:Int = 500;
|
2021-09-16 19:28:29 +00:00
|
|
|
var daSound:FlxSound;
|
|
|
|
|
2021-09-17 18:12:36 +00:00
|
|
|
public var visType:VISTYPE = UPDATED;
|
|
|
|
|
|
|
|
public var col:Int = FlxColor.WHITE;
|
|
|
|
public var daHeight:Float = FlxG.height;
|
|
|
|
|
|
|
|
public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE, ?height:Float = 720)
|
2021-09-16 16:04:46 +00:00
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
2021-09-16 19:28:29 +00:00
|
|
|
this.daSound = daSound;
|
2021-09-17 18:12:36 +00:00
|
|
|
this.col = col;
|
|
|
|
this.daHeight = height;
|
|
|
|
|
|
|
|
regenLineShit();
|
|
|
|
|
|
|
|
// makeGraphic(200, 200, FlxColor.BLACK);
|
|
|
|
}
|
2021-09-16 18:50:02 +00:00
|
|
|
|
2021-09-17 18:12:36 +00:00
|
|
|
public function regenLineShit():Void
|
|
|
|
{
|
2021-09-16 19:28:29 +00:00
|
|
|
for (i in 0...lengthOfShit)
|
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * daHeight).makeGraphic(1, 1, col);
|
2021-09-16 19:28:29 +00:00
|
|
|
lineShit.active = false;
|
2021-09-16 18:50:02 +00:00
|
|
|
add(lineShit);
|
|
|
|
}
|
2021-09-16 16:04:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 19:28:29 +00:00
|
|
|
var setBuffer:Bool = false;
|
|
|
|
var audioData:Int16Array;
|
|
|
|
var numSamples:Int = 0;
|
|
|
|
|
2021-09-16 16:04:46 +00:00
|
|
|
override function update(elapsed:Float)
|
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
if (visType == UPDATED)
|
2021-09-16 16:04:46 +00:00
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
updateVisulizer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if visType is static, call updateVisulizer() manually whenever you want to update it!
|
2021-09-16 16:04:46 +00:00
|
|
|
|
2021-09-17 18:12:36 +00:00
|
|
|
super.update(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param start is the start in milliseconds?
|
|
|
|
*/
|
|
|
|
public function generateSection(start:Float = 0, seconds:Float = 1):Void
|
|
|
|
{
|
|
|
|
checkAndSetBuffer();
|
|
|
|
|
|
|
|
if (setBuffer)
|
|
|
|
{
|
|
|
|
var samplesToGen:Int = Std.int(sampleRate * seconds);
|
|
|
|
var startingSample:Int = Std.int(FlxMath.remapToRange(start, 0, daSound.length, 0, numSamples));
|
|
|
|
|
2021-09-17 18:56:57 +00:00
|
|
|
var prevLine:FlxPoint = new FlxPoint();
|
|
|
|
|
2021-09-17 18:12:36 +00:00
|
|
|
for (i in 0...group.members.length)
|
2021-09-16 19:28:29 +00:00
|
|
|
{
|
2021-09-17 18:12:36 +00: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 18:56:57 +00: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 18:12:36 +00:00
|
|
|
|
2021-09-17 18:56:57 +00:00
|
|
|
var line = FlxVector.get(prevLine.x - group.members[i].x, prevLine.y - group.members[i].y);
|
2021-09-17 18:12:36 +00:00
|
|
|
|
2021-09-17 18:56:57 +00:00
|
|
|
group.members[i].setGraphicSize(Std.int(Math.max(line.length, 1)), Std.int(1));
|
|
|
|
group.members[i].angle = line.degrees;
|
2021-09-16 19:28:29 +00:00
|
|
|
}
|
2021-09-17 18:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkAndSetBuffer()
|
|
|
|
{
|
2021-09-20 15:32:35 +00:00
|
|
|
if (daSound != null && daSound.playing)
|
2021-09-17 18:12:36 +00:00
|
|
|
{
|
|
|
|
if (!setBuffer)
|
2021-09-16 19:28:29 +00:00
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
// Math.pow3
|
|
|
|
@:privateAccess
|
|
|
|
var buf = daSound._channel.__source.buffer;
|
|
|
|
|
|
|
|
// @:privateAccess
|
|
|
|
audioData = cast buf.data; // jank and hacky lol! kinda busted on HTML5 also!!
|
|
|
|
sampleRate = buf.sampleRate;
|
|
|
|
|
|
|
|
trace('got audio buffer shit');
|
|
|
|
trace(sampleRate);
|
|
|
|
trace(buf.bitsPerSample);
|
|
|
|
|
|
|
|
setBuffer = true;
|
|
|
|
numSamples = Std.int(audioData.length / 2);
|
2021-09-16 19:28:29 +00:00
|
|
|
}
|
2021-09-17 18:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateVisulizer():Void
|
|
|
|
{
|
|
|
|
if (daSound != null)
|
|
|
|
{
|
|
|
|
var remappedShit:Int = 0;
|
|
|
|
|
|
|
|
checkAndSetBuffer();
|
2021-09-16 16:04:46 +00:00
|
|
|
|
2021-09-16 19:28:29 +00:00
|
|
|
if (setBuffer)
|
|
|
|
{
|
2021-09-17 18:12:36 +00:00
|
|
|
if (daSound.playing)
|
|
|
|
remappedShit = Std.int(FlxMath.remapToRange(daSound.time, 0, daSound.length, 0, numSamples));
|
|
|
|
else
|
|
|
|
remappedShit = Std.int(FlxMath.remapToRange(Conductor.songPosition, 0, daSound.length, 0, numSamples));
|
|
|
|
|
2021-09-16 16:04:46 +00:00
|
|
|
var i = remappedShit;
|
|
|
|
var prevLine:FlxPoint = new FlxPoint();
|
|
|
|
|
2021-09-16 18:50:02 +00:00
|
|
|
var swagheight:Int = 200;
|
|
|
|
|
2021-09-16 19:28:29 +00:00
|
|
|
for (sample in remappedShit...remappedShit + lengthOfShit)
|
2021-09-16 16:04:46 +00:00
|
|
|
{
|
|
|
|
var left = audioData[i] / 32767;
|
2021-09-16 19:28:29 +00:00
|
|
|
var right = audioData[i + 1] / 32767;
|
|
|
|
|
|
|
|
var balanced = (left + right) / 2;
|
|
|
|
|
2021-09-16 16:04:46 +00:00
|
|
|
i += 2;
|
|
|
|
|
2021-09-16 19:28:29 +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;
|
2021-09-17 18:12:36 +00:00
|
|
|
group.members[Std.int(remappedSample)].y = prevLine.y;
|
2021-09-16 18:50:02 +00:00
|
|
|
// group.members[0].y = prevLine.y;
|
2021-09-16 16:04:46 +00:00
|
|
|
|
2021-09-16 18:50:02 +00:00
|
|
|
// FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2);
|
2021-09-17 18:12:36 +00:00
|
|
|
prevLine.x = (balanced * swagheight / 2 + swagheight / 2) + x;
|
|
|
|
prevLine.y = (Std.int(remappedSample) / lengthOfShit * daHeight) + y;
|
2021-09-17 18:56:57 +00: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 16:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:12:36 +00:00
|
|
|
|
|
|
|
enum VISTYPE
|
|
|
|
{
|
|
|
|
STATIC;
|
|
|
|
UPDATED;
|
|
|
|
}
|