From 2eedd1a20f664cac786be5c178f976b578cf4ad5 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Thu, 16 Sep 2021 15:28:29 -0400 Subject: [PATCH] more cool spectogram shit --- source/ChartingState.hx | 41 ++++++------------------ source/SpectogramSprite.hx | 65 ++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/source/ChartingState.hx b/source/ChartingState.hx index 96ab4a4a9..4daa03fc3 100644 --- a/source/ChartingState.hx +++ b/source/ChartingState.hx @@ -98,10 +98,6 @@ 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); @@ -393,43 +389,23 @@ class ChartingState extends MusicBeatState FlxG.sound.playMusic(Paths.inst(daSong), 0.6); - @:privateAccess - var audioData:Int16Array = FlxG.sound.music._channel.__source.buffer.data; + var musSpec:SpectogramSprite = new SpectogramSprite(FlxG.sound.music, FlxColor.RED); + musSpec.scrollFactor.set(); + add(musSpec); // trace(audioBuf.data.length); playheadTest = new FlxSprite(0, 0).makeGraphic(2, 255, FlxColor.RED); playheadTest.scrollFactor.set(); add(playheadTest); - var sampleLength:Int = Std.int(audioData.length / 2); - var i = 0; - - var wavHeight:Int = FlxG.height; - var funnyShit:FlxSprite = new FlxSprite().makeGraphic(FlxG.width, wavHeight, FlxColor.TRANSPARENT); - funnyShit.scrollFactor.set(); - // add(funnyShit); - - var prevLine:FlxPoint = new FlxPoint(); - - for (sampleShit in 0...FlxG.width) - { - // thnx mike welsh fo dis - var left = audioData[i] / 32767; // 16-bit audio samples are from -32767 to 32767, convert to -1.0 to 1.0 - var right = audioData[i + 1] / 32767; - - var adjusted:Int = Std.int(sampleLength / FlxG.width); - - i += 2 * adjusted; - - funnyShit.drawLine(prevLine.x, prevLine.y, FlxG.width * sampleShit / FlxG.width, left * wavHeight / 2 + wavHeight / 2); - prevLine.x = FlxG.width * sampleShit / FlxG.width; - prevLine.y = left * wavHeight / 2 + wavHeight / 2; - } - // WONT WORK FOR TUTORIAL OR TEST SONG!!! REDO LATER vocals = new FlxSound().loadEmbedded(Paths.voices(daSong)); FlxG.sound.list.add(vocals); + var spec:SpectogramSprite = new SpectogramSprite(vocals); + spec.scrollFactor.set(); + add(spec); + FlxG.sound.music.pause(); vocals.pause(); @@ -707,6 +683,9 @@ class ChartingState extends MusicBeatState var daTime:Float = 700 * FlxG.elapsed; + if (FlxG.keys.pressed.CONTROL) + daTime *= 0.2; + if (FlxG.keys.pressed.W) { FlxG.sound.music.time -= daTime; diff --git a/source/SpectogramSprite.hx b/source/SpectogramSprite.hx index bad8203fd..c6421bf52 100644 --- a/source/SpectogramSprite.hx +++ b/source/SpectogramSprite.hx @@ -5,6 +5,7 @@ import flixel.group.FlxGroup; import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup; import flixel.math.FlxMath; import flixel.math.FlxPoint; +import flixel.system.FlxSound; import flixel.util.FlxColor; import lime.utils.Int16Array; @@ -12,55 +13,79 @@ using flixel.util.FlxSpriteUtil; class SpectogramSprite extends FlxTypedSpriteGroup { - public function new() + var lengthOfShit:Int = 500; + + var daSound:FlxSound; + + public function new(daSound:FlxSound, ?col:FlxColor = FlxColor.WHITE) { super(); - for (i in 0...256) + this.daSound = daSound; + + for (i in 0...lengthOfShit) { - var lineShit:FlxSprite = new FlxSprite(100, i / 256 * FlxG.height).makeGraphic(1, 1); - // lineShit.origin.set(); - - // var xClip = lineShit.clipRect; - // xClip.width = 1; - - // lineShit.clipRect = xClip; + var lineShit:FlxSprite = new FlxSprite(100, i / lengthOfShit * FlxG.height).makeGraphic(1, 1, col); + lineShit.active = false; add(lineShit); } // makeGraphic(200, 200, FlxColor.BLACK); } + var setBuffer:Bool = false; + var audioData:Int16Array; + var numSamples:Int = 0; + override function update(elapsed:Float) { - if (FlxG.sound.music != null) + if (daSound != null) { - if (FlxG.sound.music.playing) + var remappedShit:Int = 0; + + if (daSound.playing) { - // FlxSpriteUtil.drawRect(this, 0, 0, width, height, 0x45000000); + 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)); + } - @: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)); + if (setBuffer) + { var i = remappedShit; var prevLine:FlxPoint = new FlxPoint(); var swagheight:Int = 200; - for (sample in remappedShit...remappedShit + 256) + for (sample in remappedShit...remappedShit + lengthOfShit) { var left = audioData[i] / 32767; + var right = audioData[i + 1] / 32767; + + var balanced = (left + right) / 2; + i += 2; - var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + 256, 0, 255); + var remappedSample:Float = FlxMath.remapToRange(sample, remappedShit, remappedShit + lengthOfShit, 0, lengthOfShit - 1); group.members[Std.int(remappedSample)].x = prevLine.x; // group.members[0].y = prevLine.y; // FlxSpriteUtil.drawLine(this, prevLine.x, prevLine.y, width * remappedSample, left * height / 2 + height / 2); - prevLine.x = left * swagheight / 2 + swagheight / 2; + prevLine.x = balanced * swagheight / 2 + swagheight / 2; // width * (remappedSample / 255); // prevLine.y = left * swagheight / 2 + swagheight / 2; }