sdl2: intro minimal bindings to SDL2 mixer
[nit.git] / lib / sdl2 / mixer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # SDL2 mixer with sample/sounds and music
16 #
17 # C API documentation: https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html
18 module mixer is pkgconfig "SDL2_mixer"
19
20 import sdl2_base
21
22 `{
23 #include "SDL2/SDL_mixer.h"
24 `}
25
26 redef class Sys
27 # SDL2 mixer services
28 var mix = new Mix is lazy
29 end
30
31 # SDL2 mixer services
32 class Mix
33 # Initialize supports for loading formats in `flags`
34 #
35 # Returns the initialized supports.
36 fun initialize(flags: MixInitFlags): MixInitFlags `{
37 return Mix_Init(flags);
38 `}
39
40 # FLAC format support, for `initialize`
41 fun flac: MixInitFlags `{ return MIX_INIT_FLAC; `}
42
43 # MOD format support, for `initialize`
44 fun mod: MixInitFlags `{ return MIX_INIT_MOD; `}
45
46 # MP3 format support, for `initialize`
47 fun mp3: MixInitFlags `{ return MIX_INIT_MP3; `}
48
49 # OGG format support, for `initialize`
50 fun ogg: MixInitFlags `{ return MIX_INIT_OGG; `}
51
52 # Clean up all dynamically loaded library handles
53 #
54 # May be called after loading all the sounds.
55 fun quit `{ Mix_Quit(); `}
56
57 # Initialize and configure the mixer API
58 #
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;
66 `}
67
68 # Default frequency for `open_audio` (22050)
69 fun default_frequency: Int `{ return MIX_DEFAULT_FREQUENCY; `}
70
71 # Default format for `open_audio`
72 fun default_format: MixFormat `{ return MIX_DEFAULT_FORMAT; `}
73 # TODO other formats: AUDIO_U8, AUDIO_S8, etc.
74
75 # Shutdown and cleanup the mixer API
76 fun close_audio `{ Mix_CloseAudio(); `}
77
78 # Last error produced by the mixer API
79 fun error: CString `{ return (char*)Mix_GetError(); `}
80
81 # ---
82 # Chunks/sounds
83
84 # Load `file` for use as a sample
85 #
86 # Returns a null pointer on error.
87 #
88 # Can load WAVE, AIFF, RIFF, OGG and VOC files.
89 fun load_wav(file: CString): MixChunk `{ return Mix_LoadWAV(file); `}
90
91 # Play `chunk` on `channel`
92 #
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;
98 `}
99
100 # Set the chunk volume out of `mix.max_volume` and return the previous value
101 #
102 # Use `volume = -1` to only read the previous value.
103 fun volume_chunk(chunk: MixChunk, volume: Int) `{
104 Mix_VolumeChunk(chunk, volume);
105 `}
106
107 # ---
108 # Music
109
110 # Load music from `file`
111 #
112 # Returns a null pointer on error.
113 #
114 # WAVE, MOD, MIDI, OGG, MP3, FLAC VOC files
115 fun load_mus(file: CString): MixMusic `{ return Mix_LoadMUS(file); `}
116
117 # Play `music` `loops` times
118 #
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;
122 `}
123
124 # Pause music playback
125 fun pause_music `{ Mix_PauseMusic(); `}
126
127 # Resume music playback
128 fun resume_music `{ Mix_ResumeMusic(); `}
129
130 # Set the music volume out of `mix.max_volume` and return the previous value
131 #
132 # Use `volume = -1` to only read the previous value.
133 fun volume_music(volume: Int): Int `{
134 return Mix_VolumeMusic(volume);
135 `}
136
137 # Maximum volume value for `volume_music` and `volume_chunk`
138 fun max_volume: Int `{ return MIX_MAX_VOLUME; `}
139 end
140
141 # Chunk/sound handle, loaded by `mix.load_wav`
142 extern class MixChunk
143 redef fun free `{ Mix_FreeChunk(self); `}
144 end
145
146 # Music handle, loaded by `mix.load_mus`
147 extern class MixMusic
148 redef fun free `{ Mix_FreeMusic(self); `}
149 end
150
151 # ---
152 # Flags & enums
153
154 # Flags for `mix.initialize`
155 extern class MixInitFlags `{ int `}
156
157 # Binary OR
158 fun | (other: MixInitFlags): MixInitFlags `{ return self | other; `}
159
160 private fun to_i: Int `{ return self; `}
161
162 redef fun ==(o) do return o isa MixInitFlags and o.to_i == to_i
163 end
164
165 # Sample format for `mix.open_audio`
166 extern class MixFormat `{ int `}
167 end