From 38e8b80ed9943a1d9177715a0e22e79ab2e19a57 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Fri, 17 Sep 2021 14:12:36 -0400 Subject: [PATCH] charting section visulizer stuff --- source/ChartingState.hx | 28 +++++++- source/SpectogramSprite.hx | 132 +++++++++++++++++++++++++++++-------- source/TitleState.hx | 4 ++ 3 files changed, 134 insertions(+), 30 deletions(-) diff --git a/source/ChartingState.hx b/source/ChartingState.hx index 4daa03fc3..2bd7565eb 100644 --- a/source/ChartingState.hx +++ b/source/ChartingState.hx @@ -25,6 +25,7 @@ import flixel.text.FlxText; import flixel.ui.FlxButton; import flixel.ui.FlxSpriteButton; import flixel.util.FlxColor; +import haxe.CallStack.StackItem; import haxe.Json; import lime.media.AudioBuffer; import lime.utils.Assets; @@ -362,6 +363,8 @@ class ChartingState extends MusicBeatState UI_box.addGroup(tab_group_note); } + var spec:SpectogramSprite; + function loadSong(daSong:String):Void { if (FlxG.sound.music != null) @@ -390,6 +393,8 @@ class ChartingState extends MusicBeatState FlxG.sound.playMusic(Paths.inst(daSong), 0.6); var musSpec:SpectogramSprite = new SpectogramSprite(FlxG.sound.music, FlxColor.RED); + musSpec.x += 70; + musSpec.daHeight = FlxG.height / 2; musSpec.scrollFactor.set(); add(musSpec); @@ -402,11 +407,21 @@ class ChartingState extends MusicBeatState vocals = new FlxSound().loadEmbedded(Paths.voices(daSong)); FlxG.sound.list.add(vocals); - var spec:SpectogramSprite = new SpectogramSprite(vocals); - spec.scrollFactor.set(); + var vocalSpec:SpectogramSprite = new SpectogramSprite(vocals); + vocalSpec.x += 70; + vocalSpec.daHeight = musSpec.daHeight; + vocalSpec.y = vocalSpec.daHeight; + vocalSpec.scrollFactor.set(); + add(vocalSpec); + + spec = new SpectogramSprite(vocals); + spec.x -= 150; + spec.daHeight = GRID_SIZE * 16; + spec.visType = STATIC; add(spec); FlxG.sound.music.pause(); + vocals.pause(); FlxG.sound.music.onComplete = function() @@ -520,6 +535,12 @@ class ChartingState extends MusicBeatState override function update(elapsed:Float) { + if (FlxG.keys.justPressed.B) + { + spec.visType = STATIC; + spec.generateSection(sectionStartTime(), (Conductor.stepCrochet * 32) / 1000); + } + curStep = recalculateSteps(); Conductor.songPosition = FlxG.sound.music.time; @@ -897,6 +918,9 @@ class ChartingState extends MusicBeatState function updateGrid():Void { + if (spec != null) + spec.generateSection(sectionStartTime(), (Conductor.stepCrochet * 32) / 1000); + while (curRenderedNotes.members.length > 0) { curRenderedNotes.remove(curRenderedNotes.members[0], true); diff --git a/source/SpectogramSprite.hx b/source/SpectogramSprite.hx index c6421bf52..5e282323d 100644 --- a/source/SpectogramSprite.hx +++ b/source/SpectogramSprite.hx @@ -13,24 +13,37 @@ using flixel.util.FlxSpriteUtil; class SpectogramSprite extends FlxTypedSpriteGroup { - var lengthOfShit:Int = 500; + var sampleRate:Int; + var lengthOfShit:Int = 500; var daSound:FlxSound; - public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE) + 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) { super(); this.daSound = daSound; + this.col = col; + this.daHeight = height; + regenLineShit(); + + // makeGraphic(200, 200, FlxColor.BLACK); + } + + public function regenLineShit():Void + { for (i in 0...lengthOfShit) { - var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * FlxG.height).makeGraphic(1, 1, col); + var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * daHeight).makeGraphic(1, 1, col); lineShit.active = false; add(lineShit); } - - // makeGraphic(200, 200, FlxColor.BLACK); } var setBuffer:Bool = false; @@ -38,33 +51,91 @@ class SpectogramSprite extends FlxTypedSpriteGroup var numSamples:Int = 0; override function update(elapsed:Float) + { + if (visType == UPDATED) + { + updateVisulizer(); + } + + // if visType is static, call updateVisulizer() manually whenever you want to update it! + + 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)); + + for (i in 0...group.members.length) + { + 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; + + group.members[i].x = (balanced * swagheight / 2 + swagheight / 2) + x; + + group.members[i].y = (i / group.members.length * daHeight) + y; + // group.members[0].y = prevLine.y; + + // FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2); + // prevLine.x = (balanced * swagheight / 2 + swagheight / 2) + x; + // width * (remappedSample / 255); + // prevLine.y = (i / group.members.length * daHeight) + y; + } + } + } + + public function checkAndSetBuffer() + { + if (daSound.playing) + { + if (!setBuffer) + { + // 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); + } + } + } + + public function updateVisulizer():Void { if (daSound != null) { var remappedShit:Int = 0; - if (daSound.playing) - { - if (!setBuffer) - { - @:privateAccess - audioData = cast daSound._channel.__source.buffer.data; // jank and hacky lol! - setBuffer = true; - numSamples = Std.int(audioData.length / 2); - } - else - { - remappedShit = Std.int(FlxMath.remapToRange(daSound.time, 0, daSound.length, 0, numSamples)); - } - } - else - { - if (setBuffer) - remappedShit = Std.int(FlxMath.remapToRange(Conductor.songPosition, 0, daSound.length, 0, numSamples)); - } + checkAndSetBuffer(); if (setBuffer) { + 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)); + var i = remappedShit; var prevLine:FlxPoint = new FlxPoint(); @@ -82,16 +153,21 @@ class SpectogramSprite extends FlxTypedSpriteGroup var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + lengthOfShit, 0, lengthOfShit - 1); group.members[Std.int(remappedSample)].x = prevLine.x; + group.members[Std.int(remappedSample)].y = prevLine.y; // group.members[0].y = prevLine.y; // FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2); - prevLine.x = balanced * swagheight / 2 + swagheight / 2; + prevLine.x = (balanced * swagheight / 2 + swagheight / 2) + x; // width * (remappedSample / 255); - // prevLine.y = left * swagheight / 2 + swagheight / 2; + prevLine.y = (Std.int(remappedSample) / lengthOfShit * daHeight) + y; } } } - - super.update(elapsed); } } + +enum VISTYPE +{ + STATIC; + UPDATED; +} diff --git a/source/TitleState.hx b/source/TitleState.hx index fa0e8e9eb..811e03284 100644 --- a/source/TitleState.hx +++ b/source/TitleState.hx @@ -493,6 +493,10 @@ class TitleState extends MusicBeatState cheatActive = true; FlxG.sound.playMusic(Paths.music('tutorialTitle'), 1); + + var spec:SpectogramSprite = new SpectogramSprite(FlxG.sound.music); + add(spec); + Conductor.changeBPM(190); FlxG.camera.flash(FlxColor.WHITE, 1); FlxG.sound.play(Paths.sound('confirmMenu'), 0.7);