CoriEngine
Loading...
Searching...
No Matches
Sound.cpp
Go to the documentation of this file.
1#include "Sound.hpp"
3
4namespace Cori {
5 namespace Audio {
7 if (m_Valid) {
8 Mixer::UnloadSound(m_ID);
9 }
10 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Sound '{} (SoundID: {})' destroyed." , m_Name, m_ID);
11 }
12
13 bool Sound::IsValid() const {
14 return m_Valid;
15 }
16
17 bool Sound::IsPlaceholder() const {
18 return m_Placeholder;
19 }
20
22 return m_ID;
23 }
24
25 std::shared_ptr<Sound> Sound::Create(const std::string& name, const std::filesystem::path& path, const bool preDecode) {
26 return std::shared_ptr<Sound>(new Sound(name, path, preDecode));
27 }
28
29 std::shared_ptr<Sound> Sound::Create(const Descriptor& descriptor) {
30 return Create(descriptor.m_Name, descriptor.m_Path, descriptor.m_PreDecode);
31 }
32
33 Sound::Sound(std::string name, const std::filesystem::path& path, const bool preDecode): m_Name(std::move(name)), m_ID(s_NextIndex.fetch_add(1, std::memory_order_relaxed)) {
34 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Creating Sound '{} (SoundID: {})' from: '{}'", m_Name, m_ID, path.string());
35 if (std::filesystem::exists(path)) {
36 auto result = Mixer::LoadSound(path, preDecode, m_ID);
37 if (result) {
38 m_Valid = true;
39 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Sound '{} (SoundID: {})' was created from: '{}'", m_Name, m_ID, path.string());
40 } else {
41 CORI_CORE_ERROR_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Failed to create Sound '{} (SoundID: {})'. Error: {}. Trying to load a placeholder.", m_Name, m_ID, result.error().what());
42
43 auto result_ = Mixer::LoadSound(FileSystem::PathManager::GetAliasedPath("ENGINE_DATA") / "placeholders/placeholder.ogg", preDecode, m_ID);
44 if (result_) {
45 m_Valid = true;
46 m_Placeholder = true;
47 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Placeholder loaded for Sound '{} (SoundID: {})'",m_Name, m_ID);
48 } else {
49 m_Valid = false;
50 CORI_CORE_ERROR_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Failed to load the placeholder for Sound '{} (SoundID: {})'. Error: {}. Invalid Sound object was created as a result, this should not crash as the engine prevents you from using an invalid Sound object.", m_Name, m_ID, result_.error().what());
51 }
52 }
53 } else {
54 CORI_CORE_ERROR_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Failed to create a Sound '{} (SoundID: {})' from: '{}', specified path does not exist. Trying to load a placeholder.", m_Name, m_ID, path.string());
55 auto result_ = Mixer::LoadSound(FileSystem::PathManager::GetAliasedPath("ENGINE_DATA") / "placeholders/placeholder.ogg", preDecode, m_ID);
56 if (result_) {
57 m_Valid = true;
58 m_Placeholder = true;
59 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Placeholder loaded for Sound '{} (SoundID: {})'", m_Name, m_ID);
60 } else {
61 m_Valid = false;
62 CORI_CORE_ERROR_TAGGED({ Logger::Tags::Audio::Self, Logger::Tags::Audio::Sound }, "Failed to load the placeholder for Sound '{} (SoundID: {})'. Error: {}. Invalid Sound object was created as a result, this should not crash as the engine prevents you from using an invalid Sound object.", m_Name, m_ID, result_.error().what());
63 }
64 }
65 }
66 }
67}
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_CORE_ERROR_TAGGED(...)
Definition Logger.hpp:1039
bool IsPlaceholder() const
Checks if the sound was created with a placeholder.
Definition Sound.cpp:17
static std::shared_ptr< Sound > Create(const std::string &name, const std::filesystem::path &path, const bool preDecode=true)
Creates a Sound object.
Definition Sound.cpp:25
const std::string m_Name
Definition Sound.hpp:75
SoundID GetID() const
Returns the SoundID associated with Sound.
Definition Sound.cpp:21
bool IsValid() const
Check if the Sound is valid.
Definition Sound.cpp:13
static std::filesystem::path GetAliasedPath(const std::string &alias)
Retries the full aliased path defined in fsgame.json.
Everything connected to audio is in this namespace.
Definition IDDefs.hpp:4
uint32_t SoundID
Definition IDDefs.hpp:6
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:75
static constexpr char Sound[]
Definition Logger.hpp:77