Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 399: Game does not obey sound effects volume level set in sound settings. #400

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/S3/s3sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ int S3SyncSampleVolumeAndPan(tS3_channel* chan) {
float pan_ratio; // [esp+38h] [ebp-8h]
float total_vol; // [esp+3Ch] [ebp-4h]

int volume_db;
int pan;
float linear_volume;

if (chan->type != eS3_ST_sample) {
return 1;
Expand All @@ -362,12 +360,8 @@ int S3SyncSampleVolumeAndPan(tS3_channel* chan) {
total_vol = 1.0f;
}
if (chan->descriptor && chan->descriptor->type == chan->type) {
volume_db = 510.0f / total_vol * -5.0f - 350.0f;
if (volume_db >= 0) {
volume_db = 0;
}

if (AudioBackend_SetVolume(chan->type_struct_sample, volume_db) == eAB_success && chan->spatial_sound) {
if (AudioBackend_SetVolume(chan->type_struct_sample, total_vol) == eAB_success && chan->spatial_sound) {

if (chan->left_volume != 0 && chan->right_volume > chan->left_volume) {
pan_ratio = chan->right_volume / (float)chan->left_volume;
Expand Down
29 changes: 26 additions & 3 deletions src/harness/audio/miniaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ static int kMem_S3_DOS_SOS_channel = 234;
typedef struct tMiniaudio_sample {
ma_audio_buffer_ref buffer_ref;
ma_sound sound;
int init_volume;
int init_pan;
int init_new_rate;
int initialized;
} tMiniaudio_sample;

Expand Down Expand Up @@ -158,6 +161,12 @@ tAudioBackend_error_code AudioBackend_PlaySample(void* type_struct_sample, int c
}
miniaudio->initialized = 1;

if (miniaudio->init_volume > 0) {
AudioBackend_SetVolume(type_struct_sample, miniaudio->init_volume);
AudioBackend_SetPan(type_struct_sample, miniaudio->init_pan);
AudioBackend_SetFrequency(type_struct_sample, rate, miniaudio->init_new_rate);
}

ma_sound_set_looping(&miniaudio->sound, loop);
ma_sound_start(&miniaudio->sound);
return eAB_success;
Expand All @@ -175,15 +184,19 @@ int AudioBackend_SoundIsPlaying(void* type_struct_sample) {
return 0;
}

tAudioBackend_error_code AudioBackend_SetVolume(void* type_struct_sample, int volume_db) {
tAudioBackend_error_code AudioBackend_SetVolume(void* type_struct_sample, int volume) {
tMiniaudio_sample* miniaudio;
float linear_volume;

miniaudio = (tMiniaudio_sample*)type_struct_sample;
assert(miniaudio != NULL);

// convert from directsound -10000 - 0 decibel volume scale
linear_volume = ma_volume_db_to_linear(volume_db / 100.0f);
if (!miniaudio->initialized) {
miniaudio->init_volume = volume;
return eAB_success;
}

linear_volume = volume / 510.0f;
ma_sound_set_volume(&miniaudio->sound, linear_volume);
return eAB_success;
}
Expand All @@ -194,6 +207,11 @@ tAudioBackend_error_code AudioBackend_SetPan(void* type_struct_sample, int pan)
miniaudio = (tMiniaudio_sample*)type_struct_sample;
assert(miniaudio != NULL);

if (!miniaudio->initialized) {
miniaudio->init_pan = pan;
return eAB_success;
}

// convert from directsound -10000 - 10000 pan scale
ma_sound_set_pan(&miniaudio->sound, pan / 10000.0f);
return eAB_success;
Expand All @@ -205,6 +223,11 @@ tAudioBackend_error_code AudioBackend_SetFrequency(void* type_struct_sample, int
miniaudio = (tMiniaudio_sample*)type_struct_sample;
assert(miniaudio != NULL);

if (!miniaudio->initialized) {
miniaudio->init_new_rate = new_rate;
return eAB_success;
}

// convert from directsound frequency to linear pitch scale
ma_sound_set_pitch(&miniaudio->sound, (new_rate / (float)original_rate));
return eAB_success;
Expand Down