Fix sounds (#228)
* fixes sound volume and cutoff when too far away * adds volume-multipler command line arg
This commit is contained in:
parent
a86a04fcd0
commit
1208a5061a
37
src/S3/3d.c
37
src/S3/3d.c
|
@ -141,16 +141,14 @@ void S3ServiceSoundSources() {
|
|||
}
|
||||
|
||||
int S3UpdateSpatialSound(tS3_channel* chan) {
|
||||
int v1; // eax
|
||||
int close_enough_to_play; // [esp+10h] [ebp-4h]
|
||||
|
||||
if (chan->sound_source_ptr && chan->sound_source_ptr->ambient) {
|
||||
v1 = S3Calculate3D(chan, 1);
|
||||
close_enough_to_play = S3Calculate3D(chan, 1);
|
||||
} else {
|
||||
v1 = S3Calculate3D(chan, 0);
|
||||
close_enough_to_play = S3Calculate3D(chan, 0);
|
||||
}
|
||||
close_enough_to_play = v1;
|
||||
if (v1) {
|
||||
if (close_enough_to_play) {
|
||||
S3SyncSampleVolume(chan);
|
||||
S3SyncSampleRate(chan);
|
||||
}
|
||||
|
@ -346,7 +344,7 @@ tS3_sound_tag S3ServiceSoundSource(tS3_sound_source* src) {
|
|||
|
||||
if (S3Calculate3D(&gS3_channel_template, 1) == 0) {
|
||||
src->tag = 0;
|
||||
src->channel = 0;
|
||||
src->channel = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -496,9 +494,9 @@ int S3Calculate3D(tS3_channel* chan, int pIs_ambient) {
|
|||
if (sound_source_ptr->velocity_ptr) {
|
||||
S3CopyVector3(&chan->velocity, sound_source_ptr->velocity_ptr, sound_source_ptr->brender_vector);
|
||||
} else {
|
||||
chan->velocity.x = (chan->position.x - chan->lastpos.x) / 1000.0 * (double)gS3_service_time_delta;
|
||||
chan->velocity.y = (chan->position.y - chan->lastpos.y) / 1000.0 * (double)gS3_service_time_delta;
|
||||
chan->velocity.z = (chan->position.z - chan->lastpos.z) / 1000.0 * (double)gS3_service_time_delta;
|
||||
chan->velocity.x = (chan->position.x - chan->lastpos.x) / 1000.0f * gS3_service_time_delta;
|
||||
chan->velocity.y = (chan->position.y - chan->lastpos.y) / 1000.0f * gS3_service_time_delta;
|
||||
chan->velocity.z = (chan->position.z - chan->lastpos.z) / 1000.0f * gS3_service_time_delta;
|
||||
chan->lastpos = chan->position;
|
||||
}
|
||||
}
|
||||
|
@ -508,6 +506,7 @@ int S3Calculate3D(tS3_channel* chan, int pIs_ambient) {
|
|||
if (dist_squared < 0) {
|
||||
dist_squared = dist_squared * -1.0;
|
||||
}
|
||||
|
||||
if (chan->pMax_distance_squared < dist_squared) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -517,21 +516,19 @@ int S3Calculate3D(tS3_channel* chan, int pIs_ambient) {
|
|||
dist = sqrtf(dist_squared);
|
||||
}
|
||||
if (pIs_ambient) {
|
||||
v11 = 1.0 - ((chan->position.z - gS3_listener_position_now.z) * (chan->velocity.z - gS3_listener_vel_now.z) + (chan->velocity.y - gS3_listener_vel_now.y) * (chan->position.y - gS3_listener_position_now.y) + (chan->position.x - gS3_listener_position_now.x) * (chan->velocity.x - gS3_listener_vel_now.x)) / dist / flt_531D98;
|
||||
if (v11 <= 2.0f) {
|
||||
if (v11 < 0.5) {
|
||||
v11 = 0.5;
|
||||
}
|
||||
} else {
|
||||
v11 = 2.0;
|
||||
v11 = 1.0f - ((chan->position.z - gS3_listener_position_now.z) * (chan->velocity.z - gS3_listener_vel_now.z) + (chan->velocity.y - gS3_listener_vel_now.y) * (chan->position.y - gS3_listener_position_now.y) + (chan->position.x - gS3_listener_position_now.x) * (chan->velocity.x - gS3_listener_vel_now.x)) / dist / flt_531D98;
|
||||
if (v11 > 2.0f) {
|
||||
v11 = 2.0f;
|
||||
} else if (v11 < 0.5f) {
|
||||
v11 = 0.5;
|
||||
}
|
||||
chan->rate = chan->initial_pitch * v11;
|
||||
} else {
|
||||
chan->rate = chan->initial_pitch;
|
||||
}
|
||||
vol_multiplier = 1.0 / (dist / 6.0 + 1.0);
|
||||
vol_multiplier = 1.0f / (dist / 6.0f + 1.0f);
|
||||
if (!gS3_inside_cockpit) {
|
||||
vol_multiplier = vol_multiplier * 1.3;
|
||||
vol_multiplier = vol_multiplier * 1.3f;
|
||||
}
|
||||
v10 = (chan->position.z - gS3_listener_position_now.z) * gS3_listener_left_now.z
|
||||
+ (chan->position.y - gS3_listener_position_now.y) * gS3_listener_left_now.y
|
||||
|
@ -542,11 +539,11 @@ int S3Calculate3D(tS3_channel* chan, int pIs_ambient) {
|
|||
if (v10 > 1.0) {
|
||||
v10 = v10 - floor(v10);
|
||||
}
|
||||
chan->left_volume = (v10 + 1.0) / 2.0 * ((double)chan->initial_volume * vol_multiplier) * chan->volume_multiplier;
|
||||
chan->left_volume = (v10 + 1.0f) / 2.0f * ((double)chan->initial_volume * vol_multiplier) * chan->volume_multiplier;
|
||||
if (chan->left_volume < 0) {
|
||||
chan->left_volume = 0;
|
||||
}
|
||||
chan->right_volume = (1.0 - v10) / 2.0 * ((double)chan->initial_volume * vol_multiplier) * chan->volume_multiplier;
|
||||
chan->right_volume = (1.0f - v10) / 2.0f * ((double)chan->initial_volume * vol_multiplier) * chan->volume_multiplier;
|
||||
if (chan->right_volume < 0) {
|
||||
chan->right_volume = 0;
|
||||
}
|
||||
|
|
|
@ -710,11 +710,11 @@ void S3Service(int inside_cockpit, int unk1) {
|
|||
} else if (c->spatial_sound && c->active) {
|
||||
if (S3UpdateSpatialSound(c)) {
|
||||
if (c->sound_source_ptr && c->sound_source_ptr->ambient && !S3SoundStillPlaying(c->tag)) {
|
||||
// TODO: S3UpdateSound(0, -1, c->sound_source_ptr, -1.0, -1, -1, 0, -1, -1);
|
||||
S3UpdateSoundSource(NULL, -1, c->sound_source_ptr, -1.0f, -1, -1, 0, -1, -1);
|
||||
}
|
||||
} else if (c->sound_source_ptr) {
|
||||
if (c->sound_source_ptr->ambient) {
|
||||
// TODO: S3UpdateSound(0, -1, c->sound_source_ptr, -1.0, -1, -1, 0, -1, -1);
|
||||
S3UpdateSoundSource(NULL, -1, c->sound_source_ptr, -1.0f, -1, -1, 0, -1, -1);
|
||||
}
|
||||
} else {
|
||||
S3StopChannel(c);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "s3sound.h"
|
||||
#include "audio.h"
|
||||
#include "harness/config.h"
|
||||
#include "miniaudio/miniaudio.h"
|
||||
#include "resource.h"
|
||||
#include <stdio.h>
|
||||
|
@ -15,32 +16,20 @@ tS3_sample_filter* gS3_sample_filter_disable_func;
|
|||
ma_engine engine;
|
||||
|
||||
int S3OpenSampleDevice() {
|
||||
|
||||
ma_result result;
|
||||
|
||||
ma_engine_config engineConfig;
|
||||
engineConfig = ma_engine_config_init();
|
||||
// engineConfig.sampleRate = 22050;
|
||||
engineConfig.sampleRate = 22050;
|
||||
|
||||
result = ma_engine_init(&engineConfig, &engine);
|
||||
if (result != MA_SUCCESS) {
|
||||
printf("Failed to initialize audio engine.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ma_engine_set_volume(&engine, 0.5f);
|
||||
ma_engine_set_volume(&engine, harness_game_config.volume_multiplier);
|
||||
|
||||
// if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) == -1) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// Mix_AllocateChannels(20);
|
||||
|
||||
// if (DirectSoundCreate(0, &gS3_direct_sound_ptr, 0)) {
|
||||
// return 0;
|
||||
// }
|
||||
// if (gS3_direct_sound_ptr->lpVtbl->SetCooperativeLevel(gS3_direct_sound_ptr, hWnd, 3)) {
|
||||
// return 0;
|
||||
// }
|
||||
S3Enable();
|
||||
return 1;
|
||||
}
|
||||
|
@ -311,6 +300,7 @@ int S3SyncSampleVolume(tS3_channel* chan) {
|
|||
|
||||
int volume_db;
|
||||
int pan;
|
||||
float linear_volume;
|
||||
|
||||
if (chan->type != eS3_ST_sample) {
|
||||
return 1;
|
||||
|
@ -325,14 +315,10 @@ int S3SyncSampleVolume(tS3_channel* chan) {
|
|||
volume_db = 0;
|
||||
}
|
||||
|
||||
// if (!sound_buffer->lpVtbl->SetVolume(sound_buffer, v3) {
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// convert from directsound -10000-0 volume scale
|
||||
float linear_vol = 1.0f - (volume_db / -10000.0f);
|
||||
linear_volume = ma_volume_db_to_linear(volume_db / 100.0f);
|
||||
ma_sound_set_volume(chan->descriptor->sound_buffer, linear_volume);
|
||||
|
||||
ma_sound_set_volume(chan->descriptor->sound_buffer, linear_vol);
|
||||
if (chan->spatial_sound) {
|
||||
if (chan->left_volume != 0 && chan->right_volume > chan->left_volume) {
|
||||
pan_ratio = chan->right_volume / (float)chan->left_volume;
|
||||
|
|
|
@ -190,6 +190,8 @@ void Harness_Init(int* argc, char* argv[]) {
|
|||
harness_game_config.demo_timeout = 240000;
|
||||
// disable developer diagnostics by default
|
||||
harness_game_config.enable_diagnostics = 0;
|
||||
// no volume multiplier
|
||||
harness_game_config.volume_multiplier = 1.0f;
|
||||
|
||||
// install signal handler by default
|
||||
harness_game_config.install_signalhandler = 1;
|
||||
|
@ -271,6 +273,11 @@ int Harness_ProcessCommandLine(int* argc, char* argv[]) {
|
|||
} else if (strcasecmp(argv[i], "--enable-diagnostics") == 0) {
|
||||
harness_game_config.enable_diagnostics = 1;
|
||||
handled = 1;
|
||||
} else if (strstr(argv[i], "--volume-multiplier=") != NULL) {
|
||||
char* s = strstr(argv[i], "=");
|
||||
harness_game_config.volume_multiplier = atof(s + 1);
|
||||
LOG_INFO("Volume multiplier set to %f", harness_game_config.volume_multiplier);
|
||||
handled = 1;
|
||||
}
|
||||
|
||||
if (handled) {
|
||||
|
|
|
@ -36,6 +36,7 @@ typedef struct tHarness_game_config {
|
|||
int freeze_timer;
|
||||
unsigned demo_timeout;
|
||||
int enable_diagnostics;
|
||||
float volume_multiplier;
|
||||
|
||||
int install_signalhandler;
|
||||
} tHarness_game_config;
|
||||
|
|
Loading…
Reference in New Issue