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 "gob/sound/soundblaster.h"
24 #include "gob/sound/sounddesc.h"
25
26 namespace Gob {
27
SoundBlaster(Audio::Mixer & mixer)28 SoundBlaster::SoundBlaster(Audio::Mixer &mixer) : SoundMixer(mixer, Audio::Mixer::kSFXSoundType) {
29 _curSoundDesc = 0;
30
31 _compositionSamples = 0;
32 _compositionSampleCount = 0;
33 _compositionPos = -1;
34
35 _compositionRepCount = 0;
36 }
37
~SoundBlaster()38 SoundBlaster::~SoundBlaster() {
39 }
40
playSample(SoundDesc & sndDesc,int16 repCount,int16 frequency,int16 fadeLength)41 void SoundBlaster::playSample(SoundDesc &sndDesc, int16 repCount,
42 int16 frequency, int16 fadeLength) {
43 SoundMixer::play(sndDesc, repCount, frequency, fadeLength);
44 }
45
stopSound(int16 fadeLength,SoundDesc * sndDesc)46 void SoundBlaster::stopSound(int16 fadeLength, SoundDesc *sndDesc) {
47 Common::StackLock slock(_mutex);
48
49 if (sndDesc && (sndDesc != _curSoundDesc))
50 return;
51
52 _compositionRepCount = 0;
53
54 if (fadeLength <= 0)
55 _curSoundDesc = 0;
56
57 SoundMixer::stop(fadeLength);
58 }
59
stopComposition()60 void SoundBlaster::stopComposition() {
61 if (_compositionPos != -1) {
62 stopSound(0);
63 _compositionPos = -1;
64 }
65 }
66
endComposition()67 void SoundBlaster::endComposition() {
68 _compositionPos = -1;
69 _compositionRepCount = 0;
70 }
71
nextCompositionPos()72 void SoundBlaster::nextCompositionPos() {
73 int8 slot;
74
75 while ((++_compositionPos < 50) &&
76 ((slot = _composition[_compositionPos]) != -1)) {
77 if ((slot >= 0) && (slot < _compositionSampleCount)) {
78 SoundDesc &sample = _compositionSamples[slot];
79 if (!sample.empty() && (sample.getType() == SOUND_SND)) {
80 setSample(sample, 1, 0, 0);
81 return;
82 }
83 }
84 if (_compositionPos == 49)
85 _compositionPos = -1;
86 }
87
88 _compositionPos = -1;
89 }
90
playComposition(const int16 * composition,int16 freqVal,SoundDesc * sndDescs,int8 sndCount)91 void SoundBlaster::playComposition(const int16 *composition, int16 freqVal,
92 SoundDesc *sndDescs, int8 sndCount) {
93
94 _compositionSamples = sndDescs;
95 _compositionSampleCount = sndCount;
96
97 int i = -1;
98 do {
99 i++;
100 _composition[i] = composition[i];
101 } while ((i < 49) && (composition[i] != -1));
102
103 _compositionPos = -1;
104 nextCompositionPos();
105 }
106
repeatComposition(int32 repCount)107 void SoundBlaster::repeatComposition(int32 repCount) {
108 _compositionRepCount = repCount;
109 }
110
setSample(SoundDesc & sndDesc,int16 repCount,int16 frequency,int16 fadeLength)111 void SoundBlaster::setSample(SoundDesc &sndDesc, int16 repCount, int16 frequency,
112 int16 fadeLength) {
113
114 _curSoundDesc = &sndDesc;
115 SoundMixer::setSample(sndDesc, repCount, frequency, fadeLength);
116 }
117
checkEndSample()118 void SoundBlaster::checkEndSample() {
119 if (_compositionPos != -1) {
120 nextCompositionPos();
121 return;
122 }
123
124 if (_compositionRepCount != 0) {
125 if (_compositionRepCount > 0)
126 _compositionRepCount--;
127
128 nextCompositionPos();
129 if (_compositionPos != -1)
130 return;
131 }
132
133 SoundMixer::checkEndSample();
134 }
135
endFade()136 void SoundBlaster::endFade() {
137 if (_fadeVolStep > 0) {
138 _compositionPos = -1;
139 _curSoundDesc = 0;
140 }
141
142 SoundMixer::endFade();
143 }
144
145 } // End of namespace Gob
146