Skip to content

Commit 66e6ec5

Browse files
committed
Rebase Audio Rework
1 parent e7f2529 commit 66e6ec5

27 files changed

+869
-571
lines changed

source/funkin/Conductor.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ class Conductor
538538
if (soundToCheck == null) soundToCheck = FlxG.sound.music;
539539

540540
@:privateAccess
541-
this.songPosition = soundToCheck._channel.position;
541+
this.songPosition = soundToCheck.getActualTime();
542542
return this.songPosition;
543543
}
544544

source/funkin/FunkinMemory.hx

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ class FunkinMemory
2121
static var currentCachedTextures:Map<String, FlxGraphic> = [];
2222
static var previousCachedTextures:Map<String, FlxGraphic> = [];
2323

24-
static var permanentCachedSounds:Map<String, Sound> = [];
25-
static var currentCachedSounds:Map<String, Sound> = [];
26-
static var previousCachedSounds:Map<String, Sound> = [];
27-
2824
static var purgeFilter:Array<String> = ["/week", "/characters", "/charSelect", "/results"];
2925

3026
/**
@@ -102,8 +98,6 @@ class FunkinMemory
10298
{
10399
preparePurgeTextureCache();
104100
purgeTextureCache();
105-
preparePurgeSoundCache();
106-
purgeSoundCache();
107101
#if (cpp || neko || hl)
108102
if (callGarbageCollector) funkin.util.MemoryUtil.collect(true);
109103
#end
@@ -331,21 +325,7 @@ class FunkinMemory
331325
*/
332326
public static function cacheSound(key:String):Void
333327
{
334-
if (currentCachedSounds.exists(key)) return;
335-
336-
if (previousCachedSounds.exists(key))
337-
{
338-
// Move the texture from the previous cache to the current cache.
339-
var sound:Null<Sound> = previousCachedSounds.get(key);
340-
previousCachedSounds.remove(key);
341-
if (sound != null) currentCachedSounds.set(key, sound);
342-
return;
343-
}
344-
345-
var sound:Null<Sound> = Assets.getSound(key, true);
346-
if (sound == null) return;
347-
else
348-
currentCachedSounds.set(key, sound);
328+
flixel.sound.FlxSoundData.fromAssetKey(key);
349329
}
350330

351331
/**
@@ -354,64 +334,8 @@ class FunkinMemory
354334
*/
355335
public static function permanentCacheSound(key:String):Void
356336
{
357-
if (permanentCachedSounds.exists(key)) return;
358-
359-
var sound:Null<Sound> = Assets.getSound(key, true);
360-
if (sound == null) return;
361-
else
362-
permanentCachedSounds.set(key, sound);
363-
364-
if (sound != null) currentCachedSounds.set(key, sound);
365-
}
366-
367-
/**
368-
* Prepares the cache for purging unused sounds.
369-
*/
370-
public static function preparePurgeSoundCache():Void
371-
{
372-
previousCachedSounds = currentCachedSounds.copy();
373-
374-
for (key in previousCachedSounds.keys())
375-
{
376-
if (permanentCachedSounds.exists(key))
377-
{
378-
previousCachedSounds.remove(key);
379-
}
380-
}
381-
382-
currentCachedSounds = permanentCachedSounds.copy();
383-
}
384-
385-
/**
386-
* Purges unused sounds from the cache.
387-
*/
388-
public static inline function purgeSoundCache():Void
389-
{
390-
for (key in previousCachedSounds.keys())
391-
{
392-
if (permanentCachedSounds.exists(key))
393-
{
394-
previousCachedSounds.remove(key);
395-
continue;
396-
}
397-
398-
var sound:Null<Sound> = previousCachedSounds.get(key);
399-
if (sound != null)
400-
{
401-
Assets.cache.removeSound(key);
402-
previousCachedSounds.remove(key);
403-
}
404-
}
405-
Assets.cache.clear("songs");
406-
Assets.cache.clear("music");
407-
// Felt lazy.
408-
var key = Paths.music("freakyMenu/freakyMenu");
409-
var sound:Null<Sound> = Assets.getSound(key, true);
410-
if (sound != null)
411-
{
412-
permanentCachedSounds.set(key, sound);
413-
currentCachedSounds.set(key, sound);
414-
}
337+
var soundData = flixel.sound.FlxSoundData.fromAssetKey(key);
338+
if (soundData != null) soundData.persist = true;
415339
}
416340

