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 "bladerunner/audio_player.h"
24 
25 #include "bladerunner/archive.h"
26 #include "bladerunner/aud_stream.h"
27 #include "bladerunner/audio_cache.h"
28 #include "bladerunner/audio_mixer.h"
29 #include "bladerunner/bladerunner.h"
30 
31 #include "common/debug.h"
32 #include "common/stream.h"
33 #include "common/random.h"
34 
35 namespace Common {
36 	class MemoryReadStream;
37 }
38 
39 namespace BladeRunner {
40 
AudioPlayer(BladeRunnerEngine * vm)41 AudioPlayer::AudioPlayer(BladeRunnerEngine *vm) {
42 	_vm = vm;
43 
44 	for (int i = 0; i != kTracks; ++i) {
45 		_tracks[i].priority = 0;
46 		_tracks[i].isActive = false;
47 		_tracks[i].channel = -1;
48 		_tracks[i].stream = nullptr;
49 	}
50 
51 	// _sfxVolume here sets a percentage to be appied on the specified track volume
52 	// before sending it to the audio player
53 	// (setting _sfxVolume to 100 renders it indifferent)
54 	_sfxVolume = BLADERUNNER_ORIGINAL_SETTINGS ? 65 : 100;
55 }
56 
~AudioPlayer()57 AudioPlayer::~AudioPlayer() {
58 	stopAll();
59 }
60 
stopAll()61 void AudioPlayer::stopAll() {
62 	for (int i = 0; i != kTracks; ++i) {
63 		stop(i, true);
64 	}
65 	for (int i = 0; i != kTracks; ++i) {
66 		while (isActive(i)) {
67 			// wait for all tracks to finish
68 		}
69 	}
70 }
71 
adjustVolume(int track,int volume,uint32 delaySeconds,bool overrideVolume)72 void AudioPlayer::adjustVolume(int track, int volume, uint32 delaySeconds, bool overrideVolume) {
73 	if (track < 0 || track >= kTracks || !_tracks[track].isActive || _tracks[track].channel == -1) {
74 		return;
75 	}
76 
77 	int actualVolume = volume;
78 	if (!overrideVolume) {
79 		actualVolume = actualVolume * _sfxVolume / 100;
80 	}
81 
82 	_tracks[track].volume = actualVolume;
83 	_vm->_audioMixer->adjustVolume(_tracks[track].channel, actualVolume, 60u * delaySeconds);
84 }
85 
adjustPan(int track,int pan,uint32 delaySeconds)86 void AudioPlayer::adjustPan(int track, int pan, uint32 delaySeconds) {
87 	if (track < 0 || track >= kTracks || !_tracks[track].isActive || _tracks[track].channel == -1) {
88 		return;
89 	}
90 
91 	_tracks[track].pan = pan;
92 	_vm->_audioMixer->adjustPan(_tracks[track].channel, pan, 60u * delaySeconds);
93 }
94 
95 // We no longer set the _sfxVolume (audio player's default volume percent) via a public method
96 // It is set in AudioPlayer::AudioPlayer() constructor and keeps its value constant.
97 //void AudioPlayer::setVolume(int volume) {
98 //	_sfxVolume = volume;
99 //}
100 
getVolume() const101 int AudioPlayer::getVolume() const {
102 	return _sfxVolume;
103 }
104 
playSample()105 void AudioPlayer::playSample() {
106 	Common::String name;
107 
108 	int rnd = _vm->_rnd.getRandomNumber(3);
109 	if (rnd == 0) {
110 		name = "gunmiss1.aud";
111 	} else if (rnd == 1) {
112 		name = "gunmiss2.aud";
113 	} else if (rnd == 2) {
114 		name = "gunmiss3.aud";
115 	} else {
116 		name = "gunmiss4.aud";
117 	}
118 
119 	playAud(name, 100, 0, 0, 100, 0);
120 }
121 
remove(int channel)122 void AudioPlayer::remove(int channel) {
123 	Common::StackLock lock(_mutex);
124 	for (int i = 0; i != kTracks; ++i) {
125 		if (_tracks[i].channel == channel) {
126 			_tracks[i].isActive = false;
127 			_tracks[i].priority = 0;
128 			_tracks[i].channel = -1;
129 			_tracks[i].stream = nullptr;
130 			break;
131 		}
132 	}
133 }
134 
mixerChannelEnded(int channel,void * data)135 void AudioPlayer::mixerChannelEnded(int channel, void *data) {
136 	AudioPlayer *audioPlayer = (AudioPlayer *)data;
137 	audioPlayer->remove(channel);
138 }
139 
playAud(const Common::String & name,int volume,int panStart,int panEnd,int priority,byte flags,Audio::Mixer::SoundType type)140 int AudioPlayer::playAud(const Common::String &name, int volume, int panStart, int panEnd, int priority, byte flags, Audio::Mixer::SoundType type) {
141 	/* Find first available track or, alternatively, the lowest priority playing track */
142 	int track = -1;
143 	int lowestPriority = 1000000;
144 	int lowestPriorityTrack = -1;
145 
146 	for (int i = 0; i != kTracks; ++i) {
147 		if (!isActive(i)) {
148 			//debug("Assigned track %i to %s", i, name.c_str());
149 			track = i;
150 			break;
151 		}
152 
153 		if (lowestPriorityTrack == -1 || _tracks[i].priority < lowestPriority) {
154 			lowestPriority = _tracks[i].priority;
155 			lowestPriorityTrack = i;
156 		}
157 	}
158 
159 	/* If there's no available track, stop the lowest priority track if it's lower than
160 	 * the new priority
161 	 */
162 	if (track == -1 && lowestPriority < priority) {
163 		//debug("Stop lowest priority  track (with lower prio: %d %d), for %s %d!", lowestPriorityTrack, lowestPriority, name.c_str(), priority);
164 		stop(lowestPriorityTrack, true);
165 		track = lowestPriorityTrack;
166 	}
167 
168 	/* If there's still no available track, give up */
169 	if (track == -1) {
170 		//debug("No available track for %s %d - giving up", name.c_str(), priority);
171 		return -1;
172 	}
173 
174 	/* Load audio resource and store in cache. Playback will happen directly from there. */
175 	int32 hash = MIXArchive::getHash(name);
176 	if (!_vm->_audioCache->findByHash(hash)) {
177 		Common::SeekableReadStream *r = _vm->getResourceStream(name);
178 		if (!r) {
179 			//debug("Could not get stream for %s %d - giving up", name.c_str(), priority);
180 			return -1;
181 		}
182 
183 		int32 size = r->size();
184 		while (!_vm->_audioCache->canAllocate(size)) {
185 			if (!_vm->_audioCache->dropOldest()) {
186 				delete r;
187 				//debug("No available mem in cache for %s %d - giving up", name.c_str(), priority);
188 				return -1;
189 			}
190 		}
191 		_vm->_audioCache->storeByHash(hash, r);
192 		delete r;
193 	}
194 
195 	AudStream *audioStream = new AudStream(_vm->_audioCache, hash);
196 
197 	int actualVolume = volume;
198 	if (!(flags & kAudioPlayerOverrideVolume)) {
199 		actualVolume = _sfxVolume * volume / 100;
200 	}
201 
202 	int channel = _vm->_audioMixer->play(
203 		type,
204 		audioStream,
205 		priority,
206 		flags & kAudioPlayerLoop,
207 		actualVolume,
208 		panStart,
209 		mixerChannelEnded,
210 		this,
211 		audioStream->getLength()
212 		);
213 
214 	if (channel == -1) {
215 		delete audioStream;
216 		//debug("No available channel for %s %d - giving up", name.c_str(), priority);
217 		return -1;
218 	}
219 
220 	if (panStart != panEnd) {
221 		_vm->_audioMixer->adjustPan(channel, panEnd, (60u * audioStream->getLength()) / 1000u);
222 	}
223 
224 	_tracks[track].isActive = true;
225 	_tracks[track].channel  = channel;
226 	_tracks[track].priority = priority;
227 	_tracks[track].volume   = actualVolume;
228 	_tracks[track].stream   = audioStream;
229 
230 	return track;
231 }
232 
isActive(int track) const233 bool AudioPlayer::isActive(int track) const {
234 	Common::StackLock lock(_mutex);
235 	if (track < 0 || track >= kTracks) {
236 		return false;
237 	}
238 
239 	return _tracks[track].isActive;
240 }
241 
242 /**
243 * Return the track's length in milliseconds
244 */
getLength(int track) const245 uint32 AudioPlayer::getLength(int track) const {
246 	Common::StackLock lock(_mutex);
247 	if (track < 0 || track >= kTracks) {
248 		return 0;
249 	}
250 
251 	return _tracks[track].stream->getLength();
252 }
253 
stop(int track,bool immediately)254 void AudioPlayer::stop(int track, bool immediately) {
255 	if (isActive(track)) {
256 		// If parameter "immediately" is not set,
257 		// the delay for audio stop is 1 second (multiplied by 60u as expected by AudioMixer::stop())
258 		_vm->_audioMixer->stop(_tracks[track].channel, immediately ? 0u : 60u);
259 	}
260 }
261 
262 } // End of namespace BladeRunner
263