OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_sound.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_sound.cpp 27507 2016-02-08 21:05:57Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "engine_base.h"
14 #include "newgrf.h"
15 #include "newgrf_engine.h"
16 #include "newgrf_sound.h"
17 #include "vehicle_base.h"
18 #include "sound_func.h"
19 #include "fileio_func.h"
20 #include "debug.h"
21 #include "settings_type.h"
22
23 #include "safeguards.h"
24
25 static SmallVector<SoundEntry, 8> _sounds;
26
27
33 SoundEntry *AllocateSound(uint num)
34 {
35 SoundEntry *sound = _sounds.Append(num);
36 MemSetT(sound, 0, num);
37 return sound;
38 }
39
40
41 void InitializeSoundPool()
42 {
43 _sounds.Clear();
44
45 /* Copy original sound data to the pool */
46 SndCopyToPool();
47 }
48
49
50 SoundEntry *GetSound(SoundID index)
51 {
52 if (index >= _sounds.Length()) return NULL;
53 return &_sounds[index];
54 }
55
56
57 uint GetNumSounds()
58 {
59 return _sounds.Length();
60 }
61
62
68 bool LoadNewGRFSound(SoundEntry *sound)
69 {
70 if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
71
72 FioSeekToFile(sound->file_slot, sound->file_offset);
73
74 /* Skip ID for container version >= 2 as we only look at the first
75 * entry and ignore any further entries with the same ID. */
76 if (sound->grf_container_ver >= 2) FioReadDword();
77
78 /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
79
80 uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
81 if (FioReadByte() != 0xFF) return false;
82 if (FioReadByte() != 0xFF) return false;
83
84 uint8 name_len = FioReadByte();
85 char *name = AllocaM(char, name_len + 1);
86 FioReadBlock(name, name_len + 1);
87
88 /* Test string termination */
89 if (name[name_len] != 0) {
90 DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
91 return false;
92 }
93
94 DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
95
96 if (FioReadDword() != BSWAP32('RIFF')) {
97 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
98 return false;
99 }
100
101 uint32 total_size = FioReadDword();
102 uint header_size = 11;
103 if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
104 if (total_size + name_len + header_size > num) {
105 DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
106 return false;
107 }
108
109 if (FioReadDword() != BSWAP32('WAVE')) {
110 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
111 return false;
112 }
113
114 while (total_size >= 8) {
115 uint32 tag = FioReadDword();
116 uint32 size = FioReadDword();
117 total_size -= 8;
118 if (total_size < size) {
119 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
120 return false;
121 }
122 total_size -= size;
123
124 switch (tag) {
125 case ' tmf': // 'fmt '
126 /* Audio format, must be 1 (PCM) */
127 if (size < 16 || FioReadWord() != 1) {
128 DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
129 return false;
130 }
131 sound->channels = FioReadWord();
132 sound->rate = FioReadDword();
133 FioReadDword();
134 FioReadWord();
135 sound->bits_per_sample = FioReadWord();
136
137 /* The rest will be skipped */
138 size -= 16;
139 break;
140
141 case 'atad': // 'data'
142 sound->file_size = size;
143 sound->file_offset = FioGetPos();
144
145 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);
146 return true; // the fmt chunk has to appear before data, so we are finished
147
148 default:
149 /* Skip unknown chunks */
150 break;
151 }
152
153 /* Skip rest of chunk */
154 if (size > 0) FioSkipBytes(size);
155 }
156
157 DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
158
159 /* Clear everything that was read */
160 MemSetT(sound, 0);
161 return false;
162 }
163
170 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
171 {
172 /* Global sound? */
173 if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
174
175 sound_id -= ORIGINAL_SAMPLE_COUNT;
176 if (file == NULL || sound_id >= file->num_sounds) return INVALID_SOUND;
177
178 return file->sound_offset + sound_id;
179 }
180
187 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
188 {
189 if (!_settings_client.sound.vehicle) return true;
190
191 const GRFFile *file = v->GetGRF();
192 uint16 callback;
193
194 /* If the engine has no GRF ID associated it can't ever play any new sounds */
195 if (file == NULL) return false;
196
197 /* Check that the vehicle type uses the sound effect callback */
198 if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
199
200 callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
201 /* Play default sound if callback fails */
202 if (callback == CALLBACK_FAILED) return false;
203
204 callback = GetNewGRFSoundID(file, callback);
205
206 /* Play no sound, if result is invalid */
207 if (callback == INVALID_SOUND) return true;
208
209 assert(callback < GetNumSounds());
210 SndPlayVehicleFx(callback, v);
211 return true;
212 }
213
220 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
221 {
222 sound_id = GetNewGRFSoundID(file, sound_id);
223 if (sound_id == INVALID_SOUND) return;
224
225 assert(sound_id < GetNumSounds());
226 SndPlayTileFx(sound_id, tile);
227 }