From d00f2268f927b8911a520702d6a972c22176be91 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Thu, 16 Sep 2021 12:04:46 -0400 Subject: [PATCH] spectogram visulizer in progress --- source/ChartingState.hx | 4 +++ source/SpectogramSprite.hx | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 source/SpectogramSprite.hx diff --git a/source/ChartingState.hx b/source/ChartingState.hx index 20686d4e4..6669c2adb 100644 --- a/source/ChartingState.hx +++ b/source/ChartingState.hx @@ -98,6 +98,10 @@ class ChartingState extends MusicBeatState trace(audioBuf.sampleRate); + var spec:SpectogramSprite = new SpectogramSprite(); + spec.scrollFactor.set(); + add(spec); + gridBG = FlxGridOverlay.create(GRID_SIZE, GRID_SIZE, GRID_SIZE * 8, GRID_SIZE * 16); add(gridBG); diff --git a/source/SpectogramSprite.hx b/source/SpectogramSprite.hx new file mode 100644 index 000000000..068136cb7 --- /dev/null +++ b/source/SpectogramSprite.hx @@ -0,0 +1,55 @@ +package; + +import flixel.FlxSprite; +import flixel.math.FlxMath; +import flixel.math.FlxPoint; +import flixel.util.FlxColor; +import lime.utils.Int16Array; + +using flixel.util.FlxSpriteUtil; + +class SpectogramSprite extends FlxSprite +{ + public function new() + { + super(); + + makeGraphic(200, 200, FlxColor.BLUE); + } + + override function update(elapsed:Float) + { + if (FlxG.sound.music != null) + { + if (FlxG.sound.music.playing) + { + FlxSpriteUtil.drawRect(this, 0, 0, width, height, FlxColor.BLUE); + + @:privateAccess + var audioData:Int16Array = FlxG.sound.music._channel.__source.buffer.data; // jank and hacky lol! + + var numSamples:Int = Std.int(audioData.length / 2); + + var remappedShit:Int = Std.int(FlxMath.remapToRange(FlxG.sound.music.time, 0, FlxG.sound.music.length, 0, numSamples)); + + var i = remappedShit; + + var prevLine:FlxPoint = new FlxPoint(); + + for (sample in remappedShit...remappedShit + 256) + { + var left = audioData[i] / 32767; + i += 2; + + var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + 256, 0, 1); + + FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2); + prevLine.x = width * remappedSample; + prevLine.y = left * height / 2 + height / 2; + } + } + } + + super.update(elapsed); + } +}