Refactor sounds

This commit is contained in:
2021-09-09 14:58:07 +01:00
parent be7b2d2cd7
commit 70c97f5367
26 changed files with 113 additions and 162 deletions
+5 -21
View File
@@ -55,7 +55,7 @@ playSoundAndUpdate sData oldSounds newSounds
mergeSound :: Sound -> Sound -> Sound
mergeSound oldS newS
| _soundChunkID newS == _soundChunkID oldS && _soundStatus oldS /= FadingOut
| _soundChunkID newS == _soundChunkID oldS
= newS & soundChannel .~ _soundChannel oldS
& soundStatus .~ _soundStatus oldS
| otherwise = newS & soundChannel .~ _soundChannel oldS
@@ -87,25 +87,18 @@ tryPlay sd s = do
& soundChannel ?~ i
& soundStatus .~ JustStartedPlaying
--repetitions :: Sound -> Mix.Times
--repetitions s = case _soundTime s of
-- Nothing -> Mix.Once
-- _ -> Mix.Forever
tryGetChannel :: Sound -> MaybeT IO Mix.Channel
tryGetChannel s = case _soundChannel s of
Just i -> return i
Nothing -> MaybeT $ Mix.getAvailable Mix.DefaultGroup
decrementTimer :: Sound -> IO Sound
decrementTimer s = case _soundTime s of
Just t
| t > 0 -> return $ s & soundTime ?~ t - 1
| otherwise -> fadeOutMaybe (_soundChannel s) (_soundFadeTime s)
>> return (s & soundTime .~ Nothing
& soundStatus .~ FadingOut)
| otherwise -> do
forM_ (_soundChannel s) Mix.halt
return $ s & soundTime .~ Nothing
Nothing -> return s
applyPosition :: Sound -> IO Sound
@@ -114,14 +107,6 @@ applyPosition s = case _soundAngDist s of
Just (a,d) -> Mix.effectPosition (fromJust $ _soundChannel s) a d
>> return (s & soundAngDist .~ Nothing)
fadeOutMaybe
:: Maybe Mix.Channel
-> Int -- ^ fade out time: UNUSED, Mix.fadeOut is buggy
-> IO ()
--fadeOutMaybe (Just x) fadeT = Mix.fadeOut (fromIntegral fadeT + 1) x
fadeOutMaybe (Just x) _ = Mix.halt x
fadeOutMaybe _ _ = return ()
cleanupHalted :: Sound -> MaybeT IO Sound
cleanupHalted s = do
i <- MaybeT $ return $ _soundChannel s
@@ -137,10 +122,10 @@ Use this if you don't care about timing, overlapping, fading, or sound positions
-}
playSoundQueue :: IM.IntMap Mix.Chunk -> [Int] -> IO ()
playSoundQueue chunkMap ns = forM_ ns $ \n -> runMaybeT $ playIfFree (chunkMap IM.! n) Mix.Once
{- | Given a chunk, attempt to play this on a free channel a given number of
times. Returns 'Just' the channel if succeeds.
-}
playIfFree :: Mix.Chunk -> Mix.Times -> MaybeT IO Mix.Channel
playIfFree c times = do
i <- MaybeT $ Mix.getAvailable Mix.DefaultGroup
@@ -171,4 +156,3 @@ Behind the scenes, scales a float [0,1] to an Int [0..128].
-}
setMusicVolume :: Float -> IO ()
setMusicVolume x = Mix.setMusicVolume (round (x * 128))