Skip to content

Commit e7c1e48

Browse files
committed
WIP web stuff
1 parent 44b2522 commit e7c1e48

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

source/funkin/audio/waveform/WaveformDataParser.hx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package funkin.audio.waveform;
22

3+
import lime.utils.Float32Array;
34
import funkin.util.TimerUtil;
45
import haxe.ds.Vector;
56
import haxe.io.Bytes;
7+
#if web
8+
import lime.utils.UInt8Array;
9+
import js.html.audio.AnalyserNode;
10+
#end
611

712
@:nullSafety
813
class WaveformDataParser
@@ -47,14 +52,27 @@ class WaveformDataParser
4752

4853
public static function interpretAudioBuffer(soundBuffer:lime.media.AudioBuffer):Null<WaveformData>
4954
{
55+
#if web
56+
var sampleRate = soundBuffer.src._sounds[0]._node.context.sampleRate;
57+
var channels = 2;
58+
var bitsPerSample = 32;
59+
60+
var analyser:AnalyserNode = soundBuffer.src._sounds[0]._node.context.createAnalyser();
61+
analyser.fftSize = 2048;
62+
63+
var data = new Float32Array(analyser.frequencyBinCount);
64+
analyser.getFloatTimeDomainData(data);
65+
var soundData = data.toBytes();
66+
#else
5067
var sampleRate = soundBuffer.sampleRate;
5168
var channels = soundBuffer.channels;
5269
var bitsPerSample = soundBuffer.bitsPerSample;
70+
var soundData:Bytes = soundBuffer.data.toBytes();
71+
#end
72+
5373
var samplesPerPoint:Int = 256; // I don't think we need to configure this.
5474
var pointsPerSecond:Float = sampleRate / samplesPerPoint; // 172 samples per second for most songs is plenty precise while still being performant..
5575

56-
// TODO: Make this work better on HTML5.
57-
var soundData:Bytes = soundBuffer.data.toBytes();
5876
var fakeBitsPerSample:Int = bitsPerSample;
5977
var minSampleValue:Int;
6078
var maxSampleValue:Int;

source/funkin/ui/debug/WaveformTestState.hx

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package funkin.ui.debug;
22

3+
import funkin.util.assets.SoundUtil;
34
import flixel.math.FlxRect;
45
import flixel.FlxSprite;
56
import flixel.util.FlxColor;
67
import funkin.audio.FunkinSound;
78
import funkin.audio.waveform.WaveformData;
89
import funkin.audio.waveform.WaveformDataParser;
10+
import funkin.audio.waveform.WaveformSprite;
911
import funkin.graphics.rendering.MeshRender;
1012

1113
class WaveformTestState extends MusicBeatState
@@ -20,7 +22,7 @@ class WaveformTestState extends MusicBeatState
2022

2123
var waveformAudio:FunkinSound;
2224

23-
// var waveformSprite:WaveformSprite;
25+
var waveformSprite:WaveformSprite;
2426
// var waveformSprite2:WaveformSprite;
2527
var timeMarker:FlxSprite;
2628

@@ -37,30 +39,34 @@ class WaveformTestState extends MusicBeatState
3739
testSprite.clipRect = new FlxRect(0, 0, FlxG.width, FlxG.height);
3840
// add(testSprite);
3941

40-
waveformAudio = FunkinSound.load(Paths.inst('bopeebo', '-erect'));
41-
42-
waveformData = WaveformDataParser.interpretFlxSound(waveformAudio);
42+
Assets.loadSound(Paths.music('chartEditorLoop/chartEditorLoop')).onComplete(sound -> {
43+
waveformAudio = FunkinSound.load(sound);
44+
waveformData = WaveformDataParser.interpretFlxSound(waveformAudio);
45+
46+
waveformSprite = WaveformSprite.buildFromWaveformData(waveformData, HORIZONTAL, FlxColor.fromString("#ADD8E6"));
47+
waveformSprite.duration = 5.0 * 160;
48+
waveformSprite.width = FlxG.width * 160;
49+
waveformSprite.height = FlxG.height; // / 2;
50+
waveformSprite.amplitude = 2.0;
51+
waveformSprite.minWaveformSize = 25;
52+
waveformSprite.clipRect = new FlxRect(0, 0, FlxG.width, FlxG.height);
53+
add(waveformSprite);
54+
55+
//
56+
// waveformSprite2 = WaveformSprite.buildFromWaveformData(waveformData2, HORIZONTAL, FlxColor.fromString("#FF0000"), 5.0);
57+
// waveformSprite2.width = FlxG.width;
58+
// waveformSprite2.height = FlxG.height / 2;
59+
// waveformSprite2.y = FlxG.height / 2;
60+
// add(waveformSprite2);
61+
}).onError(e -> {
62+
throw 'FUCKKKK $e';
63+
});
4364

4465
polygonSprite = new MeshRender(FlxG.width / 2, FlxG.height / 2, FlxColor.WHITE);
4566

4667
setPolygonVertices(vertexCount);
4768
add(polygonSprite);
4869

49-
// waveformSprite = WaveformSprite.buildFromWaveformData(waveformData, HORIZONTAL, FlxColor.fromString("#ADD8E6"));
50-
// waveformSprite.duration = 5.0 * 160;
51-
// waveformSprite.width = FlxG.width * 160;
52-
// waveformSprite.height = FlxG.height; // / 2;
53-
// waveformSprite.amplitude = 2.0;
54-
// waveformSprite.minWaveformSize = 25;
55-
// waveformSprite.clipRect = new FlxRect(0, 0, FlxG.width, FlxG.height);
56-
// add(waveformSprite);
57-
//
58-
// waveformSprite2 = WaveformSprite.buildFromWaveformData(waveformData2, HORIZONTAL, FlxColor.fromString("#FF0000"), 5.0);
59-
// waveformSprite2.width = FlxG.width;
60-
// waveformSprite2.height = FlxG.height / 2;
61-
// waveformSprite2.y = FlxG.height / 2;
62-
// add(waveformSprite2);
63-
6470
timeMarker = new FlxSprite(0, FlxG.height * 1 / 6);
6571
timeMarker.makeGraphic(1, Std.int(FlxG.height * 2 / 3), FlxColor.RED);
6672
add(timeMarker);
@@ -74,13 +80,13 @@ class WaveformTestState extends MusicBeatState
7480

7581
if (FlxG.keys.justPressed.SPACE)
7682
{
77-
if (waveformAudio.isPlaying)
83+
if (waveformAudio != null && waveformAudio.isPlaying)
7884
{
79-
waveformAudio.stop();
85+
waveformAudio?.stop();
8086
}
8187
else
8288
{
83-
waveformAudio.play();
89+
waveformAudio?.play();
8490
}
8591
}
8692

@@ -98,11 +104,11 @@ class WaveformTestState extends MusicBeatState
98104
// }
99105
}
100106

101-
if (waveformAudio.isPlaying)
107+
if (waveformAudio?.isPlaying ?? false)
102108
{
103109
// waveformSprite takes a time in fractional seconds, not milliseconds.
104110
var timeSeconds = waveformAudio.time / 1000;
105-
// waveformSprite.time = timeSeconds;
111+
waveformSprite.time = timeSeconds;
106112
// waveformSprite2.time = timeSeconds;
107113
}
108114

0 commit comments

Comments
 (0)