xoreos  0.0.5
sound.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/aurora/resman.h"
26 #include "src/aurora/gff3file.h"
27 
28 #include "src/sound/sound.h"
29 
31 
33 
34 namespace Engines {
35 
36 namespace KotOR {
37 
39  Common::UString temp = sound.getString("TemplateResRef");
40 
42  if (!temp.empty())
43  uts.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTS, MKTAG('U', 'T', 'S', ' ')));
44 
45  if (!uts)
46  throw Common::Exception("Sound \"%s\" has no blueprint", _tag.c_str());
47 
48  const Aurora::GFF3Struct &gff = uts->getTopLevel();
49 
50  Aurora::GFF3List soundFileList = gff.getList("Sounds");
51  for (Aurora::GFF3List::const_iterator c = soundFileList.begin(); c != soundFileList.end(); ++c) {
52  _soundFiles.push_back((*c)->getString("Sound"));
53  }
54 
55  _looping = gff.getBool("Looping");
56  _positional = gff.getBool("Positional");
57  _random = gff.getBool("Random");
58 
59  _interval = gff.getUint("Interval");
60 
61  // TODO: Investigate how kotor handles randomness of sounds.
62  Common::UString soundFile = _soundFiles[0];
63  if (_random)
64  soundFile = _soundFiles[std::rand() % _soundFiles.size()];
65 
66  Common::SeekableReadStream *soundStream = ResMan.getResource(Aurora::kResourceSound, soundFile);
67 
68  _sound = SoundMan.playSoundFile(soundStream, Sound::kSoundTypeSFX, _looping);
69 
70  _name = gff.getString("Tag");
71 
72  if (gff.getBool("Active")) {
73  play();
74  }
75 
77  static_cast<float>(sound.getDouble("XPosition")),
78  static_cast<float>(sound.getDouble("YPosition")),
79  static_cast<float>(sound.getDouble("ZPosition"))
80  );
81 
82  SoundMan.setChannelGain(_sound, static_cast<float>(gff.getUint("Volume"))/100.0f);
83  SoundMan.setChannelRelative(_sound, false);
84 
85  SoundMan.setChannelDistance(
86  _sound,
87  static_cast<float>(gff.getDouble("MinDistance")),
88  static_cast<float>(gff.getDouble("MaxDistance"))
89  );
90 }
91 
92 void SoundObject::setPosition(float x, float y, float z) {
93  Object::setPosition(x, y, z);
94 
95  if (_positional)
96  SoundMan.setChannelPosition(_sound, x, y, z);
97 }
98 
100  SoundMan.startChannel(_sound);
101 }
102 
104  SoundMan.stopChannel(_sound);
105 }
106 
107 } // End of namespace KotOR
108 
109 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
std::vector< Common::UString > _soundFiles
Definition: sound.h:52
A class holding an UTF-8 string.
Definition: ustring.h:48
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
An ingame sound in kotor.
Aurora::GFF3File * loadOptionalGFF3(const Common::UString &gff3, Aurora::FileType type, uint32 id, bool repairNWNPremium)
Load a GFF3, but return 0 instead of throwing on error.
Definition: util.cpp:150
Sound template (user), GFF.
Definition: types.h:100
Sound::ChannelHandle _sound
Definition: sound.h:54
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Common::UString _name
The object&#39;s display name.
Definition: object.h:142
double getDouble(const Common::UString &field, double def=0.0) const
Definition: gff3file.cpp:514
void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: sound.cpp:92
The global sound manager, handling all sound output.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
StackException Exception
Definition: error.h:59
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
A sound resource.
Definition: types.h:410
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:152
unsigned int _interval
Definition: sound.h:50
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString _tag
Definition: object.h:56
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Sound effect.
Definition: types.h:45
Generic Aurora engines utility functions.
Interface for a seekable & readable data stream.
Definition: readstream.h:265
SoundObject(const Aurora::GFF3Struct &sound)
Definition: sound.cpp:38
The global resource manager for Aurora resources.