Skip to content

Instantly share code, notes, and snippets.

@ivanperez-keera
Last active November 4, 2015 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanperez-keera/4155135931eb48201b6f to your computer and use it in GitHub Desktop.
Save ivanperez-keera/4155135931eb48201b6f to your computer and use it in GitHub Desktop.
Play a sound in SDL-mixer

This is a trivial example that plays a sound in SDL mixer.

It includes arbitrary Template Haskell to check that everything links properly.

Do not copy this to your own project. Use these functions or check out the Haskell Game Programming wiki instead.

-- This is a trivial Haskell program that loads a file and
-- plays it for 5 seconds. It includes a line of pointless
-- template haskell to check that ghci can also link things.
-- If you can't compile this, then odds are you won't be
-- able to link any program with TH that depends on SDL-mixer,
-- or run them from ghci directly.
{-# LANGUAGE TemplateHaskell #-}
import Control.Concurrent
import Foreign.ForeignPtr
import Graphics.UI.SDL as SDL
import qualified Graphics.UI.SDL.Mixer.General as SDL.Mixer
import qualified Graphics.UI.SDL.Mixer.Channels as SDL.Mixer.Channels
import qualified Graphics.UI.SDL.Mixer.Types as SDL.Mixer.Types
import qualified Graphics.UI.SDL.Mixer.Samples as SDL.Mixer.Samples
import Language.Haskell.TH
main :: IO ()
main = do
SDL.init [InitAudio]
SDL.Mixer.openAudio 44100 SDL.Mixer.AudioS16LSB 2 4096
SDL.Mixer.Channels.allocateChannels 16
wave <- SDL.Mixer.Samples.loadWAV "baby.wav"
SDL.Mixer.Channels.playChannel (-1) wave 0
threadDelay 5000000
-- Try also using SDL.delay 1000
touchForeignPtr wave
-- Poinless TH
return $( return (InfixE (Just (LitE (IntegerL 1))) (VarE (mkName "+")) (Just (LitE (IntegerL 2)))))
return ()
Cabal-Version: >= 1.8
Name: SDL-mixer-example
Version: 0.0.1
Maintainer: Ivan Perez <ivan.perez@keera.co.uk>
Author: Ivan Perez <ivan.perez@keera.co.uk>
Copyright: 2015 Ivan Perez - Keera Studios Ltd
License: BSD3
Build-Type: Simple
Category: Foreign binding
Synopsis: Example of SDL-mixer code
executable baby-test
Build-Depends: base >= 3 && < 5, SDL, SDL-mixer, template-haskell
Main-is: Main.hs
#!/usr/bin/env runhaskell
> module Main where
> import Distribution.Simple
> main :: IO ()
> main = defaultMainWithHooks autoconfUserHooks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment