From abcf84b3fba502ddd6b0d2940f9892a01953ec3a Mon Sep 17 00:00:00 2001 From: Evgeny608799 Date: Wed, 26 Jun 2024 13:14:09 +0500 Subject: [PATCH] Update music player in another thread (cherry picked from commit af2708ca4213012e233c2964c5140390d6c290fb) --- O21.Game/Game.fs | 6 ++---- O21.Game/Music.fs | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/O21.Game/Game.fs b/O21.Game/Game.fs index 6e6140e..1b35633 100644 --- a/O21.Game/Game.fs +++ b/O21.Game/Game.fs @@ -4,6 +4,7 @@ open System open System.Collections.Concurrent open System.Threading +open System.Threading.Tasks open JetBrains.Lifetimes open O21.Game.Engine open type Raylib_CsLo.Raylib @@ -40,6 +41,7 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) = let! player = CreateMusicPlayerAsync lt (content.SoundFontPath, data.MidiFilePath) player.Initialize() state <- { state with MusicPlayer = Some player } + Task.Run(fun() -> UpdateMusicPlayer lt &player) |> ignore } |> ignore member _.Initialize(lifetime: Lifetime): unit = @@ -73,10 +75,6 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) = SetSoundVolume(effect, state.Settings.SoundVolume) PlaySound(effect) - match state.MusicPlayer with - | Some player when player.NeedsPlay() -> player.Play() - | _ -> () - state <- { state with SoundsToStartPlaying = Set.empty } member _.Draw() = diff --git a/O21.Game/Music.fs b/O21.Game/Music.fs index 1c23c47..c2863d6 100644 --- a/O21.Game/Music.fs +++ b/O21.Game/Music.fs @@ -50,3 +50,12 @@ let CreateMusicPlayerAsync (lifetime: Lifetime) (soundFontPath: string, midiFile let buffer = Array.zeroCreate(BufferSize * 2) return MusicPlayer(Buffer = buffer, Stream = audioStream, Sequencer = sequencer) } + +let UpdateMusicPlayer (lifetime: Lifetime) (player:inref) = + try + while lifetime.IsAlive do + match player with + | player when player.NeedsPlay() -> player.Play() + | _ -> () + with + | :? LifetimeCanceledException -> ()