Skip to content

Commit 5dc1941

Browse files
committed
use streamed audio for freeplay previews
1 parent 671e143 commit 5dc1941

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

source/funkin/audio/FunkinSound.hx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import openfl.events.Event;
1919
import openfl.media.Sound;
2020
import openfl.media.SoundChannel;
2121
import openfl.media.SoundMixer;
22+
#if lime_vorbis
23+
import lime.media.vorbis.VorbisFile;
24+
#end
2225

2326
/**
2427
* A FlxSound which adds additional functionality:
@@ -342,8 +345,48 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
342345

343346
if (shouldLoadPartial)
344347
{
345-
var music = FunkinSound.loadPartial(pathToUse, params.partialParams?.start ?? 0.0, params.partialParams?.end ?? 1.0, params?.startingVolume ?? 1.0,
346-
params.loop ?? true, false, false, params.onComplete);
348+
var start:Float = params.partialParams?.start ?? 0.0;
349+
var end:Float = params.partialParams?.end ?? 1.0;
350+
351+
// If partial, attempt to load from vorbis first.
352+
#if lime_vorbis
353+
var vorbisSound:Null<Sound> = null;
354+
355+
var cacheFormat:String = '$pathToUse.partial-$start-$end';
356+
if (Assets.cache.hasSound(cacheFormat)) vorbisSound = Assets.cache.getSound(cacheFormat);
357+
358+
if (vorbisSound == null)
359+
{
360+
var vorbisFile:Null<VorbisFile> = VorbisFile.fromFile(Paths.stripLibrary(pathToUse));
361+
if (vorbisFile != null)
362+
{
363+
vorbisSound = Sound.fromAudioBuffer(lime.media.AudioBuffer.fromVorbisFile(vorbisFile));
364+
Assets.cache.setSound(cacheFormat, vorbisSound);
365+
}
366+
}
367+
368+
if (vorbisSound != null)
369+
{
370+
var sound:Null<FunkinSound> = FunkinSound.load(vorbisSound, params?.startingVolume ?? 1.0, params.loop ?? true, false, true, false, params.onComplete);
371+
if (sound != null)
372+
{
373+
sound._label = pathToUse;
374+
375+
FlxG.sound.music = sound;
376+
FlxG.sound.list.remove(FlxG.sound.music);
377+
378+
// Apply the partial limits for the sound.
379+
sound.loopTime = sound.length * start;
380+
sound.endTime = sound.length * end;
381+
382+
if (FlxG.sound.music != null && params.onLoad != null) params.onLoad();
383+
384+
return true;
385+
}
386+
}
387+
#end
388+
389+
var music = FunkinSound.loadPartial(pathToUse, start, end, params?.startingVolume ?? 1.0, params.loop ?? true, false, false, params.onComplete);
347390

348391
if (music != null)
349392
{

source/funkin/ui/freeplay/FreeplayState.hx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,13 +3038,16 @@ class FreeplayState extends MusicBeatSubState
30383038
onLoad: function() {
30393039
FlxG.sound.music.fadeIn(2, 0, previewVolume);
30403040

3041-
var fadeStart:Float = (FlxG.sound.music.length / 1000) - 2;
3041+
var musicLength:Float = FlxG.sound.music.length;
3042+
if (FlxG.sound.music.endTime != null) musicLength = (FlxG.sound.music.endTime - FlxG.sound.music.loopTime);
3043+
3044+
var fadeStart:Float = (musicLength / 1000) - 2;
30423045

30433046
previewTimers.push(new FlxTimer().start(fadeStart, function(_) {
30443047
FlxG.sound.music.fadeOut(2, 0);
30453048
}));
30463049

3047-
previewTimers.push(new FlxTimer().start(FlxG.sound.music.length / 1000, function(_) {
3050+
previewTimers.push(new FlxTimer().start(musicLength / 1000, function(_) {
30483051
playCurSongPreview();
30493052
}));
30503053
},

0 commit comments

Comments
 (0)