Play a wav file with Haskell

前端 未结 3 1434
时光说笑
时光说笑 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:36

    using OpenAL through ALUT:

    import Control.Monad
    import Sound.ALUT
    
    playSound :: IO ()
    playSound =
      withProgNameAndArgs runALUTUsingCurrentContext $ \_ _ ->
      do
        (Just device) <- openDevice Nothing
        (Just context) <- createContext device []
        currentContext $= Just context
        buffer1 <- createBuffer $ Sine 440 0 1
        buffer2 <- createBuffer HelloWorld
        [source] <- genObjectNames 1
        queueBuffers source [buffer1,buffer2]
        play [source]
        sleep 4
        closeDevice device
        return ()
    
    main = playSound
    

    to load a wav file:

    buffer3 <- createBuffer $ File "/path/to/file.wav"
    

    credit goes to Chris Double: http://bluishcoder.co.nz/articles/haskell/openal.html

    0 讨论(0)
  • 2021-02-07 00:45

    I realize this is not actually a convenient way to do it, but I had the test code lying around, so...

    {-# LANGUAGE NoImplicitPrelude #-}
    module Wav (main) where
    
    import Fay.W3C.Events
    import Fay.W3C.Html5
    
    import Language.Fay.FFI
    import Language.Fay.Prelude
    
    main :: Fay ()
    main = addWindowEventListener "load" run
    
    run :: Event -> Fay Bool
    run _ = do
        aud <- mkAudio
        setSrc aud "test.wav"
        play aud
        return False
    
    
    mkAudio :: Fay HTMLAudioElement
    mkAudio = ffi "new Audio()"
    
    addWindowEventListener :: String -> (Event -> Fay Bool) -> Fay ()
    addWindowEventListener = ffi "window['addEventListener'](%1,%2,false)"
    

    There you go--playing a WAV file in Haskell thanks to the power of HTML5! All you have to do is launch a web browser instead of mplayer. :D

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题