Add license text to the top of every source file.
[Tsunagari.git] / src / sound.cpp
blob9bda3806193a4fcece9a1af9853dd52d08723054
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** sound.cpp **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #include <boost/shared_ptr.hpp>
29 #include "python.h"
30 #include "resourcer.h"
31 #include "sound.h"
33 SoundInstance::SoundInstance(boost::optional<Gosu::SampleInstance> inst)
34 : inst(inst), volume(1.0), pan(0.0), speed(1.0)
39 bool SoundInstance::isPlaying()
41 return inst && inst->playing();
44 void SoundInstance::stop()
46 if (inst)
47 inst->stop();
51 bool SoundInstance::isPaused()
53 return inst ? inst->paused() : false;
56 void SoundInstance::setPaused(bool paused)
58 if (inst) {
59 if (paused)
60 inst->pause();
61 else
62 inst->resume();
67 double SoundInstance::getVolume()
69 return volume;
72 void SoundInstance::setVolume(double volume)
74 this->volume = volume;
75 if (inst)
76 inst->changeVolume(volume);
80 double SoundInstance::getPan()
82 return pan;
85 void SoundInstance::setPan(double pan)
87 this->pan = pan;
88 if (inst)
89 inst->changePan(pan);
93 double SoundInstance::getSpeed()
95 return speed;
98 void SoundInstance::setSpeed(double speed)
100 this->speed = speed;
101 if (inst)
102 inst->changeSpeed(speed);
108 Sound::Sound(Gosu::Sample* source)
109 : source(source)
113 SoundInstance Sound::play()
115 return SoundInstance(source->play());
118 // Helper class for Python.
119 class SoundManager
121 public:
122 SoundInstance play(const std::string& path);
125 SoundInstance SoundManager::play(const std::string& path)
127 Resourcer* rc;
128 SampleRef sample;
130 rc = Resourcer::instance();
131 sample = rc->getSample(path);
132 if (sample)
133 return sample->play();
134 else
135 return SoundInstance(NULL);
138 void exportSound()
140 boost::python::class_<SoundInstance>
141 ("SoundInstance", boost::python::no_init)
142 .add_property("paused",
143 &SoundInstance::isPaused, &SoundInstance::setPaused)
144 .add_property("volume",
145 &SoundInstance::getVolume, &SoundInstance::setVolume)
146 .add_property("pan",
147 &SoundInstance::getPan, &SoundInstance::setPan)
148 .add_property("speed",
149 &SoundInstance::getSpeed, &SoundInstance::setSpeed)
150 .add_property("playing", &SoundInstance::isPlaying)
151 .def("stop", &SoundInstance::stop)
153 boost::python::class_<SoundManager>
154 ("SoundManager", boost::python::no_init)
155 .def("play", &SoundManager::play)
157 pythonSetGlobal("Sound", new SoundManager);