Skip to content

Commit

Permalink
Update music player in another thread
Browse files Browse the repository at this point in the history
(cherry picked from commit af2708c)
  • Loading branch information
evgTSV authored and ForNeVeR committed Jun 29, 2024
1 parent e174961 commit abcf84b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 2 additions & 4 deletions O21.Game/Game.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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() =
Expand Down
9 changes: 9 additions & 0 deletions O21.Game/Music.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MusicPlayer>) =
try
while lifetime.IsAlive do
match player with
| player when player.NeedsPlay() -> player.Play()
| _ -> ()
with
| :? LifetimeCanceledException -> ()

0 comments on commit abcf84b

Please sign in to comment.