1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # SDL2 mixer with sample/sounds and music
17 # C API documentation: https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html
18 module mixer
is pkgconfig
"SDL2_mixer"
23 #include "SDL2/SDL_mixer.h"
28 var mix
= new Mix is lazy
33 # Initialize supports for loading formats in `flags`
35 # Returns the initialized supports.
36 fun initialize
(flags
: MixInitFlags): MixInitFlags `{
37 return Mix_Init(flags);
40 # FLAC format support, for `initialize`
41 fun flac
: MixInitFlags `{ return MIX_INIT_FLAC; `}
43 # MOD format support, for `initialize
`
44 fun mod: MixInitFlags `{ return MIX_INIT_MOD; `}
46 # MP3 format support, for `initialize`
47 fun mp3
: MixInitFlags `{ return MIX_INIT_MP3; `}
49 # OGG format support, for `initialize
`
50 fun ogg: MixInitFlags `{ return MIX_INIT_OGG; `}
52 # Clean up all dynamically loaded library handles
54 # May be called after loading all the sounds.
55 fun quit
`{ Mix_Quit(); `}
57 # Initialize and configure the mixer API
59 # Playback is configured with:
60 # * The output sample `frequency
`, in samples per second (Hz).
61 # * The output sample `format
`, may be `mix_default_format
`.
62 # * The number of sound `channels
`, 1 for mono or 2 for stereo.
63 # * The number of bytes used by output sample size as `chunk_size
`.
64 fun open_audio(frequency: Int, format: MixFormat, channels, chunk_size: Int): Bool `{
65 return Mix_OpenAudio(frequency
, format
, channels
, chunk_size
) == 0;
68 # Default frequency for `open_audio
` (22050)
69 fun default_frequency: Int `{ return MIX_DEFAULT_FREQUENCY; `}
71 # Default format for `open_audio`
72 fun default_format
: MixFormat `{ return MIX_DEFAULT_FORMAT; `}
73 # TODO other formats: AUDIO_U8, AUDIO_S8, etc.
75 # Shutdown and cleanup the mixer API
76 fun close_audio `{ Mix_CloseAudio(); `}
78 # Last error produced by the mixer API
79 fun error
: CString `{ return (char*)Mix_GetError(); `}
84 # Load `file
` for use as a sample
86 # Returns a null pointer on error.
88 # Can load WAVE, AIFF, RIFF, OGG and VOC files.
89 fun load_wav(file: CString): MixChunk `{ return Mix_LoadWAV(file); `}
91 # Play `chunk` on `channel`
93 # If `channel == -1` the first unreserved channel is used.
94 # The sound is repeated `loops` times, `loops == 0` plays it once,
95 # `loops == 1` plays it twice and `loops == -1` loops infinitely.
97 # Returns the channel used, or `-1` on error.
98 fun play_channel
(channel
: Int, chunk
: MixChunk, loops
: Int): Int `{
99 return Mix_PlayChannel(channel, chunk, loops);
102 # Play `chunk` on `channel`
104 # If `channel == -1` the first unreserved channel is used.
105 # The sound is repeated `loops` times, `loops == 0` plays it once,
106 # `loops == 1` plays it twice and `loops == -1` loops infinitely.
107 # If `ticks != -1`, the sample plays for at most `ticks` milliseconds.
108 fun play_channel_timed
(channel
: Int, chunk
: MixChunk, loops
, ticks
: Int): Int `{
109 return Mix_PlayChannelTimed(channel, chunk, loops, ticks);
112 # Halt/stop `channel` playback
114 # If `channel == -1`, halt all channels.
115 fun halt_channel
(channel
: Int) `{
116 Mix_HaltChannel(channel);
119 # Halt `channel` in `ticks` milliseconds and return the number of channels set to expire
121 # If `channel == -1`, halt all channels.
122 fun expire_channel
(channel
, ticks
: Int): Int `{
123 return Mix_ExpireChannel(channel, ticks);
126 # Reserve `num` channels from being used by `play_channel(-1...)`
128 # Returns the number of of channels reserved.
129 fun reserve_channels
(num
: Int): Int `{
130 return Mix_ReserveChannels(num);
133 # Set the `volume` of `channel`, out of `mix.max_volume`
135 # If `channel == -1`, set the volume of all channels.
137 # Returns the current volume of the channel, or if `channel == -1` the average volume.
138 fun volume
(channel
, volume
: Int): Int `{
139 return Mix_Volume(channel, volume);
142 # Set the `volume` for `chunk`, out of `mix.max_volume`
144 # If `volume == -1`, only read the previous value.
146 # Returns the previous volume value.
147 fun volume_chunk
(chunk
: MixChunk, volume
: Int) `{
148 Mix_VolumeChunk(chunk, volume);
151 # Pause `channel`, or all playing channels if -1
152 fun pause
(channel
: Int) `{
156 # Unpause `channel`, or all paused channels if -1
157 fun resume
(channel
: Int) `{
164 # Load music from `file`
166 # Returns a null pointer on error.
168 # WAVE, MOD, MIDI, OGG, MP3, FLAC VOC files
169 fun load_mus
(file
: CString): MixMusic `{ return Mix_LoadMUS(file); `}
171 # Play `music
` `loops
` times
173 # The music is plays once if `loops
== 1` and repeats infinitely if `loops
== -1`.
174 fun play_music(music: MixMusic, loops: Int): Bool `{
175 return Mix_PlayMusic(music
, loops
) == 0;
178 # Pause music playback
179 fun pause_music `{ Mix_PauseMusic(); `}
181 # Resume music playback
182 fun resume_music
`{ Mix_ResumeMusic(); `}
184 # Set the music volume out of `mix
.max_volume
` and return the previous value
186 # Use `volume
= -1` to only read the previous value.
187 fun volume_music(volume: Int): Int `{
188 return Mix_VolumeMusic(volume
);
191 # Maximum volume value for `volume_music
` and `volume_chunk
`
192 fun max_volume: Int `{ return MIX_MAX_VOLUME; `}
195 # Chunk/sound handle, loaded by `mix.load_wav`
196 extern class MixChunk
197 redef fun free
`{ Mix_FreeChunk(self); `}
200 # Music handle, loaded by `mix
.load_mus
`
201 extern class MixMusic
202 redef fun free `{ Mix_FreeMusic(self); `}
208 # Flags for `mix.initialize`
209 extern class MixInitFlags `{ int `}
212 fun | (other: MixInitFlags): MixInitFlags `{ return self | other; `}
214 private fun to_i
: Int `{ return self; `}
216 redef fun ==(o) do return o isa MixInitFlags and o.to_i == to_i
219 # Sample format for `mix
.open_audio
`
220 extern class MixFormat `{ int `}