From 03439bb4317d69e6187dd1ffa41c80e074c66d1d Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 26 Sep 2023 13:08:54 +0800 Subject: [PATCH] pref: move sound preload functions to SoundPlayer --- src/kiwano-audio/AudioModule.cpp | 4 +-- src/kiwano-audio/Sound.cpp | 54 +++++--------------------------- src/kiwano-audio/Sound.h | 21 ++++++------- src/kiwano-audio/SoundPlayer.cpp | 51 ++++++++++++++++++++++++++++-- src/kiwano-audio/SoundPlayer.h | 15 +++++++++ src/kiwano-audio/Transcoder.h | 51 +----------------------------- 6 files changed, 82 insertions(+), 114 deletions(-) diff --git a/src/kiwano-audio/AudioModule.cpp b/src/kiwano-audio/AudioModule.cpp index cfc3c961..2e154a94 100644 --- a/src/kiwano-audio/AudioModule.cpp +++ b/src/kiwano-audio/AudioModule.cpp @@ -32,7 +32,7 @@ namespace audio class VoiceCallback : public IXAudio2VoiceCallback { public: - SoundCallback* cb; + SoundCallback* cb = nullptr; VoiceCallback(SoundCallback* cb) : cb(cb) @@ -99,8 +99,6 @@ void AudioModule::DestroyModule() { KGE_DEBUG_LOGF("Destroying audio resources"); - TranscoderCache::GetInstance().Clear(); - if (mastering_voice_) { mastering_voice_->DestroyVoice(); diff --git a/src/kiwano-audio/Sound.cpp b/src/kiwano-audio/Sound.cpp index ba9f2275..50edd203 100644 --- a/src/kiwano-audio/Sound.cpp +++ b/src/kiwano-audio/Sound.cpp @@ -27,52 +27,6 @@ namespace kiwano namespace audio { -SoundPtr Sound::Preload(const String& file_path, std::initializer_list callbacks) -{ - auto ptr = MakePtr(); - for (auto& cb : callbacks) - { - ptr->AddCallback(cb); - } - - size_t hash_code = std::hash{}(file_path); - if (TranscoderPtr transcoder = TranscoderCache::GetInstance().Get(hash_code)) - { - if (ptr->Load(transcoder)) - return ptr; - return nullptr; - } - - if (ptr && ptr->Load(file_path)) - { - TranscoderCache::GetInstance().Add(hash_code, ptr->coder_); - } - return ptr; -} - -SoundPtr Sound::Preload(const Resource& res, std::initializer_list callbacks) -{ - auto ptr = MakePtr(); - for (auto& cb : callbacks) - { - ptr->AddCallback(cb); - } - - size_t hash_code = res.GetId(); - if (TranscoderPtr transcoder = TranscoderCache::GetInstance().Get(hash_code)) - { - if (ptr->Load(transcoder)) - return ptr; - return nullptr; - } - - if (ptr && ptr->Load(res)) - { - TranscoderCache::GetInstance().Add(hash_code, ptr->coder_); - } - return ptr; -} - Sound::Sound(const String& file_path) : Sound() { @@ -85,6 +39,12 @@ Sound::Sound(const Resource& res) Load(res); } +Sound::Sound(TranscoderPtr transcoder) + : Sound() +{ + Load(transcoder); +} + Sound::Sound() : opened_(false) , playing_(false) @@ -308,7 +268,7 @@ SoundCallbackPtr Sound::GetCallbackChain() class SoundCallbackChain : public SoundCallback { public: - Sound* sound; + Sound* sound = nullptr; void OnStart(Sound*) override { diff --git a/src/kiwano-audio/Sound.h b/src/kiwano-audio/Sound.h index d8580d26..da73883f 100644 --- a/src/kiwano-audio/Sound.h +++ b/src/kiwano-audio/Sound.h @@ -92,14 +92,6 @@ class KGE_API Sound : public NativeObject friend class SoundPlayer; public: - /// \~chinese - /// @brief 预加载音频 - static SoundPtr Preload(const String& file_path, std::initializer_list callbacks = {}); - - /// \~chinese - /// @brief 预加载音频资源 - static SoundPtr Preload(const Resource& res, std::initializer_list callbacks = {}); - /// \~chinese /// @brief 创建音频对象 /// @param res 本地音频文件路径 @@ -110,6 +102,11 @@ public: /// @param res 音频资源 Sound(const Resource& res); + /// \~chinese + /// @brief 创建音频对象 + /// @param transcoder 音频解码器 + Sound(TranscoderPtr transcoder); + Sound(); virtual ~Sound(); @@ -162,18 +159,18 @@ public: protected: /// \~chinese - /// @brief 打开本地音频文件 + /// @brief 加载本地音频文件 /// @param res 本地音频文件路径 bool Load(const String& file_path); /// \~chinese - /// @brief 打开音频资源 + /// @brief 加载音频资源 /// @param res 音频资源 bool Load(const Resource& res); /// \~chinese - /// @brief 打开音频资源 - /// @param res 音频资源 + /// @brief 加载音频 + /// @param transcoder 音频解码器 bool Load(TranscoderPtr transcoder); SoundCallbackPtr GetCallbackChain(); diff --git a/src/kiwano-audio/SoundPlayer.cpp b/src/kiwano-audio/SoundPlayer.cpp index 737dbaeb..f25fd5d8 100644 --- a/src/kiwano-audio/SoundPlayer.cpp +++ b/src/kiwano-audio/SoundPlayer.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include +#include #include namespace kiwano @@ -53,7 +54,36 @@ SoundPlayer::SoundPlayer() } SoundPlayer::~SoundPlayer() +{} + +TranscoderPtr SoundPlayer::Preload(const String& file_path) { + size_t hash_code = std::hash{}(file_path); + if (cache_.count(hash_code)) + { + return cache_.at(hash_code); + } + TranscoderPtr ptr = AudioModule::GetInstance().CreateTranscoder(file_path); + if (ptr) + { + cache_.insert(std::make_pair(hash_code, ptr)); + } + return ptr; +} + +TranscoderPtr SoundPlayer::Preload(const Resource& res) +{ + size_t hash_code = res.GetId(); + if (cache_.count(hash_code)) + { + return cache_.at(hash_code); + } + TranscoderPtr ptr = AudioModule::GetInstance().CreateTranscoder(res); + if (ptr) + { + cache_.insert(std::make_pair(hash_code, ptr)); + } + return ptr; } void SoundPlayer::Play(SoundPtr sound, int loop_count) @@ -68,14 +98,26 @@ void SoundPlayer::Play(SoundPtr sound, int loop_count) SoundPtr SoundPlayer::Play(const String& file_path, int loop_count, std::initializer_list callbacks) { - SoundPtr sound = Sound::Preload(file_path, callbacks); + TranscoderPtr transcoder = Preload(file_path); + + SoundPtr sound = new Sound(transcoder); + for (const auto& cb : callbacks) + { + sound->AddCallback(cb); + } Play(sound, loop_count); return sound; } SoundPtr SoundPlayer::Play(const Resource& res, int loop_count, std::initializer_list callbacks) { - SoundPtr sound = Sound::Preload(res, callbacks); + TranscoderPtr transcoder = Preload(res); + + SoundPtr sound = new Sound(transcoder); + for (const auto& cb : callbacks) + { + sound->AddCallback(cb); + } Play(sound, loop_count); return sound; } @@ -114,6 +156,11 @@ void SoundPlayer::StopAll() } } +void SoundPlayer::ClearCache() +{ + cache_.clear(); +} + void SoundPlayer::OnEnd(Sound* sound) { // remove callback diff --git a/src/kiwano-audio/SoundPlayer.h b/src/kiwano-audio/SoundPlayer.h index baca0034..bc8ee10e 100644 --- a/src/kiwano-audio/SoundPlayer.h +++ b/src/kiwano-audio/SoundPlayer.h @@ -45,6 +45,15 @@ public: ~SoundPlayer(); + /// \~chinese + /// @brief 预加载音频 + /// @details + TranscoderPtr Preload(const String& file_path); + + /// \~chinese + /// @brief 预加载音频资源 + TranscoderPtr Preload(const Resource& res); + /// \~chinese /// @brief 播放音频 /// @param sound 音频 @@ -90,6 +99,10 @@ public: /// @param volume 音量大小,1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量 void SetVolume(float volume); + /// \~chinese + /// @brief 清空缓存 + void ClearCache(); + protected: void OnEnd(Sound* sound); @@ -106,6 +119,8 @@ protected: SoundList sound_list_; SoundList trash_; SoundCallbackPtr callback_; + + UnorderedMap cache_; }; /** @} */ diff --git a/src/kiwano-audio/Transcoder.h b/src/kiwano-audio/Transcoder.h index 6bc6bc2c..143dc716 100644 --- a/src/kiwano-audio/Transcoder.h +++ b/src/kiwano-audio/Transcoder.h @@ -89,56 +89,7 @@ private: WAVEFORMATEX* wave_format_; }; - -class KGE_API TranscoderCache final : public Singleton -{ - friend Singleton; - -public: - /// \~chinese - /// @brief 添加缓存 - inline void Add(size_t key, TranscoderPtr v) - { - cache_.insert(std::make_pair(key, v)); - } - - /// \~chinese - /// @brief 获取缓存 - inline TranscoderPtr Get(size_t key) const - { - if (cache_.count(key)) - { - return cache_.at(key); - } - return nullptr; - } - - /// \~chinese - /// @brief 移除缓存 - inline void Remove(size_t key) - { - cache_.erase(key); - } - - /// \~chinese - /// @brief 清空缓存 - inline void Clear() - { - cache_.clear(); - } - - ~TranscoderCache() - { - Clear(); - } - -private: - TranscoderCache() = default; - -private: - UnorderedMap cache_; -}; - /** @} */ + } // namespace audio } // namespace kiwano