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 and
95 # `loops == -1` loops infinitely.
96 fun play_channel
(channel
: Int, chunk
: MixChunk, loops
: Int): Bool `{
97 return Mix_PlayChannel(channel, chunk, loops) == 0;
100 # Set the chunk volume out of `mix.max_volume` and return the previous value
102 # Use `volume = -1` to only read the previous value.
103 fun volume_chunk
(chunk
: MixChunk, volume
: Int) `{
104 Mix_VolumeChunk(chunk, volume);
110 # Load music from `file`
112 # Returns a null pointer on error.
114 # WAVE, MOD, MIDI, OGG, MP3, FLAC VOC files
115 fun load_mus
(file
: CString): MixMusic `{ return Mix_LoadMUS(file); `}
117 # Play `music
` `loops
` times
119 # The music is plays once if `loops
== 1` and repeats infinitely if `loops
== -1`.
120 fun play_music(music: MixMusic, loops: Int): Bool `{
121 return Mix_PlayMusic(music
, loops
) == 0;
124 # Pause music playback
125 fun pause_music `{ Mix_PauseMusic(); `}
127 # Resume music playback
128 fun resume_music
`{ Mix_ResumeMusic(); `}
130 # Set the music volume out of `mix
.max_volume
` and return the previous value
132 # Use `volume
= -1` to only read the previous value.
133 fun volume_music(volume: Int): Int `{
134 return Mix_VolumeMusic(volume
);
137 # Maximum volume value for `volume_music
` and `volume_chunk
`
138 fun max_volume: Int `{ return MIX_MAX_VOLUME; `}
141 # Chunk/sound handle, loaded by `mix.load_wav`
142 extern class MixChunk
143 redef fun free
`{ Mix_FreeChunk(self); `}
146 # Music handle, loaded by `mix
.load_mus
`
147 extern class MixMusic
148 redef fun free `{ Mix_FreeMusic(self); `}
154 # Flags for `mix.initialize`
155 extern class MixInitFlags `{ int `}
158 fun | (other: MixInitFlags): MixInitFlags `{ return self | other; `}
160 private fun to_i
: Int `{ return self; `}
162 redef fun ==(o) do return o isa MixInitFlags and o.to_i == to_i
165 # Sample format for `mix
.open_audio
`
166 extern class MixFormat `{ int `}