newgrf_sound.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_sound.cpp 24846 2012-12-23 21:09:09Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "engine_base.h"
00014 #include "newgrf.h"
00015 #include "newgrf_engine.h"
00016 #include "newgrf_sound.h"
00017 #include "vehicle_base.h"
00018 #include "sound_func.h"
00019 #include "fileio_func.h"
00020 #include "debug.h"
00021 #include "settings_type.h"
00022 
00023 static SmallVector<SoundEntry, 8> _sounds;
00024 
00025 
00031 SoundEntry *AllocateSound(uint num)
00032 {
00033   SoundEntry *sound = _sounds.Append(num);
00034   MemSetT(sound, 0, num);
00035   return sound;
00036 }
00037 
00038 
00039 void InitializeSoundPool()
00040 {
00041   _sounds.Clear();
00042 
00043   /* Copy original sound data to the pool */
00044   SndCopyToPool();
00045 }
00046 
00047 
00048 SoundEntry *GetSound(SoundID index)
00049 {
00050   if (index >= _sounds.Length()) return NULL;
00051   return &_sounds[index];
00052 }
00053 
00054 
00055 uint GetNumSounds()
00056 {
00057   return _sounds.Length();
00058 }
00059 
00060 
00066 bool LoadNewGRFSound(SoundEntry *sound)
00067 {
00068   if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
00069 
00070   FioSeekToFile(sound->file_slot, sound->file_offset);
00071 
00072   /* Skip ID for container version >= 2 as we only look at the first
00073    * entry and ignore any further entries with the same ID. */
00074   if (sound->grf_container_ver >= 2) FioReadDword();
00075 
00076   /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
00077 
00078   uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
00079   if (FioReadByte() != 0xFF) return false;
00080   if (FioReadByte() != 0xFF) return false;
00081 
00082   uint8 name_len = FioReadByte();
00083   char *name = AllocaM(char, name_len + 1);
00084   FioReadBlock(name, name_len + 1);
00085 
00086   /* Test string termination */
00087   if (name[name_len] != 0) {
00088     DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
00089     return false;
00090   }
00091 
00092   DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
00093 
00094   if (FioReadDword() != BSWAP32('RIFF')) {
00095     DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
00096     return false;
00097   }
00098 
00099   uint32 total_size = FioReadDword();
00100   uint header_size = 11;
00101   if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
00102   if (total_size + name_len + header_size > num) {
00103     DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
00104     return false;
00105   }
00106 
00107   if (FioReadDword() != BSWAP32('WAVE')) {
00108     DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
00109     return false;
00110   }
00111 
00112   while (total_size >= 8) {
00113     uint32 tag  = FioReadDword();
00114     uint32 size = FioReadDword();
00115     total_size -= 8;
00116     if (total_size < size) {
00117       DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
00118       return false;
00119     }
00120     total_size -= size;
00121 
00122     switch (tag) {
00123       case ' tmf': // 'fmt '
00124         /* Audio format, must be 1 (PCM) */
00125         if (size < 16 || FioReadWord() != 1) {
00126           DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
00127           return false;
00128         }
00129         sound->channels = FioReadWord();
00130         sound->rate = FioReadDword();
00131         FioReadDword();
00132         FioReadWord();
00133         sound->bits_per_sample = FioReadWord();
00134 
00135         /* The rest will be skipped */
00136         size -= 16;
00137         break;
00138 
00139       case 'atad': // 'data'
00140         sound->file_size   = size;
00141         sound->file_offset = FioGetPos();
00142 
00143         DEBUG(grf, 2, "LoadNewGRFSound [%s]: channels %u, sample rate %u, bits per sample %u, length %u", FioGetFilename(sound->file_slot), sound->channels, sound->rate, sound->bits_per_sample, size);
00144         return true; // the fmt chunk has to appear before data, so we are finished
00145 
00146       default:
00147         /* Skip unknown chunks */
00148         break;
00149     }
00150 
00151     /* Skip rest of chunk */
00152     if (size > 0) FioSkipBytes(size);
00153   }
00154 
00155   DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
00156 
00157   /* Clear everything that was read */
00158   MemSetT(sound, 0);
00159   return false;
00160 }
00161 
00162 
00169 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
00170 {
00171   if (!_settings_client.sound.vehicle) return true;
00172 
00173   const GRFFile *file = v->GetGRF();
00174   uint16 callback;
00175 
00176   /* If the engine has no GRF ID associated it can't ever play any new sounds */
00177   if (file == NULL) return false;
00178 
00179   /* Check that the vehicle type uses the sound effect callback */
00180   if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
00181 
00182   callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
00183   /* Play default sound if callback fails */
00184   if (callback == CALLBACK_FAILED) return false;
00185 
00186   if (callback >= ORIGINAL_SAMPLE_COUNT) {
00187     callback -= ORIGINAL_SAMPLE_COUNT;
00188 
00189     /* Play no sound if result is out of range */
00190     if (callback > file->num_sounds) return true;
00191 
00192     callback += file->sound_offset;
00193   }
00194 
00195   assert(callback < GetNumSounds());
00196   SndPlayVehicleFx(callback, v);
00197   return true;
00198 }
00199 
00206 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
00207 {
00208   if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
00209     sound_id -= ORIGINAL_SAMPLE_COUNT;
00210     if (sound_id > file->num_sounds) return;
00211     sound_id += file->sound_offset;
00212   }
00213 
00214   assert(sound_id < GetNumSounds());
00215   SndPlayTileFx(sound_id, tile);
00216 }