Abstract
LISPBUILDER-SDL-MIXER provides a Common Lisp binding to the SDL_mixer library. The library mixes multiple samples into one or more audio output channels. Lisp callbacks provide the capability to implement custom mixing algorithms and custom audio effects over and above the distance attenuation, panning and reverse stereo effects provided by the SDL_mixer library. WAVE, MOD, MIDI, OGG, AIFF, RIFF and VOC formats are supported.
LISPBUILDER-SDL-MIXER has a dependency on LISPBUILDER-SDL.
The code comes with a MIT-style license so you can basically do with it whatever you want.
Download shortcut: http://www.balooga.com/lispbuilder/lispbuilder-sdl-mixer.tgz.
;; Load an mp3 music file and play the file continuously.
(sdl:with-init (sdl:sdl-init-video sdl:sdl-init-audio)
(sdl:window 50 10)
(sdl-mixer:OPEN-AUDIO)
(let ((music (sdl-mixer:load-music "sample.mp3")))
(sdl-mixer:play-music music :loop t)
(sdl:with-events ()
(:quit-event ()
(sdl-mixer:Halt-Music)
(sdl-mixer:free music)
(sdl-mixer:close-audio)
t)
(:idle () (sdl:update-display)))))
+channel-post++channels++default-channels++default-format++default-frequency++default-sample-buffer++max-volume+allocate-channelsaudio-channels-paudio-format-paudio-frequency-paudio-opened-pchannel-volumechunkclose-audiofreehalt-musichalt-samplelinked-versionload-musicload-samplemusicmusic-fading-pmusic-paused-pmusic-playing-pmusic-positionmusic-type-ofmusic-type-pmusic-volumeopen-audiopause-musicplay-musicplay-sampleregister-music-finishedregister-music-mixerregister-sample-finishedrewind-musicsample-fading-psample-from-channelsample-paused-psample-playing-psample-volumesdl-mixer-versionunregister-music-finishedunregister-music-mixerunregister-sample-finishedCurrent Version: The latest stable version of LISPBUILDER-SDL-MIXER, together with this documentation can be downloaded from http://www.balooga.com/lispbuilder/lispbuilder-sdl-mixer.tgz. The current version is 0.2.0.
Development: The latest developement version is available via anonymous SVN:
svn co https://lispbuilder.svn.sourceforge.net/svnroot/lispbuilder lispbuilder-sdl-mixer
(asdf:operate 'asdf:load-op :lispbuilder-sdl-mixer)
(asdf:operate 'asdf:load-op :lispbuilder-sdl-mixer-examples)
(SDL-MIXER-EXAMPLES:MIXER)
[Constant]
+channel-post+
Default channel of
-2used for post-processing.
[Constant]
+channels+
Default number of
8mixer channels.
[Constant]
+default-channels+
Default number of
2sound channels for Stereo output.
[Constant]
+default-format+
Default SDL audio format; little-endian is
SDL:AUDIO-S16LSB, big-endian isSDL:AUDIO-S16MSB. Audio formats are defined inSDL_audio.h;
SDL:AUDIO-U8: Unsigned 8-bit samplesSDL:AUDIO-S8: Signed 8-bit samplesSDL:AUDIO-U16LSB: Unsigned 16-bit samples, in little-endian byte orderSDL:AUDIO-S16LSB: Signed 16-bit samples, in little-endian byte orderSDL:AUDIO-U16MSB: Unsigned 16-bit samples, in big-endian byte orderSDL:AUDIO-S16MSB: Signed 16-bit samples, in big-endian byte orderSDL:AUDIO-U16: same asSDL:AUDIO-U16LSB(for backwards compatability probably)SDL:AUDIO-S16: same asSDL:AUDIO-S16LSB(for backwards compatability probably)SDL:AUDIO-U16SYS: Unsigned 16-bit samples, in system byte orderSDL:AUDIO-S16SYS: Signed 16-bit samples, in system byte order
[Constant]
+default-frequency+
Default sampling frequency of
22,050hz
[Constant]
+default-sample-buffer+
Default size of the sample output buffer is
4096bytes
[Constant]
+max-volume+
Default volume of
128for CHUNK, output channels and mix channels.
[Function]
allocate-channels num-channels => result
Allocates
NUM-CHANNELSto be used for mixing samples. Frees all allocated channnels whenNUM-CHANNELSisNILor0. A negative value forNUM-CHANNELSis ignored. Returns the number of channels currently allocated. Can be called multiple times even if samples are currently playing. If the current allocation of channels is greater thanNUM-CHANNELS, then channels greater thanNUM-CHANNELSwill be stopped and these resources freed. The callback set by REGISTER-SAMPLE-FINISHED is called for each channel halted. NOTE: Samples will continue playing whenNUM-CHANNELSis0.
[Function]
audio-channels-p => result
Returns the number of output channels used by the audio device, e.g.
2for stereo and1for mono. Does not return the number of mixing channels allocated.
[Function]
audio-format-p => result
Returns the current audio format of the audio device.
[Function]
audio-frequency-p => result
Returns the current audio frequency of the audio device.
[Function]
audio-opened-p => result
Returns the number of times the audio device has been opened by OPEN-AUDIO, or
NILif the audio device is closed.
[Accessor]
channel-volume channel => result
(setf (channel-volume channel) volume)
Returns the volume of
CHANNEL, as anINTEGER. Volume can be from 0 to +MAX-VOLUME+. IfCHANNELis NIL then the average volume over all channels is returned.
[Standard class]
chunk
[Function]
close-audio &optional all => result
Attempts to close the audio device. The audio device can be opened multiple times by OPEN-AUDIO and to properly close the audio device, CLOSE-AUDIO should be called the same number of times. Optionally
ALLwhenTwill forcibly close the audio device, no matter how many times the device was opened.
[Generic function]
free chunk => result
[Method]
free (chunk music) => result
Frees the music in MUSIC. Stops MUSIC if currently playing. Will block until any current fade effect completes. Do not reuse MUSIC once freed.
[Method]
free (chunk chunk) => result
Frees the sample in CHUNK. Do not reuse a CHUNK once freed. Do not attempt to free a CHUNK that is still being played.
[Function]
halt-music &optional fade => result
Stops playing music. The callback set by REGISTER-MUSIC-FINISHED is called when the music stops.
Parameters
FADEis the number of milliseconds to perform the fade-out effect. Will block until any current fade effect completes. Has no effect on paused or halted music. WhenFADEis NIL the music is stopped and all fade effects are immediately cancelled. The default is NIL.Returns
- T on success and NIL on failure.
[Function]
halt-sample channel &key fade ticks => result
Stops playing the sample on
CHANNEL. The callback set by REGISTER-SAMPLE-FINISHED is called when the channel stops.Parameters
CHANNELspecifies the channel to stop playing. WhenT, will stop playback samples on all channels. Ignores channels not currently playing out samples.FADEis the number of milliseconds to perform the fade-out effect. The sample is stopped when fade effect completes. WhenFADEisNILor0the sample is stopped immediately. Default isNIL.TICKSis the number of milliseconds until the sample is stopped. WhenNILor0, the sample is stopped immediately. Default isNIL.Returns
- Returns the number of samples fading out, when
FADEis set.- Returns the number of samples halted, when
TICKSis set.- Otherwise, returns T on success and NIL on failure.
[Function]
linked-version => result
Returns the version number of the SDL_mixer dynamic library in use as #(
MAJORMINORPATCH).
[Function]
load-music filepath => result
Loads the music file at location
FILEPATH. Must be aWAVE,MOD,MIDI,OGGorMP3file. Returns music as a new MUSIC object, or NIL on error.
[Function]
load-sample filepath => result
Loads the sample file at location
FILEPATH. Must be aWAVE,AIFF,RIFF,OGG, orVOCfile. Returns the sample as a new CHUNK object, or NIL on error.
[Standard class]
music
[Function]
music-fading-p => result
Returns the current fade effect on music regardless of the current play or pause state. Returns
:FADING-OUTif a fade-out is in effect,:FADING-INif a fade-in is in effect, orNILif no fade is in effect.
[Function]
music-paused-p => result
Returns T if music is currently paused or was previously paused prior to a halt, or NIL otherwise.
[Function]
music-playing-p => result
Returns
Tif music is currently playing or is paused, orNILif music is halted.
[Function]
music-position position => result
Sets the play
POSITIONof the currently playing music. ReturnsTon success andNILon failure. Applicable only toMOD,OGGandMP3music formats as described below.
MOD, jumps to the pattern number in the module identified byPOSITION.0will rewind the module to the beginning.OGG, jumps toPOSITIONseconds from the beginning of the song.MP3, jumps forwardsPOSITIONseconds from the current play position. Negative values are ignored. To rewind, use REWIND-MUSIC to jump to the start of the song, then jump forwardPOSITIONseconds using MUSIC-POSITION.
[Function]
music-type-of music type => result
Returns
Tif MUSIC is ofTYPE, returnsNILotherwise.TYPEmay be one ofWAV,MOD,MID,OGG,MP3,MP3-MAD,FLACorCMD. If MUSIC isNILreturns the type of the currently playing music. ReturnsNILis no music is playing.
[Function]
music-type-p music => result
Returns the format type of MUSIC as one of
WAV,MOD,MID,OGG,MP3,MP3-MAD,FLACorCMD. Returns the format type of the currently playing music when MUSIC isNIL. ReturnsNILis no music is playing.
[Accessor]
music-volume => result
(setf (music-volume ) volume)
Returns current music volume as an
INTEGERfrom 0 to +MAX-VOLUME+.
[Function]
open-audio &key frequency format channels chunksize => result
Initializes the mixer. SDL must be initialized with SDL-INIT-AUDIO prior to this call. OPEN-AUDIO can be called multiple times, however
FORMATis set on the first call and will not changed on subsequent calls. The audio device must be closed and re-opened for any change toFORMATto take effect. CLOSE-AUDIO must be called the same number of time to close the audio device. Use ALLOCATE-CHANNELS to set the number of mixing channels. OPEN-AUDIO will allocate +MIX-CHANNELS+ by default.Parameters
FREQUENCYis the output sampling frequency in samples per second (Hz). Default is +DEFAULT-FREQUENCY+. Most games use aFREQUENCYof +DEFAULT-FREQUENCY+. The higher theFREQUENCYthe greater the CPU resource requirements. (A value of 44100 is the CD audio rate).FORMATis the output sample format. Default is +DEFAULT-FORMAT+.CHANNELSsets the number of sound channels in the output. Default is +DEFAULT-CHANNELS+. This is not the same as ALLOCATE-CHANNELS which sets the number of mixing channels.CHUNKSIZEis the size in bytes of each mixed sample buffer. Default is +DEFAULT-SAMPLE-BUFFER+ bytes. The smaller theCHUNKSIZE, the more frequenctly the mixer hooks are called. IncreaseCHUNKSIZEto decrease CPU resources, if sound skips or if playing music. DecreaseCHUNKSIZEto decrease sound lag.Returns
Ton success andNILon failure.
[Function]
pause-music => result
Pause music. Music can only be paused if it is actively playing. You may halt paused music.
[Function]
play-music music &key loop fade position => result
Starts playing MUSIC from
POSITION. The music can be faded-in over the number of milliseconds inFADE. Automatically repeats the music when finished the number of time specified inLOOP. Any previous music will be halted. Will block until any current fade effect completes.Parameters
MUSICis the MUSIC to play.LOOPis the number of times to play the music. T continuously loops the music. Plays once whenNIL, or0. Default isNIL.FADEis the number of milliseconds to perform the fade-in effect. Default isNIL, no fade effect. Only applies to the first loop.POSITIONis the position in the music to begin playing from. Default is to start playout from the beginning.Returns
- T on success and NIL on failure.
[Function]
play-sample chunk &key channel loop fade ticks => result
Plays the sample in CHUNK for
TICKSmilliseconds, repeatedLOOPtimes. The sample is repeated the number of times specified inLOOP, or untilTICKS. The sample will fade-in overFADEmilliseconds. The callback set by REGISTER-SAMPLE-FINISHED is called when the sample stops playing.Parameters
CHUNKis the sample CHUNK.CHANNELspecifies the channel to play the sample. WhenNIL, will play on the first free unreserved channel. Default isNIL.LOOPis the number of times to loop the sample. Loops continuously whenT. Plays once whenNIL, or0. Default inNIL.FADEis the number of milliseconds to perform the fade-in effect. Default isNIL, no fade effect. Only applies to the first loop.TICKSis the number of milliseconds to play the sample. WhenTwill play the sample from start to finish. Default isT.Returns
- The channel the sample is playing on, or
NILon failure.
[Function]
register-music-finished func => result
Sets the callback that is executed when MUSIC finishes playback or is halted.
FUNCis of the format#(lambda ())Example
(REGISTER-MUSIC-FINISHED (lambda () (FORMAT T "MUSIC FINISHED")))
[Function]
register-music-mixer func => result
Sets the callback that is executed to fill the music audio output buffer.
FUNCis of the format#(lambda (user-data stream len))Example
(REGISTER-MUSIC-MIXER (lambda (user stream len) 'FILL-THE-AUDIO-OUTPUT-BUFFER))
[Function]
register-sample-finished func => result
Sets the callback that is executed when a sample CHUNK finishes playback or is halted.
FUNCis of the format#(lambda (channel))Example
(REGISTER-SAMPLE-FINISHED (lambda (channel) (FORMAT T "SAMPLE FINISHED ON CHANNEL: ~A" channel)))
[Function]
rewind-music => result
Rewind to the start of music. Safe to use on halted, paused, and currently playing music. It is not necessary to rewind the music immediately after starting playback, as it starts at the beginning by default. Only the following streams support rewind:
MOD,OGG,MP3,Native MIDI.
[Function]
sample-fading-p channel => result
Returns
Tif a fade is in effect for the sample onCHANNEL, regardless of the current play or pause state of the sample. Returns:FADING-OUTif a fade-out is in effect,:FADING-INif a fade-in is in effect, orNILif no fade is in effect.
[Function]
sample-from-channel channel => result
Returns currently playing or most recently played sample on
CHANNELas a new CHUNK object, orNILifCHANNELis not allocated orCHANNELhas not yet played out any samples. NOTE: The sample may already have been freed and therefore the pointer to the foreign object in CHUNK may not be valid.
[Function]
sample-paused-p channel => result
Returns
Tif the sample onCHANNELis currently paused or was previously paused prior to a halt, orNILotherwise. Returns the number of paused samples whenCHANNELisTorNIL.
[Function]
sample-playing-p channel => result
Returns
Tif a sample is currently playing or is paused onCHANNEL, orNILif the sample is halted. Returns the number of samples playing or paused whenCHANNELisTorNIL.
[Accessor]
sample-volume chunk => result
(setf (sample-volume chunk) volume)
Returns the volume of the sample in CHUNK, as an
INTEGERfrom 0 to +MAX-VOLUME+.
[Function]
sdl-mixer-version sdl-version => result
Sets the
SDL-VERSIONstructure with the version of the library..
[Function]
unregister-music-finished => result
Removes the callback function set by set by REGISTER-MUSIC-FINISHED.
[Function]
unregister-music-mixer => result
Removes any callback function set by REGISTER-MUSIC-MIXER.
[Function]
unregister-sample-finished => result
Removes the callback function set by REGISTER-SAMPLE-FINISHED.