Skip to content

Commit 31d3718

Browse files
KoloInDaCribEliteMasterEric
authored andcommitted
music file priority thing
(updated a misleading comment, lols!)
1 parent 5fdbd23 commit 31d3718

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

source/funkin/audio/FunkinSound.hx

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import funkin.util.tools.ICloneable;
1414
import funkin.util.flixel.sound.FlxPartialSound;
1515
import funkin.Paths.PathsFunction;
1616
import lime.app.Promise;
17+
import lime.media.AudioSource;
18+
import openfl.events.Event;
19+
import openfl.media.Sound;
20+
import openfl.media.SoundChannel;
1721
import openfl.media.SoundMixer;
1822

1923
#if (openfl >= "8.0.0")
@@ -108,6 +112,11 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
108112
return _waveformData;
109113
}
110114

115+
/**
116+
* If true, the game will forcefully add this sound's channel to the list of playing sounds.
117+
*/
118+
public var important:Bool = false;
119+
111120
/**
112121
* Are we in a state where the song should play but time is negative?
113122
*/
@@ -145,6 +154,14 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
145154
else
146155
{
147156
super.update(elapsedSec);
157+
158+
@:privateAccess
159+
{
160+
if (important && _channel != null && !SoundMixer.__soundChannels.contains(_channel))
161+
{
162+
SoundMixer.__soundChannels.push(_channel);
163+
}
164+
}
148165
}
149166
}
150167

@@ -431,13 +448,14 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
431448
* @param persist Whether to keep this `FunkinSound` between states, or destroy it.
432449
* @param onComplete Called when the sound finished playing.
433450
* @param onLoad Called when the sound finished loading. Called immediately for succesfully loaded embedded sounds.
451+
* @param important If `true`, the sound channel will forcefully be added onto the channel array, even if full. Use sparingly!
434452
* @return A `FunkinSound` object, or `null` if the sound could not be loaded.
435453
*/
436454
public static function load(embeddedSound:FlxSoundAsset, volume:Float = 1.0, looped:Bool = false, autoDestroy:Bool = false, autoPlay:Bool = false,
437-
persist:Bool = false, ?onComplete:Void->Void, ?onLoad:Void->Void):Null<FunkinSound>
455+
persist:Bool = false, ?onComplete:Void->Void, ?onLoad:Void->Void, important:Bool = false):Null<FunkinSound>
438456
{
439457
@:privateAccess
440-
if (SoundMixer.__soundChannels.length >= SoundMixer.MAX_ACTIVE_CHANNELS)
458+
if (SoundMixer.__soundChannels.length >= SoundMixer.MAX_ACTIVE_CHANNELS && !important)
441459
{
442460
FlxG.log.error('FunkinSound could not play sound, channels exhausted! Found ${SoundMixer.__soundChannels.length} active sound channels.');
443461
return null;
@@ -462,6 +480,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
462480
sound.volume = volume;
463481
sound.group = FlxG.sound.defaultSoundGroup;
464482
sound.persist = persist;
483+
sound.important = important;
465484

466485
// Make sure to add the sound to the list.
467486
// If it's already in, it won't get re-added.
@@ -534,15 +553,50 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
534553
this._label = 'unknown';
535554
}
536555

556+
@:access(openfl.media.Sound)
557+
@:access(openfl.media.SoundChannel)
558+
@:access(openfl.media.SoundMixer)
559+
override function startSound(startTime:Float)
560+
{
561+
if (!important)
562+
{
563+
super.startSound(startTime);
564+
return;
565+
}
566+
567+
_time = startTime;
568+
_paused = false;
569+
570+
if (_sound == null) return;
571+
572+
// Create a channel manually if the sound is considered important.
573+
var pan:Float = FlxMath.bound(SoundMixer.__soundTransform.pan + _transform.pan, -1, 1);
574+
var volume:Float = FlxMath.bound(SoundMixer.__soundTransform.volume * _transform.volume, 0, MAX_VOLUME);
575+
576+
var audioSource:AudioSource = new AudioSource(_sound.__buffer);
577+
audioSource.offset = Std.int(startTime);
578+
audioSource.gain = volume;
579+
580+
var position:lime.math.Vector4 = audioSource.position;
581+
position.x = pan;
582+
position.z = -1 * Math.sqrt(1 - Math.pow(pan, 2));
583+
audioSource.position = position;
584+
585+
_channel = new SoundChannel(_sound, audioSource, _transform);
586+
_channel.addEventListener(Event.SOUND_COMPLETE, stopped);
587+
pitch = _pitch;
588+
active = true;
589+
}
590+
537591
/**
538592
* Play a sound effect once, then destroy it.
539593
* @param key
540594
* @param volume
541595
* @return A `FunkinSound` object, or `null` if the sound could not be loaded.
542596
*/
543-
public static function playOnce(key:String, volume:Float = 1.0, ?onComplete:Void->Void, ?onLoad:Void->Void):Null<FunkinSound>
597+
public static function playOnce(key:String, volume:Float = 1.0, ?onComplete:Void->Void, ?onLoad:Void->Void, important:Bool = false):Null<FunkinSound>
544598
{
545-
var result:Null<FunkinSound> = FunkinSound.load(key, volume, false, true, true, false, onComplete, onLoad);
599+
var result:Null<FunkinSound> = FunkinSound.load(key, volume, false, true, true, false, onComplete, onLoad, important);
546600
return result;
547601
}
548602

source/funkin/play/Countdown.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class Countdown
255255
var path = noteStyle.getCountdownSoundPath(step);
256256
if (path == null) return null;
257257

258-
return FunkinSound.playOnce(path, Constants.COUNTDOWN_VOLUME);
258+
return FunkinSound.playOnce(path, Constants.COUNTDOWN_VOLUME, null, null, true);
259259
}
260260

261261
public static function decrement(step:CountdownStep):CountdownStep

source/funkin/play/song/Song.hx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ class SongDifficulty
801801
{
802802
var suffix:String = (instId != '') ? '-$instId' : '';
803803

804-
FlxG.sound.music = FunkinSound.load(Paths.inst(this.song.id, suffix), volume, looped, false, true);
804+
FlxG.sound.music = FunkinSound.load(Paths.inst(this.song.id, suffix), volume, looped, false, true, false, null, null, true);
805805

806806
// Workaround for a bug where FlxG.sound.music.update() was being called twice.
807807
FlxG.sound.list.remove(FlxG.sound.music);
@@ -948,16 +948,21 @@ class SongDifficulty
948948
for (playerVoice in playerVoiceList)
949949
{
950950
if (!Assets.exists(playerVoice)) continue;
951-
result.addPlayerVoice(FunkinSound.load(playerVoice));
951+
result.addPlayerVoice(FunkinSound.load(playerVoice, 1.0, false, false, false, false, null, null, true));
952952
}
953953

954954
// Add opponent vocals.
955955
for (opponentVoice in opponentVoiceList)
956956
{
957957
if (!Assets.exists(opponentVoice)) continue;
958-
result.addOpponentVoice(FunkinSound.load(opponentVoice));
958+
result.addOpponentVoice(FunkinSound.load(opponentVoice, 1.0, false, false, false, false, null, null, true));
959959
}
960960

961+
// Sometimes the sounds don't set their important value to true, so we have to do this manually.
962+
result.forEach(function(snd:FunkinSound) {
963+
snd.important = true;
964+
});
965+
961966
result.playerVoicesOffset = offsets.getVocalOffset(characters.player, instId);
962967
result.opponentVoicesOffset = offsets.getVocalOffset(characters.opponent, instId);
963968

0 commit comments

Comments
 (0)