23bool LoadSoundData(
SoundEntry &sound,
bool new_format, SoundID sound_id,
const std::string &name)
26 if (sound.file_size == 0 || sound.file_size > SIZE_MAX - 2)
return false;
28 size_t pos = sound.file->
GetPos();
29 sound.data = std::make_shared<std::vector<uint8_t>>();
31 sound.file->
SeekTo(pos, SEEK_SET);
32 if (loader->Load(sound, new_format, *sound.data))
break;
35 if (sound.data->empty()) {
40 assert(sound.bits_per_sample == 8 || sound.bits_per_sample == 16);
41 assert(sound.channels == 1);
42 assert(sound.rate != 0);
44 Debug(grf, 2,
"LoadSound [{}]: channels {}, sample rate {}, bits per sample {}, length {}", sound.file->
GetSimplifiedFilename(), sound.channels, sound.rate, sound.bits_per_sample, sound.file_size);
47 sound.data->resize(sound.data->size() + sound.channels * sound.bits_per_sample / 8);
48 sound.data->shrink_to_fit();
53static bool LoadBasesetSound(
SoundEntry &sound,
bool new_format, SoundID sound_id)
55 sound.file->
SeekTo(sound.file_offset, SEEK_SET);
58 size_t name_len = sound.file->
ReadByte();
59 std::string name(name_len,
'\0');
60 sound.file->
ReadBlock(name.data(), name_len);
62 return LoadSoundData(sound, new_format, sound_id,
StrMakeValid(name));
65bool LoadSound(
SoundEntry &sound, SoundID sound_id)
67 switch (sound.source) {
68 case SoundSource::BasesetOldFormat:
return LoadBasesetSound(sound,
false, sound_id);
69 case SoundSource::BasesetNewFormat:
return LoadBasesetSound(sound,
true, sound_id);
71 default: NOT_REACHED();
The ProviderManager manages a single Provider-type.
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Base interface for a SoundLoader implementation.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
bool LoadNewGRFSound(SoundEntry &sound, SoundID sound_id)
Extract meta data from a NewGRF sound.
Functions related to NewGRF provided sounds.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Functions related to sound loaders.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Functions related to low-level strings.