From e426c70bbcd65cce177ccafe68a69258ece13e1c Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Mar 2017 17:46:02 +0100 Subject: [PATCH] Get moved audio code linking --- src/openrct2-ui/audio/AudioChannel.cpp | 8 +++--- src/openrct2-ui/audio/AudioContext.cpp | 19 ++++++++++--- src/openrct2-ui/audio/AudioContext.h | 31 +++++++++++++++++++-- src/openrct2-ui/audio/AudioMixer.cpp | 29 ++++++++++--------- src/openrct2-ui/audio/FileAudioSource.cpp | 6 ++-- src/openrct2-ui/audio/MemoryAudioSource.cpp | 6 ++-- src/openrct2/audio/AudioChannel.h | 9 ------ src/openrct2/audio/AudioSource.h | 2 +- src/openrct2/audio/NullAudioSource.cpp | 2 +- 9 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/openrct2-ui/audio/AudioChannel.cpp b/src/openrct2-ui/audio/AudioChannel.cpp index ae2037a534..f92cfab767 100644 --- a/src/openrct2-ui/audio/AudioChannel.cpp +++ b/src/openrct2-ui/audio/AudioChannel.cpp @@ -26,10 +26,10 @@ namespace OpenRCT2 { namespace Audio { - class AudioChannelImpl : public IAudioChannel + class AudioChannelImpl : public ISDLAudioChannel { private: - IAudioSource * _source = nullptr; + ISDLAudioSource * _source = nullptr; SpeexResamplerState * _resampler = nullptr; sint32 _group = MIXER_GROUP_SOUND; @@ -232,7 +232,7 @@ namespace OpenRCT2 { namespace Audio void Play(IAudioSource * source, sint32 loop) override { - _source = source; + _source = static_cast(source); _loop = loop; _offset = 0; _done = false; @@ -290,7 +290,7 @@ namespace OpenRCT2 { namespace Audio } }; - IAudioChannel * AudioChannel::Create() + ISDLAudioChannel * AudioChannel::Create() { return new (std::nothrow) AudioChannelImpl(); } diff --git a/src/openrct2-ui/audio/AudioContext.cpp b/src/openrct2-ui/audio/AudioContext.cpp index bec4079fa6..3487732507 100644 --- a/src/openrct2-ui/audio/AudioContext.cpp +++ b/src/openrct2-ui/audio/AudioContext.cpp @@ -15,23 +15,34 @@ #pragma endregion #include +#include "AudioContext.h" namespace OpenRCT2 { namespace Audio { class AudioContext : public IAudioContext { + private: + IAudioMixer * _audioMixer; + public: - virtual ~AudioContext() + AudioContext() { + _audioMixer = AudioMixer::Create(); } - virtual void SetOutputDevice(const char * deviceName) override + IAudioMixer * GetMixer() override { + return _audioMixer; } - virtual IAudioSource * CreateStreamFromWAV(const std::string &path) override + void SetOutputDevice(const char * deviceName) override { - return nullptr; + _audioMixer->Init(deviceName); + } + + IAudioSource * CreateStreamFromWAV(const std::string &path) override + { + return AudioSource::CreateStreamFromWAV(path); } void StartTitleMusic() override { } diff --git a/src/openrct2-ui/audio/AudioContext.h b/src/openrct2-ui/audio/AudioContext.h index b523e06edd..8f5e83bd35 100644 --- a/src/openrct2-ui/audio/AudioContext.h +++ b/src/openrct2-ui/audio/AudioContext.h @@ -3,11 +3,16 @@ #include #include +#include +#include + +struct SDL_RWops; +struct SpeexResamplerState_; +typedef struct SpeexResamplerState_ SpeexResamplerState; namespace OpenRCT2 { namespace Audio { - struct AudioFormat; - interface IAudioSource; + struct AudioFormat; #pragma pack(push, 1) struct WaveFormat @@ -34,6 +39,18 @@ namespace OpenRCT2 { namespace Audio assert_struct_size(WaveFormatEx, 18); #pragma pack(pop) + interface ISDLAudioSource : public IAudioSource + { + virtual AudioFormat GetFormat() const abstract; + }; + + interface ISDLAudioChannel : public IAudioChannel + { + virtual AudioFormat GetFormat() const abstract; + virtual SpeexResamplerState * GetResampler() const abstract; + virtual void SetResampler(SpeexResamplerState * value) abstract; + }; + namespace AudioSource { IAudioSource * CreateMemoryFromCSS1(const std::string &path, size_t index, const AudioFormat * targetFormat = nullptr); @@ -41,4 +58,14 @@ namespace OpenRCT2 { namespace Audio IAudioSource * CreateStreamFromWAV(const std::string &path); IAudioSource * CreateStreamFromWAV(SDL_RWops * rw); } + + namespace AudioChannel + { + ISDLAudioChannel * Create(); + } + + namespace AudioMixer + { + IAudioMixer * Create(); + } } } diff --git a/src/openrct2-ui/audio/AudioMixer.cpp b/src/openrct2-ui/audio/AudioMixer.cpp index 075ac6a7b4..897d83a903 100644 --- a/src/openrct2-ui/audio/AudioMixer.cpp +++ b/src/openrct2-ui/audio/AudioMixer.cpp @@ -40,7 +40,6 @@ extern "C" namespace OpenRCT2 { namespace Audio { - struct Buffer { private: @@ -74,14 +73,14 @@ namespace OpenRCT2 { namespace Audio } }; - class AudioMixer final : public IAudioMixer + class AudioMixerImpl final : public IAudioMixer { private: IAudioSource * _nullSource = nullptr; SDL_AudioDeviceID _deviceId = 0; AudioFormat _format = { 0 }; - std::list _channels; + std::list _channels; float _volume = 1.0f; float _adjustSoundVolume = 0.0f; float _adjustMusicVolume = 0.0f; @@ -96,18 +95,18 @@ namespace OpenRCT2 { namespace Audio Buffer _effectBuffer; public: - AudioMixer() + AudioMixerImpl() { _nullSource = AudioSource::CreateNull(); } - ~AudioMixer() + ~AudioMixerImpl() { Close(); delete _nullSource; } - void Init(const char* device) override + void Init(const char * device) override { Close(); @@ -118,7 +117,7 @@ namespace OpenRCT2 { namespace Audio want.samples = 1024; want.callback = [](void * arg, uint8 * dst, sint32 length) -> void { - auto mixer = static_cast(arg); + auto mixer = static_cast(arg); mixer->GetNextAudioChunk(dst, (size_t)length); }; want.userdata = this; @@ -182,7 +181,7 @@ namespace OpenRCT2 { namespace Audio IAudioChannel * Play(IAudioSource * source, sint32 loop, bool deleteondone, bool deletesourceondone) override { Lock(); - IAudioChannel * channel = AudioChannel::Create(); + ISDLAudioChannel * channel = AudioChannel::Create(); if (channel != nullptr) { channel->Play(source, loop); @@ -260,11 +259,10 @@ namespace OpenRCT2 { namespace Audio Memory::Set(dst, 0, length); // Mix channels onto output buffer - std::list::iterator it = _channels.begin(); + auto it = _channels.begin(); while (it != _channels.end()) { - IAudioChannel * channel = *it; - + auto channel = *it; sint32 group = channel->GetGroup(); if (group != MIXER_GROUP_SOUND || gConfigSound.sound_enabled) { @@ -297,7 +295,7 @@ namespace OpenRCT2 { namespace Audio } } - void MixChannel(IAudioChannel * channel, uint8 * data, size_t length) + void MixChannel(ISDLAudioChannel * channel, uint8 * data, size_t length) { sint32 byteRate = _format.GetByteRate(); sint32 numSamples = (sint32)(length / byteRate); @@ -372,7 +370,7 @@ namespace OpenRCT2 { namespace Audio * Resample the given buffer into _effectBuffer. * Assumes that srcBuffer is the same format as _format. */ - size_t ApplyResample(IAudioChannel * channel, const void * srcBuffer, sint32 srcSamples, sint32 dstSamples) + size_t ApplyResample(ISDLAudioChannel * channel, const void * srcBuffer, sint32 srcSamples, sint32 dstSamples) { sint32 byteRate = _format.GetByteRate(); @@ -536,4 +534,9 @@ namespace OpenRCT2 { namespace Audio return result; } }; + + IAudioMixer * AudioMixer::Create() + { + return new AudioMixerImpl(); + } } } diff --git a/src/openrct2-ui/audio/FileAudioSource.cpp b/src/openrct2-ui/audio/FileAudioSource.cpp index 70ce35f0d1..498e26aabd 100644 --- a/src/openrct2-ui/audio/FileAudioSource.cpp +++ b/src/openrct2-ui/audio/FileAudioSource.cpp @@ -27,7 +27,7 @@ namespace OpenRCT2 { namespace Audio * An audio source where raw PCM data is streamed directly from * a file. */ - class FileAudioSource final : public IAudioSource + class FileAudioSource final : public ISDLAudioSource { private: AudioFormat _format = { 0 }; @@ -41,12 +41,12 @@ namespace OpenRCT2 { namespace Audio Unload(); } - uint64 GetLength() override + uint64 GetLength() const override { return _dataLength; } - AudioFormat GetFormat() override + AudioFormat GetFormat() const override { return _format; } diff --git a/src/openrct2-ui/audio/MemoryAudioSource.cpp b/src/openrct2-ui/audio/MemoryAudioSource.cpp index efe3d6cc3d..2fbb152b95 100644 --- a/src/openrct2-ui/audio/MemoryAudioSource.cpp +++ b/src/openrct2-ui/audio/MemoryAudioSource.cpp @@ -29,7 +29,7 @@ namespace OpenRCT2 { namespace Audio * An audio source where raw PCM data is initially loaded into RAM from * a file and then streamed. */ - class MemoryAudioSource final : public IAudioSource + class MemoryAudioSource final : public ISDLAudioSource { private: AudioFormat _format = { 0 }; @@ -43,12 +43,12 @@ namespace OpenRCT2 { namespace Audio Unload(); } - uint64 GetLength() override + uint64 GetLength() const override { return _length; } - AudioFormat GetFormat() override + AudioFormat GetFormat() const override { return _format; } diff --git a/src/openrct2/audio/AudioChannel.h b/src/openrct2/audio/AudioChannel.h index 3c1c2ef3df..f5ed6f3998 100644 --- a/src/openrct2/audio/AudioChannel.h +++ b/src/openrct2/audio/AudioChannel.h @@ -32,9 +32,6 @@ namespace OpenRCT2 { namespace Audio virtual IAudioSource * GetSource() const abstract; - // virtual SpeexResamplerState * GetResampler() const abstract; - // virtual void SetResampler(SpeexResamplerState * value) abstract; - virtual sint32 GetGroup() const abstract; virtual void SetGroup(sint32 group) abstract; @@ -74,12 +71,6 @@ namespace OpenRCT2 { namespace Audio virtual void Play(IAudioSource * source, sint32 loop = 0) abstract; virtual void UpdateOldVolume() abstract; - // virtual AudioFormat GetFormat() const abstract; virtual size_t Read(void * dst, size_t len) abstract; }; - - namespace AudioChannel - { - IAudioChannel * Create(); - } } } diff --git a/src/openrct2/audio/AudioSource.h b/src/openrct2/audio/AudioSource.h index 3cdcb193df..52d067d0a0 100644 --- a/src/openrct2/audio/AudioSource.h +++ b/src/openrct2/audio/AudioSource.h @@ -28,7 +28,7 @@ namespace OpenRCT2 { namespace Audio { virtual ~IAudioSource() = default; - virtual uint64 GetLength() abstract; + virtual uint64 GetLength() const abstract; // virtual AudioFormat GetFormat() abstract; virtual size_t Read(void * dst, uint64 offset, size_t len) abstract; }; diff --git a/src/openrct2/audio/NullAudioSource.cpp b/src/openrct2/audio/NullAudioSource.cpp index 92112459d2..4a615ad05d 100644 --- a/src/openrct2/audio/NullAudioSource.cpp +++ b/src/openrct2/audio/NullAudioSource.cpp @@ -24,7 +24,7 @@ namespace OpenRCT2 { namespace Audio class NullAudioSource : public IAudioSource { public: - uint64 GetLength() override + uint64 GetLength() const override { return 0; }