Sanitize audio.c

This commit is contained in:
Dom Light 2015-11-16 22:39:47 +00:00
parent 3a8b25a0ee
commit b736de003e
4 changed files with 174 additions and 144 deletions

View File

@ -32,25 +32,28 @@
#include "../openrct2.h"
#include "../util/util.h"
int gAudioDeviceCount;
audio_device *gAudioDevices = NULL;
int gAudioDeviceCount;
void *gCrowdSoundChannel = 0;
bool gGameSoundsOff = false;
void *gRainSoundChannel = 0;
rct_ride_music gRideMusicList[AUDIO_MAX_RIDE_MUSIC];
rct_ride_music_info *gRideMusicInfoList[NUM_DEFAULT_MUSIC_TRACKS];
rct_ride_music_params gRideMusicParamsList[AUDIO_MAX_RIDE_MUSIC];
rct_ride_music_params *gRideMusicParamsListEnd;
void *gTitleMusicChannel = 0;
rct_vehicle_sound gVehicleSoundList[AUDIO_MAX_VEHICLE_SOUNDS];
rct_vehicle_sound_params gVehicleSoundParamsList[AUDIO_MAX_VEHICLE_SOUNDS];
rct_vehicle_sound_params *gVehicleSoundParamsListEnd;
rct_ride_music gRideMusicList[AUDIO_MAX_RIDE_MUSIC];
rct_ride_music_params gRideMusicParamsList[AUDIO_MAX_RIDE_MUSIC];
rct_ride_music_params *gRideMusicParamsListEnd;
void *gCrowdSoundChannel = 0;
void *gTitleMusicChannel = 0;
void *gRainSoundChannel = 0;
bool gGameSoundsOff = false;
void audio_init(int i)
void audio_init()
{
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
log_fatal("SDL_Init %s", SDL_GetError());
exit(-1);
}
int result = SDL_Init(SDL_INIT_AUDIO);
if (result >= 0)
return;
log_fatal("SDL_Init %s", SDL_GetError());
exit(-1);
}
void audio_quit()
@ -58,29 +61,29 @@ void audio_quit()
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
/**
* Populates audio devices.
*/
void audio_get_devices()
void audio_populate_devices()
{
int i;
if (gAudioDevices != NULL)
free(gAudioDevices);
gAudioDeviceCount = SDL_GetNumAudioDevices(SDL_FALSE);
if (gAudioDeviceCount > 0) {
gAudioDeviceCount++;
gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device));
if (gAudioDeviceCount <= 0)
return;
safe_strncpy(gAudioDevices[0].name, language_get_string(5510), AUDIO_DEVICE_NAME_SIZE);
for (i = 1; i < gAudioDeviceCount; i++) {
const char *utf8_name = SDL_GetAudioDeviceName(i - 1, SDL_FALSE);
if (utf8_name == NULL)
utf8_name = language_get_string(5511);
gAudioDeviceCount++;
gAudioDevices = malloc(gAudioDeviceCount * sizeof(audio_device));
safe_strncpy(
gAudioDevices[0].name,
language_get_string(5510),
AUDIO_DEVICE_NAME_SIZE
);
safe_strncpy(gAudioDevices[i].name, utf8_name, AUDIO_DEVICE_NAME_SIZE);
}
for (int i = 1; i < gAudioDeviceCount; i++) {
const char *utf8Name = SDL_GetAudioDeviceName(i - 1, SDL_FALSE);
if (utf8Name == NULL)
utf8Name = language_get_string(5511);
safe_strncpy(gAudioDevices[i].name, utf8Name, AUDIO_DEVICE_NAME_SIZE);
}
}
@ -94,69 +97,86 @@ void audio_get_devices()
* @param y (dx)
* @param z (bp)
*/
int audio_sound_play_panned(int sound_id, int ebx, sint16 x, sint16 y, sint16 z)
int audio_sound_play_panned(int soundId, int ebx, sint16 x, sint16 y, sint16 z)
{
if (!gGameSoundsOff) {
int volumedown = 0;
int volume = 0;
if (ebx == 0x8001) {
rct_map_element* mapelement = map_get_surface_element_at(x / 32, y / 32);
if (mapelement) {
if ((mapelement->base_height * 8) - 5 > z) {
volumedown = 10;
}
if (gGameSoundsOff)
return 0;
int volume = 0;
if (ebx == 0x8001) {
int volumeDown = 0;
rct_map_element *element = map_get_surface_element_at(x / 32, y / 32);
if (element && (element->base_height * 8) - 5 > z)
volumeDown = 10;
sint16 rx;
sint16 ry;
switch (get_current_rotation()) {
case 0:
rx = y - x;
ry = ((y + x) / 2) - z;
break;
case 1:
rx = -x - y;
ry = ((y - x) / 2) - z;
break;
case 2:
rx = x - y;
ry = ((-y - x) / 2) - z;
break;
case 3:
rx = y + x;
ry = ((x - y) / 2) - z;
break;
}
rct_window *window = RCT2_GLOBAL(
RCT2_ADDRESS_NEW_WINDOW_PTR,
rct_window*
);
while (true) {
window--;
if (window < RCT2_ADDRESS(RCT2_ADDRESS_WINDOW_LIST, rct_window)) {
break;
}
sint16 rx;
sint16 ry;
switch (get_current_rotation()) {
case 0:
rx = y - x;
ry = ((y + x) / 2) - z;
break;
case 1:
rx = -x - y;
ry = ((y - x) / 2) - z;
break;
case 2:
rx = x - y;
ry = ((-y - x) / 2) - z;
break;
case 3:
rx = y + x;
ry = ((x - y) / 2) - z;
break;
}
rct_window* window = RCT2_GLOBAL(RCT2_ADDRESS_NEW_WINDOW_PTR, rct_window*);
while (1) {
window--;
if (window < RCT2_ADDRESS(RCT2_ADDRESS_WINDOW_LIST, rct_window)) {
break;
}
rct_viewport* viewport = window->viewport;
if (viewport && viewport->flags & VIEWPORT_FLAG_SOUND_ON) {
sint16 vy = ry - viewport->view_y;
sint16 vx = rx - viewport->view_x;
ebx = viewport->x + (vx >> viewport->zoom);
volume = RCT2_ADDRESS(0x0099282C, int)[sound_id] + ((-1024 * viewport->zoom - 1) << volumedown) + 1;
if (vy < 0 || vy >= viewport->view_height || vx < 0 || vx >= viewport->view_width || volume < -10000) {
return sound_id;
}
}
rct_viewport *viewport = window->viewport;
if (viewport && viewport->flags & VIEWPORT_FLAG_SOUND_ON) {
sint16 vy = ry - viewport->view_y;
sint16 vx = rx - viewport->view_x;
ebx = viewport->x + (vx >> viewport->zoom);
volume = RCT2_ADDRESS(0x0099282C, int)[soundId]
+ ((-1024 * viewport->zoom - 1) << volumeDown) + 1;
if (vy < 0
|| vy >= viewport->view_height
|| vx < 0
|| vx >= viewport->view_width
|| volume < -10000)
return soundId;
}
}
int pan;
if (ebx == (sint16)0x8000) {
pan = 0;
} else {
int x2 = ebx << 16;
uint16 screenwidth = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, uint16);
if (screenwidth < 64) {
screenwidth = 64;
}
pan = ((x2 / screenwidth) - 0x8000) >> 4;
}
Mixer_Play_Effect(sound_id, MIXER_LOOP_NONE, DStoMixerVolume(volume), DStoMixerPan(pan), 1, 1);
}
int pan = 0;
if (ebx != (sint16)0x8000) {
int x2 = ebx << 16;
uint16 screenWidth = max(
64,
RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, uint16)
);
pan = ((x2 / screenWidth) - 0x8000) >> 4;
}
Mixer_Play_Effect(
soundId,
MIXER_LOOP_NONE,
DStoMixerVolume(volume),
DStoMixerPan(pan),
1,
1
);
return 0;
}
@ -166,31 +186,33 @@ int audio_sound_play_panned(int sound_id, int ebx, sint16 x, sint16 y, sint16 z)
*/
void audio_start_title_music()
{
int musicPathId;
switch (gConfigSound.title_music) {
default:
if (gGameSoundsOff || !RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TITLE_DEMO) {
audio_stop_title_music();
return;
}
if (gTitleMusicChannel)
return;
int pathId;
switch (gConfigSound.title_music) {
case 1:
musicPathId = PATH_ID_CSS50;
pathId = PATH_ID_CSS50;
break;
case 2:
musicPathId = PATH_ID_CSS17;
pathId = PATH_ID_CSS17;
break;
case 3:
if (rand() & 1)
musicPathId = PATH_ID_CSS50;
pathId = PATH_ID_CSS50;
else
musicPathId = PATH_ID_CSS17;
pathId = PATH_ID_CSS17;
break;
default:
return;
}
if (!gGameSoundsOff && RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_TITLE_DEMO) {
if (!gTitleMusicChannel) {
gTitleMusicChannel = Mixer_Play_Music(musicPathId, MIXER_LOOP_INFINITE, true);
}
} else {
audio_stop_title_music();
}
gTitleMusicChannel = Mixer_Play_Music(pathId, MIXER_LOOP_INFINITE, true);
}
/**
@ -200,13 +222,15 @@ void audio_start_title_music()
void audio_stop_ride_music()
{
for (int i = 0; i < AUDIO_MAX_RIDE_MUSIC; i++) {
rct_ride_music* ride_music = &gRideMusicList[i];
if (ride_music->ride_id != (uint8)-1) {
if (ride_music->sound_channel) {
Mixer_Stop_Channel(ride_music->sound_channel);
}
ride_music->ride_id = -1;
rct_ride_music *rideMusic = &gRideMusicList[i];
if (rideMusic->ride_id == (uint8)-1)
continue;
if (rideMusic->sound_channel) {
Mixer_Stop_Channel(rideMusic->sound_channel);
}
rideMusic->ride_id = -1;
}
}
@ -248,22 +272,22 @@ void stop_rain_sound()
*/
void audio_init1()
{
int devicenum = 0;
audio_init2(devicenum);
int deviceNum = 0;
audio_init2(deviceNum);
for(int m = 0; m < countof(gRideMusicInfoList); m++) {
rct_ride_music_info* ride_music_info = gRideMusicInfoList[m];
const utf8* path = get_file_path(ride_music_info->path_id);
for (int m = 0; m < countof(gRideMusicInfoList); m++) {
rct_ride_music_info *rideMusicInfo = gRideMusicInfoList[m];
const utf8 *path = get_file_path(rideMusicInfo->path_id);
SDL_RWops *file = SDL_RWFromFile(path, "rb");
if (file != NULL) {
uint32 head;
SDL_RWread(file, &head, sizeof(head), 1);
SDL_RWclose(file);
RCT2_GLOBAL(0x014241BC, uint32) = 0;
if (head == 0x78787878) {
ride_music_info->length = 0;
}
}
if (file == NULL)
continue;
uint32 head;
SDL_RWread(file, &head, sizeof(head), 1);
SDL_RWclose(file);
RCT2_GLOBAL(0x014241BC, uint32) = 0;
if (head == 0x78787878)
rideMusicInfo->length = 0;
}
}
@ -275,14 +299,15 @@ void audio_init2(int device)
{
audio_close();
for (int i = 0; i < AUDIO_MAX_VEHICLE_SOUNDS; i++) {
rct_vehicle_sound* vehicle_sound = &gVehicleSoundList[i];
vehicle_sound->id = -1;
rct_vehicle_sound *vehicleSound = &gVehicleSoundList[i];
vehicleSound->id = -1;
}
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_SOUND_DEVICE, uint32) = device;
config_save_default();
for (int i = 0; i < AUDIO_MAX_RIDE_MUSIC; i++) {
rct_ride_music* ride_music = &gRideMusicList[i];
ride_music->ride_id = -1;
rct_ride_music *rideMusic = &gRideMusicList[i];
rideMusic->ride_id = -1;
}
}
@ -302,11 +327,11 @@ void audio_close()
/* rct2: 0x006BAB8A */
void audio_toggle_all_sounds(){
gConfigSound.sound = !gConfigSound.sound;
if (!gConfigSound.sound) {
if (gConfigSound.sound)
audio_unpause_sounds();
else {
audio_stop_title_music();
audio_pause_sounds();
} else {
audio_unpause_sounds();
}
}
@ -338,18 +363,23 @@ void audio_unpause_sounds()
*/
void audio_stop_vehicle_sounds()
{
if (!gOpenRCT2Headless && RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_SOUND_DEVICE, sint32) != -1) {
for (int i = 0; i < countof(gVehicleSoundList); i++) {
rct_vehicle_sound* vehicle_sound = &gVehicleSoundList[i];
if (vehicle_sound->id != 0xFFFF) {
if (vehicle_sound->sound1_id != 0xFFFF) {
Mixer_Stop_Channel(vehicle_sound->sound1_channel);
}
if (vehicle_sound->sound2_id != 0xFFFF) {
Mixer_Stop_Channel(vehicle_sound->sound2_channel);
}
}
vehicle_sound->id = 0xFFFF;
if (gOpenRCT2Headless
|| RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_SOUND_DEVICE, sint32) == -1)
return;
for (int i = 0; i < countof(gVehicleSoundList); i++) {
rct_vehicle_sound *vehicleSound = &gVehicleSoundList[i];
if (vehicleSound->id == 0xFFFF)
continue;
if (vehicleSound->sound1_id != 0xFFFF) {
Mixer_Stop_Channel(vehicleSound->sound1_channel);
}
if (vehicleSound->sound2_id != 0xFFFF) {
Mixer_Stop_Channel(vehicleSound->sound2_channel);
}
vehicleSound->id = 0xFFFF;
}
}

View File

@ -155,7 +155,7 @@ extern void *gCrowdSoundChannel;
extern bool gGameSoundsOff;
extern void *gRainSoundChannel;
extern rct_ride_music gRideMusicList[AUDIO_MAX_RIDE_MUSIC];
extern rct_ride_music_info* gRideMusicInfoList[NUM_DEFAULT_MUSIC_TRACKS];
extern rct_ride_music_info *gRideMusicInfoList[NUM_DEFAULT_MUSIC_TRACKS];
extern rct_ride_music_params gRideMusicParamsList[AUDIO_MAX_RIDE_MUSIC];
extern rct_ride_music_params *gRideMusicParamsListEnd;
extern void *gTitleMusicChannel;
@ -164,11 +164,11 @@ extern rct_vehicle_sound_params gVehicleSoundParamsList[AUDIO_MAX_VEHICLE_SOUNDS
extern rct_vehicle_sound_params *gVehicleSoundParamsListEnd;
void audio_close();
void audio_get_devices();
void audio_init();
void audio_init1();
void audio_init2(int device);
void audio_pause_sounds();
void audio_populate_devices();
void audio_quit();
int audio_sound_play_panned(int soundId, int ebx, sint16 x, sint16 y, sint16 z);
void audio_start_title_music();

View File

@ -249,7 +249,7 @@ bool openrct2_initialise()
get_system_info();
if (!gOpenRCT2Headless) {
audio_init();
audio_get_devices();
audio_populate_devices();
}
if (!language_open(gConfigGeneral.language))
{

View File

@ -852,7 +852,7 @@ static void window_options_mousedown(int widgetIndex, rct_window*w, rct_widget*
case WINDOW_OPTIONS_PAGE_AUDIO:
switch (widgetIndex) {
case WIDX_SOUND_DROPDOWN:
audio_get_devices();
audio_populate_devices();
// populate the list with the sound devices
for (i = 0; i < gAudioDeviceCount; i++) {