haskellaudio

Is it possible to play a WAV audio file from a cross-platform Haskell program without any external tools?


I have read some SO questions: Play a wav file with Haskell, How to play an audio file from Haskell code, cross-platform, What is the easiest way to play a sound sample in Haskell?, Sound lib haskell

The first and the second ones does not meet my needs because I need to install SDL libraries on my computer for SDL and SDL-mixer packages to work. It's hard to do on Windows.

The third one did not help me because it only works with MIDIs or custom Euterpea format and I wanted to play sound effects, not music.

The fourth one is about another library, OpenAL.

So, how do I play WAV audio files from a Haskell program without using any external libraries (and possibly packages)?

I would also like the answer to be cross-platform, but POSIX-only and Windows only answers are good too.


Solution

  • Instead of:

    putStrLn "upbeat"
    

    consider:

    forkIO (callProcess "mpv" ["upbeat.mp3"])
    

    (Or use whatever media player you prefer over mpv.) You can read the docs for more info about forkIO and callProcess and for suggestions for more robust alternatives if the time ever comes to graduate from prototype to serious program. Don't forget to pass -threaded when building your executable to get the threaded runtime system; this will make your inter-callProcess delays significantly more reliable.