fallout2-ce/src/sound.cc

1614 lines
41 KiB
C++
Raw Normal View History

2022-05-19 01:51:26 -07:00
#include "sound.h"
2022-09-15 02:38:23 -07:00
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
2022-05-19 01:51:26 -07:00
2022-05-29 14:40:09 -07:00
#ifdef _WIN32
2022-05-19 01:51:26 -07:00
#include <io.h>
2022-05-29 14:40:09 -07:00
#else
#include <fcntl.h>
#include <unistd.h>
#endif
2022-05-19 01:51:26 -07:00
2022-05-28 04:45:48 -07:00
#include <algorithm>
2022-07-12 02:09:53 -07:00
#include <SDL.h>
2022-09-15 02:38:23 -07:00
#include "audio_engine.h"
#include "debug.h"
#include "platform_compat.h"
2022-09-23 05:43:44 -07:00
namespace fallout {
2022-12-25 10:56:23 -08:00
typedef enum SoundStatusFlags {
SOUND_STATUS_DONE = 0x01,
SOUND_STATUS_IS_PLAYING = 0x02,
SOUND_STATUS_IS_FADING = 0x04,
SOUND_STATUS_IS_PAUSED = 0x08,
} SoundStatusFlags;
2022-07-04 23:15:43 -07:00
typedef char*(SoundFileNameMangler)(char*);
2022-08-06 03:48:16 -07:00
typedef struct FadeSound {
Sound* sound;
int deltaVolume;
int targetVolume;
int initialVolume;
int currentVolume;
2022-12-25 10:56:23 -08:00
int pause;
2022-08-06 03:48:16 -07:00
struct FadeSound* prev;
struct FadeSound* next;
} FadeSound;
2022-07-04 23:15:43 -07:00
static void* soundMallocProcDefaultImpl(size_t size);
static void* soundReallocProcDefaultImpl(void* ptr, size_t size);
static void soundFreeProcDefaultImpl(void* ptr);
static char* soundFileManglerDefaultImpl(char* fname);
static void _refreshSoundBuffers(Sound* sound);
static int _preloadBuffers(Sound* sound);
static int _soundRewind(Sound* sound);
static int _addSoundData(Sound* sound, unsigned char* buf, int size);
static int _soundSetData(Sound* sound, unsigned char* buf, int size);
static int soundContinue(Sound* sound);
static int _soundGetVolume(Sound* sound);
static void soundDeleteInternal(Sound* sound);
2022-07-12 02:09:53 -07:00
static Uint32 _doTimerEvent(Uint32 interval, void* param);
static void _removeTimedEvent(SDL_TimerID* timerId);
2022-08-06 03:48:16 -07:00
static void _removeFadeSound(FadeSound* fadeSound);
2022-07-04 23:15:43 -07:00
static void _fadeSounds();
2022-12-25 10:56:23 -08:00
static int _internalSoundFade(Sound* sound, int duration, int targetVolume, bool pause);
2022-07-04 23:15:43 -07:00
2022-05-19 01:51:26 -07:00
// 0x51D478
2022-08-06 03:48:16 -07:00
static FadeSound* _fadeHead = NULL;
2022-05-19 01:51:26 -07:00
// 0x51D47C
2022-08-06 03:48:16 -07:00
static FadeSound* _fadeFreeList = NULL;
2022-05-19 01:51:26 -07:00
// 0x51D488
2022-07-04 23:15:43 -07:00
static MallocProc* gSoundMallocProc = soundMallocProcDefaultImpl;
2022-05-19 01:51:26 -07:00
// 0x51D48C
2022-07-04 23:15:43 -07:00
static ReallocProc* gSoundReallocProc = soundReallocProcDefaultImpl;
2022-05-19 01:51:26 -07:00
// 0x51D490
2022-07-04 23:15:43 -07:00
static FreeProc* gSoundFreeProc = soundFreeProcDefaultImpl;
2022-05-19 01:51:26 -07:00
// 0x51D494
2022-07-04 23:15:43 -07:00
static SoundFileIO gSoundDefaultFileIO = {
2022-05-19 01:51:26 -07:00
open,
close,
2022-05-29 14:40:09 -07:00
compat_read,
compat_write,
compat_lseek,
compat_tell,
compat_filelength,
2022-05-19 01:51:26 -07:00
-1,
};
// 0x51D4B4
2022-07-04 23:15:43 -07:00
static SoundFileNameMangler* gSoundFileNameMangler = soundFileManglerDefaultImpl;
2022-05-19 01:51:26 -07:00
// 0x51D4B8
2022-07-04 23:15:43 -07:00
static const char* gSoundErrorDescriptions[SOUND_ERR_COUNT] = {
2022-05-19 01:51:26 -07:00
"sound.c: No error",
"sound.c: SOS driver not loaded",
"sound.c: SOS invalid pointer",
"sound.c: SOS detect initialized",
"sound.c: SOS fail on file open",
"sound.c: SOS memory fail",
"sound.c: SOS invalid driver ID",
"sound.c: SOS no driver found",
"sound.c: SOS detection failure",
"sound.c: SOS driver loaded",
"sound.c: SOS invalid handle",
"sound.c: SOS no handles",
"sound.c: SOS paused",
"sound.c: SOS not paused",
"sound.c: SOS invalid data",
"sound.c: SOS drv file fail",
"sound.c: SOS invalid port",
"sound.c: SOS invalid IRQ",
"sound.c: SOS invalid DMA",
"sound.c: SOS invalid DMA IRQ",
"sound.c: no device",
"sound.c: not initialized",
"sound.c: no sound",
"sound.c: function not supported",
"sound.c: no buffers available",
"sound.c: file not found",
"sound.c: already playing",
"sound.c: not playing",
"sound.c: already paused",
"sound.c: not paused",
"sound.c: invalid handle",
"sound.c: no memory available",
"sound.c: unknown error",
};
// 0x668150
2022-07-04 23:15:43 -07:00
static int gSoundLastError;
2022-05-19 01:51:26 -07:00
// 0x668154
2022-07-04 23:15:43 -07:00
static int _masterVol;
2022-05-19 01:51:26 -07:00
// 0x66815C
2022-07-04 23:15:43 -07:00
static int _sampleRate;
2022-05-19 01:51:26 -07:00
// Number of sounds currently playing.
//
// 0x668160
2022-07-04 23:15:43 -07:00
static int _numSounds;
2022-05-19 01:51:26 -07:00
// 0x668164
2022-07-04 23:15:43 -07:00
static int _deviceInit;
2022-05-19 01:51:26 -07:00
// 0x668168
2022-07-04 23:15:43 -07:00
static int _dataSize;
2022-05-19 01:51:26 -07:00
// 0x66816C
2022-07-04 23:15:43 -07:00
static int _numBuffers;
2022-05-19 01:51:26 -07:00
// 0x668170
2022-07-04 23:15:43 -07:00
static bool gSoundInitialized;
2022-05-19 01:51:26 -07:00
// 0x668174
2022-07-04 23:15:43 -07:00
static Sound* gSoundListHead;
2022-05-19 01:51:26 -07:00
2022-07-12 02:09:53 -07:00
static SDL_TimerID gFadeSoundsTimerId = 0;
2022-05-19 01:51:26 -07:00
// 0x4AC6F0
void* soundMallocProcDefaultImpl(size_t size)
{
return malloc(size);
}
// 0x4AC6F8
void* soundReallocProcDefaultImpl(void* ptr, size_t size)
{
return realloc(ptr, size);
}
// 0x4AC700
void soundFreeProcDefaultImpl(void* ptr)
{
free(ptr);
}
// 0x4AC708
void soundSetMemoryProcs(MallocProc* mallocProc, ReallocProc* reallocProc, FreeProc* freeProc)
{
gSoundMallocProc = mallocProc;
gSoundReallocProc = reallocProc;
gSoundFreeProc = freeProc;
}
// 0x4AC78C
char* soundFileManglerDefaultImpl(char* fname)
{
return fname;
}
// 0x4AC790
const char* soundGetErrorDescription(int err)
{
if (err == -1) {
err = gSoundLastError;
}
if (err < 0 || err > SOUND_UNKNOWN_ERROR) {
err = SOUND_UNKNOWN_ERROR;
}
return gSoundErrorDescriptions[err];
}
// 0x4AC7B0
void _refreshSoundBuffers(Sound* sound)
{
2022-12-25 10:56:23 -08:00
if ((sound->soundFlags & SOUND_FLAG_0x80) != 0) {
2022-05-19 01:51:26 -07:00
return;
}
2022-07-12 02:09:53 -07:00
unsigned int readPos;
unsigned int writePos;
bool hr = audioEngineSoundBufferGetCurrentPosition(sound->soundBuffer, &readPos, &writePos);
if (!hr) {
2022-05-19 01:51:26 -07:00
return;
}
2022-12-25 10:56:23 -08:00
if (readPos < sound->lastPosition) {
sound->numBytesRead += readPos + sound->numBuffers * sound->dataSize - sound->lastPosition;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
sound->numBytesRead += readPos - sound->lastPosition;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if ((sound->soundFlags & SOUND_FLAG_0x100) != 0) {
if ((sound->type & SOUND_TYPE_0x20) != 0) {
if ((sound->soundFlags & SOUND_FLAG_0x200) != 0) {
sound->soundFlags |= SOUND_FLAG_0x80;
2022-05-19 01:51:26 -07:00
}
} else {
2022-12-25 10:56:23 -08:00
if (sound->fileSize <= sound->numBytesRead) {
sound->soundFlags |= SOUND_FLAG_0x200 | SOUND_FLAG_0x80;
2022-05-19 01:51:26 -07:00
}
}
}
2022-12-25 10:56:23 -08:00
sound->lastPosition = readPos;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if (sound->fileSize < sound->numBytesRead) {
2022-05-19 01:51:26 -07:00
int v3;
do {
2022-12-25 10:56:23 -08:00
v3 = sound->numBytesRead - sound->fileSize;
sound->numBytesRead = v3;
} while (v3 > sound->fileSize);
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
int v6 = readPos / sound->dataSize;
if (sound->lastUpdate == v6) {
2022-05-19 01:51:26 -07:00
return;
}
int v53;
2022-12-25 10:56:23 -08:00
if (sound->lastUpdate > v6) {
v53 = v6 + sound->numBuffers - sound->lastUpdate;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
v53 = v6 - sound->lastUpdate;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (sound->dataSize * v53 >= sound->readLimit) {
v53 = (sound->readLimit + sound->dataSize - 1) / sound->dataSize;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (v53 < sound->minReadBuffer) {
2022-05-19 01:51:26 -07:00
return;
}
2022-07-12 02:09:53 -07:00
void* audioPtr1;
void* audioPtr2;
unsigned int audioBytes1;
unsigned int audioBytes2;
2022-12-25 10:56:23 -08:00
hr = audioEngineSoundBufferLock(sound->soundBuffer, sound->dataSize * sound->lastUpdate, sound->dataSize * v53, &audioPtr1, &audioBytes1, &audioPtr2, &audioBytes2, 0);
2022-07-12 02:09:53 -07:00
if (!hr) {
2022-05-19 01:51:26 -07:00
return;
}
2022-12-25 10:56:23 -08:00
if (audioBytes1 + audioBytes2 != sound->dataSize * v53) {
debugPrint("locked memory region not big enough, wanted %d (%d * %d), got %d (%d + %d)\n", sound->dataSize * v53, v53, sound->dataSize, audioBytes1 + audioBytes2, audioBytes1, audioBytes2);
debugPrint("Resetting readBuffers from %d to %d\n", v53, (audioBytes1 + audioBytes2) / sound->dataSize);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
v53 = (audioBytes1 + audioBytes2) / sound->dataSize;
if (v53 < sound->minReadBuffer) {
2022-05-19 01:51:26 -07:00
debugPrint("No longer above read buffer size, returning\n");
return;
}
}
unsigned char* audioPtr = (unsigned char*)audioPtr1;
int audioBytes = audioBytes1;
while (--v53 != -1) {
int bytesRead;
2022-12-25 10:56:23 -08:00
if ((sound->soundFlags & SOUND_FLAG_0x200) != 0) {
bytesRead = sound->dataSize;
memset(sound->data, 0, bytesRead);
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
int bytesToRead = sound->dataSize;
2022-05-19 01:51:26 -07:00
if (sound->field_58 != -1) {
int pos = sound->io.tell(sound->io.fd);
if (bytesToRead + pos > sound->field_58) {
bytesToRead = sound->field_58 - pos;
}
}
2022-12-25 10:56:23 -08:00
bytesRead = sound->io.read(sound->io.fd, sound->data, bytesToRead);
if (bytesRead < sound->dataSize) {
if ((sound->soundFlags & SOUND_LOOPING) == 0 || (sound->soundFlags & SOUND_FLAG_0x100) != 0) {
memset(sound->data + bytesRead, 0, sound->dataSize - bytesRead);
sound->soundFlags |= SOUND_FLAG_0x200;
bytesRead = sound->dataSize;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
while (bytesRead < sound->dataSize) {
if (sound->loops == -1) {
2022-05-19 01:51:26 -07:00
sound->io.seek(sound->io.fd, sound->field_54, SEEK_SET);
if (sound->callback != NULL) {
sound->callback(sound->callbackUserData, 0x0400);
}
} else {
2022-12-25 10:56:23 -08:00
if (sound->loops <= 0) {
2022-05-19 01:51:26 -07:00
sound->field_58 = -1;
sound->field_54 = 0;
2022-12-25 10:56:23 -08:00
sound->loops = 0;
sound->soundFlags &= ~SOUND_LOOPING;
bytesRead += sound->io.read(sound->io.fd, sound->data + bytesRead, sound->dataSize - bytesRead);
2022-05-19 01:51:26 -07:00
break;
}
2022-12-25 10:56:23 -08:00
sound->loops--;
2022-05-19 01:51:26 -07:00
sound->io.seek(sound->io.fd, sound->field_54, SEEK_SET);
if (sound->callback != NULL) {
sound->callback(sound->callbackUserData, 0x400);
}
}
if (sound->field_58 == -1) {
2022-12-25 10:56:23 -08:00
bytesToRead = sound->dataSize - bytesRead;
2022-05-19 01:51:26 -07:00
} else {
int pos = sound->io.tell(sound->io.fd);
2022-12-25 10:56:23 -08:00
if (sound->dataSize + bytesRead + pos <= sound->field_58) {
bytesToRead = sound->dataSize - bytesRead;
2022-05-19 01:51:26 -07:00
} else {
bytesToRead = sound->field_58 - bytesRead - pos;
}
}
2022-12-25 10:56:23 -08:00
int v20 = sound->io.read(sound->io.fd, sound->data + bytesRead, bytesToRead);
2022-05-19 01:51:26 -07:00
bytesRead += v20;
if (v20 < bytesToRead) {
break;
}
}
}
}
}
if (bytesRead > audioBytes) {
if (audioBytes != 0) {
2022-12-25 10:56:23 -08:00
memcpy(audioPtr, sound->data, audioBytes);
2022-05-19 01:51:26 -07:00
}
if (audioPtr2 != NULL) {
2022-12-25 10:56:23 -08:00
memcpy(audioPtr2, sound->data + audioBytes, bytesRead - audioBytes);
2022-05-19 01:51:26 -07:00
audioPtr = (unsigned char*)audioPtr2 + bytesRead - audioBytes;
audioBytes = audioBytes2 - bytesRead;
} else {
debugPrint("Hm, no second write pointer, but buffer not big enough, this shouldn't happen\n");
}
} else {
2022-12-25 10:56:23 -08:00
memcpy(audioPtr, sound->data, bytesRead);
2022-05-19 01:51:26 -07:00
audioPtr += bytesRead;
audioBytes -= bytesRead;
}
}
2022-07-12 02:09:53 -07:00
audioEngineSoundBufferUnlock(sound->soundBuffer, audioPtr1, audioBytes1, audioPtr2, audioBytes2);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
sound->lastUpdate = v6;
2022-05-19 01:51:26 -07:00
}
// 0x4ACC58
2022-12-25 10:56:23 -08:00
int soundInit(int a1, int numBuffers, int a3, int dataSize, int rate)
2022-05-19 01:51:26 -07:00
{
2022-07-12 02:09:53 -07:00
if (!audioEngineInit()) {
debugPrint("soundInit: Unable to init audio engine\n");
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_SOS_DETECTION_FAILURE;
return gSoundLastError;
}
_sampleRate = rate;
2022-12-25 10:56:23 -08:00
_dataSize = dataSize;
_numBuffers = numBuffers;
2022-05-19 01:51:26 -07:00
gSoundInitialized = true;
_deviceInit = 1;
_soundSetMasterVolume(VOLUME_MAX);
2022-07-12 02:09:53 -07:00
gSoundLastError = SOUND_NO_ERROR;
2022-05-29 10:07:43 -07:00
return gSoundLastError;
2022-05-19 01:51:26 -07:00
}
// 0x4AD04C
void soundExit()
{
while (gSoundListHead != NULL) {
Sound* next = gSoundListHead->next;
soundDelete(gSoundListHead);
gSoundListHead = next;
}
2022-07-12 02:09:53 -07:00
if (gFadeSoundsTimerId != 0) {
_removeTimedEvent(&gFadeSoundsTimerId);
2022-05-19 01:51:26 -07:00
}
while (_fadeFreeList != NULL) {
2022-08-06 03:48:16 -07:00
FadeSound* next = _fadeFreeList->next;
2022-05-19 01:51:26 -07:00
gSoundFreeProc(_fadeFreeList);
_fadeFreeList = next;
}
2022-07-12 02:09:53 -07:00
audioEngineExit();
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_ERROR;
gSoundInitialized = false;
}
// 0x4AD0FC
2022-12-25 10:56:23 -08:00
Sound* soundAllocate(int type, int soundFlags)
2022-05-19 01:51:26 -07:00
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return NULL;
}
2022-05-21 08:22:03 -07:00
Sound* sound = (Sound*)gSoundMallocProc(sizeof(*sound));
2022-05-19 01:51:26 -07:00
memset(sound, 0, sizeof(*sound));
memcpy(&(sound->io), &gSoundDefaultFileIO, sizeof(gSoundDefaultFileIO));
2022-12-25 10:56:23 -08:00
if ((soundFlags & SOUND_FLAG_0x02) == 0) {
soundFlags |= SOUND_FLAG_0x02;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
sound->bitsPerSample = (soundFlags & SOUND_16BIT) != 0 ? 16 : 8;
2022-07-12 02:09:53 -07:00
sound->channels = 1;
sound->rate = _sampleRate;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
sound->soundFlags = soundFlags;
sound->type = type;
sound->dataSize = _dataSize;
sound->numBytesRead = 0;
2022-07-12 02:09:53 -07:00
sound->soundBuffer = -1;
2022-12-25 10:56:23 -08:00
sound->statusFlags = 0;
sound->numBuffers = _numBuffers;
sound->readLimit = sound->dataSize * _numBuffers;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if ((type & SOUND_TYPE_INFINITE) != 0) {
sound->loops = -1;
sound->soundFlags |= SOUND_LOOPING;
2022-05-19 01:51:26 -07:00
}
sound->field_58 = -1;
2022-12-25 10:56:23 -08:00
sound->minReadBuffer = 1;
2022-05-19 01:51:26 -07:00
sound->volume = VOLUME_MAX;
sound->prev = NULL;
sound->field_54 = 0;
sound->next = gSoundListHead;
if (gSoundListHead != NULL) {
gSoundListHead->prev = sound;
}
gSoundListHead = sound;
return sound;
}
// 0x4AD308
int _preloadBuffers(Sound* sound)
{
unsigned char* buf;
int bytes_read;
int result;
int v15;
unsigned char* v14;
int size;
size = sound->io.filelength(sound->io.fd);
2022-12-25 10:56:23 -08:00
sound->fileSize = size;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_STREAMING) != 0) {
if ((sound->soundFlags & SOUND_LOOPING) == 0) {
sound->soundFlags |= SOUND_FLAG_0x100 | SOUND_LOOPING;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (sound->numBuffers * sound->dataSize >= size) {
if (size / sound->dataSize * sound->dataSize != size) {
size = (size / sound->dataSize + 1) * sound->dataSize;
2022-05-19 01:51:26 -07:00
}
} else {
2022-12-25 10:56:23 -08:00
size = sound->numBuffers * sound->dataSize;
2022-05-19 01:51:26 -07:00
}
} else {
2022-12-25 10:56:23 -08:00
sound->type &= ~(SOUND_TYPE_MEMORY | SOUND_TYPE_STREAMING);
sound->type |= SOUND_TYPE_MEMORY;
2022-05-19 01:51:26 -07:00
}
buf = (unsigned char*)gSoundMallocProc(size);
bytes_read = sound->io.read(sound->io.fd, buf, size);
if (bytes_read != size) {
2022-12-25 10:56:23 -08:00
if ((sound->soundFlags & SOUND_LOOPING) == 0 || (sound->soundFlags & SOUND_FLAG_0x100) != 0) {
2022-05-19 01:51:26 -07:00
memset(buf + bytes_read, 0, size - bytes_read);
} else {
v14 = buf + bytes_read;
v15 = bytes_read;
while (size - v15 > bytes_read) {
memcpy(v14, buf, bytes_read);
v15 += bytes_read;
v14 += bytes_read;
}
if (v15 < size) {
memcpy(v14, buf, size - v15);
}
}
}
result = _soundSetData(sound, buf, size);
gSoundFreeProc(buf);
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_MEMORY) != 0) {
2022-05-19 01:51:26 -07:00
sound->io.close(sound->io.fd);
sound->io.fd = -1;
} else {
2022-12-25 10:56:23 -08:00
if (sound->data == NULL) {
sound->data = (unsigned char*)gSoundMallocProc(sound->dataSize);
2022-05-19 01:51:26 -07:00
}
}
return result;
}
// 0x4AD498
int soundLoad(Sound* sound, char* filePath)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
sound->io.fd = sound->io.open(gSoundFileNameMangler(filePath), 0x0200);
if (sound->io.fd == -1) {
gSoundLastError = SOUND_FILE_NOT_FOUND;
return gSoundLastError;
}
return _preloadBuffers(sound);
}
// 0x4AD504
int _soundRewind(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_STREAMING) != 0) {
2022-05-19 01:51:26 -07:00
sound->io.seek(sound->io.fd, 0, SEEK_SET);
2022-12-25 10:56:23 -08:00
sound->lastUpdate = 0;
sound->lastPosition = 0;
sound->numBytesRead = 0;
sound->soundFlags &= ~(SOUND_FLAG_0x200 | SOUND_FLAG_0x80);
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferSetCurrentPosition(sound->soundBuffer, 0);
2022-05-19 01:51:26 -07:00
_preloadBuffers(sound);
} else {
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferSetCurrentPosition(sound->soundBuffer, 0);
2022-05-19 01:51:26 -07:00
}
2022-07-12 02:09:53 -07:00
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
sound->statusFlags &= ~SOUND_STATUS_DONE;
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AD5C8
int _addSoundData(Sound* sound, unsigned char* buf, int size)
{
2022-07-12 02:09:53 -07:00
bool hr;
void* audioPtr1;
unsigned int audioBytes1;
void* audioPtr2;
unsigned int audioBytes2;
hr = audioEngineSoundBufferLock(sound->soundBuffer, 0, size, &audioPtr1, &audioBytes1, &audioPtr2, &audioBytes2, AUDIO_ENGINE_SOUND_BUFFER_LOCK_FROM_WRITE_POS);
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
memcpy(audioPtr1, buf, audioBytes1);
2022-05-19 01:51:26 -07:00
2022-07-12 02:09:53 -07:00
if (audioPtr2 != NULL) {
memcpy(audioPtr2, buf + audioBytes1, audioBytes2);
2022-05-19 01:51:26 -07:00
}
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferUnlock(sound->soundBuffer, audioPtr1, audioBytes1, audioPtr2, audioBytes2);
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AD6C0
int _soundSetData(Sound* sound, unsigned char* buf, int size)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer == -1) {
sound->soundBuffer = audioEngineCreateSoundBuffer(size, sound->bitsPerSample, sound->channels, sound->rate);
if (sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
}
return _addSoundData(sound, buf, size);
}
// 0x4AD73C
int soundPlay(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
unsigned int readPos;
unsigned int writePos;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_DONE) != 0) {
2022-05-19 01:51:26 -07:00
_soundRewind(sound);
}
soundSetVolume(sound, sound->volume);
2022-12-25 10:56:23 -08:00
hr = audioEngineSoundBufferPlay(sound->soundBuffer, sound->soundFlags & SOUND_LOOPING ? AUDIO_ENGINE_SOUND_BUFFER_PLAY_LOOPING : 0);
2022-05-19 01:51:26 -07:00
2022-07-12 02:09:53 -07:00
audioEngineSoundBufferGetCurrentPosition(sound->soundBuffer, &readPos, &writePos);
2022-12-25 10:56:23 -08:00
sound->lastUpdate = readPos / sound->dataSize;
2022-05-19 01:51:26 -07:00
2022-07-12 02:09:53 -07:00
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
sound->statusFlags |= SOUND_STATUS_IS_PLAYING;
2022-05-19 01:51:26 -07:00
++_numSounds;
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AD828
int soundStop(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PLAYING) == 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NOT_PLAYING;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferStop(sound->soundBuffer);
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
sound->statusFlags &= ~SOUND_STATUS_IS_PLAYING;
2022-05-19 01:51:26 -07:00
_numSounds--;
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AD8DC
int soundDelete(Sound* sample)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sample == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
if (sample->io.fd != -1) {
sample->io.close(sample->io.fd);
sample->io.fd = -1;
}
soundDeleteInternal(sample);
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AD948
int soundContinue(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
unsigned int status;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PLAYING) == 0) {
gSoundLastError = SOUND_NOT_PLAYING;
return gSoundLastError;
}
if ((sound->statusFlags & SOUND_STATUS_IS_PAUSED) != 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NOT_PLAYING;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_DONE) != 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferGetStatus(sound->soundBuffer, &status);
if (!hr) {
2022-05-19 01:51:26 -07:00
debugPrint("Error in soundContinue, %x\n", hr);
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->soundFlags & SOUND_FLAG_0x80) == 0 && (status & (AUDIO_ENGINE_SOUND_BUFFER_STATUS_PLAYING | AUDIO_ENGINE_SOUND_BUFFER_STATUS_LOOPING)) != 0) {
if ((sound->statusFlags & SOUND_STATUS_IS_PAUSED) == 0 && (sound->type & SOUND_TYPE_STREAMING) != 0) {
2022-05-19 01:51:26 -07:00
_refreshSoundBuffers(sound);
}
2022-12-25 10:56:23 -08:00
} else if ((sound->statusFlags & SOUND_STATUS_IS_PAUSED) == 0) {
2022-05-19 01:51:26 -07:00
if (sound->callback != NULL) {
sound->callback(sound->callbackUserData, 1);
sound->callback = NULL;
}
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_FIRE_AND_FORGET) != 0) {
2022-05-19 01:51:26 -07:00
sound->callback = NULL;
soundDelete(sound);
} else {
2022-12-25 10:56:23 -08:00
sound->statusFlags |= SOUND_STATUS_DONE;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PLAYING) != 0) {
2022-05-19 01:51:26 -07:00
--_numSounds;
}
soundStop(sound);
2022-12-25 10:56:23 -08:00
sound->statusFlags &= ~(SOUND_STATUS_DONE | SOUND_STATUS_IS_PLAYING);
2022-05-19 01:51:26 -07:00
}
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4ADA84
bool soundIsPlaying(Sound* sound)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return false;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return false;
}
2022-12-25 10:56:23 -08:00
return (sound->statusFlags & SOUND_STATUS_IS_PLAYING) != 0;
2022-05-19 01:51:26 -07:00
}
// 0x4ADAC4
bool _soundDone(Sound* sound)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return false;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return false;
}
2022-12-25 10:56:23 -08:00
return (sound->statusFlags & SOUND_STATUS_DONE) != 0;
2022-05-19 01:51:26 -07:00
}
// 0x4ADB44
bool soundIsPaused(Sound* sound)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return false;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return false;
}
2022-12-25 10:56:23 -08:00
return (sound->statusFlags & SOUND_STATUS_IS_PAUSED) != 0;
2022-05-19 01:51:26 -07:00
}
// 0x4ADBC4
2022-12-25 10:56:23 -08:00
int _soundType(Sound* sound, int type)
2022-05-19 01:51:26 -07:00
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return 0;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return 0;
}
2022-12-25 10:56:23 -08:00
return sound->type & type;
2022-05-19 01:51:26 -07:00
}
// 0x4ADC04
int soundGetDuration(Sound* sound)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
int bytesPerSec = sound->bitsPerSample / 8 * sound->rate;
2022-12-25 10:56:23 -08:00
int v3 = sound->fileSize;
2022-05-19 01:51:26 -07:00
int v4 = v3 % bytesPerSec;
int result = v3 / bytesPerSec;
if (v4 != 0) {
result += 1;
}
return result;
}
// 0x4ADD00
2022-12-25 10:56:23 -08:00
int soundSetLooping(Sound* sound, int loops)
2022-05-19 01:51:26 -07:00
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if (loops != 0) {
sound->soundFlags |= SOUND_LOOPING;
sound->loops = loops;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
sound->loops = 0;
2022-05-19 01:51:26 -07:00
sound->field_58 = -1;
sound->field_54 = 0;
2022-12-25 10:56:23 -08:00
sound->soundFlags &= ~SOUND_LOOPING;
2022-05-19 01:51:26 -07:00
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4ADD68
int _soundVolumeHMItoDirectSound(int volume)
{
double normalizedVolume;
if (volume > VOLUME_MAX) {
volume = VOLUME_MAX;
}
2022-07-12 02:09:53 -07:00
// Normalize volume to SDL (0-128).
normalizedVolume = (double)(volume - VOLUME_MIN) / (double)(VOLUME_MAX - VOLUME_MIN) * 128;
2022-05-19 01:51:26 -07:00
return (int)normalizedVolume;
}
// 0x4ADE0C
int soundSetVolume(Sound* sound, int volume)
{
int normalizedVolume;
2022-07-12 02:09:53 -07:00
bool hr;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
sound->volume = volume;
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
normalizedVolume = _soundVolumeHMItoDirectSound(_masterVol * volume / VOLUME_MAX);
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferSetVolume(sound->soundBuffer, normalizedVolume);
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4ADE80
int _soundGetVolume(Sound* sound)
{
if (!_deviceInit) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
return sound->volume;
}
// 0x4ADFF0
int soundSetCallback(Sound* sound, SoundCallback* callback, void* userData)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
sound->callback = callback;
sound->callbackUserData = userData;
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AE02C
int soundSetChannels(Sound* sound, int channels)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
if (channels == 3) {
2022-07-12 02:09:53 -07:00
sound->channels = 2;
2022-05-19 01:51:26 -07:00
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AE0B0
int soundSetReadLimit(Sound* sound, int readLimit)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_DEVICE;
return gSoundLastError;
}
sound->readLimit = readLimit;
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// TODO: Check, looks like it uses couple of inlined functions.
//
// 0x4AE0E4
int soundPause(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
unsigned int readPos;
unsigned int writePos;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PLAYING) == 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NOT_PLAYING;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PAUSED) != 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_ALREADY_PAUSED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
hr = audioEngineSoundBufferGetCurrentPosition(sound->soundBuffer, &readPos, &writePos);
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
sound->pausePos = readPos;
sound->statusFlags |= SOUND_STATUS_IS_PAUSED;
2022-05-19 01:51:26 -07:00
return soundStop(sound);
}
// TODO: Check, looks like it uses couple of inlined functions.
//
// 0x4AE1F0
int soundResume(Sound* sound)
{
2022-07-12 02:09:53 -07:00
bool hr;
2022-05-19 01:51:26 -07:00
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound == NULL || sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PLAYING) != 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NOT_PAUSED;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_PAUSED) == 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NOT_PAUSED;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
hr = audioEngineSoundBufferSetCurrentPosition(sound->soundBuffer, sound->pausePos);
2022-07-12 02:09:53 -07:00
if (!hr) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
sound->statusFlags &= ~SOUND_STATUS_IS_PAUSED;
sound->pausePos = 0;
2022-05-19 01:51:26 -07:00
return soundPlay(sound);
}
// 0x4AE2FC
int soundSetFileIO(Sound* sound, SoundOpenProc* openProc, SoundCloseProc* closeProc, SoundReadProc* readProc, SoundWriteProc* writeProc, SoundSeekProc* seekProc, SoundTellProc* tellProc, SoundFileLengthProc* fileLengthProc)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
if (openProc != NULL) {
sound->io.open = openProc;
}
if (closeProc != NULL) {
sound->io.close = closeProc;
}
if (readProc != NULL) {
sound->io.read = readProc;
}
if (writeProc != NULL) {
sound->io.write = writeProc;
}
if (seekProc != NULL) {
sound->io.seek = seekProc;
}
if (tellProc != NULL) {
sound->io.tell = tellProc;
}
if (fileLengthProc != NULL) {
sound->io.filelength = fileLengthProc;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AE378
void soundDeleteInternal(Sound* sound)
{
2022-12-25 10:56:23 -08:00
Sound* next;
Sound* prev;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if ((sound->statusFlags & SOUND_STATUS_IS_FADING) != 0) {
FadeSound* fadeSound = _fadeHead;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
while (fadeSound != NULL) {
if (sound == fadeSound->sound) {
2022-05-19 01:51:26 -07:00
break;
}
2022-12-25 10:56:23 -08:00
fadeSound = fadeSound->next;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
_removeFadeSound(fadeSound);
2022-05-19 01:51:26 -07:00
}
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer != -1) {
2022-05-19 01:51:26 -07:00
// NOTE: Uninline.
if (!soundIsPlaying(sound)) {
soundStop(sound);
}
if (sound->callback != NULL) {
sound->callback(sound->callbackUserData, 1);
}
2022-07-12 02:09:53 -07:00
audioEngineSoundBufferRelease(sound->soundBuffer);
sound->soundBuffer = -1;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (sound->deleteCallback != NULL) {
sound->deleteCallback(sound->deleteUserData);
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (sound->data != NULL) {
gSoundFreeProc(sound->data);
sound->data = NULL;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
next = sound->next;
if (next != NULL) {
next->prev = sound->prev;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
prev = sound->prev;
if (prev != NULL) {
prev->next = sound->next;
2022-05-19 01:51:26 -07:00
} else {
gSoundListHead = sound->next;
}
gSoundFreeProc(sound);
}
// 0x4AE578
int _soundSetMasterVolume(int volume)
{
if (volume < VOLUME_MIN || volume > VOLUME_MAX) {
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
_masterVol = volume;
Sound* curr = gSoundListHead;
while (curr != NULL) {
soundSetVolume(curr, curr->volume);
curr = curr->next;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AE5C8
2022-07-12 02:09:53 -07:00
Uint32 _doTimerEvent(Uint32 interval, void* param)
2022-05-19 01:51:26 -07:00
{
void (*fn)();
2022-07-12 02:09:53 -07:00
if (param != NULL) {
fn = (void (*)())param;
2022-05-19 01:51:26 -07:00
fn();
}
2022-07-12 02:09:53 -07:00
return 40;
2022-05-19 01:51:26 -07:00
}
// 0x4AE614
2022-07-12 02:09:53 -07:00
void _removeTimedEvent(SDL_TimerID* timerId)
2022-05-19 01:51:26 -07:00
{
2022-07-12 02:09:53 -07:00
if (*timerId != 0) {
SDL_RemoveTimer(*timerId);
*timerId = 0;
2022-05-19 01:51:26 -07:00
}
}
// 0x4AE634
int _soundGetPosition(Sound* sound)
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
unsigned int readPos;
2022-07-12 02:09:53 -07:00
unsigned int writePos;
2022-12-25 10:56:23 -08:00
audioEngineSoundBufferGetCurrentPosition(sound->soundBuffer, &readPos, &writePos);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_STREAMING) != 0) {
if (readPos < sound->lastPosition) {
readPos += sound->numBytesRead + sound->numBuffers * sound->dataSize - sound->lastPosition;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
readPos -= sound->lastPosition + sound->numBytesRead;
2022-05-19 01:51:26 -07:00
}
}
2022-12-25 10:56:23 -08:00
return readPos;
2022-05-19 01:51:26 -07:00
}
// 0x4AE6CC
2022-12-25 10:56:23 -08:00
int _soundSetPosition(Sound* sound, int pos)
2022-05-19 01:51:26 -07:00
{
if (!gSoundInitialized) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer == -1) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
if ((sound->type & SOUND_TYPE_STREAMING) != 0) {
int section = pos / sound->dataSize % sound->numBuffers;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
audioEngineSoundBufferSetCurrentPosition(sound->soundBuffer, section * sound->dataSize + pos % sound->dataSize);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
sound->io.seek(sound->io.fd, section * sound->dataSize, SEEK_SET);
int bytesRead = sound->io.read(sound->io.fd, sound->data, sound->dataSize);
if (bytesRead < sound->dataSize) {
if ((sound->type & SOUND_TYPE_STREAMING) != 0) {
2022-05-19 01:51:26 -07:00
sound->io.seek(sound->io.fd, 0, SEEK_SET);
2022-12-25 10:56:23 -08:00
sound->io.read(sound->io.fd, sound->data + bytesRead, sound->dataSize - bytesRead);
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
memset(sound->data + bytesRead, 0, sound->dataSize - bytesRead);
2022-05-19 01:51:26 -07:00
}
}
2022-12-25 10:56:23 -08:00
int nextSection = section + 1;
sound->numBytesRead = pos;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
if (nextSection < sound->numBuffers) {
sound->lastUpdate = nextSection;
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
sound->lastUpdate = 0;
2022-05-19 01:51:26 -07:00
}
soundContinue(sound);
} else {
2022-12-25 10:56:23 -08:00
audioEngineSoundBufferSetCurrentPosition(sound->soundBuffer, pos);
2022-05-19 01:51:26 -07:00
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AE830
2022-08-06 03:48:16 -07:00
void _removeFadeSound(FadeSound* fadeSound)
2022-05-19 01:51:26 -07:00
{
2022-08-06 03:48:16 -07:00
FadeSound* prev;
FadeSound* next;
FadeSound* tmp;
2022-05-19 01:51:26 -07:00
2022-08-06 03:48:16 -07:00
if (fadeSound == NULL) {
2022-05-19 01:51:26 -07:00
return;
}
2022-08-06 03:48:16 -07:00
if (fadeSound->sound == NULL) {
2022-05-19 01:51:26 -07:00
return;
}
2022-12-25 10:56:23 -08:00
if ((fadeSound->sound->statusFlags & SOUND_STATUS_IS_FADING) == 0) {
2022-05-19 01:51:26 -07:00
return;
}
2022-08-06 03:48:16 -07:00
prev = fadeSound->prev;
2022-05-19 01:51:26 -07:00
if (prev != NULL) {
2022-08-06 03:48:16 -07:00
prev->next = fadeSound->next;
2022-05-19 01:51:26 -07:00
} else {
2022-08-06 03:48:16 -07:00
_fadeHead = fadeSound->next;
2022-05-19 01:51:26 -07:00
}
2022-08-06 03:48:16 -07:00
next = fadeSound->next;
2022-05-19 01:51:26 -07:00
if (next != NULL) {
2022-08-06 03:48:16 -07:00
next->prev = fadeSound->prev;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
fadeSound->sound->statusFlags &= ~SOUND_STATUS_IS_FADING;
2022-08-06 03:48:16 -07:00
fadeSound->sound = NULL;
2022-05-19 01:51:26 -07:00
tmp = _fadeFreeList;
2022-08-06 03:48:16 -07:00
_fadeFreeList = fadeSound;
fadeSound->next = tmp;
2022-05-19 01:51:26 -07:00
}
// 0x4AE8B0
void _fadeSounds()
{
2022-12-25 10:56:23 -08:00
FadeSound* fadeSound;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
fadeSound = _fadeHead;
while (fadeSound != NULL) {
if ((fadeSound->currentVolume > fadeSound->targetVolume || fadeSound->currentVolume + fadeSound->deltaVolume < fadeSound->targetVolume) && (fadeSound->currentVolume < fadeSound->targetVolume || fadeSound->currentVolume + fadeSound->deltaVolume > fadeSound->targetVolume)) {
fadeSound->currentVolume += fadeSound->deltaVolume;
soundSetVolume(fadeSound->sound, fadeSound->currentVolume);
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
if (fadeSound->targetVolume == 0) {
if (fadeSound->pause) {
soundPause(fadeSound->sound);
soundSetVolume(fadeSound->sound, fadeSound->initialVolume);
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
if ((fadeSound->sound->type & SOUND_TYPE_FIRE_AND_FORGET) != 0) {
soundDelete(fadeSound->sound);
2022-05-19 01:51:26 -07:00
} else {
2022-12-25 10:56:23 -08:00
soundStop(fadeSound->sound);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
fadeSound->initialVolume = fadeSound->targetVolume;
fadeSound->currentVolume = fadeSound->targetVolume;
fadeSound->deltaVolume = 0;
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
soundSetVolume(fadeSound->sound, fadeSound->targetVolume);
2022-05-19 01:51:26 -07:00
}
}
}
2022-12-25 10:56:23 -08:00
_removeFadeSound(fadeSound);
2022-05-19 01:51:26 -07:00
}
}
2022-07-12 02:09:53 -07:00
if (_fadeHead == NULL) {
// NOTE: Uninline.
_removeTimedEvent(&gFadeSoundsTimerId);
2022-05-19 01:51:26 -07:00
}
}
// 0x4AE988
2022-12-25 10:56:23 -08:00
int _internalSoundFade(Sound* sound, int duration, int targetVolume, bool pause)
2022-05-19 01:51:26 -07:00
{
2022-12-25 10:56:23 -08:00
FadeSound* fadeSound;
2022-05-19 01:51:26 -07:00
if (!_deviceInit) {
gSoundLastError = SOUND_NOT_INITIALIZED;
return gSoundLastError;
}
if (sound == NULL) {
gSoundLastError = SOUND_NO_SOUND;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
fadeSound = NULL;
if ((sound->statusFlags & SOUND_STATUS_IS_FADING) != 0) {
fadeSound = _fadeHead;
while (fadeSound != NULL) {
if (fadeSound->sound == sound) {
2022-05-19 01:51:26 -07:00
break;
}
2022-12-25 10:56:23 -08:00
fadeSound = fadeSound->next;
2022-05-19 01:51:26 -07:00
}
}
2022-12-25 10:56:23 -08:00
if (fadeSound == NULL) {
2022-05-19 01:51:26 -07:00
if (_fadeFreeList != NULL) {
2022-12-25 10:56:23 -08:00
fadeSound = _fadeFreeList;
2022-05-19 01:51:26 -07:00
_fadeFreeList = _fadeFreeList->next;
} else {
2022-12-25 10:56:23 -08:00
fadeSound = (FadeSound*)gSoundMallocProc(sizeof(FadeSound));
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
if (fadeSound != NULL) {
2022-05-19 01:51:26 -07:00
if (_fadeHead != NULL) {
2022-12-25 10:56:23 -08:00
_fadeHead->prev = fadeSound;
2022-05-19 01:51:26 -07:00
}
2022-12-25 10:56:23 -08:00
fadeSound->sound = sound;
fadeSound->prev = NULL;
fadeSound->next = _fadeHead;
_fadeHead = fadeSound;
2022-05-19 01:51:26 -07:00
}
}
2022-12-25 10:56:23 -08:00
if (fadeSound == NULL) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_MEMORY_AVAILABLE;
return gSoundLastError;
}
2022-12-25 10:56:23 -08:00
fadeSound->targetVolume = targetVolume;
fadeSound->initialVolume = _soundGetVolume(sound);
fadeSound->currentVolume = fadeSound->initialVolume;
fadeSound->pause = pause;
2022-05-19 01:51:26 -07:00
// TODO: Check.
2022-12-25 10:56:23 -08:00
fadeSound->deltaVolume = 8 * (125 * (targetVolume - fadeSound->initialVolume)) / (40 * duration);
2022-05-19 01:51:26 -07:00
2022-12-25 10:56:23 -08:00
sound->statusFlags |= SOUND_STATUS_IS_FADING;
2022-05-19 01:51:26 -07:00
2022-08-06 03:48:16 -07:00
bool shouldPlay;
2022-05-19 01:51:26 -07:00
if (gSoundInitialized) {
2022-07-12 02:09:53 -07:00
if (sound->soundBuffer != -1) {
2022-12-25 10:56:23 -08:00
shouldPlay = (sound->statusFlags & SOUND_STATUS_IS_PLAYING) == 0;
2022-05-19 01:51:26 -07:00
} else {
gSoundLastError = SOUND_NO_SOUND;
2022-08-06 03:48:16 -07:00
shouldPlay = true;
2022-05-19 01:51:26 -07:00
}
} else {
gSoundLastError = SOUND_NOT_INITIALIZED;
2022-08-06 03:48:16 -07:00
shouldPlay = true;
2022-05-19 01:51:26 -07:00
}
2022-08-06 03:48:16 -07:00
if (shouldPlay) {
2022-05-19 01:51:26 -07:00
soundPlay(sound);
}
2022-07-12 02:09:53 -07:00
if (gFadeSoundsTimerId != 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
2022-07-12 02:09:53 -07:00
gFadeSoundsTimerId = SDL_AddTimer(40, _doTimerEvent, (void*)_fadeSounds);
if (gFadeSoundsTimerId == 0) {
2022-05-19 01:51:26 -07:00
gSoundLastError = SOUND_UNKNOWN_ERROR;
return gSoundLastError;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
// 0x4AEB0C
2022-08-06 03:48:16 -07:00
int _soundFade(Sound* sound, int duration, int targetVolume)
2022-05-19 01:51:26 -07:00
{
2022-12-25 10:56:23 -08:00
return _internalSoundFade(sound, duration, targetVolume, false);
2022-05-19 01:51:26 -07:00
}
// 0x4AEB54
void soundDeleteAll()
{
while (gSoundListHead != NULL) {
soundDelete(gSoundListHead);
}
}
// 0x4AEBE0
void soundContinueAll()
{
Sound* curr = gSoundListHead;
while (curr != NULL) {
// Sound can be deallocated in `soundContinue`.
Sound* next = curr->next;
soundContinue(curr);
curr = next;
}
}
// 0x4AEC00
int soundSetDefaultFileIO(SoundOpenProc* openProc, SoundCloseProc* closeProc, SoundReadProc* readProc, SoundWriteProc* writeProc, SoundSeekProc* seekProc, SoundTellProc* tellProc, SoundFileLengthProc* fileLengthProc)
{
if (openProc != NULL) {
gSoundDefaultFileIO.open = openProc;
}
if (closeProc != NULL) {
gSoundDefaultFileIO.close = closeProc;
}
if (readProc != NULL) {
gSoundDefaultFileIO.read = readProc;
}
if (writeProc != NULL) {
gSoundDefaultFileIO.write = writeProc;
}
if (seekProc != NULL) {
gSoundDefaultFileIO.seek = seekProc;
}
if (tellProc != NULL) {
gSoundDefaultFileIO.tell = tellProc;
}
if (fileLengthProc != NULL) {
gSoundDefaultFileIO.filelength = fileLengthProc;
}
gSoundLastError = SOUND_NO_ERROR;
return gSoundLastError;
}
2022-09-23 05:43:44 -07:00
} // namespace fallout