scummvm/engines/petka/sound.cpp

121 lines
3.3 KiB
C++
Raw Normal View History

2019-06-06 20:49:07 +00:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "audio/audiostream.h"
#include "audio/decoders/wave.h"
#include "common/substream.h"
#include "common/system.h"
2019-07-25 07:15:38 +00:00
#include "common/debug.h"
2019-06-06 20:49:07 +00:00
#include "petka/petka.h"
#include "petka/sound.h"
namespace Petka {
Sound::Sound(Common::SeekableReadStream *stream, Audio::Mixer::SoundType type)
: _type(type), _stream(stream->readStream(stream->size())) {
delete stream;
}
2019-06-06 20:49:07 +00:00
Sound::~Sound() {
stop();
}
void Sound::play(bool isLoop) {
if (!_stream)
return;
2019-06-06 20:49:07 +00:00
stop();
Audio::AudioStream *audioStream;
Audio::SeekableAudioStream *wavStream = Audio::makeWAVStream(_stream.get(), DisposeAfterUse::NO);
2019-06-06 20:49:07 +00:00
if (isLoop)
audioStream = Audio::makeLoopingAudioStream(wavStream, 0, 0, 0);
else
audioStream = wavStream;
g_system->getMixer()->playStream(_type, &_handle , audioStream);
}
bool Sound::isPlaying() {
return g_system->getMixer()->isSoundHandleActive(_handle);
}
void Sound::stop() {
g_system->getMixer()->stopHandle(_handle);
_stream->seek(0, SEEK_SET);
2019-06-06 20:49:07 +00:00
}
void Sound::setBalance(uint16 x, uint16 width) {
// original scales from -12.5 db to 12.5 db
2019-06-06 20:49:07 +00:00
g_system->getMixer()->setChannelBalance(_handle, (int8)(255 * (2 * x - width) / (2 * width)));
}
Audio::Mixer::SoundType Sound::type() {
return _type;
}
void Sound::pause(bool p) {
g_system->getMixer()->pauseHandle(_handle, p);
}
2019-06-06 21:32:36 +00:00
2019-06-06 20:49:07 +00:00
Sound *SoundMgr::addSound(const Common::String &name, Audio::Mixer::SoundType type) {
Sound *sound = findSound(name);
if (sound)
return sound;
2020-06-05 17:48:16 +00:00
Common::SeekableReadStream *s = _vm.openFile(name, false);
if (s) {
2019-07-25 07:15:38 +00:00
debug("SoundMgr: added sound %s", name.c_str());
2020-10-06 10:36:26 +00:00
sound = new Sound(s, type);
_sounds.getVal(name).reset(sound);
}
2019-06-06 20:49:07 +00:00
return sound;
}
Sound *SoundMgr::findSound(const Common::String &name) const {
2019-06-06 21:32:36 +00:00
SoundsMap::iterator it = _sounds.find(name);
2019-06-06 20:49:07 +00:00
return it != _sounds.end() ? it->_value.get() : nullptr;
}
void SoundMgr::removeSound(const Common::String &name) {
2019-07-25 07:15:38 +00:00
debug("SoundMgr::removeSound %s", name.c_str());
_sounds.erase(name);
2019-06-06 20:49:07 +00:00
}
void SoundMgr::removeAll() {
2019-07-25 07:15:38 +00:00
debug("SoundMgr::removeAll");
_sounds.clear(false);
2019-06-06 20:49:07 +00:00
}
void SoundMgr::removeSoundsWithType(Audio::Mixer::SoundType type) {
2019-06-06 21:32:36 +00:00
SoundsMap::iterator it;
2019-06-06 20:49:07 +00:00
for (it = _sounds.begin(); it != _sounds.end(); ++it) {
Sound *s = it->_value.get();
2020-05-29 22:12:44 +00:00
if (s->type() == type) {
_sounds.erase(it); // it is safe to inc iterator after erasing in our hashmap impl
2019-06-06 20:49:07 +00:00
}
}
}
} // End of namespace Petka