Skip to content

Commit

Permalink
Wire in new audio mixer api
Browse files Browse the repository at this point in the history
  • Loading branch information
katajakasa committed Jan 7, 2024
1 parent 8894e94 commit a0624b3
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 164 deletions.
99 changes: 86 additions & 13 deletions src/audio/audio.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,93 @@
#ifndef AUDIO_H
#define AUDIO_H

#include "audio/music.h"
#include "audio/sink.h"
#include "audio/sound.h"
#include "audio/source.h"
#include "audio/sources/dumb_source.h"
#include "audio/sources/vorbis_source.h"
#include "audio/stream.h"

int audio_is_sink_available(const char *sink_name);
const char *audio_get_first_sink_name();
int audio_init(const char *sink_name);
void audio_render();
#include <stdbool.h>

#include "resources/ids.h"

#define VOLUME_DEFAULT 1.0f
#define PANNING_DEFAULT 0.0f
#define PITCH_DEFAULT 1.0f

#define VOLUME_MAX 1.0f
#define PANNING_MAX 1.0f
#define PITCH_MAX 2.0f

#define VOLUME_MIN 0.0f
#define PANNING_MIN -1.0f
#define PITCH_MIN 0.5f

typedef struct audio_mod_resampler {
int internal_id;
int is_default;
const char *name;
} audio_mod_resampler;

typedef struct audio_freq {
int freq;
int is_default;
const char *name;
} audio_freq;

/**
* Initializes the audio subsystem
*
* @param freq Wanted output frequency (48000 should be fine)
* @param mono True if 1 channel, False for 2.
* @param resampler Music module resampler interpolation
* @param music_volume Initial music volume
* @param sound_volume Initial audio volume
* @return True if initialized, false if not.
*/
bool audio_init(int freq, bool mono, int resampler, float music_volume, float sound_volume);

/**
* Closes the audio subsystem.
*/
void audio_close();

audio_sink *audio_get_sink();
/**
* Plays sound with given parameters.
*
* @param id Sound resource identifier
* @param volume Volume 0.0f ... 1.0f
* @param panning Sound panning -1.0f ... 1.0f
* @param pitch Sound pitch 0.0f ... n
*/
void audio_play_sound(int id, float volume, float panning, float pitch);

/**
* Starts background music playback. If there is something already playing,
* switches to new track.
* @param id Music file resource identifier.
*/
void audio_play_music(resource_id id);

/**
* Stops music playback.
*/
void audio_stop_music();

/**
* Set music volume
* @param volume 0.0f ... 1.0f
*/
void audio_set_music_volume(float volume);

/**
* Set sound volume 0.0f ... 1.0f
* @param volume
*/
void audio_set_sound_volume(float volume);

/**
* Get supported audio frequencies list
*/
const audio_freq *audio_get_freqs();

/**
* Get supported music resamplers list
*/
const audio_mod_resampler *audio_get_resamplers();

