ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

track.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/mutex.h"
00024 #include "common/str.h"
00025 #include "common/stream.h"
00026 #include "audio/mixer.h"
00027 #include "audio/audiostream.h"
00028 #include "engines/grim/savegame.h"
00029 #include "engines/grim/emi/sound/track.h"
00030 #include "common/textconsole.h"
00031 #include "engines/grim/grim.h"
00032 #include "engines/grim/set.h"
00033 
00034 namespace Grim {
00035 
00036 SoundTrack::SoundTrack() {
00037     _stream = nullptr;
00038     _handle = nullptr;
00039     _paused = false;
00040     _positioned = false;
00041     _balance = 0;
00042     _volume = Audio::Mixer::kMaxChannelVolume;
00043     _disposeAfterPlaying = DisposeAfterUse::YES;
00044     _sync = 0;
00045     _fadeMode = FadeNone;
00046     _fade = 1.0f;
00047     _attenuation = 1.0f;
00048 
00049     // Initialize to a plain sound for now
00050     _soundType = Audio::Mixer::kPlainSoundType;
00051 }
00052 
00053 SoundTrack::~SoundTrack() {
00054     if (_stream && (_disposeAfterPlaying == DisposeAfterUse::NO || !_handle))
00055         delete _stream;
00056 }
00057 
00058 Common::String SoundTrack::getSoundName() {
00059     return _soundName;
00060 }
00061 
00062 void SoundTrack::setSoundName(const Common::String &name) {
00063     _soundName = name;
00064 }
00065 
00066 void SoundTrack::setVolume(int volume) {
00067     _volume = MIN(volume, static_cast<int>(Audio::Mixer::kMaxChannelVolume));
00068     if (_handle) {
00069         g_system->getMixer()->setChannelVolume(*_handle, (byte)getEffectiveVolume());
00070     }
00071 }
00072 
00073 void SoundTrack::setPosition(bool positioned, const Math::Vector3d &pos) {
00074     _positioned = positioned;
00075     _pos = pos;
00076     updatePosition();
00077 }
00078 
00079 void SoundTrack::updatePosition() {
00080     if (!_positioned)
00081         return;
00082 
00083     Set *set = g_grim->getCurrSet();
00084     Set::Setup *setup = set->getCurrSetup();
00085     Math::Vector3d cameraPos = setup->_pos;
00086     Math::Vector3d vector = _pos - cameraPos;
00087     float distance = vector.getMagnitude();
00088     _attenuation = MAX(0.0f, 1.0f - distance / (_volume * 100.0f / Audio::Mixer::kMaxChannelVolume));
00089     if (!isfinite(_attenuation)) {
00090         _attenuation = 0.0f;
00091     }
00092 
00093     Math::Matrix4 worldRot = setup->_rot;
00094     Math::Vector3d relPos = (_pos - setup->_pos);
00095     Math::Vector3d p(relPos);
00096     worldRot.inverseRotate(&p);
00097     float angle = atan2(p.x(), p.z());
00098     float pan = sin(angle);
00099     _balance = (int)(pan * 127.0f);
00100 
00101     if (_handle) {
00102         g_system->getMixer()->setChannelBalance(*_handle, _balance);
00103         g_system->getMixer()->setChannelVolume(*_handle, (byte)getEffectiveVolume());
00104     }
00105 }
00106 
00107 void SoundTrack::setBalance(int balance) {
00108     if (_positioned)
00109         return;
00110     _balance = balance;
00111     if (_handle) {
00112         g_system->getMixer()->setChannelBalance(*_handle, _balance);
00113     }
00114 }
00115 
00116 bool SoundTrack::play() {
00117     if (_stream) {
00118         if (isPlaying()) {
00119             warning("sound: %s already playing, don't start again!", _soundName.c_str());
00120             return true;
00121         }
00122         // If _disposeAfterPlaying is NO, the destructor will take care of the stream.
00123         g_system->getMixer()->playStream(_soundType, _handle, _stream, -1, (byte)getEffectiveVolume(), _balance, _disposeAfterPlaying);
00124         return true;
00125     }
00126     return false;
00127 }
00128 
00129 void SoundTrack::pause() {
00130     _paused = !_paused;
00131     if (_stream) {
00132         g_system->getMixer()->pauseHandle(*_handle, _paused);
00133     }
00134 }
00135 
00136 void SoundTrack::stop() {
00137     if (_handle)
00138         g_system->getMixer()->stopHandle(*_handle);
00139 }
00140 
00141 void SoundTrack::setFade(float fade) {
00142     _fade = fade;
00143     if (_handle) {
00144         g_system->getMixer()->setChannelVolume(*_handle, (byte)getEffectiveVolume());
00145     }
00146 }
00147 
00148 int SoundTrack::getEffectiveVolume() {
00149     return _volume * _attenuation * _fade;
00150 }
00151 
00152 } // end of namespace Grim


Generated on Sat May 18 2019 05:01:27 for ResidualVM by doxygen 1.7.1
curved edge   curved edge