00001
00002
00003
00004
00005
00006
00007
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
00025
00026 SoundEntry *AllocateSound()
00027 {
00028 SoundEntry *sound = _sounds.Append();
00029 MemSetT(sound, 0);
00030 return sound;
00031 }
00032
00033
00034 void InitializeSoundPool()
00035 {
00036 _sounds.Clear();
00037
00038
00039 SndCopyToPool();
00040 }
00041
00042
00043 SoundEntry *GetSound(SoundID index)
00044 {
00045 if (index >= _sounds.Length()) return NULL;
00046 return &_sounds[index];
00047 }
00048
00049
00050 uint GetNumSounds()
00051 {
00052 return _sounds.Length();
00053 }
00054
00055
00061 bool LoadNewGRFSound(SoundEntry *sound)
00062 {
00063 if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
00064
00065 FioSeekToFile(sound->file_slot, sound->file_offset);
00066
00067
00068
00069 if (sound->grf_container_ver >= 2) FioReadDword();
00070
00071
00072
00073 uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
00074 if (FioReadByte() != 0xFF) return false;
00075 if (FioReadByte() != 0xFF) return false;
00076
00077 uint8 name_len = FioReadByte();
00078 char *name = AllocaM(char, name_len + 1);
00079 FioReadBlock(name, name_len + 1);
00080
00081
00082 if (name[name_len] != 0) {
00083 DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
00084 return false;
00085 }
00086
00087 DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
00088
00089 if (FioReadDword() != BSWAP32('RIFF')) {
00090 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
00091 return false;
00092 }
00093
00094 uint32 total_size = FioReadDword();
00095 uint header_size = 11;
00096 if (sound->grf_container_ver >= 2) header_size++;
00097 if (total_size + name_len + header_size > num) {
00098 DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
00099 return false;
00100 }
00101
00102 if (FioReadDword() != BSWAP32('WAVE')) {
00103 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
00104 return false;
00105 }
00106
00107 while (total_size >= 8) {
00108 uint32 tag = FioReadDword();
00109 uint32 size = FioReadDword();
00110 total_size -= 8;
00111 if (total_size < size) {
00112 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
00113 return false;
00114 }
00115 total_size -= size;
00116
00117 switch (tag) {
00118 case ' tmf':
00119
00120 if (size < 16 || FioReadWord() != 1) {
00121 DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
00122 return false;
00123 }
00124 sound->channels = FioReadWord();
00125 sound->rate = FioReadDword();
00126 FioReadDword();
00127 FioReadWord();
00128 sound->bits_per_sample = FioReadWord();
00129
00130
00131 size -= 16;
00132 break;
00133
00134 case 'atad':
00135 sound->file_size = size;
00136 sound->file_offset = FioGetPos();
00137
00138 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);
00139 return true;
00140
00141 default:
00142
00143 break;
00144 }
00145
00146
00147 if (size > 0) FioSkipBytes(size);
00148 }
00149
00150 DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
00151
00152
00153 MemSetT(sound, 0);
00154 return false;
00155 }
00156
00157
00164 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
00165 {
00166 const GRFFile *file = v->GetGRF();
00167 uint16 callback;
00168
00169
00170 if (file == NULL) return false;
00171
00172
00173 if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
00174
00175 callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
00176
00177 if (callback == CALLBACK_FAILED) return false;
00178
00179 if (callback >= ORIGINAL_SAMPLE_COUNT) {
00180 callback -= ORIGINAL_SAMPLE_COUNT;
00181
00182
00183 if (callback > file->num_sounds) return true;
00184
00185 callback += file->sound_offset;
00186 }
00187
00188 assert(callback < GetNumSounds());
00189 SndPlayVehicleFx(callback, v);
00190 return true;
00191 }
00192
00199 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
00200 {
00201 if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
00202 sound_id -= ORIGINAL_SAMPLE_COUNT;
00203 if (sound_id > file->num_sounds) return;
00204 sound_id += file->sound_offset;
00205 }
00206
00207 assert(sound_id < GetNumSounds());
00208 SndPlayTileFx(sound_id, tile);
00209 }