diff options
author | Rubidium <rubidium@openttd.org> | 2021-04-13 21:58:56 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-08 12:39:34 +0200 |
commit | c097bc9d7d610b613f76152b3cdcb9be2af7a1c7 (patch) | |
tree | 1f6f7fad0d4b12626bc626a08127ebf50cd6721b /src/sound.cpp | |
parent | b144e56b2c4d92baa868435818a99faa8ed06439 (diff) | |
download | openttd-c097bc9d7d610b613f76152b3cdcb9be2af7a1c7.tar.xz |
Codechange: let NewGRF sounds make use of RandomAccessFile instead of the FIO slot functions
Diffstat (limited to 'src/sound.cpp')
-rw-r--r-- | src/sound.cpp | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/src/sound.cpp b/src/sound.cpp index d91476729..413c70af2 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -11,7 +11,7 @@ #include "landscape.h" #include "mixer.h" #include "newgrf_sound.h" -#include "fios.h" +#include "random_access_file_type.h" #include "window_gui.h" #include "vehicle_base.h" @@ -25,14 +25,20 @@ static SoundEntry _original_sounds[ORIGINAL_SAMPLE_COUNT]; static void OpenBankFile(const char *filename) { + /** + * The sound file for the original sounds, i.e. those not defined/overridden by a NewGRF. + * Needs to be kept alive during the game as _original_sounds[n].file refers to this. + */ + static std::unique_ptr<RandomAccessFile> original_sound_file; + memset(_original_sounds, 0, sizeof(_original_sounds)); /* If there is no sound file (nosound set), don't load anything */ if (filename == nullptr) return; - FioOpenFile(SOUND_SLOT, filename, BASESET_DIR); - size_t pos = FioGetPos(); - uint count = FioReadDword(); + original_sound_file.reset(new RandomAccessFile(filename, BASESET_DIR)); + size_t pos = original_sound_file->GetPos(); + uint count = original_sound_file->ReadDword(); /* The new format has the highest bit always set */ bool new_format = HasBit(count, 31); @@ -48,43 +54,43 @@ static void OpenBankFile(const char *filename) return; } - FioSeekTo(pos, SEEK_SET); + original_sound_file->SeekTo(pos, SEEK_SET); for (uint i = 0; i != ORIGINAL_SAMPLE_COUNT; i++) { - _original_sounds[i].file_slot = SOUND_SLOT; - _original_sounds[i].file_offset = GB(FioReadDword(), 0, 31) + pos; - _original_sounds[i].file_size = FioReadDword(); + _original_sounds[i].file = original_sound_file.get(); + _original_sounds[i].file_offset = GB(original_sound_file->ReadDword(), 0, 31) + pos; + _original_sounds[i].file_size = original_sound_file->ReadDword(); } for (uint i = 0; i != ORIGINAL_SAMPLE_COUNT; i++) { SoundEntry *sound = &_original_sounds[i]; char name[255]; - FioSeekTo(sound->file_offset, SEEK_SET); + original_sound_file->SeekTo(sound->file_offset, SEEK_SET); /* Check for special case, see else case */ - FioReadBlock(name, FioReadByte()); // Read the name of the sound + original_sound_file->ReadBlock(name, original_sound_file->ReadByte()); // Read the name of the sound if (new_format || strcmp(name, "Corrupt sound") != 0) { - FioSeekTo(12, SEEK_CUR); // Skip past RIFF header + original_sound_file->SeekTo(12, SEEK_CUR); // Skip past RIFF header /* Read riff tags */ for (;;) { - uint32 tag = FioReadDword(); - uint32 size = FioReadDword(); + uint32 tag = original_sound_file->ReadDword(); + uint32 size = original_sound_file->ReadDword(); if (tag == ' tmf') { - FioReadWord(); // wFormatTag - sound->channels = FioReadWord(); // wChannels - sound->rate = FioReadDword(); // samples per second - if (!new_format) sound->rate = 11025; // seems like all old samples should be played at this rate. - FioReadDword(); // avg bytes per second - FioReadWord(); // alignment - sound->bits_per_sample = FioReadByte(); // bits per sample - FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR); + original_sound_file->ReadWord(); // wFormatTag + sound->channels = original_sound_file->ReadWord(); // wChannels + sound->rate = original_sound_file->ReadDword(); // samples per second + if (!new_format) sound->rate = 11025; // seems like all old samples should be played at this rate. + original_sound_file->ReadDword(); // avg bytes per second + original_sound_file->ReadWord(); // alignment + sound->bits_per_sample = original_sound_file->ReadByte(); // bits per sample + original_sound_file->SeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR); } else if (tag == 'atad') { sound->file_size = size; - sound->file_slot = SOUND_SLOT; - sound->file_offset = FioGetPos(); + sound->file = original_sound_file.get(); + sound->file_offset = original_sound_file->GetPos(); break; } else { sound->file_size = 0; @@ -100,8 +106,8 @@ static void OpenBankFile(const char *filename) sound->channels = 1; sound->rate = 11025; sound->bits_per_sample = 8; - sound->file_slot = SOUND_SLOT; - sound->file_offset = FioGetPos(); + sound->file = original_sound_file.get(); + sound->file_offset = original_sound_file->GetPos(); } } } @@ -119,8 +125,9 @@ static bool SetBankSource(MixerChannel *mc, const SoundEntry *sound) mem[sound->file_size ] = 0; mem[sound->file_size + 1] = 0; - FioSeekToFile(sound->file_slot, sound->file_offset); - FioReadBlock(mem, sound->file_size); + RandomAccessFile *file = sound->file; + file->SeekTo(sound->file_offset, SEEK_SET); + file->ReadBlock(mem, sound->file_size); /* 16-bit PCM WAV files should be signed by default */ if (sound->bits_per_sample == 8) { @@ -163,10 +170,10 @@ static void StartSound(SoundID sound_id, float pan, uint volume) if (sound == nullptr) return; /* NewGRF sound that wasn't loaded yet? */ - if (sound->rate == 0 && sound->file_slot != 0) { + if (sound->rate == 0 && sound->file != nullptr) { if (!LoadNewGRFSound(sound)) { /* Mark as invalid. */ - sound->file_slot = 0; + sound->file = nullptr; return; } } |