HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulesdl2-mixer-1.2.0.0Haskell2010

SDL.Mixer

Bindings to the SDL2_mixer library.

Contents
  1. Audio setup
  2. Alternative
  3. Loading audio data
  4. Chunks
  5. Playing chunks
  6. Grouping channels
  7. Controlling playback
  8. Setting the volume
  9. Querying for status
  10. Fading out
  11. Reacting to finish
  12. Music
  13. Playing music
  14. Controlling playback
  15. Setting the volume
  16. Querying for status
  17. Fading out
  18. Reacting to finish
  19. Effects
  20. In-built effects
  21. Other
Synopsis

Audio setup

In order to use the rest of the library, you need to supply withAudio or openAudio with an Audio configuration.

datadata Format

A sample format.

Constructors

Instances5Bounded, Eq, Ord, Read, Show
  • Bounded FormatDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Eq FormatDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Ord FormatDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Read FormatDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Show FormatDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
datadata Output

The number of sound channels in output.

Instances5Bounded, Eq, Ord, Read, Show
  • Bounded OutputDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Eq OutputDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Ord OutputDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Read OutputDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Show OutputDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
typetype ChunkSize = Int

The size of each mixed sample.

The smaller this is, the more often callbacks will be invoked. If this is made too small on a slow system, the sounds may skip. If made too large, sound effects could lag.

Alternative

valuecloseAudio :: MonadIO m => m ()

Shut down and clean up the SDL2_mixer API.

After calling this, all audio stops.

You don't have to call this if you're using withAudio.

Loading audio data

Use load or decode to get both Chunk and Music values.

classclass Loadable a where

A class of all values that can be loaded from some source. You can load both Chunks and Music this way.

Note that you must call withAudio before using these, since they have to know the audio configuration to properly convert the data for playback.

Methods

Instances2Loadable
valuechunkDecoders :: MonadIO m => m [String]

Returns the names of all chunk decoders currently available.

These depend on the availability of shared libraries for each of the formats. The list may contain any of the following, and possibly others: WAVE, AIFF, VOC, OFF, FLAC, MP3.

newtypenewtype Music

A loaded music file.

Music is played on a separate channel different from the normal mixing Channels.

To manipulate Music outside of post-processing callbacks, use the music variant functions listed below.

Constructors

Instances3Eq, Show, Loadable
  • Eq MusicDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Show MusicDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Loadable MusicDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
valuemusicDecoders :: MonadIO m => m [String]

Returns the names of all music decoders currently available.

These depend on the availability of shared libraries for each of the formats. The list may contain any of the following, and possibly others: WAVE, MODPLUG, MIKMOD, TIMIDITY, FLUIDSYNTH, NATIVEMIDI, OGG, FLAC, MP3.

Chunks

Chunks are played on Channels, which can be combined into Groups.

Playing chunks

newtypenewtype Channel

A mixing channel.

Use the Integral instance to define these: the first channel is 0, the second 1 and so on.

The default number of Channels available at startup is 8, so note that you cannot usemore than these starting 8 if you haven't created more with setChannels.

The starting Volume of each Channel is the maximum: 128.

Instances8Enum, Eq, Integral, Num, Ord, Real, …
valuesetChannels :: MonadIO m => Int -> m ()

Prepares a given number of Channels for use.

There are 8 such Channels already prepared for use after withAudio is called.

You may call this multiple times, even with sounds playing. If setting a lesser number of Channels than are currently in use, the higher Channels will be stopped, their finish callbacks invoked, and their memory freed. Passing in 0 or less will therefore stop and free all mixing channels.

Any Music playing is not affected by this function.

newtypenewtype Times

How many times should a certain Chunk be played?

Instances6Enum, Eq, Integral, Num, Ord, Real
  • Enum TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Eq TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Integral TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Num TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Ord TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Real TimesDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer

Grouping channels

newtypenewtype Group

A group of Channels.

Grouping Channels together allows you to perform some operations on all of them at once.

By default, all Channels are members of the DefaultGroup.

Instances6Enum, Eq, Integral, Num, Ord, Real
  • Enum GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Eq GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Integral GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Num GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Ord GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Real GroupDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer

Controlling playback

Setting the volume

typetype Volume = Int

A volume, where 0 is silent and 128 loudest.

Volumes lesser than 0 or greater than 128 function as if they are 0 and 128, respectively.

classclass HasVolume a where

A class of all values that have a Volume.

Methods

Instances2HasVolume

Querying for status

datadata Fading

Describes whether a Channel is fading in, out, or not at all.

Instances4Eq, Ord, Read, Show
  • Eq FadingDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Ord FadingDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Read FadingDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer
  • Show FadingDefined in sdl2-mixer-1.2.0.0 · SDL.Mixer

Fading out

Reacting to finish

Music

Chunks and Music differ by the way they are played. While multiple Chunks can be played on different desired Channels at the same time, there can only be one Music playing at the same time.

Therefore, the functions used for Music are separate.

Playing music

valueplayMusic :: MonadIO m => Times -> Music -> m ()

Plays a given Music a certain number of Times.

The previously playing Music will be halted, unless it is fading out in which case a blocking wait occurs until it fades out completely.

Controlling playback

valuerewindMusic :: MonadIO m => m ()

Rewinds the Music to the beginning.

When playing new Music, it starts at the beginning by default.

This function only works with MOD, OGG, MP3 and NATIVEMIDI streams.

Setting the volume

Querying for status

Fading out

Reacting to finish

valuewhenMusicFinished :: MonadIO m => IO () -> m ()

Sets a callback that gets invoked each time a Music finishes playing.

Note: don't call other SDL.Mixer functions within this callback.

Effects

typetype Effect = Channel -> IOVector Word8 -> IO ()

A post-processing effect as a function operating on a mutable stream.

Note that, at the moment, this is a stream of bytes. Depending on the Audio Format you're using, you're probably going to want to treat is as a stream of 16-bit values instead.

typetype EffectFinished = Channel -> IO ()

A function called when a processor is finished being used.

This allows you to clean up any state you might have had.

In-built effects

valueeffectPan :: MonadIO m => Channel -> Volume -> Volume -> m (m ())

Applies an in-built effect implementing panning.

Sets the left-channel and right-channel Volume to the given values.

This only works when Audio's Output is Stereo, which is the default.

Returns an action that, when executed, removes this effect. That action simply calls effectPan with Volumes 128 and 128.

valueeffectDistance :: MonadIO m => Channel -> Word8 -> m (m ())

Applies a different volume based on the distance (as Word8) specified.

The volume is loudest at distance 0, quietest at distance 255.

Returns an action that, when executed, removes this effect. That action simply calls effectDistance with a distance of 0.

valueeffectPosition :: MonadIO m => Channel -> Int16 -> Word8 -> m (m ())

Simulates a simple 3D audio effect.

Accepts the angle in degrees (as Int16) in relation to the source of the sound (0 is directly in front, 90 directly to the right, and so on) and a distance (as Word8) from the source of the sound (where 255 is very far away, and 0 extremely close).

Returns an action that, when executed, removes this effect. That action simply calls effectPosition with both angle and distance set to 0.

Other

valueinitialize :: (Foldable f, MonadIO m) => f InitFlag -> m ()

Initialize the library by loading support for a certain set of sample/music formats.

Note that calling this is not strictly necessary: support for a certain format will be loaded automatically when attempting to load data in that format. Using initialize allows you to decide when to load support.

You may call this function multiple times.