Skip to content
Permalink
Browse files

refactor audio (module system like that of renderer)

  • Loading branch information...
Akaricchi committed Mar 5, 2019
1 parent 68111e6 commit abe4ddf260c7ecc66fb6cac77abf24051eb361bd
@@ -146,12 +146,6 @@ if package_data and not enable_zip
error('ZIP support must be enabled for data packaging to work')
endif

if dep_sdl2_mixer.found() and get_option('enable_audio') != 'false'
taisei_deps += dep_sdl2_mixer
elif get_option('enable_audio') == 'true'
error('Audio support enabled but SDL2_mixer not found')
endif

config.set('TAISEI_BUILDCONF_USE_ZIP', taisei_deps.contains(dep_zip))

have_posix = cc.has_header_symbol('unistd.h', '_POSIX_VERSION')
@@ -278,48 +272,55 @@ if meson.is_cross_build()
systype = '@0@ (cross-compiling)'.format(systype)
endif

summary = '''
Summary:
Taisei version: @9@
version_deps = []

System type: @0@
Audio enabled: @1@
Package data: @2@
subdir('misc')
subdir('external')
subdir('resources')
subdir('doc')
subdir('xdg')
subdir('atlas')
subdir('src')

Relative install paths: @3@
Prefix: @4@
Executables: @5@
Data: @6@
Documentation: @7@
summary = '''
Build type: @8@
Summary:
Taisei version: @0@
System type: @1@
Build type: @2@
Audio backends: @3@ (default: @4@)
Renderers: @5@ (default: @6@)
Shader translation: @7@
ZIP support: @8@
Package data: @9@
Relative install paths: @10@
Prefix: @11@
Executables: @12@
Data: @13@
Documentation: @14@
'''.format(
taisei_version_string,
systype,
taisei_deps.contains(dep_sdl2_mixer),
taisei_deps.contains(dep_zip),
get_option('buildtype'),

', '.join(enabled_audio_backends),
get_option('a_default'),
', '.join(enabled_renderers),
get_option('r_default'),
get_option('shader_transpiler'),
enable_zip,
package_data,

config.get('TAISEI_BUILDCONF_RELATIVE_DATA_PATH'),
get_option('prefix'),

# the $ is intentional
join_paths('$prefix', bindir),
join_paths('$prefix', data_path),
join_paths('$prefix', doc_path),

get_option('buildtype'),
taisei_version_string
join_paths('$prefix', doc_path)
)

version_deps = []

subdir('misc')
subdir('external')
subdir('resources')
subdir('doc')
subdir('xdg')
subdir('atlas')
subdir('src')

message(summary)

run_command(postconf_command)
@@ -11,13 +11,6 @@ option(
description : 'Make a "developer" build with cheats and extra debugging features'
)

option(
'enable_audio',
type : 'combo',
choices : ['auto', 'true', 'false'],
description : 'Enable audio support (requires SDL2_mixer)'
)

option(
'enable_zip',
type : 'boolean',
@@ -156,6 +149,27 @@ option(
description : 'Build the no-op renderer (nothing is displayed)'
)

option(
'a_default',
type : 'combo',
choices : ['sdl2mixer', 'null'],
description : 'Which audio backend to use by default'
)

option(
'a_sdl2mixer',
type : 'boolean',
value : true,
description : 'Build the SDL2_Mixer audio backend'
)

option(
'a_null',
type : 'boolean',
value : true,
description : 'Build the no-op audio backend (silence); you want this on!'
)

option(
'objpools',
type : 'boolean',
@@ -12,9 +12,12 @@
#include <stdio.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;
@@ -29,6 +32,10 @@ static struct enqueued_sound {
} *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;
@@ -39,7 +46,7 @@ static void play_sound_internal(const char *name, bool is_ui, int cooldown, bool
return;
}

if(!audio_backend_initialized() || global.frameskip) {
if(global.frameskip) {
return;
}

@@ -51,7 +58,7 @@ static void play_sound_internal(const char *name, bool is_ui, int cooldown, bool

snd->lastplayframe = global.frames;

(replace ? audio_backend_sound_play_or_restart : audio_backend_sound_play)
(replace ? B.sound_play_or_restart : B.sound_play)
(snd->impl, is_ui ? SNDGROUP_UI : SNDGROUP_MAIN);
}

@@ -86,7 +93,7 @@ void play_ui_sound(const char *name) {
}

void play_loop(const char *name) {
if(!audio_backend_initialized() || global.frameskip) {
if(!audio_output_works() || global.frameskip) {
return;
}

@@ -97,9 +104,10 @@ void play_loop(const char *name) {
}

if(!snd->islooping) {
audio_backend_sound_loop(snd->impl, SNDGROUP_MAIN);
B.sound_loop(snd->impl, SNDGROUP_MAIN);
snd->islooping = true;
}

if(snd->islooping == LS_LOOPING) {
snd->lastplayframe = global.frames;
}
@@ -115,7 +123,7 @@ static void* reset_sounds_callback(const char *name, Resource *res, void *arg) {
}

if(snd->islooping && (global.frames > snd->lastplayframe + LOOPTIMEOUTFRAMES || reset)) {
audio_backend_sound_stop_loop(snd->impl);
B.sound_stop_loop(snd->impl);
snd->islooping = LS_FADEOUT;
}

@@ -145,15 +153,15 @@ void update_sounds(void) {
}

void pause_sounds(void) {
audio_backend_sound_pause_all(SNDGROUP_MAIN);
B.sound_pause_all(SNDGROUP_MAIN);
}

void resume_sounds(void) {
audio_backend_sound_resume_all(SNDGROUP_MAIN);
B.sound_resume_all(SNDGROUP_MAIN);
}

void stop_sounds(void) {
audio_backend_sound_stop_all(SNDGROUP_MAIN);
B.sound_stop_all(SNDGROUP_MAIN);
}

Sound* get_sound(const char *name) {
@@ -211,15 +219,15 @@ static void stop_bgm_internal(bool pause, double fadetime) {
stralloc(&current_bgm.name, NULL);
}

if(audio_backend_music_is_playing() && !audio_backend_music_is_paused()) {
if(B.music_is_playing() && !B.music_is_paused()) {
if(pause) {
audio_backend_music_pause();
B.music_pause();
log_debug("BGM paused");
} else if(fadetime > 0) {
audio_backend_music_fade(fadetime);
B.music_fade(fadetime);
log_debug("BGM fading out");
} else {
audio_backend_music_stop();
B.music_stop();
log_debug("BGM stopped");
}
} else {
@@ -255,7 +263,7 @@ void start_bgm(const char *name) {

// if BGM has changed, change it and start from beginning
if(!current_bgm.name || strcmp(name, current_bgm.name)) {
audio_backend_music_stop();
B.music_stop();

stralloc(&current_bgm.name, name);

@@ -268,15 +276,15 @@ void start_bgm(const char *name) {
}
}

if(audio_backend_music_is_paused()) {
audio_backend_music_resume();
if(B.music_is_paused()) {
B.music_resume();
}

if(audio_backend_music_is_playing()) {
if(B.music_is_playing()) {
return;
}

if(!audio_backend_music_play(current_bgm.music->impl)) {
if(!B.music_play(current_bgm.music->impl)) {
return;
}

@@ -293,12 +301,12 @@ void start_bgm(const char *name) {
}

static bool audio_config_updated(SDL_Event *evt, void *arg) {
if (config_get_int(CONFIG_MUTE_AUDIO) == 1) {
audio_backend_set_sfx_volume(0.0);
audio_backend_set_bgm_volume(0.0);
if(config_get_int(CONFIG_MUTE_AUDIO)) {
B.sound_set_global_volume(0.0);
B.music_set_global_volume(0.0);
} else {
audio_backend_set_sfx_volume(config_get_float(CONFIG_SFX_VOLUME));
audio_backend_set_bgm_volume(config_get_float(CONFIG_BGM_VOLUME));
B.sound_set_global_volume(config_get_float(CONFIG_SFX_VOLUME));
B.music_set_global_volume(config_get_float(CONFIG_BGM_VOLUME));
}

return false;
@@ -315,6 +323,14 @@ void audio_init(void) {

void audio_shutdown(void) {
events_unregister_handler(audio_config_updated);
audio_backend_shutdown();
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);
}
@@ -6,8 +6,8 @@
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@alienslab.net>.
*/

#ifndef IGUARD_audio_h
#define IGUARD_audio_h
#ifndef IGUARD_audio_audio_h
#define IGUARD_audio_audio_h

#include "taisei.h"

@@ -19,6 +19,26 @@

#define LOOPFADEOUT 50

typedef struct MusicImpl MusicImpl;
typedef struct SoundImpl SoundImpl;

typedef enum {
LS_OFF,
LS_LOOPING,
LS_FADEOUT,
} LoopState;

typedef struct Sound {
int lastplayframe;
LoopState islooping;
void *impl;
} Sound;

typedef struct Music {
char *title;
MusicImpl *impl;
} Music;

typedef struct CurrentBGM {
char *name;
char *title;
@@ -29,35 +49,10 @@ typedef struct CurrentBGM {

extern CurrentBGM current_bgm;

typedef enum {
SNDGROUP_ALL,
SNDGROUP_MAIN,
SNDGROUP_UI,
} AudioBackendSoundGroup;

void audio_backend_init(void);
void audio_backend_shutdown(void);
bool audio_backend_initialized(void);
void audio_backend_set_sfx_volume(float gain);
void audio_backend_set_bgm_volume(float gain);
bool audio_backend_music_is_paused(void);
bool audio_backend_music_is_playing(void);
void audio_backend_music_resume(void);
void audio_backend_music_stop(void);
void audio_backend_music_fade(double fadetime);
void audio_backend_music_pause(void);
bool audio_backend_music_play(void *impl);
bool audio_backend_music_set_position(double pos);
bool audio_backend_sound_play(void *impl, AudioBackendSoundGroup group);
bool audio_backend_sound_play_or_restart(void *impl, AudioBackendSoundGroup group);
bool audio_backend_sound_loop(void *impl, AudioBackendSoundGroup group);
bool audio_backend_sound_stop_loop(void *impl);
bool audio_backend_sound_pause_all(AudioBackendSoundGroup group);
bool audio_backend_sound_resume_all(AudioBackendSoundGroup group);
bool audio_backend_sound_stop_all(AudioBackendSoundGroup group);

void audio_init(void);
void audio_shutdown(void);
bool audio_output_works(void);
void audio_music_set_position(double pos);

void play_sound(const char *name) attr_nonnull(1);
void play_sound_ex(const char *name, int cooldown, bool replace) attr_nonnull(1);
@@ -82,4 +77,4 @@ void resume_bgm(void);
void save_bgm(void); // XXX: this is broken
void restore_bgm(void); // XXX: this is broken

#endif // IGUARD_audio_h
#endif // IGUARD_audio_audio_h
Oops, something went wrong.

0 comments on commit abe4ddf

Please sign in to comment.
You can’t perform that action at this time.