/
SoundManager.cpp
171 lines (132 loc) · 3.92 KB
/
SoundManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "SoundManager.h"
#include "SoundFileLoader.h"
#include "ifilesystem.h"
#include "debugging/ScopedDebugTimer.h"
#include <algorithm>
#include "itextstream.h"
namespace sound
{
// Constructor
SoundManager::SoundManager() :
_defLoader(std::bind(&SoundManager::loadShadersFromFilesystem, this)),
_emptyShader(new SoundShader("", ""))
{}
// Enumerate shaders
void SoundManager::forEachShader(std::function<void(const ISoundShader&)> f)
{
ensureShadersLoaded();
for (const ShaderMap::value_type& pair : _shaders)
{
f(*pair.second);
}
}
bool SoundManager::playSound(const std::string& fileName)
{
return playSound(fileName, false);
}
bool SoundManager::playSound(const std::string& fileName, bool loopSound)
{
// Make a copy of the filename
std::string name = fileName;
// Try to open the file as it is
ArchiveFilePtr file = GlobalFileSystem().openFile(name);
rConsole() << "Trying: " << name << std::endl;
if (file)
{
// File found, play it
rConsole() << "Found file: " << name << std::endl;
if (_soundPlayer) _soundPlayer->play(*file, loopSound);
return true;
}
std::string root = name;
// File not found, try to strip the extension
if (name.rfind(".") != std::string::npos)
{
root = name.substr(0, name.rfind("."));
}
// Try to open the .ogg variant
name = root + ".ogg";
rConsole() << "Trying: " << name << std::endl;
file = GlobalFileSystem().openFile(name);
if (file)
{
rConsole() << "Found file: " << name << std::endl;
if (_soundPlayer) _soundPlayer->play(*file, loopSound);
return true;
}
// Try to open the file with .wav extension
name = root + ".wav";
rConsole() << "Trying: " << name << std::endl;
file = GlobalFileSystem().openFile(name);
if (file)
{
rConsole() << "Found file: " << name << std::endl;
if (_soundPlayer) _soundPlayer->play(*file, loopSound);
return true;
}
// File not found
return false;
}
void SoundManager::stopSound()
{
if (_soundPlayer) _soundPlayer->stop();
}
ISoundShaderPtr SoundManager::getSoundShader(const std::string& shaderName)
{
ensureShadersLoaded();
ShaderMap::const_iterator found = _shaders.find(shaderName);
// If the name was found, return it, otherwise return an empty shader object
return found != _shaders.end() ? found->second : _emptyShader;
}
const std::string& SoundManager::getName() const
{
static std::string _name(MODULE_SOUNDMANAGER);
return _name;
}
const StringSet& SoundManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty()) {
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
}
return _dependencies;
}
void SoundManager::loadShadersFromFilesystem()
{
ShaderMapPtr foundShaders = std::make_shared<ShaderMap>();
// Pass a SoundFileLoader to the filesystem
SoundFileLoader loader(*foundShaders);
GlobalFileSystem().forEachFile(
SOUND_FOLDER, // directory
"sndshd", // required extension
[&](const vfs::FileInfo& fileInfo) { loader(fileInfo.name); }, // loader callback
99 // max depth
);
_shaders.swap(*foundShaders);
rMessage() << _shaders.size() << " sound shaders found." << std::endl;
}
void SoundManager::ensureShadersLoaded()
{
_defLoader.ensureFinished();
}
void SoundManager::initialiseModule(const ApplicationContext& ctx)
{
// Create the SoundPlayer if sound is not disabled
const ApplicationContext::ArgumentList& args = ctx.getCmdLineArgs();
ApplicationContext::ArgumentList::const_iterator found(
std::find(args.begin(), args.end(), "--disable-sound")
);
if (found == args.end())
{
rMessage() << "SoundManager: initialising sound playback"
<< std::endl;
_soundPlayer = std::shared_ptr<SoundPlayer>(new SoundPlayer);
}
else
{
rMessage() << "SoundManager: sound output disabled"
<< std::endl;
}
_defLoader.start();
}
} // namespace sound