Skip to content

Commit a87f000

Browse files
committed
use streamed audio for freeplay previews
1 parent 5868cb1 commit a87f000

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

source/funkin/audio/FunkinSound.hx

Lines changed: 37 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,40 @@ 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 vorbisFile:Null<VorbisFile> = VorbisFile.fromFile(Paths.stripLibrary(pathToUse));
354+
if (vorbisFile != null)
355+
{
356+
var vorbisSound:Null<Sound> = Sound.fromAudioBuffer(lime.media.AudioBuffer.fromVorbisFile(vorbisFile));
357+
358+
if (vorbisSound != null)
359+
{
360+
var sound:Null<FunkinSound> = FunkinSound.load(vorbisSound, params?.startingVolume ?? 1.0, params.loop ?? true, false, true, false,
361+
params.onComplete);
362+
if (sound != null)
363+
{
364+
sound._label = pathToUse;
365+
366+
FlxG.sound.music = sound;
367+
FlxG.sound.list.remove(FlxG.sound.music);
368+
369+
// Apply the partial limits for the sound.
370+
sound.loopTime = sound.length * start;
371+
sound.endTime = sound.length * end;
372+
373+
if (FlxG.sound.music != null && params.onLoad != null) params.onLoad();
374+
375+
return true;
376+
}
377+
}
378+
}
379+
#end
380+
381+
var music = FunkinSound.loadPartial(pathToUse, start, end, params?.startingVolume ?? 1.0, params.loop ?? true, false, false, params.onComplete);
347382

348383
if (music != null)
349384
{

source/funkin/ui/freeplay/FreeplayState.hx

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

2990-
var fadeStart:Float = (FlxG.sound.music.length / 1000) - 2;
2990+
var musicLength:Float = FlxG.sound.music.length;
2991+
if (FlxG.sound.music.endTime != null) musicLength = (FlxG.sound.music.endTime - FlxG.sound.music.loopTime);
2992+
2993+
var fadeStart:Float = (musicLength / 1000) - 2;
29912994

29922995
previewTimers.push(new FlxTimer().start(fadeStart, function(_) {
29932996
FlxG.sound.music.fadeOut(2, 0);
29942997
}));
29952998

2996-
previewTimers.push(new FlxTimer().start(FlxG.sound.music.length / 1000, function(_) {
2999+
previewTimers.push(new FlxTimer().start(musicLength / 1000, function(_) {
29973000
playCurSongPreview();
29983001
}));
29993002
},

0 commit comments

Comments
 (0)