/
audio.c
332 lines (262 loc) · 7.4 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
/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#include "taisei.h"
#include "audio.h"
#include "backend.h"
#include "resource/resource.h"
#include "global.h"
#define B (_a_backend.funcs)
CurrentBGM current_bgm = { .name = NULL };
static char *saved_bgm;
static ht_str2int_t sfx_volumes;
static struct enqueued_sound {
LIST_INTERFACE(struct enqueued_sound);
char *name;
int time;
int cooldown;
bool replace;
} *sound_queue;
static void play_sound_internal(const char *name, bool is_ui, int cooldown, bool replace, int delay) {
if(!audio_output_works()) {
return;
}
if(delay > 0) {
struct enqueued_sound *s = malloc(sizeof(struct enqueued_sound));
s->time = global.frames + delay;
s->name = strdup(name);
s->cooldown = cooldown;
s->replace = replace;
list_push(&sound_queue, s);
return;
}
if(global.frameskip) {
return;
}
Sound *snd = get_sound(name);
if(!snd || (!is_ui && snd->lastplayframe + 3 + cooldown >= global.frames)) {
return;
}
snd->lastplayframe = global.frames;
(replace ? B.sound_play_or_restart : B.sound_play)
(snd->impl, is_ui ? SNDGROUP_UI : SNDGROUP_MAIN);
}
static void* discard_enqueued_sound(List **queue, List *vsnd, void *arg) {
struct enqueued_sound *snd = (struct enqueued_sound*)vsnd;
free(snd->name);
free(list_unlink(queue, vsnd));
return NULL;
}
static void* play_enqueued_sound(struct enqueued_sound **queue, struct enqueued_sound *snd, void *arg) {
play_sound_internal(snd->name, false, snd->cooldown, snd->replace, 0);
free(snd->name);
free(list_unlink(queue, snd));
return NULL;
}
void play_sound(const char *name) {
play_sound_internal(name, false, 0, false, 0);
}
void play_sound_ex(const char *name, int cooldown, bool replace) {
play_sound_internal(name, false, cooldown, replace, 0);
}
void play_sound_delayed(const char *name, int cooldown, bool replace, int delay) {
play_sound_internal(name, false, cooldown, replace, delay);
}
void play_ui_sound(const char *name) {
play_sound_internal(name, true, 0, true, 0);
}
void play_loop(const char *name) {
if(!audio_output_works() || global.frameskip) {
return;
}
Sound *snd = get_sound(name);
if(!snd) {
return;
}
if(!snd->islooping) {
B.sound_loop(snd->impl, SNDGROUP_MAIN);
snd->islooping = true;
}
if(snd->islooping == LS_LOOPING) {
snd->lastplayframe = global.frames;
}
}
static void* reset_sounds_callback(const char *name, Resource *res, void *arg) {
bool reset = (intptr_t)arg;
Sound *snd = res->data;
if(snd) {
if(reset) {
snd->lastplayframe = 0;
}
if(snd->islooping && (global.frames > snd->lastplayframe + LOOPTIMEOUTFRAMES || reset)) {
B.sound_stop_loop(snd->impl);
snd->islooping = LS_FADEOUT;
}
if(snd->islooping && (global.frames > snd->lastplayframe + LOOPTIMEOUTFRAMES + LOOPFADEOUT*60/1000. || reset)) {
snd->islooping = LS_OFF;
}
}
return NULL;
}
void reset_sounds(void) {
resource_for_each(RES_SFX, reset_sounds_callback, (void*)true);
list_foreach(&sound_queue, discard_enqueued_sound, NULL);
}
void update_sounds(void) {
resource_for_each(RES_SFX, reset_sounds_callback, (void*)false);
for(struct enqueued_sound *s = sound_queue, *next; s; s = next) {
next = (struct enqueued_sound*)s->next;
if(s->time <= global.frames) {
play_enqueued_sound(&sound_queue, s, NULL);
}
}
}
void pause_sounds(void) {
B.sound_pause_all(SNDGROUP_MAIN);
}
void resume_sounds(void) {
B.sound_resume_all(SNDGROUP_MAIN);
}
void stop_sounds(void) {
B.sound_stop_all(SNDGROUP_MAIN);
}
Sound* get_sound(const char *name) {
return get_resource_data(RES_SFX, name, RESF_OPTIONAL);
}
Music* get_music(const char *name) {
return get_resource_data(RES_BGM, name, RESF_OPTIONAL);
}
static bool store_sfx_volume(const char *key, const char *val, void *data) {
int vol = atoi(val);
if(vol < 0 || vol > 128) {
log_warn("Volume %i for sfx %s is out of range; must be within [0, 128]", vol, key);
vol = vol < 0 ? 0 : 128;
}
log_debug("Default volume for %s is now %i", key, vol);
if(vol != DEFAULT_SFX_VOLUME) {
ht_set(&sfx_volumes, key, vol);
}
return true;
}
static void load_config_files(void) {
ht_create(&sfx_volumes);
parse_keyvalue_file_cb(SFX_PATH_PREFIX "volumes.conf", store_sfx_volume, NULL);
}
static inline char* get_bgm_title(char *name) {
MusicMetadata *meta = get_resource_data(RES_BGM_METADATA, name, RESF_OPTIONAL);
return meta ? meta->title : NULL;
}
int get_default_sfx_volume(const char *sfx) {
return ht_get(&sfx_volumes, sfx, DEFAULT_SFX_VOLUME);
}
void resume_bgm(void) {
start_bgm(current_bgm.name); // In most cases it just unpauses existing music.
}
static void stop_bgm_internal(bool pause, double fadetime) {
if(!current_bgm.name) {
return;
}
current_bgm.started_at = -1;
if(!pause) {
stralloc(¤t_bgm.name, NULL);
}
if(B.music_is_playing() && !B.music_is_paused()) {
if(pause) {
B.music_pause();
log_debug("BGM paused");
} else if(fadetime > 0) {
B.music_fade(fadetime);
log_debug("BGM fading out");
} else {
B.music_stop();
log_debug("BGM stopped");
}
} else {
log_debug("No BGM was playing");
}
}
void stop_bgm(bool force) {
stop_bgm_internal(!force, false);
}
void fade_bgm(double fadetime) {
stop_bgm_internal(false, fadetime);
}
void save_bgm(void) {
// XXX: this is broken
stralloc(&saved_bgm, current_bgm.name);
}
void restore_bgm(void) {
// XXX: this is broken
start_bgm(saved_bgm);
free(saved_bgm);
saved_bgm = NULL;
}
void start_bgm(const char *name) {
if(!name || !*name) {
stop_bgm(false);
return;
}
// if BGM has changed, change it and start from beginning
if(!current_bgm.name || strcmp(name, current_bgm.name)) {
B.music_stop();
stralloc(¤t_bgm.name, name);
if((current_bgm.music = get_music(name)) == NULL) {
log_warn("BGM '%s' does not exist", current_bgm.name);
stop_bgm(true);
free(current_bgm.name);
current_bgm.name = NULL;
return;
}
}
if(B.music_is_paused()) {
B.music_resume();
}
if(B.music_is_playing()) {
return;
}
if(!B.music_play(current_bgm.music->impl)) {
return;
}
// Support drawing BGM title in game loop (only when music changed!)
if((current_bgm.title = get_bgm_title(current_bgm.name)) != NULL) {
current_bgm.started_at = global.frames;
// Boss BGM title color may differ from the one at beginning of stage
current_bgm.isboss = strendswith(current_bgm.name, "boss");
} else {
current_bgm.started_at = -1;
}
log_info("Started %s", (current_bgm.title ? current_bgm.title : current_bgm.name));
}
static bool audio_config_updated(SDL_Event *evt, void *arg) {
if(config_get_int(CONFIG_MUTE_AUDIO)) {
B.sound_set_global_volume(0.0);
B.music_set_global_volume(0.0);
} else {
B.sound_set_global_volume(config_get_float(CONFIG_SFX_VOLUME));
B.music_set_global_volume(config_get_float(CONFIG_BGM_VOLUME));
}
return false;
}
void audio_init(void) {
load_config_files();
audio_backend_init();
events_register_handler(&(EventHandler) {
audio_config_updated, NULL, EPRIO_SYSTEM, MAKE_TAISEI_EVENT(TE_CONFIG_UPDATED)
});
audio_config_updated(NULL, NULL);
}
void audio_shutdown(void) {
events_unregister_handler(audio_config_updated);
B.shutdown();
ht_destroy(&sfx_volumes);
}
bool audio_output_works(void) {
return B.output_works();
}
void audio_music_set_position(double pos) {
B.music_set_position(pos);
}