mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 19:13:07 +01:00
openrct2-ui Audio clean-up and style/usage fixes (#10115)
* openrct2-ui Audio clean-up and style/usage fixes Co-authored-by: Gymnasiast <m.o.steenbeek@gmail.com> Co-authored-by: Tulio Leao <tupaschoal@gmail.com>
This commit is contained in:
@@ -10,21 +10,20 @@
|
||||
#include "AudioContext.h"
|
||||
#include "AudioFormat.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <openrct2/audio/AudioChannel.h>
|
||||
#include <openrct2/audio/AudioSource.h>
|
||||
#include <openrct2/audio/audio.h>
|
||||
#include <openrct2/common.h>
|
||||
#include <speex/speex_resampler.h>
|
||||
|
||||
namespace OpenRCT2::Audio
|
||||
{
|
||||
class AudioChannelImpl : public ISDLAudioChannel
|
||||
template<typename AudioSource_ = ISDLAudioSource> class AudioChannelImpl : public ISDLAudioChannel
|
||||
{
|
||||
static_assert(std::is_base_of_v<IAudioSource, AudioSource_>);
|
||||
|
||||
private:
|
||||
ISDLAudioSource* _source = nullptr;
|
||||
AudioSource_* _source = nullptr;
|
||||
SpeexResamplerState* _resampler = nullptr;
|
||||
|
||||
int32_t _group = MIXER_GROUP_SOUND;
|
||||
@@ -118,12 +117,12 @@ namespace OpenRCT2::Audio
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual int32_t GetLoop() const override
|
||||
[[nodiscard]] int32_t GetLoop() const override
|
||||
{
|
||||
return _loop;
|
||||
}
|
||||
|
||||
virtual void SetLoop(int32_t value) override
|
||||
void SetLoop(int32_t value) override
|
||||
{
|
||||
_loop = value;
|
||||
}
|
||||
@@ -227,7 +226,7 @@ namespace OpenRCT2::Audio
|
||||
|
||||
void Play(IAudioSource* source, int32_t loop) override
|
||||
{
|
||||
_source = static_cast<ISDLAudioSource*>(source);
|
||||
_source = static_cast<AudioSource_*>(source);
|
||||
_loop = loop;
|
||||
_offset = 0;
|
||||
_done = false;
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace OpenRCT2::Audio
|
||||
|
||||
[[nodiscard]] int32_t BytesPerSample() const
|
||||
{
|
||||
return (SDL_AUDIO_BITSIZE(format)) / 8;
|
||||
return (SDL_AUDIO_BITSIZE(format)) / 8; // NOLINT(hicpp-signed-bitwise)
|
||||
}
|
||||
|
||||
[[nodiscard]] int32_t GetByteRate() const
|
||||
|
||||
@@ -22,9 +22,6 @@
|
||||
#include <openrct2/audio/audio.h>
|
||||
#include <openrct2/common.h>
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/core/Guard.hpp>
|
||||
#include <openrct2/localisation/Localisation.h>
|
||||
#include <openrct2/platform/platform.h>
|
||||
#include <speex/speex_resampler.h>
|
||||
#include <vector>
|
||||
|
||||
@@ -57,7 +54,7 @@ namespace OpenRCT2::Audio
|
||||
_nullSource = AudioSource::CreateNull();
|
||||
}
|
||||
|
||||
~AudioMixerImpl()
|
||||
~AudioMixerImpl() override
|
||||
{
|
||||
Close();
|
||||
delete _nullSource;
|
||||
@@ -246,19 +243,19 @@ namespace OpenRCT2::Audio
|
||||
if (_settingSoundVolume != gConfigSound.sound_volume)
|
||||
{
|
||||
_settingSoundVolume = gConfigSound.sound_volume;
|
||||
_adjustSoundVolume = powf(_settingSoundVolume / 100.f, 10.f / 6.f);
|
||||
_adjustSoundVolume = powf(static_cast<float>(_settingSoundVolume) / 100.f, 10.f / 6.f);
|
||||
}
|
||||
if (_settingMusicVolume != gConfigSound.ride_music_volume)
|
||||
{
|
||||
_settingMusicVolume = gConfigSound.ride_music_volume;
|
||||
_adjustMusicVolume = powf(_settingMusicVolume / 100.f, 10.f / 6.f);
|
||||
_adjustMusicVolume = powf(static_cast<float>(_settingMusicVolume) / 100.f, 10.f / 6.f);
|
||||
}
|
||||
}
|
||||
|
||||
void MixChannel(ISDLAudioChannel* channel, uint8_t* data, size_t length)
|
||||
{
|
||||
int32_t byteRate = _format.GetByteRate();
|
||||
int32_t numSamples = static_cast<int32_t>(length / byteRate);
|
||||
auto numSamples = static_cast<int32_t>(length / byteRate);
|
||||
double rate = 1;
|
||||
if (_format.format == AUDIO_S16SYS)
|
||||
{
|
||||
@@ -283,8 +280,8 @@ namespace OpenRCT2::Audio
|
||||
}
|
||||
|
||||
// Read raw PCM from channel
|
||||
int32_t readSamples = static_cast<int32_t>(numSamples * rate);
|
||||
size_t readLength = static_cast<size_t>(readSamples / cvt.len_ratio) * byteRate;
|
||||
int32_t readSamples = numSamples * rate;
|
||||
auto readLength = static_cast<size_t>(readSamples / cvt.len_ratio) * byteRate;
|
||||
_channelBuffer.resize(readLength);
|
||||
size_t bytesRead = channel->Read(_channelBuffer.data(), readLength);
|
||||
|
||||
@@ -312,7 +309,7 @@ namespace OpenRCT2::Audio
|
||||
// Apply effects
|
||||
if (rate != 1)
|
||||
{
|
||||
int32_t inRate = static_cast<int32_t>(bufferLen / byteRate);
|
||||
auto inRate = static_cast<int32_t>(bufferLen / byteRate);
|
||||
int32_t outRate = numSamples;
|
||||
if (bytesRead != readLength)
|
||||
{
|
||||
@@ -384,7 +381,8 @@ namespace OpenRCT2::Audio
|
||||
int32_t ApplyVolume(const IAudioChannel* channel, void* buffer, size_t len)
|
||||
{
|
||||
float volumeAdjust = _volume;
|
||||
volumeAdjust *= gConfigSound.master_sound_enabled ? (gConfigSound.master_volume / 100.0f) : 0;
|
||||
volumeAdjust *= gConfigSound.master_sound_enabled ? (static_cast<float>(gConfigSound.master_volume) / 100.0f)
|
||||
: 0.0f;
|
||||
|
||||
switch (channel->GetGroup())
|
||||
{
|
||||
@@ -402,14 +400,14 @@ namespace OpenRCT2::Audio
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t startVolume = static_cast<int32_t>(channel->GetOldVolume() * volumeAdjust);
|
||||
int32_t endVolume = static_cast<int32_t>(channel->GetVolume() * volumeAdjust);
|
||||
int32_t startVolume = channel->GetOldVolume() * volumeAdjust;
|
||||
int32_t endVolume = channel->GetVolume() * volumeAdjust;
|
||||
if (channel->IsStopping())
|
||||
{
|
||||
endVolume = 0;
|
||||
}
|
||||
|
||||
int32_t mixVolume = static_cast<int32_t>(channel->GetVolume() * volumeAdjust);
|
||||
int32_t mixVolume = channel->GetVolume() * volumeAdjust;
|
||||
if (startVolume != endVolume)
|
||||
{
|
||||
// Set to max since we are adjusting the volume ourselves
|
||||
@@ -432,7 +430,7 @@ namespace OpenRCT2::Audio
|
||||
|
||||
static void EffectPanS16(const IAudioChannel* channel, int16_t* data, int32_t length)
|
||||
{
|
||||
const float dt = 1.0f / (length * 2);
|
||||
const float dt = 1.0f / static_cast<float>(length * 2.0f);
|
||||
float volumeL = channel->GetOldVolumeL();
|
||||
float volumeR = channel->GetOldVolumeR();
|
||||
const float d_left = dt * (channel->GetVolumeL() - channel->GetOldVolumeL());
|
||||
@@ -440,8 +438,8 @@ namespace OpenRCT2::Audio
|
||||
|
||||
for (int32_t i = 0; i < length * 2; i += 2)
|
||||
{
|
||||
data[i] = static_cast<int16_t>(data[i] * volumeL);
|
||||
data[i + 1] = static_cast<int16_t>(data[i + 1] * volumeR);
|
||||
data[i + 0] = static_cast<int16_t>(volumeL * static_cast<float>(data[i + 0]));
|
||||
data[i + 1] = static_cast<int16_t>(volumeR * static_cast<float>(data[i + 1]));
|
||||
volumeL += d_left;
|
||||
volumeR += d_right;
|
||||
}
|
||||
@@ -456,7 +454,7 @@ namespace OpenRCT2::Audio
|
||||
|
||||
for (int32_t i = 0; i < length * 2; i += 2)
|
||||
{
|
||||
float t = static_cast<float>(i) / (length * 2);
|
||||
float t = static_cast<float>(i) / static_cast<float>(length * 2.0f);
|
||||
data[i] = static_cast<uint8_t>(data[i] * ((1.0 - t) * oldVolumeL + t * volumeL));
|
||||
data[i + 1] = static_cast<uint8_t>(data[i + 1] * ((1.0 - t) * oldVolumeR + t * volumeR));
|
||||
}
|
||||
@@ -471,7 +469,7 @@ namespace OpenRCT2::Audio
|
||||
for (int32_t i = 0; i < length; i++)
|
||||
{
|
||||
float t = static_cast<float>(i) / length;
|
||||
data[i] = static_cast<int16_t>(data[i] * ((1 - t) * startvolume_f + t * endvolume_f));
|
||||
data[i] = static_cast<int16_t>(data[i] * ((1.0f - t) * startvolume_f + t * endvolume_f));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,7 +482,7 @@ namespace OpenRCT2::Audio
|
||||
for (int32_t i = 0; i < length; i++)
|
||||
{
|
||||
float t = static_cast<float>(i) / length;
|
||||
data[i] = static_cast<uint8_t>(data[i] * ((1 - t) * startvolume_f + t * endvolume_f));
|
||||
data[i] = static_cast<uint8_t>(data[i] * ((1.0f - t) * startvolume_f + t * endvolume_f));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,17 +30,17 @@ namespace OpenRCT2::Audio
|
||||
uint64_t _dataLength = 0;
|
||||
|
||||
public:
|
||||
~FileAudioSource()
|
||||
~FileAudioSource() override
|
||||
{
|
||||
Unload();
|
||||
}
|
||||
|
||||
uint64_t GetLength() const override
|
||||
[[nodiscard]] uint64_t GetLength() const override
|
||||
{
|
||||
return _dataLength;
|
||||
}
|
||||
|
||||
AudioFormat GetFormat() const override
|
||||
[[nodiscard]] AudioFormat GetFormat() const override
|
||||
{
|
||||
return _format;
|
||||
}
|
||||
@@ -107,7 +107,7 @@ namespace OpenRCT2::Audio
|
||||
|
||||
uint64_t chunkStart = SDL_RWtell(rw);
|
||||
|
||||
WaveFormat waveFormat;
|
||||
WaveFormat waveFormat{};
|
||||
SDL_RWread(rw, &waveFormat, sizeof(waveFormat), 1);
|
||||
SDL_RWseek(rw, chunkStart + fmtChunkSize, RW_SEEK_SET);
|
||||
if (waveFormat.encoding != pcmformat)
|
||||
@@ -144,7 +144,7 @@ namespace OpenRCT2::Audio
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t FindChunk(SDL_RWops* rw, uint32_t wantedId)
|
||||
static uint32_t FindChunk(SDL_RWops* rw, uint32_t wantedId)
|
||||
{
|
||||
uint32_t subchunkId = SDL_ReadLE32(rw);
|
||||
uint32_t subchunkSize = SDL_ReadLE32(rw);
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
#include <SDL.h>
|
||||
#include <algorithm>
|
||||
#include <openrct2/audio/AudioMixer.h>
|
||||
#include <openrct2/audio/AudioSource.h>
|
||||
#include <openrct2/common.h>
|
||||
#include <vector>
|
||||
@@ -37,7 +36,7 @@ namespace OpenRCT2::Audio
|
||||
}
|
||||
|
||||
public:
|
||||
~MemoryAudioSource()
|
||||
~MemoryAudioSource() override
|
||||
{
|
||||
Unload();
|
||||
}
|
||||
@@ -126,7 +125,7 @@ namespace OpenRCT2::Audio
|
||||
SDL_RWread(rw, &pcmSize, sizeof(pcmSize), 1);
|
||||
_length = pcmSize;
|
||||
|
||||
WaveFormatEx waveFormat;
|
||||
WaveFormatEx waveFormat{};
|
||||
SDL_RWread(rw, &waveFormat, sizeof(waveFormat), 1);
|
||||
_format.freq = waveFormat.frequency;
|
||||
_format.format = AUDIO_S16LSB;
|
||||
|
||||
Reference in New Issue
Block a user