-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
339 lines (281 loc) · 7.1 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "moonbase.h"
int audio_initialized;
static struct {
int frequency, audio_channels, samples, sound_channels;
Uint16 format;
} audio_options;
void audio_initialize( )
{
if ( !SDL_WasInit(SDL_INIT_AUDIO) ) {
if ( SDL_InitSubSystem(SDL_INIT_AUDIO) ) {
fatal( "%s", SDL_GetError() );
}
}
}
void audio_start_mixer( )
{
SDL_AudioSpec desired;
memset( &desired, 0, sizeof(desired) );
desired.freq = audio_options.frequency;
desired.format = audio_options.format;
desired.channels = audio_options.audio_channels;
desired.samples = audio_options.samples;
if ( Mix_OpenAudio(desired.freq, desired.format, desired.channels, desired.samples) ) {
fatal( "%s", Mix_GetError() );
}
Mix_Init( MIX_INIT_OGG | MIX_INIT_MP3 );
if ( Mix_AllocateChannels(audio_options.sound_channels) < audio_options.sound_channels ) {
fatal( "%s", Mix_GetError() );
}
Mix_Resume( -1 );
audio_initialized = 1;
}
void audio_stop_mixer( )
{
Mix_Pause( -1 );
Mix_CloseAudio( );
audio_initialized = 0;
}
void audio_shutdown( )
{
SDL_AudioQuit( );
SDL_QuitSubSystem( SDL_INIT_AUDIO );
}
int audio_get_audio_channels( )
{
return audio_options.audio_channels;
}
Uint16 audio_get_format( )
{
return audio_options.format;
}
int audio_get_frequency( )
{
return audio_options.frequency;
}
int audio_get_samples( )
{
return audio_options.samples;
}
int audio_get_sound_channels( )
{
return audio_options.sound_channels;
}
void audio_set_audio_channels( int channels )
{
audio_options.audio_channels = channels;
}
void audio_set_format( Uint16 format )
{
audio_options.format = format;
}
void audio_set_frequency( int frequency )
{
audio_options.frequency = frequency;
}
void audio_set_samples( int samples )
{
audio_options.samples = samples;
}
void audio_set_sound_channels( int channels )
{
audio_options.sound_channels = channels;
if ( audio_initialized ) {
Mix_AllocateChannels( channels );
}
}
int sound_play( void *sound, int fade_ms, int loop )
{
if ( fade_ms != 0 ) {
return Mix_FadeInChannel( -1, asset_sound_handle(sound), loop ? -1 : 0, fade_ms );
}
return Mix_PlayChannel( -1, asset_sound_handle(sound), loop ? -1 : 0 );
}
static int moonbase_audio_pause( lua_State *s )
{
audio_pause( );
return 0;
}
static int moonbase_audio_resume( lua_State *s )
{
audio_resume( );
return 0;
}
static int moonbase_audio_stop( lua_State *s )
{
int fade_ms;
fade_ms = luaL_optint( s, 1, 0 );
audio_stop( fade_ms );
return 0;
}
static int moonbase_audio_restart( lua_State *s )
{
audio_restart( );
return 0;
}
static int moonbase_audio_get_audio_channels( lua_State *s )
{
lua_pushinteger( s, audio_get_audio_channels() );
return 1;
}
static int moonbase_audio_get_format( lua_State *s )
{
lua_pushunsigned( s, audio_get_format() );
return 1;
}
static int moonbase_audio_get_frequency( lua_State *s )
{
lua_pushinteger( s, audio_get_frequency() );
return 1;
}
static int moonbase_audio_get_samples( lua_State *s )
{
lua_pushinteger( s, audio_get_samples() );
return 1;
}
static int moonbase_audio_get_sound_channels( lua_State *s )
{
lua_pushinteger( s, audio_get_sound_channels() );
return 1;
}
static int moonbase_audio_get_volume( lua_State *s )
{
lua_pushnumber( s, audio_get_volume() );
return 1;
}
static int moonbase_audio_set_audio_channels( lua_State *s )
{
audio_set_audio_channels( luaL_checkinteger(s, 1) );
return 0;
}
static int moonbase_audio_set_format( lua_State *s )
{
audio_set_format( luaL_checkunsigned(s, 1) );
return 0;
}
static int moonbase_audio_set_frequency( lua_State *s )
{
audio_set_frequency( luaL_checkinteger(s, 1) );
return 0;
}
static int moonbase_audio_set_samples( lua_State *s )
{
audio_set_samples( luaL_checkinteger(s, 1) );
return 0;
}
static int moonbase_audio_set_sound_channels( lua_State *s )
{
audio_set_sound_channels( luaL_checkinteger(s, 1) );
return 0;
}
static int moonbase_audio_set_volume( lua_State *s )
{
audio_set_volume( luaL_checknumber(s, 1) );
return 0;
}
static luaL_Reg moonbase_audio_methods[] = {
/* Operations */
{ "pause", moonbase_audio_pause },
{ "resume", moonbase_audio_resume },
{ "stop", moonbase_audio_stop },
{ "restart", moonbase_audio_restart },
/* Accessors */
{ "getAudioChannels", moonbase_audio_get_audio_channels },
{ "getFormat", moonbase_audio_get_format },
{ "getFrequency", moonbase_audio_get_frequency },
{ "getSamples", moonbase_audio_get_samples },
{ "getSoundChannels", moonbase_audio_get_sound_channels },
{ "getVolume", moonbase_audio_get_volume },
/* Mutators */
{ "setAudioChannels", moonbase_audio_set_audio_channels },
{ "setFormat", moonbase_audio_set_format },
{ "setFrequency", moonbase_audio_set_frequency },
{ "setSamples", moonbase_audio_set_samples },
{ "setSoundChannels", moonbase_audio_set_sound_channels },
{ "setVolume", moonbase_audio_set_volume },
{ NULL, NULL }
};
int moonbase_audio_initialize( lua_State *s )
{
luaL_newlib( s, moonbase_audio_methods );
return 1;
}
static int moonbase_channel_pause( lua_State *s )
{
int channel;
channel = *(int*)luaL_checkudata( s, 1, "moonbase_channel" );
channel_pause( channel );
return 0;
}
static int moonbase_channel_resume( lua_State *s )
{
int channel;
channel = *(int*)luaL_checkudata( s, 1, "moonbase_channel" );
channel_resume( channel );
return 0;
}
static int moonbase_channel_stop( lua_State *s )
{
int channel, fade_ms;
channel = *(int*)luaL_checkudata( s, 1, "moonbase_channel" );
fade_ms = luaL_optint( s, 2, 0 );
channel_stop( channel, fade_ms );
return 0;
}
static int moonbase_channel_get_volume( lua_State *s )
{
int channel;
channel = *(int*)luaL_checkudata( s, 1, "moonbase_channel" );
lua_pushnumber( s, channel_get_volume(channel) );
return 1;
}
static int moonbase_channel_set_volume( lua_State *s )
{
int channel;
float volume;
channel = *(int*)luaL_checkudata( s, 1, "moonbase_channel" );
volume = luaL_checknumber( s, 2 );
channel_set_volume( channel, volume );
return 0;
}
static luaL_Reg moonbase_channel_methods[] = {
{ "pause", moonbase_channel_pause },
{ "resume", moonbase_channel_resume },
{ "stop", moonbase_channel_stop },
{ "getVolume", moonbase_channel_get_volume },
{ "setVolume", moonbase_channel_set_volume },
{ NULL, NULL }
};
static int moonbase_sound_play( lua_State *s )
{
void *sound;
int fade_ms, channel;
sound = *(void**)luaL_checkudata( s, 1, "moonbase_sound" );
fade_ms = luaL_optint( s, 2, 0 );
channel = sound_play( sound, fade_ms, 0 );
luacom_create_object( s, "moonbase_channel", &channel, sizeof(channel), moonbase_channel_methods );
return 1;
}
static int moonbase_sound_loop( lua_State *s )
{
void *sound;
int channel, fade_ms;
sound = *(void**)luaL_checkudata( s, 1, "moonbase_sound" );
fade_ms = luaL_optint( s, 2, 0 );
channel = sound_play( sound, fade_ms, 1 );
luacom_create_object( s, "moonbase_channel", &channel, sizeof(channel), moonbase_channel_methods );
return 1;
}
static int moonbase_sound_gc( lua_State *s )
{
void *sound;
sound = *(void**)luaL_checkudata( s, 1, "moonbase_sound" );
asset_release( sound );
return 0;
}
luaL_Reg moonbase_sound_methods[] = {
{ "play", moonbase_sound_play },
{ "loop", moonbase_sound_loop },
{ "__gc", moonbase_sound_gc },
{ NULL, NULL }
};