Skip to content

Commit 4513cb5

Browse files
committed
Changes to the Audio Muffler:
- Add the ability to process sound and music mixers separately - Don't muffle the drowning music
1 parent a4f600a commit 4513cb5

File tree

5 files changed

+93
-43
lines changed

5 files changed

+93
-43
lines changed

src/core/audio.c

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ static float master_volume = DEFAULT_VOLUME; /* a value in [0,1] affecting all m
7676
static float mixer_percentage = DEFAULT_MIXER_PERCENTAGE; /* a value in [0,1] that controls music & sfx volume */
7777
static bool is_globally_muted = false; /* global mute / unmute */
7878
static mufflerprofile_t current_muffler_profile = DEFAULT_MUFFLER_PROFILE;
79-
static bool is_muffler_activated = false;
79+
static int current_muffler_flags = MUFFLE_NOTHING;
8080

8181
static int preload_sample(const char* vpath, void* data);
8282
static void set_master_gain(float gain);
8383
static void handle_haltresume_event(const ALLEGRO_EVENT* event, void* context);
84-
static void set_muffler(mufflerprofile_t profile);
84+
static void update_muffler(mufflerprofile_t profile, int flags);
85+
static void muffle_mixer(ALLEGRO_MIXER* mixer, mufflerprofile_t profile);
8586
static const float* muffler_sigma(mufflerprofile_t profile);
8687
static void muffler_postprocess(void* buf, unsigned int num_samples, void* data);
8788

@@ -575,9 +576,7 @@ void audio_init()
575576
engine_add_event_listener(ALLEGRO_EVENT_DISPLAY_HALT_DRAWING, NULL, handle_haltresume_event);
576577
engine_add_event_listener(ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING, NULL, handle_haltresume_event);
577578

578-
current_muffler_profile = DEFAULT_MUFFLER_PROFILE;
579-
is_muffler_activated = false;
580-
set_muffler(MUFFLER_OFF);
579+
update_muffler(DEFAULT_MUFFLER_PROFILE, MUFFLE_NOTHING);
581580
}
582581

583582
/*
@@ -718,6 +717,15 @@ void audio_set_muted(bool muted)
718717
*
719718
*/
720719

720+
/*
721+
* audio_muffler_profile()
722+
* Get the current profile of the muffler
723+
*/
724+
mufflerprofile_t audio_muffler_profile()
725+
{
726+
return current_muffler_profile;
727+
}
728+
721729
/*
722730
* audio_muffler_set_profile()
723731
* Set the profile of the muffler
@@ -732,46 +740,33 @@ void audio_muffler_set_profile(mufflerprofile_t profile)
732740
logfile_message("Changing the muffler profile to %s", MUFFLER_PROFILE_NAME[profile]);
733741

734742
/* update muffler */
735-
current_muffler_profile = profile;
736-
if(is_muffler_activated)
737-
set_muffler(current_muffler_profile);
743+
update_muffler(profile, current_muffler_flags);
738744
}
739745

740746
/*
741-
* audio_muffler_profile()
742-
* Get the current profile of the muffler
747+
* audio_muffler_is_activated()
748+
* Check whether or not the muffler is activated at this time
743749
*/
744-
mufflerprofile_t audio_muffler_profile()
750+
bool audio_muffler_is_activated()
745751
{
746-
return current_muffler_profile;
752+
return current_muffler_flags != MUFFLE_NOTHING;
747753
}
748754

749755
/*
750756
* audio_muffler_activate()
751-
* Activate or deactivate the muffler at this time
757+
* Activate or deactivate the muffler
752758
*/
753-
void audio_muffler_activate(bool on_off)
759+
void audio_muffler_activate(int flags)
754760
{
755761
/* nothing to do */
756-
if(is_muffler_activated == on_off)
762+
if(current_muffler_flags == flags)
757763
return;
758764

759765
/* update muffler */
760-
is_muffler_activated = on_off;
761-
if(is_muffler_activated)
762-
set_muffler(current_muffler_profile);
763-
else
764-
set_muffler(MUFFLER_OFF);
766+
update_muffler(current_muffler_profile, flags);
765767
}
766768

767-
/*
768-
* audio_muffler_is_activated()
769-
* Check whether or not the muffler is activated at this time
770-
*/
771-
bool audio_muffler_is_activated()
772-
{
773-
return is_muffler_activated;
774-
}
769+
775770

776771

777772

@@ -817,14 +812,42 @@ void handle_haltresume_event(const ALLEGRO_EVENT* event, void* context)
817812
}
818813
}
819814

