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_speech.h"
24 
25 #include "bladerunner/actor.h"
26 #include "bladerunner/aud_stream.h"
27 #include "bladerunner/audio_mixer.h"
28 #include "bladerunner/audio_player.h"
29 #include "bladerunner/bladerunner.h"
30 
31 #include "common/str.h"
32 
33 namespace BladeRunner {
34 
35 // Note: Speech samples here should be from A.TLK file
36 const int kSpeechSamplesNumber = 23;
37 const int AudioSpeech::kSpeechSamples[kSpeechSamplesNumber] = { 65, 355, 490, 465, 480, 485, 505, 760, 7655, 7770, 7740, 8170, 2705, 7200, 6460, 5560, 4870, 4555, 3880, 3525, 3595, 3250, 3070 };
38 
ended()39 void AudioSpeech::ended() {
40 	//Common::StackLock lock(_mutex);
41 	_isActive = false;
42 	_channel = -1;
43 }
44 
mixerChannelEnded(int channel,void * data)45 void AudioSpeech::mixerChannelEnded(int channel, void *data) {
46 	AudioSpeech *audioSpeech = (AudioSpeech *)data;
47 	audioSpeech->ended();
48 }
49 
AudioSpeech(BladeRunnerEngine * vm)50 AudioSpeech::AudioSpeech(BladeRunnerEngine *vm) {
51 	_vm = vm;
52 	// _speechVolume here sets a percentage to be appied on the specified voice cue volume
53 	// before sending it to the audio player
54 	// (setting _speechVolume to 100 renders it indifferent)
55 	_speechVolume = BLADERUNNER_ORIGINAL_SETTINGS ? 50 : 100;
56 	_isActive = false;
57 	_data = new byte[kBufferSize];
58 	_channel = -1;
59 }
60 
~AudioSpeech()61 AudioSpeech::~AudioSpeech() {
62 	stopSpeech();
63 	while (isPlaying()) {
64 		// wait for the mixer to finish
65 	}
66 
67 	delete[] _data;
68 }
69 
playSpeech(const Common::String & name,int pan)70 bool AudioSpeech::playSpeech(const Common::String &name, int pan) {
71 	if (isPlaying()) {
72 		stopSpeech();
73 	}
74 
75 	// Audio cache is not usable as hash function is producing collision for speech lines.
76 	// It was not used in the original game either
77 
78 	Common::ScopedPtr<Common::SeekableReadStream> r(_vm->getResourceStream(name));
79 
80 	if (!r) {
81 		warning("AudioSpeech::playSpeech: AUD resource \"%s\" not found", name.c_str());
82 		return false;
83 	}
84 
85 	if (r->size() > kBufferSize) {
86 		warning("AudioSpeech::playSpeech: AUD larger than buffer size (%d > %d)", (int)r->size(), kBufferSize);
87 		return false;
88 	}
89 
90 	if (isPlaying()) {
91 		stopSpeech();
92 	}
93 
94 	r->read(_data, r->size());
95 	if (r->err()) {
96 		warning("AudioSpeech::playSpeech: Error reading resource \"%s\"", name.c_str());
97 		return false;
98 	}
99 
100 	AudStream *audioStream = new AudStream(_data, _vm->_shortyMode ? 33000 : -1);
101 
102 	_channel = _vm->_audioMixer->play(
103 		Audio::Mixer::kSpeechSoundType,
104 		audioStream,
105 		100,
106 		false,
107 		_speechVolume,
108 		pan,
109 		mixerChannelEnded,
110 		this,
111 		audioStream->getLength());
112 
113 	_isActive = true;
114 
115 	return true;
116 }
117 
stopSpeech()118 void AudioSpeech::stopSpeech() {
119 	//Common::StackLock lock(_mutex);
120 	if (_channel != -1) {
121 		_vm->_audioMixer->stop(_channel, 0u);
122 	}
123 }
124 
isPlaying() const125 bool AudioSpeech::isPlaying() const {
126 	if (_channel == -1) {
127 		return false;
128 	}
129 	return _isActive;
130 }
131 
playSpeechLine(int actorId,int sentenceId,int volume,int a4,int priority)132 bool AudioSpeech::playSpeechLine(int actorId, int sentenceId, int volume, int a4, int priority) {
133 	int pan = _vm->_actors[actorId]->soundPan();
134 	Common::String name = Common::String::format("%02d-%04d%s.AUD", actorId, sentenceId, _vm->_languageCode.c_str());
135 	return _vm->_audioPlayer->playAud(name, _speechVolume * volume / 100, pan, pan, priority, kAudioPlayerOverrideVolume, Audio::Mixer::kSpeechSoundType);
136 }
137 
138 // We no longer set the _speechVolume (speech default volume percent) via a public method
139 // It is set in AudioSpeech::AudioSpeech() constructor and keeps its value constant.
140 //void AudioSpeech::setVolume(int volume) {
141 //	_speechVolume = volume;
142 //}
143 
getVolume() const144 int AudioSpeech::getVolume() const {
145 	return _speechVolume;
146 }
147 
playSample()148 void AudioSpeech::playSample() {
149 #if BLADERUNNER_ORIGINAL_BUGS
150 	_vm->_playerActor->speechPlay(kSpeechSamples[_vm->_rnd.getRandomNumber(kSpeechSamplesNumber-1)], true);
151 #else
152 	if (_vm->openArchive("A.TLK")) {
153 		// load sample speech even when in initial KIA screen (upon launch - but before loading a game)
154 		_vm->_playerActor->speechPlay(kSpeechSamples[_vm->_rnd.getRandomNumber(kSpeechSamplesNumber-1)], true);
155 	}
156 #endif // BLADERUNNER_ORIGINAL_BUGS
157 }
158 
159 } // End of namespace BladeRunner
160