1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program 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 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "gnap/sound.h"
24 #include "audio/audiostream.h"
25 #include "audio/decoders/wave.h"
26 
27 namespace Gnap {
28 
SoundMan(GnapEngine * vm)29 SoundMan::SoundMan(GnapEngine *vm) : _vm(vm) {
30 }
31 
~SoundMan()32 SoundMan::~SoundMan() {
33 }
34 
playSound(int resourceId,bool looping)35 void SoundMan::playSound(int resourceId, bool looping) {
36 	SoundItem soundItem;
37 	soundItem._resourceId = resourceId;
38 
39 	SoundResource *soundResource = _vm->_soundCache->get(resourceId);
40 	Common::MemoryReadStream *stream = new Common::MemoryReadStream(soundResource->_data, soundResource->_size, DisposeAfterUse::NO);
41 	Audio::AudioStream *audioStream = Audio::makeLoopingAudioStream(Audio::makeWAVStream(stream, DisposeAfterUse::YES), looping ? 0 : 1);
42 
43 	_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &soundItem._handle, audioStream);
44 
45 	_items.push_back(soundItem);
46 
47 }
48 
stopSound(int resourceId)49 void SoundMan::stopSound(int resourceId) {
50 	const int index = find(resourceId);
51 	if (index < 0)
52 		return;
53 
54 	_vm->_soundCache->release(_items[index]._resourceId);
55 	_vm->_mixer->stopHandle(_items[index]._handle);
56 	_items.remove_at(index);
57 }
58 
setSoundVolume(int resourceId,int volume)59 void SoundMan::setSoundVolume(int resourceId, int volume) {
60 	if (resourceId == -1 || volume < 0 || volume > 100)
61 		return;
62 
63 	const int index = find(resourceId);
64 	if (index < 0)
65 		return;
66 
67 	int realVol = volume * 2.55;
68 	_vm->_mixer->setChannelVolume(_items[index]._handle, realVol);
69 }
70 
isSoundPlaying(int resourceId)71 bool SoundMan::isSoundPlaying(int resourceId) {
72 	const int index = find(resourceId);
73 	if (index < 0)
74 		return false;
75 
76 	return _vm->_mixer->isSoundHandleActive(_items[index]._handle);
77 }
78 
stopAll()79 void SoundMan::stopAll() {
80 	for (int index = 0; index < (int)_items.size(); ++index) {
81 		_vm->_soundCache->release(_items[index]._resourceId);
82 		_vm->_mixer->stopHandle(_items[index]._handle);
83 	}
84 }
85 
update()86 void SoundMan::update() {
87 	for (int index = 0; index < (int)_items.size(); ++index)
88 		if (!_vm->_mixer->isSoundHandleActive(_items[index]._handle)) {
89 			_vm->_soundCache->release(_items[index]._resourceId);
90 			_items.remove_at(index);
91 			--index;
92 		}
93 }
94 
find(int resourceId)95 int SoundMan::find(int resourceId) {
96 	for (int index = 0; index < (int)_items.size(); ++index)
97 		if (_items[index]._resourceId == resourceId)
98 			return index;
99 	return -1;
100 }
101 
102 } // End of namespace Gnap
103