| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name sound.cpp - The sound. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer, Fabrice Rossi, |
| 13 |
// and Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
|
| 42 |
#include "SDL.h" |
| 43 |
|
| 44 |
#include "sound.h" |
| 45 |
#include "video.h" |
| 46 |
#include "sound.h" |
| 47 |
#include "unitsound.h" |
| 48 |
#include "unittype.h" |
| 49 |
#include "player.h" |
| 50 |
#include "unit.h" |
| 51 |
#include "sound_server.h" |
| 52 |
#include "missile.h" |
| 53 |
#include "map.h" |
| 54 |
#include "ui.h" |
| 55 |
#include "widgets.h" |
| 56 |
|
| 57 |
/*---------------------------------------------------------------------------- |
| 58 |
-- Variables |
| 59 |
----------------------------------------------------------------------------*/ |
| 60 |
|
| 61 |
/** |
| 62 |
** Various sounds used in game. |
| 63 |
** |
| 64 |
** FIXME: @todo Must remove static config. |
| 65 |
*/ |
| 66 |
GameSound GameSounds |
| 67 |
#ifndef laterUSE_CCL |
| 68 |
={ |
| 69 |
SoundConfig("placement error"), |
| 70 |
SoundConfig("placement success"), |
| 71 |
SoundConfig("click"), |
| 72 |
SoundConfig("transport docking"), |
| 73 |
SoundConfig("building construction"), |
| 74 |
SoundConfig("rescue (human) UNUSED"), |
| 75 |
} |
| 76 |
#endif |
| 77 |
; |
| 78 |
|
| 79 |
/** |
| 80 |
** Selection handling |
| 81 |
*/ |
| 82 |
struct SelectionHandling { |
| 83 |
Origin Source; /// origin of the sound |
| 84 |
CSound *Sound; /// last sound played by this unit |
| 85 |
unsigned char HowMany; /// number of sound played in this group |
| 86 |
}; |
| 87 |
|
| 88 |
SelectionHandling SelectionHandler; |
| 89 |
|
| 90 |
static int ViewPointOffset; /// Distance to Volume Mapping |
| 91 |
int DistanceSilent; /// silent distance |
| 92 |
|
| 93 |
/*---------------------------------------------------------------------------- |
| 94 |
-- Functions |
| 95 |
----------------------------------------------------------------------------*/ |
| 96 |
|
| 97 |
/** |
| 98 |
** "Randomly" choose a sample from a sound group. |
| 99 |
*/ |
| 100 |
static CSample *SimpleChooseSample(const CSound *sound) |
| 101 |
{ |
| 102 |
if (sound->Number == ONE_SOUND) { |
| 103 |
return sound->Sound.OneSound; |
| 104 |
} else { |
| 105 |
return sound->Sound.OneGroup[FrameCounter % sound->Number]; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
** Choose the sample to play |
| 111 |
*/ |
| 112 |
static CSample *ChooseSample(CSound *sound, bool selection, Origin &source) |
| 113 |
{ |
| 114 |
CSample *result = NULL; |
| 115 |
|
| 116 |
if (!sound || !SoundEnabled()) { |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
if (sound->Number == TWO_GROUPS) { |
| 121 |
// handle a special sound (selection) |
| 122 |
if (SelectionHandler.Source.Base == source.Base && |
| 123 |
SelectionHandler.Source.Id == source.Id) { |
| 124 |
if (SelectionHandler.Sound == sound->Sound.TwoGroups.First) { |
| 125 |
result = SimpleChooseSample(SelectionHandler.Sound); |
| 126 |
SelectionHandler.HowMany++; |
| 127 |
if (SelectionHandler.HowMany >= 3) { |
| 128 |
SelectionHandler.HowMany = 0; |
| 129 |
SelectionHandler.Sound = sound->Sound.TwoGroups.Second; |
| 130 |
} |
| 131 |
} else { |
| 132 |
// check whether the second group is really a group |
| 133 |
if (SelectionHandler.Sound->Number > 1) { |
| 134 |
result = SelectionHandler.Sound->Sound.OneGroup[SelectionHandler.HowMany]; |
| 135 |
SelectionHandler.HowMany++; |
| 136 |
if (SelectionHandler.HowMany >= SelectionHandler.Sound->Number) { |
| 137 |
SelectionHandler.HowMany = 0; |
| 138 |
SelectionHandler.Sound = sound->Sound.TwoGroups.First; |
| 139 |
} |
| 140 |
} else { |
| 141 |
result = SelectionHandler.Sound->Sound.OneSound; |
| 142 |
SelectionHandler.HowMany = 0; |
| 143 |
SelectionHandler.Sound = sound->Sound.TwoGroups.First; |
| 144 |
} |
| 145 |
} |
| 146 |
} else { |
| 147 |
SelectionHandler.Source = source; |
| 148 |
SelectionHandler.Sound = sound->Sound.TwoGroups.First; |
| 149 |
result = SimpleChooseSample(SelectionHandler.Sound); |
| 150 |
SelectionHandler.HowMany = 1; |
| 151 |
} |
| 152 |
} else { |
| 153 |
// normal sound/sound group handling |
| 154 |
result = SimpleChooseSample(sound); |
| 155 |
if (selection) { |
| 156 |
SelectionHandler.Source = source; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return result; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
** Maps a UnitVoiceGroup to a CSound*. |
| 165 |
** |
| 166 |
** @param unit Sound initiator |
| 167 |
** @param voice Type of sound wanted |
| 168 |
** |
| 169 |
** @return Sound identifier |
| 170 |
*/ |
| 171 |
static CSound *ChooseUnitVoiceSound(const CUnit *unit, UnitVoiceGroup voice) |
| 172 |
{ |
| 173 |
switch (voice) { |
| 174 |
case VoiceAcknowledging: |
| 175 |
return unit->Type->Sound.Acknowledgement.Sound; |
| 176 |
case VoiceReady: |
| 177 |
return unit->Type->Sound.Ready.Sound; |
| 178 |
case VoiceSelected: |
| 179 |
return unit->Type->Sound.Selected.Sound; |
| 180 |
case VoiceHelpMe: |
| 181 |
return unit->Type->Sound.Help.Sound; |
| 182 |
case VoiceDying: |
| 183 |
return unit->Type->Sound.Dead.Sound; |
| 184 |
case VoiceBuilding: |
| 185 |
return GameSounds.BuildingConstruction.Sound; |
| 186 |
case VoiceDocking: |
| 187 |
return GameSounds.Docking.Sound; |
| 188 |
} |
| 189 |
|
| 190 |
return NO_SOUND; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
** Compute a suitable volume for something taking place at a given |
| 195 |
** distance from the current view point. |
| 196 |
** |
| 197 |
** @param d distance |
| 198 |
** @param range range |
| 199 |
** |
| 200 |
** @return volume for given distance (0..??) |
| 201 |
*/ |
| 202 |
static unsigned char VolumeForDistance(unsigned short d, unsigned char range) |
| 203 |
{ |
| 204 |
int d_tmp; |
| 205 |
int range_tmp; |
| 206 |
|
| 207 |
// FIXME: THIS IS SLOW!!!!!!! |
| 208 |
if (d <= ViewPointOffset || range == INFINITE_SOUND_RANGE) { |
| 209 |
return MaxVolume; |
| 210 |
} else { |
| 211 |
if (range) { |
| 212 |
d -= ViewPointOffset; |
| 213 |
d_tmp = d * MAX_SOUND_RANGE; |
| 214 |
range_tmp = DistanceSilent * range; |
| 215 |
if (d_tmp > range_tmp) { |
| 216 |
return 0; |
| 217 |
} else { |
| 218 |
return (unsigned char)((range_tmp - d_tmp) * |
| 219 |
MAX_SOUND_RANGE / range_tmp); |
| 220 |
} |
| 221 |
} else { |
| 222 |
return 0; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
** Calculate the volume associated with a request, either by clipping the |
| 229 |
** range parameter of this request, or by mapping this range to a volume. |
| 230 |
*/ |
| 231 |
static unsigned char CalculateVolume(bool isVolume, int power, |
| 232 |
unsigned char range) |
| 233 |
{ |
| 234 |
if (isVolume) { |
| 235 |
if (power > MaxVolume) { |
| 236 |
return MaxVolume; |
| 237 |
} else { |
| 238 |
return (unsigned char)power; |
| 239 |
} |
| 240 |
} else { |
| 241 |
// map distance to volume |
| 242 |
return VolumeForDistance(power, range); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
** Calculate the stereo value for a unit |
| 248 |
*/ |
| 249 |
static char CalculateStereo(const CUnit *unit) |
| 250 |
{ |
| 251 |
int stereo; |
| 252 |
|
| 253 |
stereo = ((unit->X * TileSizeX + unit->Type->TileWidth * TileSizeX / 2 + |
| 254 |
unit->IX - UI.SelectedViewport->MapX * TileSizeX) * 256 / |
| 255 |
((UI.SelectedViewport->MapWidth - 1) * TileSizeX)) - 128; |
| 256 |
if (stereo < -128) { |
| 257 |
stereo = -128; |
| 258 |
} else if (stereo > 127) { |
| 259 |
stereo = 127; |
| 260 |
} |
| 261 |
|
| 262 |
return stereo; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
** Ask to the sound server to play a sound attached to a unit. The |
| 267 |
** sound server may discard the sound if needed (e.g., when the same |
| 268 |
** unit is already speaking). |
| 269 |
** |
| 270 |
** @param unit Sound initiator, unit speaking |
| 271 |
** @param voice Type of sound wanted (Ready,Die,Yes,...) |
| 272 |
*/ |
| 273 |
void PlayUnitSound(const CUnit *unit, UnitVoiceGroup voice) |
| 274 |
{ |
| 275 |
CSound *sound = ChooseUnitVoiceSound(unit, voice); |
| 276 |
if (!sound) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
bool selection = (voice == VoiceSelected || voice == VoiceBuilding); |
| 281 |
Origin source = {unit, unit->Slot}; |
| 282 |
|
| 283 |
int channel = PlaySample(ChooseSample(sound, selection, source)); |
| 284 |
if (channel == -1) { |
| 285 |
return; |
| 286 |
} |
| 287 |
SetChannelVolume(channel, CalculateVolume(false, ViewPointDistanceToUnit(unit), sound->Range)); |
| 288 |
SetChannelStereo(channel, CalculateStereo(unit)); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
** Ask to the sound server to play a sound attached to a unit. The |
| 293 |
** sound server may discard the sound if needed (e.g., when the same |
| 294 |
** unit is already speaking). |
| 295 |
** |
| 296 |
** @param unit Sound initiator, unit speaking |
| 297 |
** @param sound Sound to be generated |
| 298 |
*/ |
| 299 |
void PlayUnitSound(const CUnit *unit, CSound *sound) |
| 300 |
{ |
| 301 |
Origin source = {unit, unit->Slot}; |
| 302 |
int channel = PlaySample(ChooseSample(sound, false, source)); |
| 303 |
if (channel == -1) { |
| 304 |
return; |
| 305 |
} |
| 306 |
SetChannelVolume(channel, CalculateVolume(false, ViewPointDistanceToUnit(unit), sound->Range)); |
| 307 |
SetChannelStereo(channel, CalculateStereo(unit)); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
** Ask the sound server to play a sound for a missile. |
| 312 |
** |
| 313 |
** @param missile Sound initiator, missile exploding |
| 314 |
** @param sound Sound to be generated |
| 315 |
*/ |
| 316 |
void PlayMissileSound(const Missile *missile, CSound *sound) |
| 317 |
{ |
| 318 |
int stereo; |
| 319 |
|
| 320 |
stereo = ((missile->X + missile->Type->G->Width / 2 - |
| 321 |
UI.SelectedViewport->MapX * TileSizeX) * 256 / |
| 322 |
((UI.SelectedViewport->MapWidth - 1) * TileSizeX)) - 128; |
| 323 |
if (stereo < -128) { |
| 324 |
stereo = -128; |
| 325 |
} else if (stereo > 127) { |
| 326 |
stereo = 127; |
| 327 |
} |
| 328 |
|
| 329 |
Origin source = {NULL, 0}; |
| 330 |
|
| 331 |
int channel = PlaySample(ChooseSample(sound, false, source)); |
| 332 |
if (channel == -1) { |
| 333 |
return; |
| 334 |
} |
| 335 |
SetChannelVolume(channel, CalculateVolume(false, ViewPointDistanceToMissile(missile), sound->Range)); |
| 336 |
SetChannelStereo(channel, stereo); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
** Play a game sound |
| 341 |
** |
| 342 |
** @param sound Sound to play |
| 343 |
** @param volume Volume level to play the sound |
| 344 |
*/ |
| 345 |
void PlayGameSound(CSound *sound, unsigned char volume) |
| 346 |
{ |
| 347 |
Origin source = {NULL, 0}; |
| 348 |
|
| 349 |
int channel = PlaySample(ChooseSample(sound, false, source)); |
| 350 |
if (channel == -1) { |
| 351 |
return; |
| 352 |
} |
| 353 |
SetChannelVolume(channel, CalculateVolume(true, volume, sound->Range)); |
| 354 |
} |
| 355 |
|
| 356 |
static std::map<int, LuaActionListener *> ChannelMap; |
| 357 |
|
| 358 |
/** |
| 359 |
** Callback for PlaySoundFile |
| 360 |
*/ |
| 361 |
static void PlaySoundFileCallback(int channel) |
| 362 |
{ |
| 363 |
LuaActionListener *listener = ChannelMap[channel]; |
| 364 |
if (listener != NULL) { |
| 365 |
listener->action(""); |
| 366 |
ChannelMap[channel] = NULL; |
| 367 |
} |
| 368 |
delete GetChannelSample(channel); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
** Play a sound file |
| 373 |
** |
| 374 |
** @param name Filename of a sound to play |
| 375 |
** @param listener Optional lua callback |
| 376 |
** |
| 377 |
** @return Channel number the sound is playing on, -1 for error |
| 378 |
*/ |
| 379 |
int PlayFile(const std::string &name, LuaActionListener *listener) |
| 380 |
{ |
| 381 |
int channel = -1; |
| 382 |
CSample *sample; |
| 383 |
|
| 384 |
sample = LoadSample(name); |
| 385 |
if (sample) { |
| 386 |
channel = PlaySample(sample); |
| 387 |
if (channel != -1) { |
| 388 |
SetChannelVolume(channel, MaxVolume); |
| 389 |
SetChannelFinishedCallback(channel, PlaySoundFileCallback); |
| 390 |
ChannelMap[channel] = listener; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
return channel; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
** Ask the sound server to change the range of a sound. |
| 399 |
** |
| 400 |
** @param sound the id of the sound to modify. |
| 401 |
** @param range the new range for this sound. |
| 402 |
*/ |
| 403 |
void SetSoundRange(CSound *sound, unsigned char range) |
| 404 |
{ |
| 405 |
if (sound != NO_SOUND) { |
| 406 |
sound->Range = range; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
** Ask the sound server to register a sound (and currently to load it) |
| 412 |
** and to return an unique identifier for it. The unique identifier is |
| 413 |
** memory pointer of the server. |
| 414 |
** |
| 415 |
** @param files An array of wav files. |
| 416 |
** @param number Number of files belonging together. |
| 417 |
** |
| 418 |
** @return the sound unique identifier |
| 419 |
*/ |
| 420 |
CSound *RegisterSound(const char *files[], unsigned number) |
| 421 |
{ |
| 422 |
unsigned i; |
| 423 |
CSound *id; |
| 424 |
|
| 425 |
id = new CSound; |
| 426 |
if (number > 1) { // load a sound group |
| 427 |
id->Sound.OneGroup = new CSample *[number]; |
| 428 |
id->Number = number; |
| 429 |
for (i = 0; i < number; ++i) { |
| 430 |
id->Sound.OneGroup[i] = LoadSample(files[i]); |
| 431 |
if (!id->Sound.OneGroup[i]) { |
| 432 |
delete id; |
| 433 |
return NO_SOUND; |
| 434 |
} |
| 435 |
} |
| 436 |
} else { // load a unique sound |
| 437 |
id->Sound.OneSound = LoadSample(files[0]); |
| 438 |
if (!id->Sound.OneSound) { |
| 439 |
delete id; |
| 440 |
return NO_SOUND; |
| 441 |
} |
| 442 |
id->Number = ONE_SOUND; |
| 443 |
} |
| 444 |
id->Range = MAX_SOUND_RANGE; |
| 445 |
return id; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
** Ask the sound server to put together two sounds to form a special sound. |
| 450 |
** |
| 451 |
** @param first first part of the group |
| 452 |
** @param second second part of the group |
| 453 |
** |
| 454 |
** @return the special sound unique identifier |
| 455 |
*/ |
| 456 |
CSound *RegisterTwoGroups(CSound *first, CSound *second) |
| 457 |
{ |
| 458 |
CSound *id; |
| 459 |
|
| 460 |
if (first == NO_SOUND || second == NO_SOUND) { |
| 461 |
return NO_SOUND; |
| 462 |
} |
| 463 |
id = new CSound; |
| 464 |
id->Number = TWO_GROUPS; |
| 465 |
id->Sound.TwoGroups.First = first; |
| 466 |
id->Sound.TwoGroups.Second = second; |
| 467 |
id->Range = MAX_SOUND_RANGE; |
| 468 |
|
| 469 |
return id; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
** Lookup the sound id's for the game sounds. |
| 474 |
*/ |
| 475 |
void InitSoundClient(void) |
| 476 |
{ |
| 477 |
if (!SoundEnabled()) { // No sound enabled |
| 478 |
return; |
| 479 |
} |
| 480 |
// let's map game sounds, look if already setup in ccl. |
| 481 |
|
| 482 |
if (!GameSounds.PlacementError.Sound) { |
| 483 |
GameSounds.PlacementError.Sound = |
| 484 |
SoundForName(GameSounds.PlacementError.Name); |
| 485 |
} |
| 486 |
if (!GameSounds.PlacementSuccess.Sound) { |
| 487 |
GameSounds.PlacementSuccess.Sound = |
| 488 |
SoundForName(GameSounds.PlacementSuccess.Name); |
| 489 |
} |
| 490 |
if (!GameSounds.Click.Sound) { |
| 491 |
GameSounds.Click.Sound = SoundForName(GameSounds.Click.Name); |
| 492 |
} |
| 493 |
if (!GameSounds.Docking.Sound) { |
| 494 |
GameSounds.Docking.Sound = |
| 495 |
SoundForName(GameSounds.Docking.Name); |
| 496 |
} |
| 497 |
if (!GameSounds.BuildingConstruction.Sound) { |
| 498 |
GameSounds.BuildingConstruction.Sound = |
| 499 |
SoundForName(GameSounds.BuildingConstruction.Name); |
| 500 |
} |
| 501 |
if (!GameSounds.Rescue.Sound && !GameSounds.Rescue.Name.empty()) { |
| 502 |
GameSounds.Rescue.Sound = |
| 503 |
SoundForName(GameSounds.Rescue.Name); |
| 504 |
} |
| 505 |
if (!GameSounds.ChatMessage.Sound && !GameSounds.ChatMessage.Name.empty()) { |
| 506 |
GameSounds.ChatMessage.Sound = |
| 507 |
SoundForName(GameSounds.ChatMessage.Name); |
| 508 |
} |
| 509 |
|
| 510 |
int MapWidth = (UI.MapArea.EndX - UI.MapArea.X + TileSizeX) / TileSizeX; |
| 511 |
int MapHeight = (UI.MapArea.EndY - UI.MapArea.Y + TileSizeY) / TileSizeY; |
| 512 |
DistanceSilent = 3 * std::max(MapWidth, MapHeight); |
| 513 |
ViewPointOffset = std::max(MapWidth / 2, MapHeight / 2); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
CSound::~CSound() |
| 518 |
{ |
| 519 |
if (this->Number == ONE_SOUND) { |
| 520 |
delete Sound.OneSound; |
| 521 |
} else if (this->Number == TWO_GROUPS) { |
| 522 |
} else { |
| 523 |
for (int i = 0; i < this->Number; ++i) { |
| 524 |
delete this->Sound.OneGroup[i]; |
| 525 |
} |
| 526 |
delete[] this->Sound.OneGroup; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
//@} |