#endif // AUDIO_H
4 changes: 2 additions & 2 deletions src/console/console_cmd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "audio/music.h"
#include "audio/audio.h"
#include "console/console.h"
#include "console/console_type.h"
#include "game/scenes/arena.h"
Expand Down Expand Up @@ -228,7 +228,7 @@ int console_cmd_music(game_state *gs, int argc, char **argv) {
if(argc == 2) {
int i;
if(strtoint(argv[1], &i)) {
music_play(PSM_END + i);
audio_play_music(PSM_END + i);
}
}
return 0;
Expand Down
47 changes: 13 additions & 34 deletions src/engine.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "engine.h"
#include "audio/audio.h"
#include "audio/music.h"
#include "console/console.h"
#include "formats/altpal.h"
#include "game/game_state.h"
Expand Down Expand Up @@ -30,42 +29,27 @@ int engine_init() {
int vsync = setting->video.vsync;
int scale_factor = setting->video.scale_factor;
char *scaler = setting->video.scaler;
const char *audiosink = setting->sound.sink;
int frequency = setting->sound.music_frequency;
int resampler = setting->sound.music_resampler;
bool mono = setting->sound.music_mono;
float music_volume = setting->sound.music_vol / 10.0;
float sound_volume = setting->sound.sound_vol / 10.0;

// Initialize everything.
if(video_init(w, h, fs, vsync, scaler, scale_factor)) {
if(video_init(w, h, fs, vsync, scaler, scale_factor))
goto exit_0;
}
if(!audio_is_sink_available(audiosink)) {
const char *prev_sink = audiosink;
audiosink = audio_get_first_sink_name();
if(audiosink == NULL) {
INFO("Could not find requested sink '%s'. No other sinks available; disabling audio.", prev_sink);
} else {
INFO("Could not find requested sink '%s'. Falling back to '%s'.", prev_sink, audiosink);
}
}
if(audio_init(audiosink)) {
if(!audio_init(frequency, mono, resampler, music_volume, sound_volume))
goto exit_1;
}
sound_set_volume(setting->sound.sound_vol / 10.0f);
music_set_volume(setting->sound.music_vol / 10.0f);

if(sounds_loader_init()) {
if(sounds_loader_init())
goto exit_2;
}
if(lang_init()) {
if(lang_init())
goto exit_3;
}
if(fonts_init()) {
if(fonts_init())
goto exit_4;
}
if(altpals_init()) {
if(altpals_init())
goto exit_5;
}
if(console_init()) {
if(console_init())
goto exit_6;
}

// Return successfully
run = 1;
Expand Down Expand Up @@ -118,7 +102,7 @@ void engine_run(engine_init_flags *init_flags) {
}

// apply volume settings
sound_set_volume(settings_get()->sound.sound_vol / 10.0f);
audio_set_sound_volume(settings_get()->sound.sound_vol / 10.0f);

// Set up game
game_state *gs = omf_calloc(1, sizeof(game_state));
Expand Down Expand Up @@ -253,11 +237,6 @@ void engine_run(engine_init_flags *init_flags) {
dynamic_wait -= game_state_ms_per_dyntick(gs);
}

// Handle audio
if(!visual_debugger) {
audio_render();
}

// Do the actual video rendering jobs
if(enable_screen_updates) {

Expand Down
4 changes: 2 additions & 2 deletions src/game/gui/menu.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "game/gui/menu.h"
#include "audio/sound.h"
#include "audio/audio.h"
#include "game/gui/menu_background.h"
#include "game/gui/sizer.h"
#include "utils/allocator.h"
Expand Down Expand Up @@ -158,7 +158,7 @@ static int menu_action(component *mc, int action) {

} while(component_is_disabled(c));
// Play menu sound
sound_play(19, 0.5f, 0.0f, 2.0f);
audio_play_sound(19, 0.5f, 0.0f, 2.0f);
component_select(c, 1);
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/gui/textbutton.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string.h>

#include "audio/sound.h"
#include "audio/audio.h"
#include "game/gui/menu_background.h"
#include "game/gui/textbutton.h"
#include "game/gui/widget.h"
Expand Down Expand Up @@ -81,7 +81,7 @@ static int textbutton_action(component *c, int action) {
if(tb->click_cb) {
tb->click_cb(c, tb->userdata);
}
sound_play(20, 0.5f, 0.0f, 2.0f);
audio_play_sound(20, 0.5f, 0.0f, 2.0f);
return 0;
}
return 1;
Expand Down
6 changes: 3 additions & 3 deletions src/game/gui/textselector.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>

#include "audio/sound.h"
#include "audio/audio.h"
#include "game/gui/textselector.h"
#include "game/gui/widget.h"
#include "utils/allocator.h"
Expand Down Expand Up @@ -87,7 +87,7 @@ static int textselector_action(component *c, int action) {
if(tb->toggle) {
tb->toggle(c, tb->userdata, *tb->pos);
}
sound_play(20, 0.5f, 0.5f, 2.0f);
audio_play_sound(20, 0.5f, 0.5f, 2.0f);
// reset ticks so text is bright
tb->ticks = 0;
tb->dir = 0;
Expand All @@ -103,7 +103,7 @@ static int textselector_action(component *c, int action) {
if(tb->toggle) {
tb->toggle(c, tb->userdata, *tb->pos);
}
sound_play(20, 0.5f, -0.5f, 2.0f);
audio_play_sound(20, 0.5f, -0.5f, 2.0f);
// reset ticks so text is bright
tb->ticks = 0;
tb->dir = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/game/gui/textslider.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string.h>

#include "audio/sound.h"
#include "audio/audio.h"
#include "game/gui/textslider.h"
#include "game/gui/widget.h"
#include "utils/allocator.h"
Expand Down Expand Up @@ -57,7 +57,7 @@ static int textslider_action(component *c, int action) {
*tb->pos = tb->positions;
} else {
// Play menu sound
sound_play(20, 0.5f, 0.5f, 2.0f);
audio_play_sound(20, 0.5f, 0.5f, 2.0f);
}
if(tb->slide) {
tb->slide(c, tb->userdata, *tb->pos);
Expand All @@ -72,7 +72,7 @@ static int textslider_action(component *c, int action) {
*tb->pos = 0;
} else {
// Play menu sound
sound_play(20, 0.5f, -0.5f, 2.0f);
audio_play_sound(20, 0.5f, -0.5f, 2.0f);
}
if(tb->slide) {
tb->slide(c, tb->userdata, *tb->pos);
Expand Down
6 changes: 3 additions & 3 deletions src/game/objects/har.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>

#include "audio/sound.h"
#include "audio/audio.h"
#include "controller/controller.h"
#include "formats/af.h"
#include "game/common_defines.h"
Expand Down Expand Up @@ -452,7 +452,7 @@ void har_floor_landing_effects(object *obj) {
// Landing sound
float d = ((float)obj->pos.x) / 640.0f;
float pos_pan = d - 0.25f;
sound_play(56, 0.3f, pos_pan, 2.2f);
audio_play_sound(56, 0.3f, pos_pan, 2.2f);
}

void har_move(object *obj) {
Expand Down Expand Up @@ -780,7 +780,7 @@ void har_block(object *obj, vec2i hit_coord) {
object_set_layers(scrape, LAYER_SCRAP);
object_dynamic_tick(scrape);
object_dynamic_tick(scrape);
sound_play(3, 0.7f, 0.5f, 1.0f);
audio_play_sound(3, 0.7f, 0.5f, 1.0f);
game_state_add_object(obj->gs, scrape, RENDER_LAYER_MIDDLE, 0, 0);
h->damage_received = 1;
if(h->state == STATE_CROUCHBLOCK) {
Expand Down
12 changes: 5 additions & 7 deletions src/game/protos/player.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include <assert.h>
#include <math.h>

#include "audio/music.h"
#include "audio/sink.h"
#include "audio/sound.h"
#include "audio/audio.h"
#include "formats/error.h"
#include "formats/script.h"
#include "game/game_player.h"
Expand Down Expand Up @@ -480,13 +478,13 @@ void player_run(object *obj) {
// Music playback
if(sd_script_isset(frame, "smo")) {
if(sd_script_get(frame, "smo") == 0) {
music_stop();
audio_stop_music();
return;
}
music_play(PSM_END + (sd_script_get(frame, "smo") - 1));
audio_play_music(PSM_END + (sd_script_get(frame, "smo") - 1));
}
if(sd_script_isset(frame, "smf")) {
music_stop();
audio_stop_music();
}

// Sound playback
Expand All @@ -507,7 +505,7 @@ void player_run(object *obj) {
}
if(obj->sound_translation_table) {
int sound_id = obj->sound_translation_table[sd_script_get(frame, "s")] - 1;
sound_play(sound_id, volume, panning, pitch);
audio_play_sound(sound_id, volume, panning, pitch);
}
}

Expand Down
Loading

0 comments on commit a0624b3

Please sign in to comment.