Play a wav file with Haskell

前端 未结 3 1435
时光说笑
时光说笑 2021-02-06 23:49

Is there a simple, direct way to play a WAV file from Haskell using some library and possibly such that I play many sounds at once?

I\'m aware of OpenAL but I\'m not wri

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 00:46

    This is how to play multiple sounds on multiple channels at once with SDL. I think this answers the question criteria. WAV files, simple, Haskell, multiple channels.

    import Control.Monad
    import Control.Monad.Fix
    import Graphics.UI.SDL as SDL
    import Graphics.UI.SDL.Mixer as Mix
    
    main = do
      SDL.init [SDL.InitAudio]
      result <- openAudio audioRate audioFormat audioChannels audioBuffers
      classicJungle <- Mix.loadWAV "/home/chris/Samples/ClassicJungle/A4.wav"
      realTech      <- Mix.loadWAV "/home/chris/Samples/RealTech/A4.wav"
      ch1 <- Mix.playChannel anyChannel classicJungle 0
      SDL.delay 1000
      ch2 <- Mix.playChannel anyChannel realTech 0
      fix $ \loop -> do
        SDL.delay 50
        stillPlaying <- numChannelsPlaying
        when (stillPlaying /= 0) loop
      Mix.closeAudio
      SDL.quit
    
      where audioRate     = 22050
            audioFormat   = Mix.AudioS16LSB
            audioChannels = 2
            audioBuffers  = 4096
            anyChannel    = (-1)
    

提交回复
热议问题