Fix INT_MAX definition on some newer systems.
[Tsunagari.git] / src / sound.cpp
blob8fd9888e79fa098ddc221f287e06cd4d356a2da3
1 /***************************************
2 ** Tsunagari Tile Engine **
3 ** sound.cpp **
4 ** Copyright 2011-2013 PariahSoft LLC **
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 <Gosu/Math.hpp>
29 #include "client-conf.h"
30 #include "python.h"
31 #include "python-bindings-template.cpp"
32 #include "reader.h"
33 #include "sound.h"
35 SoundInstance::SoundInstance(Gosu::SampleInstance inst)
36 : inst(inst), volume(conf.soundVolume), pan(0.0), speed(1.0)
38 inst.changeVolume(volume / 100.0);
42 bool SoundInstance::isPlaying()
44 return inst.playing();
47 void SoundInstance::stop()
49 inst.stop();
53 bool SoundInstance::isPaused()
55 return inst.paused();
58 void SoundInstance::setPaused(bool paused)
60 if (paused)
61 inst.pause();
62 else
63 inst.resume();
67 int SoundInstance::getVolume()
69 return volume;
72 void SoundInstance::setVolume(int volume)
74 if (0 < volume || volume > 100) {
75 Log::info("SoundInstance", "volume can only be set between 0 and 100");
76 volume = Gosu::clamp(volume, 0, 100);
78 this->volume = volume;
79 inst.changeVolume(volume / 100.0);
83 double SoundInstance::getPan()
85 return pan;
88 void SoundInstance::setPan(double pan)
90 this->pan = pan;
91 inst.changePan(pan);
95 double SoundInstance::getSpeed()
97 return speed;
100 void SoundInstance::setSpeed(double speed)
102 this->speed = speed;
103 inst.changeSpeed(speed);
109 Sound::Sound(Gosu::Sample* source)
110 : source(source)
114 SoundInstance Sound::play()
116 return SoundInstance(source->play());
119 // Helper class for Python.
120 class SoundManager
122 public:
123 SoundInstanceRef play(const std::string& path);
126 SoundInstanceRef SoundManager::play(const std::string& path)
128 SampleRef sample;
130 sample = Reader::getSample(path);
131 if (sample)
132 return SoundInstanceRef(new SoundInstance(sample->play()));
133 else
134 return SoundInstanceRef(NULL);
137 void exportSound()
139 using namespace boost::python;
141 class_<SoundInstance, SoundInstanceRef>
142 ("SoundInstance", no_init)
143 .add_property("paused",
144 &SoundInstance::isPaused, &SoundInstance::setPaused)
145 .add_property("volume",
146 &SoundInstance::getVolume, &SoundInstance::setVolume)
147 .add_property("pan",
148 &SoundInstance::getPan, &SoundInstance::setPan)
149 .add_property("speed",
150 &SoundInstance::getSpeed, &SoundInstance::setSpeed)
151 .add_property("playing", &SoundInstance::isPlaying)
152 .def("stop", &SoundInstance::stop)
154 class_<SoundManager>
155 ("SoundManager", no_init)
156 .def("play", &SoundManager::play)
158 pythonSetGlobal("Sound", new SoundManager);