newgrf_sound.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_sound.cpp 24074 2012-03-26 20:38:53Z rubidium $ */
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 
00022 static SmallVector<SoundEntry, 8> _sounds;
00023 
00024 
00030 SoundEntry *AllocateSound(uint num)
00031 {
00032   SoundEntry *sound = _sounds.Append(num);
00033   MemSetT(sound, 0, num);
00034   return sound;
00035 }
00036 
00037 
00038 void InitializeSoundPool()
00039 {
00040   _sounds.Clear();
00041 
00042   /* Copy original sound data to the pool */
00043   SndCopyToPool();
00044 }
00045 
00046 
00047 SoundEntry *GetSound(SoundID index)
00048 {
00049   if (index >= _sounds.Length()) return NULL;
00050   return &_sounds[index];
00051 }
00052 
00053 
00054 uint GetNumSounds()
00055 {
00056   return _sounds.Length();
00057 }
00058 
00059 
00065 bool LoadNewGRFSound(SoundEntry *sound)
00066 {
00067   if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
00068 
00069   FioSeekToFile(sound->file_slot, sound->file_offset);
00070 
00071   /* Skip ID for container version >= 2 as we only look at the first
00072    * entry and ignore any further entries with the same ID. */
00073   if (sound->grf_container_ver >= 2) FioReadDword();
00074 
00075   /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
00076 
00077   uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
00078   if (FioReadByte() != 0xFF) return false;
00079   if (FioReadByte() != 0xFF) return false;
00080 
00081   uint8 name_len = FioReadByte();
00082   char *name = AllocaM(char, name_len + 1);
00083   FioReadBlock(name, name_len + 1);
00084 
00085   /* Test string termination */
00086   if (name[name_len] != 0) {
00087     DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
00088     return false;
00089   }
00090 
00091   DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
00092 
00093   if (FioReadDword() != BSWAP32('RIFF')) {
00094     DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
00095     return false;
00096   }
00097 
00098   uint32 total_size = FioReadDword();
00099   uint header_size = 11;
00100   if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
00101   if (total_size + name_len + header_size > num) {
00102     DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
00103     return false;
00104   }
00105 
00106   if (FioReadDword() != BSWAP32('WAVE')) {
00107     DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
00108     return false;
00109   }
00110 
00111   while (total_size >= 8) {
00112     uint32 tag  = FioReadDword();
00113     uint32 size = FioReadDword();
00114     total_size -= 8;
00115     if (total_size < size) {
00116       DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
00117       return false;
00118     }
00119     total_size -= size;
00120 
00121     switch (tag) {
00122       case ' tmf': // 'fmt '
00123         /* Audio format, must be 1 (PCM) */
00124         if (size < 16 || FioReadWord() != 1) {
00125           DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
00126           return false;
00127         }
00128         sound->channels = FioReadWord();
00129         sound->rate = FioReadDword();
00130         FioReadDword();
00131         FioReadWord();
00132         sound->bits_per_sample = FioReadWord();
00133 
00134         /* The rest will be skipped */
00135         size -= 16;
00136         break;
00137 
00138       case 'atad': // 'data'
00139         sound->file_size   = size;
00140         sound->file_offset = FioGetPos();
00141 
00142         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);
00143         return true; // the fmt chunk has to appear before data, so we are finished
00144 
00145       default:
00146         /* Skip unknown chunks */
00147         break;
00148     }
00149 
00150     /* Skip rest of chunk */
00151     if (size > 0) FioSkipBytes(size);
00152   }
00153 
00154   DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
00155 
00156   /* Clear everything that was read */
00157   MemSetT(sound, 0);
00158   return false;
00159 }
00160 
00161 
00168 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
00169 {
00170   const GRFFile *file = v->GetGRF();
00171   uint16 callback;
00172 
00173   /* If the engine has no GRF ID associated it can't ever play any new sounds */
00174   if (file == NULL) return false;
00175 
00176   /* Check that the vehicle type uses the sound effect callback */
00177   if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
00178 
00179   callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
00180   /* Play default sound if callback fails */
00181   if (callback == CALLBACK_FAILED) return false;
00182 
00183   if (callback >= ORIGINAL_SAMPLE_COUNT) {
00184     callback -= ORIGINAL_SAMPLE_COUNT;
00185 
00186     /* Play no sound if result is out of range */
00187     if (callback > file->num_sounds) return true;
00188 
00189     callback += file->sound_offset;
00190   }
00191 
00192   assert(callback < GetNumSounds());
00193   SndPlayVehicleFx(callback, v);
00194   return true;
00195 }
00196 
00203 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
00204 {
00205   if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
00206     sound_id -= ORIGINAL_SAMPLE_COUNT;
00207     if (sound_id > file->num_sounds) return;
00208     sound_id += file->sound_offset;
00209   }
00210 
00211   assert(sound_id < GetNumSounds());
00212   SndPlayTileFx(sound_id, tile);
00213 }