820-
void set_muffler(mufflerprofile_t profile)
815+
void update_muffler(mufflerprofile_t profile, int flags)
821816
{
822-
size_t num_channels = al_get_channel_count(al_get_mixer_channels(master_mixer));
823-
size_t depth_size = al_get_audio_depth_size(al_get_mixer_depth(master_mixer));
817+
current_muffler_profile = profile;
818+
current_muffler_flags = flags;
819+
820+
/* only one mixer can be muffled at any given time */
821+
if((flags & MUFFLE_EVERYTHING) == MUFFLE_EVERYTHING) {
822+
muffle_mixer(master_mixer, profile);
823+
muffle_mixer(sound_mixer, MUFFLER_OFF);
824+
muffle_mixer(music_mixer, MUFFLER_OFF);
825+
}
826+
else if((flags & MUFFLE_SOUNDS) == MUFFLE_SOUNDS) {
827+
muffle_mixer(master_mixer, MUFFLER_OFF);
828+
muffle_mixer(sound_mixer, profile);
829+
muffle_mixer(music_mixer, MUFFLER_OFF);
830+
}
831+
else if((flags & MUFFLE_MUSICS) == MUFFLE_MUSICS) {
832+
muffle_mixer(master_mixer, MUFFLER_OFF);
833+
muffle_mixer(sound_mixer, MUFFLER_OFF);
834+
muffle_mixer(music_mixer, profile);
835+
}
836+
else {
837+
muffle_mixer(master_mixer, MUFFLER_OFF);
838+
muffle_mixer(sound_mixer, MUFFLER_OFF);
839+
muffle_mixer(music_mixer, MUFFLER_OFF);
840+
}
841+
}
842+
843+
void muffle_mixer(ALLEGRO_MIXER* mixer, mufflerprofile_t profile)
844+
{
845+
size_t num_channels = al_get_channel_count(al_get_mixer_channels(mixer));
846+
size_t depth_size = al_get_audio_depth_size(al_get_mixer_depth(mixer));
824847

825848
if(num_channels != 2 || depth_size != sizeof(float))
826849
logfile_message("Can't set the mixer postprocess callback: num_channels = %u, depth_size = %u, sizeof(float) = %u", num_channels, depth_size, sizeof(float));
827-
else if(!al_set_mixer_postprocess_callback(master_mixer, muffler_postprocess, (void*)muffler_sigma(profile)))
850+
else if(!al_set_mixer_postprocess_callback(mixer, profile != MUFFLER_OFF ? muffler_postprocess : NULL, (void*)muffler_sigma(profile)))
828851
logfile_message("Can't set the mixer postprocess callback.");
829852
}
830853

@@ -850,7 +873,8 @@ const float* muffler_sigma(mufflerprofile_t profile)
850873
}
851874
}
852875

