Codechange: let NewGRF sounds make use of RandomAccessFile instead of the FIO slot functions

This commit is contained in:
Rubidium
2021-04-13 21:58:56 +02:00
committed by rubidium42
parent b144e56b2c
commit c097bc9d7d
7 changed files with 79 additions and 64 deletions

View File

@@ -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;
}
}