1
0
Fork 0

Change: Allow base sounds set to be changed mid-game.

pull/11566/head
Peter Nelson 2023-12-11 00:30:28 +00:00
parent 6dab8884d5
commit 0435e29a06
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
5 changed files with 49 additions and 8 deletions

View File

@ -35,6 +35,7 @@ struct MixerChannel {
};
static std::atomic<uint8_t> _active_channels;
static std::atomic<uint8_t> _stop_channels;
static MixerChannel _channels[8];
static uint32_t _play_rate = 11025;
static uint32_t _max_size = UINT_MAX;
@ -108,6 +109,16 @@ static void MxCloseChannel(uint8_t channel_index)
_active_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
}
/**
* Close all mixer channels.
* This signals to the mixer that each channel should be closed even if it has not played all remaining smaples.
* This is safe (and designed) to be called from the main thread.
*/
void MxCloseAllChannels()
{
_stop_channels.fetch_or(~0, std::memory_order_release);
}
void MxMixSamples(void *buffer, uint samples)
{
PerformanceMeasurer framerate(PFE_SOUND);
@ -126,6 +137,12 @@ void MxMixSamples(void *buffer, uint samples)
if (_music_stream) _music_stream((int16_t*)buffer, samples);
}
/* Check if any channels should be stopped. */
uint8_t stop = _stop_channels.load(std::memory_order_acquire);
for (uint8_t idx : SetBitIterator(stop)) {
MxCloseChannel(idx);
}
/* Apply simple x^3 scaling to master effect volume. This increases the
* perceived difference in loudness to better match expectations. effect_vol
* is expected to be in the range 0-127 hence the division by 127 * 127 to
@ -201,6 +218,7 @@ void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
void MxActivateChannel(MixerChannel *mc)
{
uint8_t channel_index = mc - _channels;
_stop_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
_active_channels.fetch_or((1 << channel_index), std::memory_order_release);
}

View File

@ -27,6 +27,7 @@ MixerChannel *MxAllocateChannel();
void MxSetChannelRawSrc(MixerChannel *mc, const std::shared_ptr<std::vector<byte>> &mem, uint rate, bool is16bit);
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan);
void MxActivateChannel(MixerChannel*);
void MxCloseAllChannels();
uint32_t MxSetMusicSource(MxStreamCallback music_callback);

View File

@ -46,6 +46,7 @@
#include "network/network_gui.h"
#include "network/network_survey.h"
#include "video/video_driver.hpp"
#include "sound_func.h"
#include "safeguards.h"
@ -696,13 +697,7 @@ struct GameOptionsWindow : Window {
break;
case WID_GO_BASE_SFX_DROPDOWN:
if (_game_mode == GM_MENU) {
auto* set = BaseSounds::GetSet(index);
BaseSounds::ini_set = set->name;
BaseSounds::SetSet(set);
this->reload = true;
this->InvalidateData();
}
ChangeSoundSet(index);
break;
case WID_GO_BASE_MUSIC_DROPDOWN:
@ -733,7 +728,6 @@ struct GameOptionsWindow : Window {
this->SetWidgetLoweredState(WID_GO_GUI_SCALE_BEVEL_BUTTON, _settings_client.gui.scale_bevels);
this->SetWidgetDisabledState(WID_GO_BASE_GRF_DROPDOWN, _game_mode != GM_MENU);
this->SetWidgetDisabledState(WID_GO_BASE_SFX_DROPDOWN, _game_mode != GM_MENU);
this->SetWidgetDisabledState(WID_GO_BASE_GRF_PARAMETERS, BaseGraphics::GetUsedSet() == nullptr || !BaseGraphics::GetUsedSet()->IsConfigurable());

View File

@ -331,6 +331,33 @@ void SndCopyToPool()
}
}
/**
* Change the configured sound set and reset sounds.
* @param index Index of sound set to switch to.
*/
void ChangeSoundSet(int index)
{
if (BaseSounds::GetIndexOfUsedSet() == index) return;
auto* set = BaseSounds::GetSet(index);
BaseSounds::ini_set = set->name;
BaseSounds::SetSet(set);
MxCloseAllChannels();
InitializeSound();
/* Replace baseset sounds in the pool with the updated original sounds. This is safe to do as
* any sound still playing holds its own shared_ptr to the sample data. */
for (uint i = 0; i < ORIGINAL_SAMPLE_COUNT; i++) {
SoundEntry *sound = GetSound(i);
if (sound != nullptr && sound->source != SoundSource::NewGRF) {
*sound = _original_sounds[_sound_idx[i]];
sound->volume = _sound_base_vol[i];
sound->priority = 0;
}
}
}
/**
* Decide 'where' (between left and right speaker) to play the sound effect.
* Note: Callers must determine if sound effects are enabled. This plays a sound regardless of the setting.

View File

@ -15,6 +15,7 @@
#include "tile_type.h"
bool LoadSound(SoundEntry &sound, bool new_format, SoundID sound_id, const std::string &name);
void ChangeSoundSet(int index);
void SndPlayTileFx(SoundID sound, TileIndex tile);
void SndPlayVehicleFx(SoundID sound, const Vehicle *v);