853-
/* this function runs in a dedicated audio thread */
876+
/* this function runs in a dedicated audio thread
877+
Only one mixer can be muffled at any given time - notice the static variables */
854878
void muffler_postprocess(void* buf, unsigned int num_samples, void* data)
855879
{
856880
/* the input buffer is expected to be float32 stereo, where
@@ -861,16 +885,22 @@ void muffler_postprocess(void* buf, unsigned int num_samples, void* data)
861885
NUM_CHANNELS = 2,
862886
DEPTH_SIZE = sizeof(float)
863887
};
864-
static bool is_initialized = false;
865888

866889
/* read input */
867890
float sigma = *((const float*)data); /* no need of mutexes */
868891

892+
#if 0
869893
/* nothing to do */
894+
static bool is_initialized = false;
870895
if(sigma == 0.0f) {
871896
is_initialized = false;
872897
return;
873898
}
899+
#else
900+
/* nothing to do */
901+
if(sigma == 0.0f)
902+
return;
903+
#endif
874904

875905
/* validate */
876906
if(sigma > MAX_SIGMA)
@@ -880,7 +910,7 @@ void muffler_postprocess(void* buf, unsigned int num_samples, void* data)
880910
return;
881911

882912
/* calculate a Gaussian */
883-
static float g0[1 + 2 * (3 * MAX_SIGMA)];
913+
static float g0[1 + 2 * (3 * MAX_SIGMA)] = { 0.0f };
884914
const size_t n = sizeof(g0) / sizeof(float);
885915
const int c = (n-1) / 2;
886916
static int w = -1;
@@ -901,10 +931,13 @@ void muffler_postprocess(void* buf, unsigned int num_samples, void* data)
901931
memcpy(samples, samples + num_samples * NUM_CHANNELS, buf_size);
902932
memcpy(samples + num_samples * NUM_CHANNELS, buf, buf_size);
903933

934+
#if 0
935+
/* do we really need this? no audible difference... */
904936
if(!is_initialized) { /* wait one more frame */
905937
is_initialized = true;
906938
return;
907939
}
940+
#endif
908941

909942
/* find the initial index of the output. We introduce a small delay.
910943
start is an even number (NUM_CHANNELS is 2) and points to a L sample */

src/core/audio.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ enum mufflerprofile_t
8080

8181
void audio_muffler_set_profile(mufflerprofile_t profile);
8282
mufflerprofile_t audio_muffler_profile();
83-
void audio_muffler_activate(bool on_off);
83+
void audio_muffler_activate(int flags);
8484
bool audio_muffler_is_activated();
8585

86+
enum /* muffler flags */
87+
{
88+
MUFFLE_NOTHING = 0,
89+
MUFFLE_SOUNDS = 1,
90+
MUFFLE_MUSICS = 2,
91+
MUFFLE_EVERYTHING = MUFFLE_SOUNDS | MUFFLE_MUSICS
92+
};
93+
8694
#endif

src/entities/waterfx.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../entities/player.h"
3131
#include "../scenes/level.h"
3232
#include "../util/util.h"
33+
#include "../util/stringutil.h"
3334

3435
/* fragment shader */
3536
static const char watershader_fs_glsl[] = ""
@@ -205,7 +206,15 @@ void waterfx_update()
205206
/* enable the muffler if the active player is underwater */
206207
const player_t* player = level_player();
207208
bool is_underwater = player_is_underwater(player);
208-
audio_muffler_activate(is_underwater);
209+
bool is_almost_drowning = music_current() != NULL &&
210+
0 == str_icmp("drowning.ogg", str_basename(music_path(music_current()))); /* hard coded hack :( */
211+
212+
if(!is_underwater)
213+
audio_muffler_activate(MUFFLE_NOTHING);
214+
else if(!is_almost_drowning)
215+
audio_muffler_activate(MUFFLE_EVERYTHING);
216+
else
217+
audio_muffler_activate(MUFFLE_SOUNDS); /* don't muffle the drowning music */
209218
}
210219

211220
/*

src/scenes/level.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ void level_init(void *path_to_lev_file)
12191219
clear_level_state(&saved_state);
12201220
mobilegamepad_fadein();
12211221

1222-
audio_muffler_activate(false);
1222+
audio_muffler_activate(MUFFLE_NOTHING);
12231223
camera_init();
12241224
entitymanager_init();
12251225
create_obstaclemap();
@@ -1288,7 +1288,7 @@ void level_release()
12881288
destroy_obstaclemap();
12891289
entitymanager_release();
12901290
camera_release();
1291-
audio_muffler_activate(false);
1291+
audio_muffler_activate(MUFFLE_NOTHING);
12921292

12931293
clear_level_state(&saved_state);
12941294

@@ -1410,7 +1410,7 @@ void level_update()
14101410

14111411
wants_to_leave = FALSE;
14121412
music_pause();
1413-
audio_muffler_activate(false);
1413+
audio_muffler_activate(MUFFLE_NOTHING);
14141414
scenestack_push(storyboard_get_scene(SCENE_CONFIRMBOX), &cbd);
14151415
return;
14161416
}
@@ -1436,7 +1436,7 @@ void level_update()
14361436
bool block_pause = got_dying_player || (level_timer < 1.0f);
14371437
if(wants_to_pause && !block_pause) {
14381438
wants_to_pause = FALSE;
1439-
audio_muffler_activate(false);
1439+
audio_muffler_activate(MUFFLE_NOTHING);
14401440
scenestack_push(storyboard_get_scene(SCENE_PAUSE), NULL);
14411441
return;
14421442
}

src/scenes/settings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,12 @@ void change_muffler(settings_entry_t* e)
11401140

11411141
void highlight_muffler(settings_entry_t* e)
11421142
{
1143-
audio_muffler_activate(true);
1143+
audio_muffler_activate(MUFFLE_EVERYTHING);
11441144
}
11451145

11461146
void dehighlight_muffler(settings_entry_t* e)
11471147
{
1148-
audio_muffler_activate(false);
1148+
audio_muffler_activate(MUFFLE_NOTHING);
11491149
}
11501150

11511151

0 commit comments

Comments
 (0)