From 2d0e7bdfd8b0e05d13c175d99ec230fb681b2a8c Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 10 May 2020 15:05:26 +0200 Subject: [PATCH] openrct2-ui Audio clean-up and style/usage fixes (#10115) * openrct2-ui Audio clean-up and style/usage fixes Co-authored-by: Gymnasiast Co-authored-by: Tulio Leao --- src/openrct2-ui/audio/AudioChannel.cpp | 15 ++++---- src/openrct2-ui/audio/AudioFormat.h | 2 +- src/openrct2-ui/audio/AudioMixer.cpp | 38 ++++++++++----------- src/openrct2-ui/audio/FileAudioSource.cpp | 10 +++--- src/openrct2-ui/audio/MemoryAudioSource.cpp | 5 ++- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/openrct2-ui/audio/AudioChannel.cpp b/src/openrct2-ui/audio/AudioChannel.cpp index c1f7927a4d..a676582ac8 100644 --- a/src/openrct2-ui/audio/AudioChannel.cpp +++ b/src/openrct2-ui/audio/AudioChannel.cpp @@ -10,21 +10,20 @@ #include "AudioContext.h" #include "AudioFormat.h" -#include #include #include -#include #include -#include #include #include namespace OpenRCT2::Audio { - class AudioChannelImpl : public ISDLAudioChannel + template class AudioChannelImpl : public ISDLAudioChannel { + static_assert(std::is_base_of_v); + 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(source); + _source = static_cast(source); _loop = loop; _offset = 0; _done = false; diff --git a/src/openrct2-ui/audio/AudioFormat.h b/src/openrct2-ui/audio/AudioFormat.h index 95a04867d5..c13ac6680e 100644 --- a/src/openrct2-ui/audio/AudioFormat.h +++ b/src/openrct2-ui/audio/AudioFormat.h @@ -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 diff --git a/src/openrct2-ui/audio/AudioMixer.cpp b/src/openrct2-ui/audio/AudioMixer.cpp index a3974a02e7..79e8959eb3 100644 --- a/src/openrct2-ui/audio/AudioMixer.cpp +++ b/src/openrct2-ui/audio/AudioMixer.cpp @@ -22,9 +22,6 @@ #include #include #include -#include -#include -#include #include #include @@ -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(_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(_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(length / byteRate); + auto numSamples = static_cast(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(numSamples * rate); - size_t readLength = static_cast(readSamples / cvt.len_ratio) * byteRate; + int32_t readSamples = numSamples * rate; + auto readLength = static_cast(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(bufferLen / byteRate); + auto inRate = static_cast(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(gConfigSound.master_volume) / 100.0f) + : 0.0f; switch (channel->GetGroup()) { @@ -402,14 +400,14 @@ namespace OpenRCT2::Audio break; } - int32_t startVolume = static_cast(channel->GetOldVolume() * volumeAdjust); - int32_t endVolume = static_cast(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(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(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(data[i] * volumeL); - data[i + 1] = static_cast(data[i + 1] * volumeR); + data[i + 0] = static_cast(volumeL * static_cast(data[i + 0])); + data[i + 1] = static_cast(volumeR * static_cast(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(i) / (length * 2); + float t = static_cast(i) / static_cast(length * 2.0f); data[i] = static_cast(data[i] * ((1.0 - t) * oldVolumeL + t * volumeL)); data[i + 1] = static_cast(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(i) / length; - data[i] = static_cast(data[i] * ((1 - t) * startvolume_f + t * endvolume_f)); + data[i] = static_cast(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(i) / length; - data[i] = static_cast(data[i] * ((1 - t) * startvolume_f + t * endvolume_f)); + data[i] = static_cast(data[i] * ((1.0f - t) * startvolume_f + t * endvolume_f)); } } diff --git a/src/openrct2-ui/audio/FileAudioSource.cpp b/src/openrct2-ui/audio/FileAudioSource.cpp index a4ad9952ed..7d96a9430b 100644 --- a/src/openrct2-ui/audio/FileAudioSource.cpp +++ b/src/openrct2-ui/audio/FileAudioSource.cpp @@ -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); diff --git a/src/openrct2-ui/audio/MemoryAudioSource.cpp b/src/openrct2-ui/audio/MemoryAudioSource.cpp index 5e1751bb1d..841fb5ca2f 100644 --- a/src/openrct2-ui/audio/MemoryAudioSource.cpp +++ b/src/openrct2-ui/audio/MemoryAudioSource.cpp @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -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;