| 1 |
#include "SoundManager.h" |
| 2 |
|
| 3 |
#include "ifilesystem.h" |
| 4 |
#include "icommandsystem.h" |
| 5 |
#include "ideclmanager.h" |
| 6 |
|
| 7 |
#include "os/path.h" |
| 8 |
#include "os/fs.h" |
| 9 |
#include "string/case_conv.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include "itextstream.h" |
| 13 |
|
| 14 |
#include "WavFileLoader.h" |
| 15 |
#include "OggFileLoader.h" |
| 16 |
#include "decl/DeclarationCreator.h" |
| 17 |
|
| 18 |
namespace sound |
| 19 |
{ |
| 20 |
|
| 21 |
namespace |
| 22 |
{ |
| 23 |
|
| 24 |
/// Sound directory name |
| 25 |
constexpr const char* const SOUND_FOLDER = "sound/"; |
| 26 |
constexpr const char* const SOUND_FILE_EXTENSION = ".sndshd"; |
| 27 |
|
| 28 |
// Load the given file, trying different extensions (first OGG, then WAV) as fallback |
| 29 |
ArchiveFilePtr openSoundFile(const std::string& fileName) |
| 30 |
{ |
| 31 |
// Try to open the file as it is |
| 32 |
auto file = GlobalFileSystem().openFile(fileName); |
| 33 |
|
| 34 |
if (file) |
| 35 |
{ |
| 36 |
// File found, play it |
| 37 |
return file; |
| 38 |
} |
| 39 |
|
| 40 |
file = GlobalFileSystem().openFile(os::replaceExtension(fileName, ".ogg")); |
| 41 |
|
| 42 |
if (file) |
| 43 |
{ |
| 44 |
return file; |
| 45 |
} |
| 46 |
|
| 47 |
// Try to open the file with .wav extension |
| 48 |
return GlobalFileSystem().openFile(os::replaceExtension(fileName, ".wav")); |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
SoundManager::SoundManager() |
| 54 |
{ |
| 55 |
decl::DeclarationBlockSyntax defaultBlock; |
| 56 |
defaultBlock.fileInfo = vfs::FileInfo("sound/", "_autogenerated_by_darkradiant_.sndshd", vfs::Visibility::HIDDEN); |
| 57 |
|
| 58 |
_emptyShader = std::make_unique<SoundShader>(""); |
| 59 |
_emptyShader->setBlockSyntax(defaultBlock); |
| 60 |
} |
| 61 |
|
| 62 |
void SoundManager::forEachShader(std::function<void(const ISoundShader::Ptr&)> functor) |
| 63 |
{ |
| 64 |
GlobalDeclarationManager().foreachDeclaration(decl::Type::SoundShader, |
| 65 |
[&](const decl::IDeclaration::Ptr& soundShader) |
| 66 |
{ |
| 67 |
functor(std::static_pointer_cast<ISoundShader>(soundShader)); |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
bool SoundManager::playSound(const std::string& fileName) |
| 72 |
{ |
| 73 |
return playSound(fileName, false); |
| 74 |
} |
| 75 |
|
| 76 |
bool SoundManager::playSound(const std::string& fileName, bool loopSound) |
| 77 |
{ |
| 78 |
auto file = openSoundFile(fileName); |
| 79 |
|
| 80 |
if (file && _soundPlayer) |
| 81 |
{ |
| 82 |
_soundPlayer->play(*file, loopSound); |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
void SoundManager::stopSound() |
| 90 |
{ |
| 91 |
if (_soundPlayer) _soundPlayer->stop(); |
| 92 |
} |
| 93 |
|
| 94 |
ISoundShader::Ptr SoundManager::getSoundShader(const std::string& shaderName) |
| 95 |
{ |
| 96 |
auto found = std::static_pointer_cast<ISoundShader>( |
| 97 |
GlobalDeclarationManager().findDeclaration(decl::Type::SoundShader, shaderName) |
| 98 |
); |
| 99 |
|
| 100 |
return found ? found : _emptyShader; |
| 101 |
} |
| 102 |
|
| 103 |
const std::string& SoundManager::getName() const |
| 104 |
{ |
| 105 |
static std::string _name(MODULE_SOUNDMANAGER); |
| 106 |
return _name; |
| 107 |
} |
| 108 |
|
| 109 |
const StringSet& SoundManager::getDependencies() const |
| 110 |
{ |
| 111 |
static StringSet _dependencies |
| 112 |
{ |
| 113 |
MODULE_VIRTUALFILESYSTEM, |
| 114 |
MODULE_COMMANDSYSTEM, |
| 115 |
MODULE_DECLMANAGER, |
| 116 |
}; |
| 117 |
|
| 118 |
return _dependencies; |
| 119 |
} |
| 120 |
|
| 121 |
void SoundManager::initialiseModule(const IApplicationContext& ctx) |
| 122 |
{ |
| 123 |
// For now, redirect reloadSounds to the generic reloadDecls |
| 124 |
GlobalCommandSystem().addStatement("ReloadSounds", "ReloadDecls", false); |
| 125 |
|
| 126 |
// Create the SoundPlayer if sound is not disabled |
| 127 |
const auto& args = ctx.getCmdLineArgs(); |
| 128 |
|
| 129 |
if (std::find(args.begin(), args.end(), "--disable-sound") == args.end()) |
| 130 |
{ |
| 131 |
rMessage() << "SoundManager: initialising sound playback" << std::endl; |
| 132 |
_soundPlayer = std::make_unique<SoundPlayer>(); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
rMessage() << "SoundManager: sound output disabled" << std::endl; |
| 137 |
} |
| 138 |
|
| 139 |
// Register the creator capable of constructing shaders and point the decl manager to the sound folder |
| 140 |
GlobalDeclarationManager().registerDeclType("sound", std::make_shared<decl::DeclarationCreator<SoundShader>>(decl::Type::SoundShader)); |
| 141 |
GlobalDeclarationManager().registerDeclFolder(decl::Type::SoundShader, SOUND_FOLDER, SOUND_FILE_EXTENSION); |
| 142 |
|
| 143 |
// Route the decls reloaded signal to the local signal |
| 144 |
GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::SoundShader).connect( |
| 145 |
[this]() { _sigSoundShadersReloaded.emit(); } |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
float SoundManager::getSoundFileDuration(const std::string& vfsPath) |
| 150 |
{ |
| 151 |
auto file = openSoundFile(vfsPath); |
| 152 |
|
| 153 |
if (!file) |
| 154 |
{ |
| 155 |
throw std::out_of_range("Could not resolve sound file " + vfsPath); |
| 156 |
} |
| 157 |
|
| 158 |
auto extension = string::to_lower_copy(os::getExtension(file->getName())); |
| 159 |
|
| 160 |
try |
| 161 |
{ |
| 162 |
if (extension == "wav") |
| 163 |
{ |
| 164 |
return WavFileLoader::GetDuration(file->getInputStream()); |
| 165 |
} |
| 166 |
else if (extension == "ogg") |
| 167 |
{ |
| 168 |
return OggFileLoader::GetDuration(*file); |
| 169 |
} |
| 170 |
} |
| 171 |
catch (const std::runtime_error& ex) |
| 172 |
{ |
| 173 |
rError() << "Error determining sound file duration " << ex.what() << std::endl; |
| 174 |
} |
| 175 |
|
| 176 |
return 0.0f; |
| 177 |
} |
| 178 |
|
| 179 |
void SoundManager::reloadSounds() |
| 180 |
{ |
| 181 |
GlobalDeclarationManager().reloadDeclarations(); |
| 182 |
} |
| 183 |
|
| 184 |
} // namespace sound |