mirror of https://github.com/OpenTTD/OpenTTD
Fix: Apply master effect volume during mixing instead of sound start. (#8945)
This makes the volume control work as most players would expect, affecting existing playing sounds as well as new sounds.pull/8959/head
parent
1cd3a3b070
commit
130a052ed5
|
@ -11,6 +11,7 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "core/math_func.hpp"
|
#include "core/math_func.hpp"
|
||||||
#include "framerate_type.h"
|
#include "framerate_type.h"
|
||||||
|
#include "settings_type.h"
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
#include "mixer.h"
|
#include "mixer.h"
|
||||||
|
@ -60,7 +61,7 @@ static int RateConversion(T *b, int frac_pos)
|
||||||
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
|
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
|
||||||
{
|
{
|
||||||
if (samples > sc->samples_left) samples = sc->samples_left;
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
||||||
sc->samples_left -= samples;
|
sc->samples_left -= samples;
|
||||||
|
@ -69,8 +70,8 @@ static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
||||||
const int16 *b = (const int16 *)sc->memory + sc->pos;
|
const int16 *b = (const int16 *)sc->memory + sc->pos;
|
||||||
uint32 frac_pos = sc->frac_pos;
|
uint32 frac_pos = sc->frac_pos;
|
||||||
uint32 frac_speed = sc->frac_speed;
|
uint32 frac_speed = sc->frac_speed;
|
||||||
int volume_left = sc->volume_left;
|
int volume_left = sc->volume_left * effect_vol / 255;
|
||||||
int volume_right = sc->volume_right;
|
int volume_right = sc->volume_right * effect_vol / 255;
|
||||||
|
|
||||||
if (frac_speed == 0x10000) {
|
if (frac_speed == 0x10000) {
|
||||||
/* Special case when frac_speed is 0x10000 */
|
/* Special case when frac_speed is 0x10000 */
|
||||||
|
@ -96,7 +97,7 @@ static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
||||||
sc->pos = b - (const int16 *)sc->memory;
|
sc->pos = b - (const int16 *)sc->memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
|
||||||
{
|
{
|
||||||
if (samples > sc->samples_left) samples = sc->samples_left;
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
||||||
sc->samples_left -= samples;
|
sc->samples_left -= samples;
|
||||||
|
@ -105,8 +106,8 @@ static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
||||||
const int8 *b = sc->memory + sc->pos;
|
const int8 *b = sc->memory + sc->pos;
|
||||||
uint32 frac_pos = sc->frac_pos;
|
uint32 frac_pos = sc->frac_pos;
|
||||||
uint32 frac_speed = sc->frac_speed;
|
uint32 frac_speed = sc->frac_speed;
|
||||||
int volume_left = sc->volume_left;
|
int volume_left = sc->volume_left * effect_vol / 255;
|
||||||
int volume_right = sc->volume_right;
|
int volume_right = sc->volume_right * effect_vol / 255;
|
||||||
|
|
||||||
if (frac_speed == 0x10000) {
|
if (frac_speed == 0x10000) {
|
||||||
/* Special case when frac_speed is 0x10000 */
|
/* Special case when frac_speed is 0x10000 */
|
||||||
|
@ -154,13 +155,15 @@ void MxMixSamples(void *buffer, uint samples)
|
||||||
/* Fetch music if a sampled stream is available */
|
/* Fetch music if a sampled stream is available */
|
||||||
if (_music_stream) _music_stream((int16*)buffer, samples);
|
if (_music_stream) _music_stream((int16*)buffer, samples);
|
||||||
|
|
||||||
|
uint8 effect_vol = _settings_client.music.effect_vol;
|
||||||
|
|
||||||
/* Mix each channel */
|
/* Mix each channel */
|
||||||
for (mc = _channels; mc != endof(_channels); mc++) {
|
for (mc = _channels; mc != endof(_channels); mc++) {
|
||||||
if (mc->active) {
|
if (mc->active) {
|
||||||
if (mc->is16bit) {
|
if (mc->is16bit) {
|
||||||
mix_int16(mc, (int16*)buffer, samples);
|
mix_int16(mc, (int16*)buffer, samples, effect_vol);
|
||||||
} else {
|
} else {
|
||||||
mix_int8_to_int16(mc, (int16*)buffer, samples);
|
mix_int8_to_int16(mc, (int16*)buffer, samples, effect_vol);
|
||||||
}
|
}
|
||||||
if (mc->samples_left == 0) MxCloseChannel(mc);
|
if (mc->samples_left == 0) MxCloseChannel(mc);
|
||||||
}
|
}
|
||||||
|
|
|
@ -252,7 +252,7 @@ static void SndPlayScreenCoordFx(SoundID sound, int left, int right, int top, in
|
||||||
StartSound(
|
StartSound(
|
||||||
sound,
|
sound,
|
||||||
panning,
|
panning,
|
||||||
(_settings_client.music.effect_vol * _vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]) / 256
|
_vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -281,7 +281,7 @@ void SndPlayVehicleFx(SoundID sound, const Vehicle *v)
|
||||||
|
|
||||||
void SndPlayFx(SoundID sound)
|
void SndPlayFx(SoundID sound)
|
||||||
{
|
{
|
||||||
StartSound(sound, 0.5, _settings_client.music.effect_vol);
|
StartSound(sound, 0.5, UINT8_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<SoundsSet>, SoundsSet)
|
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<SoundsSet>, SoundsSet)
|
||||||
|
|
Loading…
Reference in New Issue