问题
I've been working on a game that uses multiple audio tracks whose volumes are adjusted in realtime based on mouse motion. I'm using SDl-Mixer for audio, and Reactive-Banana for the game in general. The problem is that the tracks, which have all been started at the beginning, stop playing when the input-loop starts. The cause may be something else, but I wonder if there's some strange interaction between SDL and Reactive-Banana that I don't understand. I've been trying to puzzle this out for a while, but it might just be something simple that I've overlooked. Thanks in advance. Here's the code for a simple test:
import Reactive.Banana
import Graphics.UI.SDL
import Graphics.UI.SDL.Mixer
import Control.Monad
musicdir = "/home/silas/code/haskell/river/audio/"
testNet :: AddHandler (Int,Int,[MouseButton]) -> NetworkDescription t ()
testNet mouseHdlr = do
eMouse <- fromAddHandler mouseHdlr
reactimate $ (putStrLn . show) <$> eMouse
main = withInit [InitEverything] $ do
setVideoMode 100 100 32 [SWSurface]
openAudio defaultFrequency AudioS16Sys 1 1024
allocateChannels 1
chunk <- loadWAV $ musicdir ++ "guitar1" ++ ".ogg"
playChannel 0 chunk (-1)
(mouseHdlr, mouseAction) <- newAddHandler
net <- compile $ testNet mouseHdlr
actuate net
forever $ getMouseState >>= mouseAction
Edit: The problem doesn't seem to lie in Reactive Banana necessarily. Any sort of 'forever' loop causes the audio to stop.
回答1:
This might not be your problem, but when I had a similar issue it was that the Garbage Collector could not tell that I was still using the SDL audio chunk (because only SDL was using it) and free'd the RAM out from under SDL. To solve this you not only have to make sure that the item is in scope for the entire time SDL will be using it, but that your Haskell code actually makes use of it in some way (so that the optimiser doesn't change things on you). Calls like touchForeignPtr
are very useful for this.
回答2:
This might not be related to either SDL or reactive-banana. Issues like that are often fixed by compiling with the -threaded
compiler flag. If this is a Cabal project, add
GHC-Options: -W -threaded
to your Executable
section.
来源:https://stackoverflow.com/questions/12443843/sdl-mixer-audio-stops-upon-starting-reactive-banana-input-loop