Skip to content

Commit b44d608

Browse files
committed
Tweak Sound Playback Codes
1 parent 3c14e2c commit b44d608

File tree

5 files changed

+84
-117
lines changed

5 files changed

+84
-117
lines changed

source/funkin/audio/FunkinSound.hx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
309309
if (FlxG.sound.music != null)
310310
{
311311
FlxG.sound.music.fadeTween?.cancel();
312-
FlxG.sound.music.stop();
313-
FlxG.sound.music.kill();
312+
FlxG.sound.music.destroy();
314313
}
315314

316315
if (params?.mapTimeChanges ?? true)
@@ -345,27 +344,18 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
345344

346345
if (shouldLoadPartial)
347346
{
348-
var music = FunkinSound.loadPartial(pathToUse, params.partialParams?.start ?? 0.0, params.partialParams?.end ?? 1.0, params?.startingVolume ?? 1.0,
347+
var promise = FunkinSound.loadPartial(pathToUse, params.partialParams?.start ?? 0.0, params.partialParams?.end ?? 1.0, params?.startingVolume ?? 1.0,
349348
params.loop ?? true, false, false, params.onComplete);
350349

351-
if (music != null)
352-
{
353-
partialQueue.push(music);
350+
partialQueue.push(promise);
354351

355-
@:nullSafety(Off)
356-
music.future.onComplete(function(partialMusic:Null<FunkinSound>) {
357-
FlxG.sound.music = partialMusic;
358-
FlxG.sound.list.remove(FlxG.sound.music);
359-
360-
if (FlxG.sound.music != null && params.onLoad != null) params.onLoad();
361-
});
352+
@:nullSafety(Off)
353+
promise.future.onComplete(function(partialMusic:Null<FunkinSound>) {
354+
setMusic(partialMusic);
355+
if (partialMusic != null && params.onLoad != null) params.onLoad();
356+
});
362357

363-
return true;
364-
}
365-
else
366-
{
367-
return false;
368-
}
358+
return true;
369359
}
370360
else
371361
{
@@ -511,8 +501,8 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
511501

512502
override function get_playing():Bool
513503
{
514-
return super.get_playing() || this._shouldPlay;
515-
}
504+
return loaded && (source.playing || _pausedPlay || _shouldPlay);
505+
}
516506

517507
override function get_time():Float
518508
{

source/funkin/audio/SoundGroup.hx

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package funkin.audio;
22

3-
import lime.media.AudioSource;
43
import flixel.group.FlxGroup.FlxTypedGroup;
4+
import flixel.sound.FlxSound;
55
import flixel.tweens.FlxTween;
66

77
/**
88
* A group of FunkinSounds that are all synced together.
99
* Unlike FlxSoundGroup, you can also control their time and pitch.
1010
*/
11-
@:access(flixel.sound.FlxSound)
1211
@:access(funkin.audio.FunkinSound)
1312
@:nullSafety
1413
class SoundGroup extends FlxTypedGroup<FunkinSound>
@@ -106,71 +105,47 @@ class SoundGroup extends FlxTypedGroup<FunkinSound>
106105
*/
107106
public function pause()
108107
{
109-
forEachAlive(function(sound:FunkinSound) {
110-
sound.pause();
111-
});
108+
if (members != null) FlxSound.pauseSounds(cast members);
112109
}
113110

114111
/**
115112
* Play all the sounds in the group.
116113
*/
117114
public function play(forceRestart:Bool = false, startTime:Float = 0.0, ?endTime:Float)
118115
{
119-
var sources:Array<AudioSource> = [];
116+
var sounds:Array<FlxSound> = [];
120117

121118
forEachAlive(function(sound:FunkinSound) {
122119
if (sound.playing && !forceRestart || sound.length < startTime)
123120
{
124121
return;
125122
}
126123

127-
if (sound._pausedByHandler)
128-
{
129-
sound.play(true, startTime, endTime);
130-
}
131-
else
132-
{
133-
sound.prepare(startTime, endTime);
134-
sources.push(sound.source);
135-
136-
sound._paused = false;
137-
sound._completed = false;
138-
sound.active = true;
139-
}
124+
sound.prepare(startTime, endTime);
125+
sounds.push(sound);
140126
});
141127

142-
AudioSource.playSources(sources);
128+
FlxSound.playSounds(sounds);
143129
}
144130

145131
/**
146132
* Resume all the sounds in the group.
147133
*/
148134
public function resume()
149135
{
150-
var sources:Array<AudioSource> = [];
136+
var sounds:Array<FlxSound> = [];
151137

152138
forEachAlive(function(sound:FunkinSound) {
153139
if (sound.playing || !sound._paused)
154140
{
155141
return;
156142
}
157143

158-
if (sound._pausedByHandler)
159-
{
160-
sound.resume();
161-
}
162-
else
163-
{
164-
sound.prepare(sound.time);
165-
sources.push(sound.source);
166-
167-
sound._paused = false;
168-
sound._completed = false;
169-
sound.active = true;
170-
}
144+
sound.prepare(sound.time);
145+
sounds.push(sound);
171146
});
172147

173-
AudioSource.playSources(sources);
148+
FlxSound.playSounds(sounds);
174149
}
175150

176151
/**
@@ -200,12 +175,7 @@ class SoundGroup extends FlxTypedGroup<FunkinSound>
200175
*/
201176
public function stop():Void
202177
{
203-
if (members != null)
204-
{
205-
forEachAlive(function(sound:FunkinSound) {
206-
sound.stop();
207-
});
208-
}
178+
if (members != null) FlxSound.stopSounds(cast members);
209179
}
210180

211181
public override function destroy():Void
@@ -241,30 +211,19 @@ class SoundGroup extends FlxTypedGroup<FunkinSound>
241211
{
242212
// account for different offsets per sound?
243213

244-
var sources:Array<AudioSource> = [];
214+
var sounds:Array<FlxSound> = [];
245215

246216
forEachAlive(function(sound:FunkinSound) {
247-
if (!sound.loaded || sound.length < time)
217+
if (!sound.loaded || sound.length < time || !sound.playing)
248218
{
249219
return;
250220
}
251221

252-
if (sound._pausedByHandler || !sound.source.playing)
253-
{
254-
sound.time = time;
255-
}
256-
else
257-
{
258-
sound.prepare(time);
259-
sources.push(sound.source);
260-
261-
sound._paused = false;
262-
sound._completed = false;
263-
sound.active = true;
264-
}
222+
sound.prepare(time);
223+
sounds.push(sound);
265224
});
266225

267-
AudioSource.playSources(sources);
226+
FlxSound.playSounds(sounds);
268227

269228
return time;
270229
}

source/funkin/audio/VoicesGroup.hx

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package funkin.audio;
22

3-
import lime.media.AudioSource;
43
import flixel.group.FlxGroup.FlxTypedGroup;
4+
import flixel.sound.FlxSound;
55
import funkin.audio.waveform.WaveformData;
66

7-
@:access(flixel.sound.FlxSound)
87
@:access(funkin.audio.FunkinSound)
98
@:nullSafety
109
class VoicesGroup extends SoundGroup
@@ -56,39 +55,47 @@ class VoicesGroup extends SoundGroup
5655
return playerVolume = volume;
5756
}
5857

59-
override function set_time(time:Float):Float
58+
override function play(forceRestart:Bool = false, startTime:Float = 0.0, ?endTime:Float)
6059
{
61-
// account for different offsets per sound?
62-
63-
var sources:Array<AudioSource> = [];
60+
var sounds:Array<FlxSound> = [];
6461

6562
forEachAlive(function(sound:FunkinSound) {
66-
var localTime = time;
63+
var localTime = startTime;
6764

6865
if (playerVoices?.members.contains(sound) ?? false) localTime -= playerVoicesOffset;
6966
else if (opponentVoices?.members.contains(sound) ?? false) localTime -= opponentVoicesOffset;
7067

71-
if (!sound.loaded || sound.length < localTime)
68+
if (sound.playing && !forceRestart || sound.length < localTime)
7269
{
7370
return;
7471
}
7572

76-
if (sound._pausedByHandler || !sound.source.playing)
77-
{
78-
sound.time = localTime;
79-
}
80-
else
81-
{
82-
sound.prepare(localTime);
83-
sources.push(sound.source);
73+
sound.prepare(localTime, endTime);
74+
sounds.push(sound);
75+
});
8476

85-
sound._paused = false;
86-
sound._completed = false;
87-
sound.active = true;
88-
}
77+
FlxSound.playSounds(sounds);
78+
}
79+
80+
override function set_time(time:Float):Float
81+
{
82+
// account for different offsets per sound?
83+
84+
var sounds:Array<FlxSound> = [];
85+
86+
forEachAlive(function(sound:FunkinSound) {
87+
var localTime = time;
88+
89+
if (playerVoices?.members.contains(sound) ?? false) localTime -= playerVoicesOffset;
90+
else if (opponentVoices?.members.contains(sound) ?? false) localTime -= opponentVoicesOffset;
91+
92+
if (!sound.loaded || sound.length < localTime || !sound.playing) return;
93+
94+
sound.prepare(localTime);
95+
sounds.push(sound);
8996
});
9097

91-
AudioSource.playSources(sources);
98+
FlxSound.playSounds(sounds);
9299

93100
return time;
94101
}

source/funkin/play/GameOverSubState.hx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class GameOverSubState extends MusicBeatSubState
144144

145145
parentPlayState = cast _parentState;
146146

147+
gameOverMusic = FunkinSound.load(null);
148+
147149
//
148150
// Set up the visuals
149151
//
@@ -509,10 +511,11 @@ class GameOverSubState extends MusicBeatSubState
509511
}
510512
else if (gameOverMusic == null || !gameOverMusic.playing || force)
511513
{
512-
if (gameOverMusic != null) gameOverMusic.stop();
513-
514-
gameOverMusic = FunkinSound.load(musicPath);
515-
if (gameOverMusic == null) return;
514+
if (gameOverMusic == null) gameOverMusic = FunkinSound.load(musicPath);
515+
{
516+
gameOverMusic.stop();
517+
gameOverMusic.loadEmbedded(musicPath);
518+
}
516519

517520
gameOverMusic.volume = startingVolume;
518521
gameOverMusic.looped = !(isEnding || isStarting);

source/funkin/play/PlayState.hx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,12 +1828,12 @@ class PlayState extends MusicBeatSubState
18281828
@:privateAccess // todo: maybe make the groups public :thinking:
18291829
{
18301830
vocals.playerVoices?.forEachAlive(function(voice:FunkinSound) {
1831-
var currentRawVoiceTime:Float = voice.getActualTime() + vocals.playerVoicesOffset;
1831+
var currentRawVoiceTime:Float = voice.getActualTime() + vocals.playerVoicesOffset + Conductor.instance.instrumentalOffset;
18321832
if (Math.abs(currentRawVoiceTime - correctSync) > Math.abs(playerVoicesError) && voice.playing) playerVoicesError = currentRawVoiceTime - correctSync;
18331833
});
18341834

18351835
vocals.opponentVoices?.forEachAlive(function(voice:FunkinSound) {
1836-
var currentRawVoiceTime:Float = voice.getActualTime() + vocals.opponentVoicesOffset;
1836+
var currentRawVoiceTime:Float = voice.getActualTime() + vocals.opponentVoicesOffset + Conductor.instance.instrumentalOffset;
18371837
if (Math.abs(currentRawVoiceTime - correctSync) > Math.abs(opponentVoicesError) && voice.playing) opponentVoicesError = currentRawVoiceTime - correctSync;
18381838
});
18391839
}
@@ -2557,21 +2557,15 @@ class PlayState extends MusicBeatSubState
25572557

25582558
if (vocals != null)
25592559
{
2560-
trace('Playing vocals...');
25612560
add(vocals);
25622561

25632562
vocals.pitch = playbackRate;
25642563
vocals.playerVolume = playerVocalsVolume;
25652564
vocals.opponentVolume = opponentVocalsVolume;
2566-
2567-
// trace('STARTING SONG AT:');
2568-
// trace('${FlxG.sound.music.time}');
2569-
// trace('${vocals.time}');
2570-
2571-
vocals.play(true, startTimestamp - Conductor.instance.instrumentalOffset);
25722565
}
25732566

2574-
FlxG.sound.music.play(true, startTimestamp);
2567+
Conductor.instance.update(startTimestamp);
2568+
resyncVocals(true);
25752569

25762570
#if FEATURE_DISCORD_RPC
25772571
// Updating Discord Rich Presence (with Time Left)
@@ -2606,12 +2600,26 @@ class PlayState extends MusicBeatSubState
26062600
{
26072601
if (FlxG.sound.music != null && force || (vocals != null && FlxG.sound.music.playing))
26082602
{
2609-
var timeToPlayAt:Float = Math.min(FlxG.sound.music.length,
2610-
Math.max(Math.min(Conductor.instance.combinedOffset, 0), Conductor.instance.songPosition) - Conductor.instance.combinedOffset);
2603+
var timeToPlayAt:Float = (Conductor.instance.songPosition - Conductor.instance.combinedOffset).clamp(0, FlxG.sound.music.length);
26112604
trace('Resyncing vocals to ${timeToPlayAt}');
26122605

2613-
FlxG.sound.music.play(true, timeToPlayAt);
2614-
vocals?.play(true, timeToPlayAt);
2606+
//FlxG.sound.music.play(true, timeToPlayAt);
2607+
//vocals?.play(true, timeToPlayAt - Conductor.instance.instrumentalOffset);
2608+
2609+
var sounds:Array<FlxSound> = [FlxG.sound.music];
2610+
2611+
FlxG.sound.music.prepare(timeToPlayAt);
2612+
if (vocals != null)
2613+
{
2614+
var vocalsTimeToPlayAt:Float = timeToPlayAt - Conductor.instance.instrumentalOffset;
2615+
vocals.forEachAlive(function(sound:FunkinSound)
2616+
{
2617+
sounds.push(sound);
2618+
sound.prepare(vocalsTimeToPlayAt);
2619+
});
2620+
}
2621+
2622+
FlxSound.playSounds(sounds);
26152623
}
26162624
}
26172625

0 commit comments

Comments
 (0)