Minor cleanup around audio code

This commit is contained in:
Matt 2020-10-03 02:16:08 +03:00
parent e53cde1d5d
commit 86b1c0dbe1
No known key found for this signature in database
GPG Key ID: 6D4C24A61C93E208
5 changed files with 70 additions and 46 deletions

View File

@ -1374,15 +1374,15 @@ static void window_options_audio_mousedown(rct_window* w, rct_widgetindex widget
audio_populate_devices();
// populate the list with the sound devices
for (size_t i = 0; static_cast<int32_t>(i) < gAudioDeviceCount; i++)
for (size_t i = 0; static_cast<int32_t>(i) < audio_get_device_count(); i++)
{
gDropdownItemsFormat[i] = STR_OPTIONS_DROPDOWN_ITEM;
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(gAudioDevices[i].name);
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(audio_get_device_name(i).c_str());
}
window_options_show_dropdown(w, widget, gAudioDeviceCount);
window_options_show_dropdown(w, widget, audio_get_device_count());
dropdown_set_checked(gAudioCurrentDevice, true);
dropdown_set_checked(audio_get_device_index(), true);
break;
case WIDX_TITLE_MUSIC_DROPDOWN:
uint32_t num_items = 4;
@ -1409,7 +1409,7 @@ static void window_options_audio_dropdown(rct_window* w, rct_widgetindex widgetI
{
case WIDX_SOUND_DROPDOWN:
audio_init_ride_sounds(dropdownIndex);
if (dropdownIndex < gAudioDeviceCount)
if (dropdownIndex < audio_get_device_count())
{
if (dropdownIndex == 0)
{
@ -1418,7 +1418,7 @@ static void window_options_audio_dropdown(rct_window* w, rct_widgetindex widgetI
}
else
{
char* devicename = gAudioDevices[dropdownIndex].name;
const char* devicename = audio_get_device_name(dropdownIndex).c_str();
Mixer_Init(devicename);
SafeFree(gConfigSound.device);
gConfigSound.device = strndup(devicename, AUDIO_DEVICE_NAME_SIZE);
@ -1513,7 +1513,8 @@ static void window_options_audio_invalidate(rct_window* w)
// Sound device
rct_string_id audioDeviceStringId = STR_OPTIONS_SOUND_VALUE_DEFAULT;
const char* audioDeviceName = nullptr;
if (gAudioCurrentDevice == -1)
const int32_t currentDeviceIndex = audio_get_device_index();
if (currentDeviceIndex == -1)
{
audioDeviceStringId = STR_SOUND_NONE;
}
@ -1521,14 +1522,14 @@ static void window_options_audio_invalidate(rct_window* w)
{
audioDeviceStringId = STR_STRING;
#ifndef __linux__
if (gAudioCurrentDevice == 0)
if (currentDeviceIndex == 0)
{
audioDeviceStringId = STR_OPTIONS_SOUND_VALUE_DEFAULT;
}
#endif // __linux__
if (audioDeviceStringId == STR_STRING)
{
audioDeviceName = gAudioDevices[gAudioCurrentDevice].name;
audioDeviceName = audio_get_device_name(currentDeviceIndex).c_str();
}
}

View File

@ -28,6 +28,7 @@
#include "AudioMixer.h"
#include <algorithm>
#include <vector>
using namespace OpenRCT2::Audio;
@ -38,9 +39,8 @@ struct AudioParams
int32_t pan;
};
audio_device* gAudioDevices = nullptr;
int32_t gAudioDeviceCount;
int32_t gAudioCurrentDevice = -1;
static std::vector<std::string> _audioDevices;
static int32_t _currentAudioDevice = -1;
bool gGameSoundsOff = false;
int32_t gVolumeAdjustZoom = 0;
@ -125,23 +125,36 @@ static int32_t SoundVolumeAdjust[RCT2SoundCount] =
static AudioParams audio_get_params_from_location(SoundId soundId, const CoordsXYZ& location);
bool audio_is_available()
{
if (_currentAudioDevice == -1)
return false;
if (gGameSoundsOff)
return false;
if (!gConfigSound.sound_enabled)
return false;
if (gOpenRCT2Headless)
return false;
return true;
}
void audio_init()
{
if (str_is_null_or_empty(gConfigSound.device))
{
Mixer_Init(nullptr);
gAudioCurrentDevice = 0;
_currentAudioDevice = 0;
}
else
{
Mixer_Init(gConfigSound.device);
audio_populate_devices();
for (int32_t i = 0; i < gAudioDeviceCount; i++)
for (int32_t i = 0; i < audio_get_device_count(); i++)
{
if (String::Equals(gAudioDevices[i].name, gConfigSound.device))
if (_audioDevices[i] == gConfigSound.device)
{
gAudioCurrentDevice = i;
_currentAudioDevice = i;
}
}
}
@ -149,8 +162,6 @@ void audio_init()
void audio_populate_devices()
{
SafeFree(gAudioDevices);
auto audioContext = OpenRCT2::GetContext()->GetAudioContext();
std::vector<std::string> devices = audioContext->GetOutputDevices();
@ -169,18 +180,12 @@ void audio_populate_devices()
devices.insert(devices.begin(), defaultDevice);
#endif
gAudioDeviceCount = static_cast<int32_t>(devices.size());
gAudioDevices = Memory::AllocateArray<audio_device>(gAudioDeviceCount);
for (int32_t i = 0; i < gAudioDeviceCount; i++)
{
auto device = &gAudioDevices[i];
String::Set(device->name, sizeof(device->name), devices[i].c_str());
}
_audioDevices = devices;
}
void audio_play_sound_at_location(SoundId soundId, const CoordsXYZ& loc)
{
if (gGameSoundsOff)
if (!audio_is_available())
return;
AudioParams params = audio_get_params_from_location(soundId, loc);
@ -311,6 +316,22 @@ void audio_stop_all_music_and_sounds()
audio_stop_weather_sound();
}
int32_t audio_get_device_count()
{
return static_cast<int32_t>(_audioDevices.size());
}
const std::string& audio_get_device_name(int32_t index)
{
Guard::Assert(index >= 0 && index < audio_get_device_count());
return _audioDevices[index];
}
int32_t audio_get_device_index()
{
return _currentAudioDevice;
}
void audio_stop_title_music()
{
if (gTitleMusicChannel != nullptr)
@ -368,7 +389,7 @@ void audio_init_ride_sounds(int32_t device)
vehicleSound.id = SOUND_ID_NULL;
}
gAudioCurrentDevice = device;
_currentAudioDevice = device;
config_save_default();
for (auto& rideMusic : gRideMusicList)
{
@ -382,7 +403,7 @@ void audio_close()
audio_stop_title_music();
audio_stop_ride_music();
audio_stop_weather_sound();
gAudioCurrentDevice = -1;
_currentAudioDevice = -1;
}
void audio_toggle_all_sounds()
@ -417,10 +438,8 @@ void audio_unpause_sounds()
void audio_stop_vehicle_sounds()
{
if (gAudioCurrentDevice == -1)
{
if (!audio_is_available())
return;
}
for (auto& vehicleSound : gVehicleSoundList)
{

View File

@ -23,11 +23,6 @@
enum class SoundId : uint8_t;
struct CoordsXYZ;
struct audio_device
{
char name[AUDIO_DEVICE_NAME_SIZE];
};
struct rct_ride_music
{
ride_id_t ride_id;
@ -153,10 +148,6 @@ enum class SoundId : uint8_t
constexpr uint8_t RCT2SoundCount = static_cast<uint32_t>(SoundId::Portcullis) + 1;
extern audio_device* gAudioDevices;
extern int32_t gAudioDeviceCount;
extern int32_t gAudioCurrentDevice;
extern bool gGameSoundsOff;
extern int32_t gVolumeAdjustZoom;
@ -170,6 +161,22 @@ extern rct_ride_music_params* gRideMusicParamsListEnd;
extern rct_vehicle_sound gVehicleSoundList[AUDIO_MAX_VEHICLE_SOUNDS];
/**
* Returns false when no audio device is available or when audio is turned off, otherwise true.
*/
bool audio_is_available();
/*
* Returns the amount of available audio devices.
*/
int32_t audio_get_device_count();
/**
* Returns the device name by index.
*/
const std::string& audio_get_device_name(int32_t index);
/**
* Returns the currently used device index, -1 if not available.
*/
int32_t audio_get_device_index();
/**
* Deregisters the audio device.
* rct2: 0x006BAB21

View File

@ -1268,7 +1268,7 @@ static void UpdateSound(const SoundId id, int32_t volume, rct_vehicle_sound_para
*/
void vehicle_sounds_update()
{
if (gAudioCurrentDevice == -1 || gGameSoundsOff || !gConfigSound.sound_enabled || gOpenRCT2Headless)
if (!audio_is_available())
return;
std::vector<rct_vehicle_sound_params> vehicleSoundParamsList;

View File

@ -205,12 +205,9 @@ void climate_force_weather(uint8_t weather)
void climate_update_sound()
{
if (gAudioCurrentDevice == -1)
return;
if (gGameSoundsOff)
return;
if (!gConfigSound.sound_enabled)
if (!audio_is_available())
return;
if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
return;