Improve sound_decoder.cc accuracy

This commit is contained in:
Alexander Batalov 2023-02-13 11:02:17 +03:00
parent f5d3cfb5e3
commit 36b5ceba8a
6 changed files with 111 additions and 116 deletions

View File

@ -7,7 +7,6 @@
#include "db.h" #include "db.h"
#include "debug.h" #include "debug.h"
#include "memory_manager.h" #include "memory_manager.h"
#include "pointer_registry.h"
#include "sound.h" #include "sound.h"
#include "sound_decoder.h" #include "sound_decoder.h"
@ -20,7 +19,7 @@ typedef enum AudioFlags {
typedef struct Audio { typedef struct Audio {
int flags; int flags;
int stream; File* stream;
SoundDecoder* soundDecoder; SoundDecoder* soundDecoder;
int fileSize; int fileSize;
int sampleRate; int sampleRate;
@ -29,7 +28,7 @@ typedef struct Audio {
} Audio; } Audio;
static bool defaultCompressionFunc(char* filePath); static bool defaultCompressionFunc(char* filePath);
static int audioSoundDecoderReadHandler(int fileHandle, void* buf, unsigned int size); static int audioSoundDecoderReadHandler(void* data, void* buf, unsigned int size);
// 0x5108BC // 0x5108BC
static AudioQueryCompressedFunc* queryCompressedFunc = defaultCompressionFunc; static AudioQueryCompressedFunc* queryCompressedFunc = defaultCompressionFunc;
@ -52,9 +51,9 @@ static bool defaultCompressionFunc(char* filePath)
} }
// 0x41A2D0 // 0x41A2D0
static int audioSoundDecoderReadHandler(int handle, void* buffer, unsigned int size) static int audioSoundDecoderReadHandler(void* data, void* buffer, unsigned int size)
{ {
return fileRead(buffer, 1, size, (File*)intToPtr(handle)); return fileRead(buffer, 1, size, reinterpret_cast<File*>(data));
} }
// AudioOpen // AudioOpen
@ -116,7 +115,7 @@ int audioOpen(const char* fname, int flags, ...)
Audio* audioFile = &(gAudioList[index]); Audio* audioFile = &(gAudioList[index]);
audioFile->flags = AUDIO_IN_USE; audioFile->flags = AUDIO_IN_USE;
audioFile->stream = ptrToInt(stream); audioFile->stream = stream;
if (compression == 2) { if (compression == 2) {
audioFile->flags |= AUDIO_COMPRESSED; audioFile->flags |= AUDIO_COMPRESSED;
@ -135,7 +134,7 @@ int audioOpen(const char* fname, int flags, ...)
int audioClose(int handle) int audioClose(int handle)
{ {
Audio* audioFile = &(gAudioList[handle - 1]); Audio* audioFile = &(gAudioList[handle - 1]);
fileClose((File*)intToPtr(audioFile->stream, true)); fileClose(audioFile->stream);
if ((audioFile->flags & AUDIO_COMPRESSED) != 0) { if ((audioFile->flags & AUDIO_COMPRESSED) != 0) {
soundDecoderFree(audioFile->soundDecoder); soundDecoderFree(audioFile->soundDecoder);
@ -155,7 +154,7 @@ int audioRead(int handle, void* buffer, unsigned int size)
if ((audioFile->flags & AUDIO_COMPRESSED) != 0) { if ((audioFile->flags & AUDIO_COMPRESSED) != 0) {
bytesRead = soundDecoderDecode(audioFile->soundDecoder, buffer, size); bytesRead = soundDecoderDecode(audioFile->soundDecoder, buffer, size);
} else { } else {
bytesRead = fileRead(buffer, 1, size, (File*)intToPtr(audioFile->stream)); bytesRead = fileRead(buffer, 1, size, audioFile->stream);
} }
audioFile->position += bytesRead; audioFile->position += bytesRead;
@ -189,7 +188,7 @@ long audioSeek(int handle, long offset, int origin)
if ((audioFile->flags & AUDIO_COMPRESSED) != 0) { if ((audioFile->flags & AUDIO_COMPRESSED) != 0) {
if (pos < audioFile->position) { if (pos < audioFile->position) {
soundDecoderFree(audioFile->soundDecoder); soundDecoderFree(audioFile->soundDecoder);
fileSeek((File*)intToPtr(audioFile->stream), 0, SEEK_SET); fileSeek(audioFile->stream, 0, SEEK_SET);
audioFile->soundDecoder = soundDecoderInit(audioSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize)); audioFile->soundDecoder = soundDecoderInit(audioSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize));
audioFile->position = 0; audioFile->position = 0;
audioFile->fileSize *= 2; audioFile->fileSize *= 2;
@ -224,7 +223,7 @@ long audioSeek(int handle, long offset, int origin)
return audioFile->position; return audioFile->position;
} else { } else {
return fileSeek((File*)intToPtr(audioFile->stream), offset, origin); return fileSeek(audioFile->stream, offset, origin);
} }
} }

View File

@ -7,7 +7,6 @@
#include "debug.h" #include "debug.h"
#include "memory_manager.h" #include "memory_manager.h"
#include "platform_compat.h" #include "platform_compat.h"
#include "pointer_registry.h"
#include "sound.h" #include "sound.h"
#include "sound_decoder.h" #include "sound_decoder.h"
@ -20,7 +19,7 @@ typedef enum AudioFileFlags {
typedef struct AudioFile { typedef struct AudioFile {
int flags; int flags;
int stream; FILE* stream;
SoundDecoder* soundDecoder; SoundDecoder* soundDecoder;
int fileSize; int fileSize;
int sampleRate; int sampleRate;
@ -29,7 +28,7 @@ typedef struct AudioFile {
} AudioFile; } AudioFile;
static bool defaultCompressionFunc(char* filePath); static bool defaultCompressionFunc(char* filePath);
static int audioFileSoundDecoderReadHandler(int handle, void* buffer, unsigned int size); static int audioFileSoundDecoderReadHandler(void* data, void* buffer, unsigned int size);
// 0x5108C0 // 0x5108C0
static AudioFileQueryCompressedFunc* queryCompressedFunc = defaultCompressionFunc; static AudioFileQueryCompressedFunc* queryCompressedFunc = defaultCompressionFunc;
@ -52,9 +51,9 @@ static bool defaultCompressionFunc(char* filePath)
} }
// 0x41A870 // 0x41A870
static int audioFileSoundDecoderReadHandler(int handle, void* buffer, unsigned int size) static int audioFileSoundDecoderReadHandler(void* data, void* buffer, unsigned int size)
{ {
return fread(buffer, 1, size, (FILE*)intToPtr(handle)); return fread(buffer, 1, size, reinterpret_cast<FILE*>(data));
} }
// 0x41A88C // 0x41A88C
@ -114,7 +113,7 @@ int audioFileOpen(const char* fname, int flags, ...)
AudioFile* audioFile = &(gAudioFileList[index]); AudioFile* audioFile = &(gAudioFileList[index]);
audioFile->flags = AUDIO_FILE_IN_USE; audioFile->flags = AUDIO_FILE_IN_USE;
audioFile->stream = ptrToInt(stream); audioFile->stream = stream;
if (compression == 2) { if (compression == 2) {
audioFile->flags |= AUDIO_FILE_COMPRESSED; audioFile->flags |= AUDIO_FILE_COMPRESSED;
@ -133,7 +132,7 @@ int audioFileOpen(const char* fname, int flags, ...)
int audioFileClose(int handle) int audioFileClose(int handle)
{ {
AudioFile* audioFile = &(gAudioFileList[handle - 1]); AudioFile* audioFile = &(gAudioFileList[handle - 1]);
fclose((FILE*)intToPtr(audioFile->stream, true)); fclose(audioFile->stream);
if ((audioFile->flags & AUDIO_FILE_COMPRESSED) != 0) { if ((audioFile->flags & AUDIO_FILE_COMPRESSED) != 0) {
soundDecoderFree(audioFile->soundDecoder); soundDecoderFree(audioFile->soundDecoder);
@ -155,7 +154,7 @@ int audioFileRead(int handle, void* buffer, unsigned int size)
if ((ptr->flags & AUDIO_FILE_COMPRESSED) != 0) { if ((ptr->flags & AUDIO_FILE_COMPRESSED) != 0) {
bytesRead = soundDecoderDecode(ptr->soundDecoder, buffer, size); bytesRead = soundDecoderDecode(ptr->soundDecoder, buffer, size);
} else { } else {
bytesRead = fread(buffer, 1, size, (FILE*)intToPtr(ptr->stream)); bytesRead = fread(buffer, 1, size, ptr->stream);
} }
ptr->position += bytesRead; ptr->position += bytesRead;
@ -190,7 +189,7 @@ long audioFileSeek(int handle, long offset, int origin)
if (a4 <= audioFile->position) { if (a4 <= audioFile->position) {
soundDecoderFree(audioFile->soundDecoder); soundDecoderFree(audioFile->soundDecoder);
fseek((FILE*)intToPtr(audioFile->stream), 0, 0); fseek(audioFile->stream, 0, 0);
audioFile->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize)); audioFile->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize));
audioFile->fileSize *= 2; audioFile->fileSize *= 2;
@ -222,7 +221,7 @@ long audioFileSeek(int handle, long offset, int origin)
return audioFile->position; return audioFile->position;
} }
return fseek((FILE*)intToPtr(audioFile->stream), offset, origin); return fseek(audioFile->stream, offset, origin);
} }
// 0x41AD20 // 0x41AD20

View File

@ -17,27 +17,27 @@ namespace fallout {
typedef int (*ReadBandFunc)(SoundDecoder* soundDecoder, int offset, int bits); typedef int (*ReadBandFunc)(SoundDecoder* soundDecoder, int offset, int bits);
static bool soundDecoderPrepare(SoundDecoder* a1, SoundDecoderReadProc* readProc, int fileHandle); static bool soundDecoderPrepare(SoundDecoder* soundDecoder, SoundDecoderReadProc* readProc, void* data);
static unsigned char soundDecoderReadNextChunk(SoundDecoder* a1); static unsigned char soundDecoderReadNextChunk(SoundDecoder* soundDecoder);
static void _init_pack_tables(); static void init_pack_tables();
static int _ReadBand_Fail_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fail(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt0_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt0(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt3_16_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt3_16(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt17_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt17(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt18_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt18(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt19_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt19(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt20_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt20(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt21_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt21(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt22_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt22(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt23_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt23(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt24_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt24(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt26_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt26(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt27_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt27(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBand_Fmt29_(SoundDecoder* soundDecoder, int offset, int bits); static int ReadBand_Fmt29(SoundDecoder* soundDecoder, int offset, int bits);
static int _ReadBands_(SoundDecoder* ptr); static int ReadBands(SoundDecoder* ptr);
static void _untransform_subband0(unsigned char* a1, unsigned char* a2, int a3, int a4); static void untransform_subband0(unsigned char* a1, unsigned char* a2, int a3, int a4);
static void _untransform_subband(unsigned char* a1, unsigned char* a2, int a3, int a4); static void untransform_subband(unsigned char* a1, unsigned char* a2, int a3, int a4);
static void _untransform_all(SoundDecoder* a1); static void untransform_all(SoundDecoder* soundDecoder);
static inline void soundDecoderRequireBits(SoundDecoder* soundDecoder, int bits); static inline void soundDecoderRequireBits(SoundDecoder* soundDecoder, int bits);
static inline void soundDecoderDropBits(SoundDecoder* soundDecoder, int bits); static inline void soundDecoderDropBits(SoundDecoder* soundDecoder, int bits);
@ -47,38 +47,38 @@ static int gSoundDecodersCount = 0;
// 0x51E330 // 0x51E330
static ReadBandFunc _ReadBand_tbl[32] = { static ReadBandFunc _ReadBand_tbl[32] = {
_ReadBand_Fmt0_, ReadBand_Fmt0,
_ReadBand_Fail_, ReadBand_Fail,
_ReadBand_Fail_, ReadBand_Fail,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt3_16_, ReadBand_Fmt3_16,
_ReadBand_Fmt17_, ReadBand_Fmt17,
_ReadBand_Fmt18_, ReadBand_Fmt18,
_ReadBand_Fmt19_, ReadBand_Fmt19,
_ReadBand_Fmt20_, ReadBand_Fmt20,
_ReadBand_Fmt21_, ReadBand_Fmt21,
_ReadBand_Fmt22_, ReadBand_Fmt22,
_ReadBand_Fmt23_, ReadBand_Fmt23,
_ReadBand_Fmt24_, ReadBand_Fmt24,
_ReadBand_Fail_, ReadBand_Fail,
_ReadBand_Fmt26_, ReadBand_Fmt26,
_ReadBand_Fmt27_, ReadBand_Fmt27,
_ReadBand_Fail_, ReadBand_Fail,
_ReadBand_Fmt29_, ReadBand_Fmt29,
_ReadBand_Fail_, ReadBand_Fail,
_ReadBand_Fail_, ReadBand_Fail,
}; };
// 0x6AD960 // 0x6AD960
@ -97,10 +97,10 @@ static unsigned char* _AudioDecoder_scale0;
static unsigned char* _AudioDecoder_scale_tbl; static unsigned char* _AudioDecoder_scale_tbl;
// 0x4D3BB0 // 0x4D3BB0
static bool soundDecoderPrepare(SoundDecoder* soundDecoder, SoundDecoderReadProc* readProc, int fileHandle) static bool soundDecoderPrepare(SoundDecoder* soundDecoder, SoundDecoderReadProc* readProc, void* data)
{ {
soundDecoder->readProc = readProc; soundDecoder->readProc = readProc;
soundDecoder->fd = fileHandle; soundDecoder->data = data;
soundDecoder->bufferIn = (unsigned char*)malloc(SOUND_DECODER_IN_BUFFER_SIZE); soundDecoder->bufferIn = (unsigned char*)malloc(SOUND_DECODER_IN_BUFFER_SIZE);
if (soundDecoder->bufferIn == NULL) { if (soundDecoder->bufferIn == NULL) {
@ -116,7 +116,7 @@ static bool soundDecoderPrepare(SoundDecoder* soundDecoder, SoundDecoderReadProc
// 0x4D3BE0 // 0x4D3BE0
static unsigned char soundDecoderReadNextChunk(SoundDecoder* soundDecoder) static unsigned char soundDecoderReadNextChunk(SoundDecoder* soundDecoder)
{ {
soundDecoder->remainingInSize = soundDecoder->readProc(soundDecoder->fd, soundDecoder->bufferIn, soundDecoder->bufferInSize); soundDecoder->remainingInSize = soundDecoder->readProc(soundDecoder->data, soundDecoder->bufferIn, soundDecoder->bufferInSize);
if (soundDecoder->remainingInSize == 0) { if (soundDecoder->remainingInSize == 0) {
memset(soundDecoder->bufferIn, 0, soundDecoder->bufferInSize); memset(soundDecoder->bufferIn, 0, soundDecoder->bufferInSize);
soundDecoder->remainingInSize = soundDecoder->bufferInSize; soundDecoder->remainingInSize = soundDecoder->bufferInSize;
@ -128,7 +128,7 @@ static unsigned char soundDecoderReadNextChunk(SoundDecoder* soundDecoder)
} }
// 0x4D3C78 // 0x4D3C78
static void _init_pack_tables() static void init_pack_tables()
{ {
// 0x51E32C // 0x51E32C
static bool inited = false; static bool inited = false;
@ -167,13 +167,13 @@ static void _init_pack_tables()
} }
// 0x4D3D9C // 0x4D3D9C
static int _ReadBand_Fail_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fail(SoundDecoder* soundDecoder, int offset, int bits)
{ {
return 0; return 0;
} }
// 0x4D3DA0 // 0x4D3DA0
static int _ReadBand_Fmt0_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt0(SoundDecoder* soundDecoder, int offset, int bits)
{ {
int* p = (int*)soundDecoder->samples; int* p = (int*)soundDecoder->samples;
p += offset; p += offset;
@ -189,7 +189,7 @@ static int _ReadBand_Fmt0_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D3DC8 // 0x4D3DC8
static int _ReadBand_Fmt3_16_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt3_16(SoundDecoder* soundDecoder, int offset, int bits)
{ {
int value; int value;
int v14; int v14;
@ -218,7 +218,7 @@ static int _ReadBand_Fmt3_16_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D3E90 // 0x4D3E90
static int _ReadBand_Fmt17_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt17(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -272,7 +272,7 @@ static int _ReadBand_Fmt17_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D3F98 // 0x4D3F98
static int _ReadBand_Fmt18_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt18(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -310,7 +310,7 @@ static int _ReadBand_Fmt18_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4068 // 0x4D4068
static int _ReadBand_Fmt19_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt19(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
base -= 1; base -= 1;
@ -348,7 +348,7 @@ static int _ReadBand_Fmt19_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4158 // 0x4D4158
static int _ReadBand_Fmt20_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt20(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -411,7 +411,7 @@ static int _ReadBand_Fmt20_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4254 // 0x4D4254
static int _ReadBand_Fmt21_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt21(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -458,7 +458,7 @@ static int _ReadBand_Fmt21_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4338 // 0x4D4338
static int _ReadBand_Fmt22_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt22(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
base -= 2; base -= 2;
@ -500,7 +500,7 @@ static int _ReadBand_Fmt22_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4434 // 0x4D4434
static int _ReadBand_Fmt23_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt23(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -569,7 +569,7 @@ static int _ReadBand_Fmt23_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4584 // 0x4D4584
static int _ReadBand_Fmt24_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt24(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -623,7 +623,7 @@ static int _ReadBand_Fmt24_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4698 // 0x4D4698
static int _ReadBand_Fmt26_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt26(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -679,7 +679,7 @@ static int _ReadBand_Fmt26_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D47A4 // 0x4D47A4
static int _ReadBand_Fmt27_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt27(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -719,7 +719,7 @@ static int _ReadBand_Fmt27_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D4870 // 0x4D4870
static int _ReadBand_Fmt29_(SoundDecoder* soundDecoder, int offset, int bits) static int ReadBand_Fmt29(SoundDecoder* soundDecoder, int offset, int bits)
{ {
short* base = (short*)_AudioDecoder_scale0; short* base = (short*)_AudioDecoder_scale0;
@ -751,7 +751,7 @@ static int _ReadBand_Fmt29_(SoundDecoder* soundDecoder, int offset, int bits)
} }
// 0x4D493C // 0x4D493C
static int _ReadBands_(SoundDecoder* soundDecoder) static int ReadBands(SoundDecoder* soundDecoder)
{ {
int v9; int v9;
int v15; int v15;
@ -788,7 +788,7 @@ static int _ReadBands_(SoundDecoder* soundDecoder)
v21 -= v15; v21 -= v15;
} }
_init_pack_tables(); init_pack_tables();
for (int index = 0; index < soundDecoder->subbands; index++) { for (int index = 0; index < soundDecoder->subbands; index++) {
soundDecoderRequireBits(soundDecoder, 5); soundDecoderRequireBits(soundDecoder, 5);
@ -804,7 +804,7 @@ static int _ReadBands_(SoundDecoder* soundDecoder)
} }
// 0x4D4ADC // 0x4D4ADC
static void _untransform_subband0(unsigned char* a1, unsigned char* a2, int a3, int a4) static void untransform_subband0(unsigned char* a1, unsigned char* a2, int a3, int a4)
{ {
short* p; short* p;
@ -902,7 +902,7 @@ static void _untransform_subband0(unsigned char* a1, unsigned char* a2, int a3,
} }
// 0x4D4D1C // 0x4D4D1C
static void _untransform_subband(unsigned char* a1, unsigned char* a2, int a3, int a4) static void untransform_subband(unsigned char* a1, unsigned char* a2, int a3, int a4)
{ {
int v13; int v13;
int* v14; int* v14;
@ -993,7 +993,7 @@ static void _untransform_subband(unsigned char* a1, unsigned char* a2, int a3, i
} }
// 0x4D4E80 // 0x4D4E80
static void _untransform_all(SoundDecoder* soundDecoder) static void untransform_all(SoundDecoder* soundDecoder)
{ {
int v8; int v8;
unsigned char* ptr; unsigned char* ptr;
@ -1019,7 +1019,7 @@ static void _untransform_all(SoundDecoder* soundDecoder)
v4 *= 2; v4 *= 2;
_untransform_subband0(soundDecoder->prev_samples, ptr, v3, v4); untransform_subband0(soundDecoder->prev_samples, ptr, v3, v4);
v5 = (int*)ptr; v5 = (int*)ptr;
for (v6 = 0; v6 < v4; v6++) { for (v6 = 0; v6 < v4; v6++) {
@ -1034,7 +1034,7 @@ static void _untransform_all(SoundDecoder* soundDecoder)
if (v3 == 0) { if (v3 == 0) {
break; break;
} }
_untransform_subband(j, ptr, v3, v4); untransform_subband(j, ptr, v3, v4);
j += 8 * v3; j += 8 * v3;
} }
@ -1063,11 +1063,11 @@ size_t soundDecoderDecode(SoundDecoder* soundDecoder, void* buffer, size_t size)
break; break;
} }
if (!_ReadBands_(soundDecoder)) { if (!ReadBands(soundDecoder)) {
break; break;
} }
_untransform_all(soundDecoder); untransform_all(soundDecoder);
soundDecoder->file_cnt -= soundDecoder->total_samples; soundDecoder->file_cnt -= soundDecoder->total_samples;
soundDecoder->samp_ptr = soundDecoder->samples; soundDecoder->samp_ptr = soundDecoder->samples;
@ -1122,7 +1122,7 @@ void soundDecoderFree(SoundDecoder* soundDecoder)
} }
// 0x4D50A8 // 0x4D50A8
SoundDecoder* soundDecoderInit(SoundDecoderReadProc* readProc, int fileHandle, int* channelsPtr, int* sampleRatePtr, int* sampleCountPtr) SoundDecoder* soundDecoderInit(SoundDecoderReadProc* readProc, void* data, int* channelsPtr, int* sampleRatePtr, int* sampleCountPtr)
{ {
int v14; int v14;
int v20; int v20;
@ -1137,7 +1137,7 @@ SoundDecoder* soundDecoderInit(SoundDecoderReadProc* readProc, int fileHandle, i
gSoundDecodersCount++; gSoundDecodersCount++;
if (!soundDecoderPrepare(soundDecoder, readProc, fileHandle)) { if (!soundDecoderPrepare(soundDecoder, readProc, data)) {
goto L66; goto L66;
} }

View File

@ -5,11 +5,11 @@
namespace fallout { namespace fallout {
typedef int(SoundDecoderReadProc)(int fileHandle, void* buffer, unsigned int size); typedef int(SoundDecoderReadProc)(void* data, void* buffer, unsigned int size);
typedef struct SoundDecoder { typedef struct SoundDecoder {
SoundDecoderReadProc* readProc; SoundDecoderReadProc* readProc;
int fd; void* data;
unsigned char* bufferIn; unsigned char* bufferIn;
size_t bufferInSize; size_t bufferInSize;
@ -41,7 +41,7 @@ typedef struct SoundDecoder {
size_t soundDecoderDecode(SoundDecoder* soundDecoder, void* buffer, size_t size); size_t soundDecoderDecode(SoundDecoder* soundDecoder, void* buffer, size_t size);
void soundDecoderFree(SoundDecoder* soundDecoder); void soundDecoderFree(SoundDecoder* soundDecoder);
SoundDecoder* soundDecoderInit(SoundDecoderReadProc* readProc, int fileHandle, int* channelsPtr, int* sampleRatePtr, int* sampleCountPtr); SoundDecoder* soundDecoderInit(SoundDecoderReadProc* readProc, void* data, int* channelsPtr, int* sampleRatePtr, int* sampleCountPtr);
} // namespace fallout } // namespace fallout

View File

@ -40,7 +40,7 @@ static void soundEffectsCacheFreeHandles();
static int soundEffectsCreate(int* handlePtr, int id, void* data, CacheEntry* cacheHandle); static int soundEffectsCreate(int* handlePtr, int id, void* data, CacheEntry* cacheHandle);
static bool soundEffectsIsValidHandle(int a1); static bool soundEffectsIsValidHandle(int a1);
static int soundEffectsCacheFileReadCompressed(int handle, void* buf, unsigned int size); static int soundEffectsCacheFileReadCompressed(int handle, void* buf, unsigned int size);
static int _sfxc_ad_reader(int handle, void* buf, unsigned int size); static int soundEffectsCacheSoundDecoderReadHandler(void* data, void* buf, unsigned int size);
// 0x50DE04 // 0x50DE04
static const char* off_50DE04 = ""; static const char* off_50DE04 = "";
@ -476,7 +476,7 @@ static int soundEffectsCacheFileReadCompressed(int handle, void* buf, unsigned i
int channels; int channels;
int sampleRate; int sampleRate;
int sampleCount; int sampleCount;
SoundDecoder* soundDecoder = soundDecoderInit(_sfxc_ad_reader, handle, &channels, &sampleRate, &sampleCount); SoundDecoder* soundDecoder = soundDecoderInit(soundEffectsCacheSoundDecoderReadHandler, &handle, &channels, &sampleRate, &sampleCount);
if (soundEffect->position != 0) { if (soundEffect->position != 0) {
void* temp = internal_malloc(soundEffect->position); void* temp = internal_malloc(soundEffect->position);
@ -505,12 +505,13 @@ static int soundEffectsCacheFileReadCompressed(int handle, void* buf, unsigned i
} }
// 0x4A9774 // 0x4A9774
static int _sfxc_ad_reader(int handle, void* buf, unsigned int size) static int soundEffectsCacheSoundDecoderReadHandler(void* data, void* buf, unsigned int size)
{ {
if (size == 0) { if (size == 0) {
return 0; return 0;
} }
int handle = *reinterpret_cast<int*>(data);
SoundEffect* soundEffect = &(gSoundEffects[handle]); SoundEffect* soundEffect = &(gSoundEffects[handle]);
unsigned int bytesToRead = soundEffect->fileSize - soundEffect->dataPosition; unsigned int bytesToRead = soundEffect->fileSize - soundEffect->dataPosition;

View File

@ -9,7 +9,6 @@
#include "debug.h" #include "debug.h"
#include "memory.h" #include "memory.h"
#include "platform_compat.h" #include "platform_compat.h"
#include "pointer_registry.h"
#include "sound_decoder.h" #include "sound_decoder.h"
namespace fallout { namespace fallout {
@ -28,7 +27,7 @@ static int soundEffectsListCopyFileNames(char** fileNameList);
static int soundEffectsListPopulateFileSizes(); static int soundEffectsListPopulateFileSizes();
static int soundEffectsListSort(); static int soundEffectsListSort();
static int soundEffectsListCompareByName(const void* a1, const void* a2); static int soundEffectsListCompareByName(const void* a1, const void* a2);
static int _sfxl_ad_reader(int fileHandle, void* buf, unsigned int size); static int soundEffectsListSoundDecoderReadHandler(void* data, void* buf, unsigned int size);
// 0x51C8F8 // 0x51C8F8
static bool gSoundEffectsListInitialized = false; static bool gSoundEffectsListInitialized = false;
@ -424,16 +423,13 @@ static int soundEffectsListPopulateFileSizes()
return 1; return 1;
} }
int fileHandle = ptrToInt((void*)stream);
int channels; int channels;
int sampleRate; int sampleRate;
int sampleCount; int sampleCount;
SoundDecoder* soundDecoder = soundDecoderInit(_sfxl_ad_reader, fileHandle, &channels, &sampleRate, &sampleCount); SoundDecoder* soundDecoder = soundDecoderInit(soundEffectsListSoundDecoderReadHandler, stream, &channels, &sampleRate, &sampleCount);
entry->dataSize = 2 * sampleCount; entry->dataSize = 2 * sampleCount;
soundDecoderFree(soundDecoder); soundDecoderFree(soundDecoder);
fileClose(stream); fileClose(stream);
intToPtr(fileHandle, true);
} }
break; break;
default: default:
@ -468,9 +464,9 @@ static int soundEffectsListCompareByName(const void* a1, const void* a2)
} }
// read via xfile // read via xfile
static int _sfxl_ad_reader(int fileHandle, void* buf, unsigned int size) static int soundEffectsListSoundDecoderReadHandler(void* data, void* buf, unsigned int size)
{ {
return fileRead(buf, 1, size, (File*)intToPtr(fileHandle)); return fileRead(buf, 1, size, reinterpret_cast<File*>(data));
} }
} // namespace fallout } // namespace fallout