Tweak sound

This commit is contained in:
2021-04-05 14:34:32 +02:00
parent a7e56d727d
commit f7e0b40cd5
3 changed files with 43 additions and 9 deletions
+32 -1
View File
@@ -33,6 +33,33 @@ there may be a leak in the current typical usage.
playAndUpdate :: Ord a => M.Map a Sound -> SoundData a -> IO (SoundData a)
playAndUpdate new sd = playSounds $ over playingSounds (updatePlaying new) sd
--updateSound :: Sound -> Sound -> IO Sound
--updateSound newS oldS
-- | _soundIsFadingOut oldS
-- = Mix.playOn i times
-- | otherwise = newS & soundChannel .~ _soundChannel oldS
startPlaying :: SoundData a -> Sound -> IO (Maybe Sound)
startPlaying sd s = case _soundChannel s of
Just _ -> return $ Just s
Nothing -> playSoundIfFree (_loadedChunks sd IM.! _soundChunkID s) s
decrimentTimer :: Sound -> IO Sound
decrimentTimer s = case _soundTime s of
Just t | t > 0 -> return $ s & soundTime ?~ t - 1
| otherwise -> fadeOutMaybe (_soundChannel s) (_soundFadeTime s)
>> return (s & soundTime .~ Nothing
& soundIsFadingOut .~ True)
cleanupHalted :: Sound -> IO (Maybe Sound)
cleanupHalted s = case _soundChannel s of
Nothing -> return Nothing
Just i -> do
isPlaying <- Mix.playing i
if isPlaying
then return $ Just s
else return Nothing
{- | Given a chunk, attempt to play this on a free channel a given number of
times. Returns 'Just' the channel if succeeds.
-}
@@ -61,6 +88,10 @@ haltMaybe :: Maybe Mix.Channel -> IO (Maybe Sound)
haltMaybe (Just x) = Mix.halt x >> return Nothing
haltMaybe Nothing = return Nothing
fadeOutMaybe :: Maybe Mix.Channel -> Int -> IO ()
fadeOutMaybe (Just x) fadeT = Mix.fadeOut (fromIntegral fadeT) x
fadeOutMaybe _ _ = return ()
{- | Play a sound according to it's specification.
Logic: check if sound is playing:
@@ -79,7 +110,7 @@ updateOrStartSound sd s = case _soundChannel s of
then return (Just s)
else return Nothing
)
Nothing -> playSoundIfFree (_loadedChunks sd IM.! _soundType s) s
Nothing -> playSoundIfFree (_loadedChunks sd IM.! _soundChunkID s) s
{- | Play all sounds and create new sound data.
-}