Skip to content

Commit

Permalink
improved waveform
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Jun 10, 2024
1 parent d94175d commit 6f02151
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 91 deletions.
27 changes: 10 additions & 17 deletions assets/preload/data/characters/bf-holding-gf.hx
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import funkin.objects.HealthIcon;
var gfIcon:HealthIcon;
var doStuff:Bool = false; // Prevent null error

var gfIcon;
var hasIcon = false; // Prevent null error

function createPost():Void {
doStuff = ScriptChar.iconSpr != null;
if (doStuff) {
hasIcon = ScriptChar.iconSpr != null;
if (hasIcon) {
gfIcon = new HealthIcon('gf');
gfIcon.cameras = [State.camHUD];
State.iconGroup.add(gfIcon);
setObjMap(gfIcon, 'gfIcon');

ScriptChar.iconSpr.staticSize = 0.75;
}
}

function beatHit():Void {
if (doStuff) {
ScriptChar.iconSpr.bumpIcon(1.3/1.25);
}
}

function updatePost():Void {
if (doStuff) {
if (hasIcon) {
gfIcon.flipX = ScriptChar.iconSpr.flipX;
var offset:Float = 50;
if (!ScriptChar.isPlayer) {
offset *= -1;
}

var offset = 50 * ScriptChar.iconSpr.lodDiv;
if (!ScriptChar.isPlayer)
offset = -offset;

gfIcon.x = ScriptChar.iconSpr.x + offset;
gfIcon.y = ScriptChar.iconSpr.y;
Expand Down
Binary file modified assets/shared/images/skins/default/ratings/nums.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/shared/images/skins/pixel/ratings/nums.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@

<haxedev set='webgl' if="web" />

<haxelib name="flixel" />
<haxelib name="flixel-ui" />
<haxelib name="flixel-addons" />
<haxelib name="flixel"/>
<haxelib name="flixel-ui"/>
<haxelib name="flixel-addons"/>
<haxelib name="haxeui-core" unless="mac"/>
<haxelib name="haxeui-openfl" unless="mac"/>
<haxelib name="maru-hscript" />
<haxedef name="hscriptPos"/>
<haxelib name="maru-hscript"/>

<section if="cpp">
<haxelib name="hxvlc" if="VIDEOS_ALLOWED"/>
<haxelib name="discord_rpc" if="DISCORD_ALLOWED"/>
</section>

<define name="TEXTURES_OFF" if="hl || web"/>
<haxedef name="hscriptPos"/>

<!-- ______________________________ Haxedefines _____________________________ -->

Expand Down
95 changes: 47 additions & 48 deletions source/funkin/sound/AudioWaveform.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class AudioWaveform extends FlxSpriteExt
var graphics:Graphics;
var bounds:Rectangle;

var waveformWidth:Float;

public function new(X:Float = 0.0, Y:Float = 0.0, Width:Float = 250.0, Height:Float = 500.0, ?sound:Sound):Void
{
super(X, Y);
Expand All @@ -22,6 +24,8 @@ class AudioWaveform extends FlxSpriteExt
canvas = new Sprite();
graphics = canvas.graphics;

waveformWidth = Width * 0.5;

if (sound != null)
setSound(sound);
}
Expand All @@ -34,42 +38,54 @@ class AudioWaveform extends FlxSpriteExt
static inline var QUALITY:Int = 250;

var sampleRate:Float = 0.0;
var avgBytes:Array<Int> = [];

var data:Array<#if cpp cpp.Float32 #else Float #end> = [];
override function destroy():Void {
super.destroy();
avgBytes = null;
data = null;
canvas = null;
graphics = null;
bounds = null;
}

public function setSound(sound:Sound):Void
{
sampleRate = sound.__buffer.sampleRate / QUALITY;
var bytes:Bytes = sound.__buffer.data.toBytes();
avgBytes.splice(0, avgBytes.length);


public function setSound(sound:Sound):Void {
data.clear();

var buffer = sound.__buffer;
if (buffer == null || buffer.data == null)
return;

sampleRate = buffer.sampleRate / QUALITY;
var bytes:Bytes = buffer.data.toBytes();

var i:Int = 0;
var l:Int = bytes.length;
var bigByte:Int = 0;

while(i < l)
{
var sum:Int = 0;
var count:Int = 0;

while(i < l) {
var byte = bytes.getUInt16(i);
if (byte > (65535 / 2))
byte -= 65535;

if (Math.abs(byte) > Math.abs(bigByte))
bigByte = byte;

if (i % QUALITY == 0) {
avgBytes.push(Std.int((bigByte / 65535) * 50));
bigByte = 0;

sum += FlxMath.absInt(byte);
count++;

if (count == QUALITY) {
var average = sum / count;
data.push(average * (1 / 65535 * waveformWidth));
sum = 0;
count = 0;
}

i++;
}

// Handle any remaining samples
if (count > 0) {
var average = sum / count;
data.push(average * (1 / 65535 * waveformWidth));
}
}

public var audioOffset:Float;
Expand All @@ -86,40 +102,29 @@ class AudioWaveform extends FlxSpriteExt

public function redrawWaveform():Void
{
graphics.__bounds = bounds;
//graphics.__bounds = bounds;

if (!visible || avgBytes.length <= 0 || end <= 0) {
if (!visible || data.length <= 0 || end <= 0) {
return;
}

final w:Int = Std.int(width * .5);
final h:Int = Std.int(height);
var w:Int = Std.int(width * .5);

graphics.beginFill();
graphics.lineStyle(0.7, -1, 1.0);
graphics.moveTo(w, 0);
graphics.beginFill(FlxColor.WHITE);

var i:Int = start;
var l:Int = FlxMath.minInt(end, avgBytes.length);
var lastByte:Int = 0;
var l:Int = FlxMath.minInt(end, data.length);

while (i < l)
{
final byte:Int = avgBytes.unsafeGet(i);

if (byte != lastByte)
{
lastByte = byte;
var byte = data.unsafeGet(i);

final y:Float = inline FlxMath.remapToRange((i - start), 0, (l - start), 0, h);
lineTo(w + byte, y);
//lineTo(w, y);
}
var y:Float = inline FlxMath.remapToRange((i - start), 0, (l - start), 0, height);
graphics.drawRect(w - (byte * 0.5), y, byte, 1);

i = (i + 1);
i++;
}

lineTo(w, h);
graphics.endFill();
graphics.__dirty = true;

Expand All @@ -141,10 +146,4 @@ class AudioWaveform extends FlxSpriteExt
}
return super.set_visible(value);
}

inline function lineTo(x:Float, y:Float):Void {
graphics.__positionX = x;
graphics.__positionY = y;
graphics.__commands.lineTo(x, y);
}
}
4 changes: 2 additions & 2 deletions source/funkin/states/LoadingState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class LoadingState extends MusicBeatState
});
}
else {
//var sound:Sound = streamSounds ? AssetManager.__streamSound(key) : AssetManager.__getFileSound(key);
assetCache.set(key, {sound: AssetManager.__getFileSound(key)});
var sound:Sound = streamSounds ? AssetManager.__streamSound(key) : AssetManager.__getFileSound(key);
assetCache.set(key, {sound: sound});
}
}

Expand Down
14 changes: 8 additions & 6 deletions source/funkin/states/editors/chart/ChartTabs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,18 @@ class ChartTabs extends FlxUITabMenu

var check_waveform_inst = new FlxUICheckBox(check_mute_inst.x + 125, check_mute_inst.y, null, null, "Instrumental Waveform", 100);
check_waveform_inst.checked = false;
check_waveform_inst.callback = function() {
ChartingState.instance.mainGrid.instWaveform.visible = check_waveform_inst.checked;
ChartingState.instance.mainGrid.instWaveform.redrawWaveform();
check_waveform_inst.callback = () -> {
var waveform = ChartingState.instance.mainGrid.instWaveform;
waveform.visible = check_waveform_inst.checked;
waveform.redrawWaveform();
};

var check_waveform_voices = new FlxUICheckBox(check_waveform_inst.x, check_waveform_inst.y + 30, null, null, "Voices Waveform", 100);
check_waveform_voices.checked = false;
check_waveform_voices.callback = function() {
ChartingState.instance.mainGrid.voicesWaveform.visible = check_waveform_voices.checked;
ChartingState.instance.mainGrid.voicesWaveform.redrawWaveform();
check_waveform_voices.callback = () -> {
var waveform = ChartingState.instance.mainGrid.voicesWaveform;
waveform.visible = check_waveform_voices.checked;
waveform.redrawWaveform();
};

check_hitsound = new FlxUICheckBox(check_mute_inst.x, check_mute_voices.y + 30, null, null, "Use Hitsounds", 100);
Expand Down
3 changes: 0 additions & 3 deletions source/funkin/states/editors/chart/grid/ChartNoteGrid.hx
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ class ChartNoteGrid extends ChartGridBase<ChartNote>

sustainsGroup = new TypedGroup<ChartSustain>();
insert(members.indexOf(group), sustainsGroup);

//textGroup = new TypedGroup<FlxBitmapText>();
//insert(members.indexOf(gridShadow), textGroup);
}

public function updateWaveform():Void
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/states/menus/TitleState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class TitleState extends MusicBeatState
case 'gay':
if (!gay) {
FlxG.camera.flash(flashy ? FlxColor.WHITE : 0x79ffffff, 3);
CoolUtil.playMusic("gay", 0);
CoolUtil.playMusic("gay", 0, true, true); // Stream this if possible :p
FlxG.sound.music.fadeIn();
codes.remove("gay");
gay = true;
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/util/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ class CoolUtil {
lil shortcut to play music
itll also change the conductor bpm to the music's data text file thing
*/
public static function playMusic(music:String, volume:Float = 1, looped:Bool = true):Void {
FlxG.sound.playMusic(Paths.music(music), volume, looped);
public static function playMusic(music:String, volume:Float = 1, looped:Bool = true, ?stream:Bool):Void {
FlxG.sound.playMusic(Paths.music(music, null, null, stream), volume, looped);

var musicDataPath:String = Paths.getPath('music/$music-data.txt', MUSIC, null);
if (Paths.exists(musicDataPath, TEXT)) {
Expand Down
8 changes: 4 additions & 4 deletions source/funkin/util/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ class Paths
return soundExt(key, "music", library, level);
}

inline static public function music(key:String, ?library:String, ?level:String):Sound {
inline static public function music(key:String, ?library:String, ?level:String, ?stream:Bool):Sound {
var musicPath = musicFolder(key, library, level);
return AssetManager.cacheSoundPath(musicPath);
return AssetManager.cacheSoundPath(musicPath, false, null, stream);
}

/*
Expand All @@ -189,7 +189,7 @@ class Paths
return songAudioAssetPath(song, "Voices", globalAsset);
}

inline static public function voices(song:String, ?globalAsset:Bool, stream:Bool = false):Sound {
inline static public function voices(song:String, ?globalAsset:Bool, ?stream:Bool):Sound {
var voicesPath:String = voicesPath(song, globalAsset);
return AssetManager.cacheSoundPath(voicesPath, false, null, stream);
}
Expand All @@ -198,7 +198,7 @@ class Paths
return songAudioAssetPath(song, "Inst", globalAsset);
}

inline static public function inst(song:String, ?globalAsset:Bool, stream:Bool = false):Sound {
inline static public function inst(song:String, ?globalAsset:Bool, ?stream:Bool):Sound {
var instPath:String = instPath(song, globalAsset);
return AssetManager.cacheSoundPath(instPath, false, null, stream);
}
Expand Down
14 changes: 11 additions & 3 deletions source/funkin/util/backend/AssetManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class AssetManager
* SOUND CACHE
*/

public static function cacheSoundPath(path:String, staticAsset:Bool = false, ?key:String, stream:Bool = false):Sound
public static function cacheSoundPath(path:String, staticAsset:Bool = false, ?key:String, ?stream:Bool):Sound
{
if (key == null)
key = path;
Expand All @@ -354,6 +354,7 @@ class AssetManager
if (asset != null)
return asset.asset;

stream ??= false;
var sound = stream ? __streamSound(path) : __getFileSound(path);

var asset = Asset.fromAsset(sound, key);
Expand Down Expand Up @@ -434,8 +435,15 @@ class AssetManager
asset.dispose();
assetsMap.remove(key);

if (tempAssets.contains(key)) tempAssets.removeAt(tempAssets.indexOf(key));
else if (staticAssets.contains(key)) staticAssets.removeAt(staticAssets.indexOf(key));
var tempIndex:Int = tempAssets.indexOf(key);
if (tempIndex != -1) {
tempAssets.removeAt(tempIndex);
}
else {
var staticIndex:Int = staticAssets.indexOf(key);
if (staticIndex != -1)
staticAssets.removeAt(staticIndex);
}

return true;
}
Expand Down

0 comments on commit 6f02151

Please sign in to comment.