From 6f88865e0bd827b1858e3a7dbcd3ca7b2bdd3d9f Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Fri, 23 Feb 2024 04:00:31 -0500 Subject: [PATCH] viz in progress --- Project.xml | 1 + assets | 2 +- source/funkin/audio/visualize/ABotVis.hx | 214 +++++++++++---------- source/funkin/audio/visualize/AudioClip.hx | 23 +++ source/funkin/play/PlayState.hx | 4 +- 5 files changed, 140 insertions(+), 104 deletions(-) create mode 100644 source/funkin/audio/visualize/AudioClip.hx diff --git a/Project.xml b/Project.xml index c58153575..434ae52c9 100644 --- a/Project.xml +++ b/Project.xml @@ -109,6 +109,7 @@ + diff --git a/assets b/assets index ed3eb91f4..d0dc456b4 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit ed3eb91f4b04fa0473128698e0e079a28998401e +Subproject commit d0dc456b49bd9d62c924b2f87f0205dba338705e diff --git a/source/funkin/audio/visualize/ABotVis.hx b/source/funkin/audio/visualize/ABotVis.hx index 1bd7d0457..2501ca6e6 100644 --- a/source/funkin/audio/visualize/ABotVis.hx +++ b/source/funkin/audio/visualize/ABotVis.hx @@ -8,20 +8,26 @@ import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup; import flixel.math.FlxMath; import flixel.sound.FlxSound; import funkin.util.MathUtil; +import funkVis.dsp.SpectralAnalyzer; using Lambda; class ABotVis extends FlxTypedSpriteGroup { - public var vis:VisShit; + // public var vis:VisShit; + var analyzer:SpectralAnalyzer; var volumes:Array = []; + public var snd:FlxSound; + public function new(snd:FlxSound) { super(); - vis = new VisShit(snd); + this.snd = snd; + + // vis = new VisShit(snd); // vis.snd = snd; var visFrms:FlxAtlasFrames = Paths.getSparrowAtlas('aBotViz'); @@ -48,118 +54,124 @@ class ABotVis extends FlxTypedSpriteGroup } } + public function initAnalyzer() + { + @:privateAccess + analyzer = new SpectralAnalyzer(7, new AudioClip(cast snd._channel.__source), 0.005, 30); + } + override function update(elapsed:Float) { // updateViz(); - updateFFT(elapsed); + // updateFFT(elapsed); super.update(elapsed); } - function updateFFT(elapsed:Float) + static inline function min(x:Int, y:Int):Int { - if (vis.snd != null) - { - vis.checkAndSetBuffer(); - - if (vis.setBuffer) - { - var remappedShit:Int = 0; - - if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, vis.numSamples)); - else - remappedShit = Std.int(FlxMath.remapToRange(Conductor.instance.songPosition, 0, vis.snd.length, 0, vis.numSamples)); - - var fftSamples:Array = []; - - var swagBucks = remappedShit; - - for (i in remappedShit...remappedShit + (Std.int((44100 * (1 / 144))))) - { - var left = vis.audioData[swagBucks] / 32767; - var right = vis.audioData[swagBucks + 1] / 32767; - - var balanced = (left + right) / 2; - - swagBucks += 2; - - fftSamples.push(balanced); - } - - var freqShit = vis.funnyFFT(fftSamples); - - for (i in 0...group.members.length) - { - var getSliceShit = function(s:Int) { - var powShit = FlxMath.remapToRange(s, 0, group.members.length, 0, MathUtil.logBase(10, freqShit[0].length)); - return Math.round(Math.pow(10, powShit)); - }; - - // var powShit:Float = getSliceShit(i); - var hzSliced:Int = getSliceShit(i); - - var sliceLength:Int = Std.int(freqShit[0].length / group.members.length); - - var volSlice = freqShit[0].slice(hzSliced, getSliceShit(i + 1)); - - var avgVel:Float = 0; - - for (slice in volSlice) - { - avgVel += slice; - } - - avgVel /= volSlice.length; - - avgVel *= 10000000; - - volumes[i] += avgVel - (elapsed * (volumes[i] * 50)); - - var animFrame:Int = Std.int(volumes[i]); - - animFrame = Math.floor(Math.min(5, animFrame)); - animFrame = Math.floor(Math.max(0, animFrame)); - - animFrame = Std.int(Math.abs(animFrame - 5)); // shitty dumbass flip, cuz dave got da shit backwards lol! - - group.members[i].animation.curAnim.curFrame = animFrame; - if (FlxG.keys.justPressed.U) - { - trace(avgVel); - trace(group.members[i].animation.curAnim.curFrame); - } - } - - // group.members[0].animation.curAnim.curFrame = - } - } + return x > y ? y : x; } - public function updateViz() + override function draw() { - if (vis.snd != null) + if (analyzer == null) { - var remappedShit:Int = 0; - vis.checkAndSetBuffer(); - - if (vis.setBuffer) - { - // var startingSample:Int = Std.int(FlxMath.remapToRange) - - if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, vis.numSamples)); - - for (i in 0...group.members.length) - { - var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, remappedShit, remappedShit + 500)); - - var left = vis.audioData[sampleApprox] / 32767; - - var animFrame:Int = Std.int(FlxMath.remapToRange(left, -1, 1, 0, 6)); - - group.members[i].animation.curAnim.curFrame = animFrame; - } - } + super.draw(); + return; } + + var levels = analyzer.getLevels(false); + + for (i in 0...min(group.members.length, levels.length)) + { + var animFrame:Int = Math.round(levels[i].value * 5); + + animFrame = Math.floor(Math.min(5, animFrame)); + animFrame = Math.floor(Math.max(0, animFrame)); + + animFrame = Std.int(Math.abs(animFrame - 5)); // shitty dumbass flip, cuz dave got da shit backwards lol! + + group.members[i].animation.curAnim.curFrame = animFrame; + } + + super.draw(); } + + // function updateFFT(elapsed:Float) + // { + // if (vis.snd != null) + // { + // vis.checkAndSetBuffer(); + // if (vis.setBuffer) + // { + // var remappedShit:Int = 0; + // if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, vis.numSamples)); + // else + // remappedShit = Std.int(FlxMath.remapToRange(Conductor.instance.songPosition, 0, vis.snd.length, 0, vis.numSamples)); + // var fftSamples:Array = []; + // var swagBucks = remappedShit; + // for (i in remappedShit...remappedShit + (Std.int((44100 * (1 / 144))))) + // { + // var left = vis.audioData[swagBucks] / 32767; + // var right = vis.audioData[swagBucks + 1] / 32767; + // var balanced = (left + right) / 2; + // swagBucks += 2; + // fftSamples.push(balanced); + // } + // var freqShit = vis.funnyFFT(fftSamples); + // for (i in 0...group.members.length) + // { + // var getSliceShit = function(s:Int) { + // var powShit = FlxMath.remapToRange(s, 0, group.members.length, 0, MathUtil.logBase(10, freqShit[0].length)); + // return Math.round(Math.pow(10, powShit)); + // }; + // // var powShit:Float = getSliceShit(i); + // var hzSliced:Int = getSliceShit(i); + // var sliceLength:Int = Std.int(freqShit[0].length / group.members.length); + // var volSlice = freqShit[0].slice(hzSliced, getSliceShit(i + 1)); + // var avgVel:Float = 0; + // for (slice in volSlice) + // { + // avgVel += slice; + // } + // avgVel /= volSlice.length; + // avgVel *= 10000000; + // volumes[i] += avgVel - (elapsed * (volumes[i] * 50)); + // var animFrame:Int = Std.int(volumes[i]); + // animFrame = Math.floor(Math.min(5, animFrame)); + // animFrame = Math.floor(Math.max(0, animFrame)); + // animFrame = Std.int(Math.abs(animFrame - 5)); // shitty dumbass flip, cuz dave got da shit backwards lol! + // group.members[i].animation.curAnim.curFrame = animFrame; + // if (FlxG.keys.justPressed.U) + // { + // trace(avgVel); + // trace(group.members[i].animation.curAnim.curFrame); + // } + // } + // // group.members[0].animation.curAnim.curFrame = + // } + // } + // } + // public function updateViz() + // { + // if (vis.snd != null) + // { + // var remappedShit:Int = 0; + // vis.checkAndSetBuffer(); + // if (vis.setBuffer) + // { + // // var startingSample:Int = Std.int(FlxMath.remapToRange) + // if (vis.snd.playing) remappedShit = Std.int(FlxMath.remapToRange(vis.snd.time, 0, vis.snd.length, 0, vis.numSamples)); + // for (i in 0...group.members.length) + // { + // var sampleApprox:Int = Std.int(FlxMath.remapToRange(i, 0, group.members.length, remappedShit, remappedShit + 500)); + // var left = vis.audioData[sampleApprox] / 32767; + // var animFrame:Int = Std.int(FlxMath.remapToRange(left, -1, 1, 0, 6)); + // group.members[i].animation.curAnim.curFrame = animFrame; + // } + // } + // } + // } } diff --git a/source/funkin/audio/visualize/AudioClip.hx b/source/funkin/audio/visualize/AudioClip.hx new file mode 100644 index 000000000..a8e353799 --- /dev/null +++ b/source/funkin/audio/visualize/AudioClip.hx @@ -0,0 +1,23 @@ +package funkin.audio.visualize; + +import flixel.FlxG; +import flixel.math.FlxMath; +import funkVis.AudioBuffer; +import lime.media.AudioSource; + +class AudioClip implements funkVis.AudioClip +{ + public var audioBuffer(default, null):AudioBuffer; + public var currentFrame(get, never):Int; + + public function new(audioSource:AudioSource) + { + var data:lime.utils.UInt16Array = cast audioSource.buffer.data; + this.audioBuffer = new AudioBuffer(data, audioSource.buffer.sampleRate); + } + + private function get_currentFrame():Int + { + return Std.int(FlxMath.remapToRange(FlxG.sound.music.time, 0, FlxG.sound.music.length, 0, audioBuffer.data.length / 2)); + } +} diff --git a/source/funkin/play/PlayState.hx b/source/funkin/play/PlayState.hx index 1dbba5b54..0112a59e8 100644 --- a/source/funkin/play/PlayState.hx +++ b/source/funkin/play/PlayState.hx @@ -1741,8 +1741,6 @@ class PlayState extends MusicBeatSubState */ function startSong():Void { - dispatchEvent(new ScriptEvent(SONG_START)); - startingSong = false; if (!overrideMusic && !isGamePaused && currentChart != null) @@ -1772,6 +1770,8 @@ class PlayState extends MusicBeatSubState // FlxG.sound.music.time = startTimestamp - Conductor.instance.instrumentalOffset; handleSkippedNotes(); } + + dispatchEvent(new ScriptEvent(SONG_START)); } /**