viz in progress

This commit is contained in:
Cameron Taylor 2024-02-23 04:00:31 -05:00
parent b25aa3badb
commit 6f88865e0b
5 changed files with 140 additions and 104 deletions

View file

@ -109,6 +109,7 @@
<haxelib name="polymod" /> <!-- Modding framework -->
<haxelib name="flxanimate" /> <!-- Texture atlas rendering -->
<haxelib name="hxCodec" if="desktop release" /> <!-- Video playback -->
<haxelib name="funkVis"/>
<haxelib name="json2object" /> <!-- JSON parsing -->
<haxelib name="thx.semver" /> <!-- Version string handling -->

2
assets

@ -1 +1 @@
Subproject commit ed3eb91f4b04fa0473128698e0e079a28998401e
Subproject commit d0dc456b49bd9d62c924b2f87f0205dba338705e

View file

@ -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<FlxSprite>
{
public var vis:VisShit;
// public var vis:VisShit;
var analyzer:SpectralAnalyzer;
var volumes:Array<Float> = [];
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<FlxSprite>
}
}
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<Float> = [];
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<Float> = [];
// 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;
// }
// }
// }
// }
}

View file

@ -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));
}
}

View file

@ -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));
}
/**