417341
///// MISC /////
@@ -445,9 +369,6 @@ class FunkinMemory
445369
if (currentCachedTextures.exists(key)) currentCachedTextures.remove(key);
446370
Assets.cache.clear(key);
447371
}
448-
449-
preparePurgeSoundCache();
450-
purgeSoundCache();
451372
}
452373

453374
/**

source/funkin/Preferences.hx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,32 @@ class Preferences
416416
return value;
417417
}
418418

419+
#if desktop
420+
/**
421+
* What audio device should it playback sounds to.
422+
* @default Default
423+
*/
424+
public static var audioDevice(get, set):String;
425+
426+
static function get_audioDevice():String
427+
{
428+
return Save?.instance?.options?.audioDevice ?? "Default";
429+
}
430+
431+
static function set_audioDevice(value:String):String
432+
{
433+
if (value == Save.instance.options.audioDevice) return value;
434+
435+
var save:Save = Save.instance;
436+
FlxG.sound.automaticDefaultDevice = value == "Default" || (FlxG.sound.deviceName = value) != value;
437+
438+
save.options.audioDevice = FlxG.sound.automaticDefaultDevice ? "Default" : value;
439+
Save.system.flush();
440+
441+
return value;
442+
}
443+
#end
444+
419445
/**
420446
* If enabled, the game will hide the mouse when taking a screenshot.
421447
* @default `true`
@@ -485,6 +511,20 @@ class Preferences
485511
setDebugDisplayMode(Preferences.debugDisplay);
486512
setDebugDisplayBGOpacity(Preferences.debugDisplayBGOpacity / 100);
487513

514+
#if desktop
515+
// Apply audio device preference, if failed, fallback to Default.
516+
FlxG.sound.deviceName = Preferences.audioDevice;
517+
if (FlxG.sound.deviceName == Preferences.audioDevice)
518+
{
519+
FlxG.sound.automaticDefaultDevice = false;
520+
}
521+
else
522+
{
523+
Preferences.audioDevice = "Default";
524+
FlxG.sound.automaticDefaultDevice = true;
525+
}
526+
#end
527+
488528
#if web
489529
toggleFramerateCap(Preferences.unlockedFramerate);
490530
#end
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package funkin.audio;
22

3-
import flash.media.Sound;
4-
import flixel.sound.FlxSound;
5-
import flixel.system.FlxAssets.FlxSoundAsset;
3+
import lime.media.AudioBuffer;
64
import openfl.Assets;
7-
#if (openfl >= "8.0.0")
5+
import openfl.media.Sound;
86
import openfl.utils.AssetType;
9-
#end
7+
import flixel.sound.FlxSound;
8+
import flixel.system.FlxAssets.FlxSoundAsset;
109

1110
/**
1211
* a FlxSound that just overrides loadEmbedded to allow for "streamed" sounds to load with better performance!
@@ -19,29 +18,16 @@ class FlxStreamSound extends FlxSound
1918
super();
2019
}
2120

22-
override public function loadEmbedded(EmbeddedSound:Null<FlxSoundAsset>, Looped:Bool = false, AutoDestroy:Bool = false, ?OnComplete:Void->Void):FlxSound
21+
override public function loadEmbedded(asset:FlxSoundAsset, ?looped:Bool, ?loopTime:Float, ?endTime:Float, autoDestroy = false, ?onComplete:Void->Void):FlxStreamSound
2322
{
24-
if (EmbeddedSound == null) return this;
25-
26-
cleanup(true);
27-
28-
if ((EmbeddedSound is Sound))
29-
{
30-
_sound = EmbeddedSound;
31-
}
32-
else if ((EmbeddedSound is Class))
23+
if ((asset is String))
3324
{
34-
_sound = Type.createInstance(EmbeddedSound, []);
25+
super.loadStreamed(asset, looped, loopTime, endTime, autoDestroy, onComplete);
3526
}
36-
else if ((EmbeddedSound is String))
27+
else
3728
{
38-
if (Assets.exists(EmbeddedSound, AssetType.SOUND)
39-
|| Assets.exists(EmbeddedSound, AssetType.MUSIC)) _sound = Assets.getMusic(EmbeddedSound);
40-
else
41-
FlxG.log.error('Could not find a Sound asset with an ID of \'$EmbeddedSound\'.');
29+
super.loadEmbedded(asset, looped, loopTime, endTime, autoDestroy, onComplete);
4230
}
43-
44-
// NOTE: can't pull ID3 info from embedded sound currently
45-
return init(Looped, AutoDestroy, OnComplete);
31+
return this;
4632
}
4733
}

0 commit comments

Comments
 (0)