Skip to content

Commit

Permalink
[iQue] Import libultra audio library files (#2433)
Browse files Browse the repository at this point in the history
Note about comments in these files:
The libultra audio library was originally available to developers in
source code form, unlike most of libultra. These files are based on
these source code forms, adapted to meet the style of this project.
The comments in the files as of this commit are, apart from the style,
comments from the original SGI authors.
  • Loading branch information
Thar0 authored Jan 21, 2025
1 parent 5346c97 commit afa0842
Show file tree
Hide file tree
Showing 28 changed files with 3,447 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ $(BUILD_DIR)/src/libultra/%.o: ASOPTFLAGS := $(EGCS_ASOPTFLAGS)
$(BUILD_DIR)/src/libultra/reg/_%.o: OPTFLAGS := -O0
$(BUILD_DIR)/src/libultra/reg/_%.o: MIPS_VERSION := -mgp64 -mfp64 -mips3

$(BUILD_DIR)/src/libultra/audio/%.o: OPTFLAGS := -O2

$(BUILD_DIR)/src/libultra/libc/ll.o: OPTFLAGS := -O0
$(BUILD_DIR)/src/libultra/libc/llcvt.o: OPTFLAGS := -O0

Expand Down
1 change: 0 additions & 1 deletion include/ultra64.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void* osViGetNextFramebuffer(void);
void osCreatePiManager(OSPri pri, OSMesgQueue* cmdQueue, OSMesg* cmdBuf, s32 cmdMsgCnt);
void __osDevMgrMain(void* arg);
s32 __osPiRawStartDma(s32 dir, u32 cartAddr, void* dramAddr, size_t size);
u32 osVirtualToPhysical(void* vaddr);
void osViBlack(u8 active);
s32 __osSiRawReadIo(void* devAddr, u32* dst);
OSId osGetThreadId(OSThread* thread);
Expand Down
2 changes: 2 additions & 0 deletions include/ultra64/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
#define OS_PHYSICAL_TO_K0(x) (void*)(((u32)(x)+0x80000000))
#define OS_PHYSICAL_TO_K1(x) (void*)(((u32)(x)+0xA0000000))

u32 osVirtualToPhysical(void* vaddr);

#endif
2 changes: 1 addition & 1 deletion src/boot/z_std_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#endif

#pragma increment_block_number "gc-eu:128 gc-eu-mq:128 gc-jp:128 gc-jp-ce:128 gc-jp-mq:128 gc-us:128 gc-us-mq:128" \
"ntsc-1.2:68 pal-1.0:66 pal-1.1:66"
"ntsc-1.2:66 pal-1.0:64 pal-1.1:64"

StackEntry sDmaMgrStackInfo;
OSMesgQueue sDmaMgrMsgQueue;
Expand Down
2 changes: 1 addition & 1 deletion src/code/fault_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* DPad-Up may be pressed to enable sending fault pages over osSyncPrintf as well as displaying them on-screen.
* DPad-Down disables sending fault pages over osSyncPrintf.
*/
#pragma increment_block_number "gc-eu:160 gc-eu-mq:160 gc-eu-mq-dbg:160 gc-jp:160 gc-jp-ce:160 gc-jp-mq:160 gc-us:160" \
#pragma increment_block_number "gc-eu:160 gc-eu-mq:160 gc-eu-mq-dbg:144 gc-jp:160 gc-jp-ce:160 gc-jp-mq:160 gc-us:160" \
"gc-us-mq:160"

#include "global.h"
Expand Down
2 changes: 1 addition & 1 deletion src/code/sys_math3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "macros.h"
#include "sys_math3d.h"

#pragma increment_block_number "gc-eu:99 gc-eu-mq:99 gc-jp:99 gc-jp-ce:99 gc-jp-mq:99 gc-us:99 gc-us-mq:99" \
#pragma increment_block_number "gc-eu:98 gc-eu-mq:98 gc-jp:98 gc-jp-ce:98 gc-jp-mq:98 gc-us:98 gc-us-mq:98" \
"ntsc-1.0:80 ntsc-1.1:80 ntsc-1.2:79 pal-1.0:80 pal-1.1:80"

s32 Math3D_LineVsLineClosestTwoPoints(Vec3f* lineAPointA, Vec3f* lineAPointB, Vec3f* lineBPointA, Vec3f* lineBPointB,
Expand Down
2 changes: 1 addition & 1 deletion src/code/z_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -3639,7 +3639,7 @@ s32 Camera_KeepOn3(Camera* camera) {
}

#pragma increment_block_number "gc-eu:128 gc-eu-mq:128 gc-jp:128 gc-jp-ce:128 gc-jp-mq:128 gc-us:128 gc-us-mq:128" \
"ntsc-1.0:130 ntsc-1.1:130 ntsc-1.2:130 pal-1.0:128 pal-1.1:128"
"ntsc-1.0:129 ntsc-1.1:129 ntsc-1.2:129 pal-1.0:127 pal-1.1:127"

s32 Camera_KeepOn4(Camera* camera) {
static Vec3f D_8015BD50;
Expand Down
2 changes: 1 addition & 1 deletion src/code/z_collision_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "z_lib.h"

#pragma increment_block_number "gc-eu:128 gc-eu-mq:128 gc-jp:128 gc-jp-ce:128 gc-jp-mq:128 gc-us:128 gc-us-mq:128" \
"ntsc-1.0:96 ntsc-1.1:96 ntsc-1.2:96 pal-1.0:96 pal-1.1:96"
"ntsc-1.0:88 ntsc-1.1:88 ntsc-1.2:88 pal-1.0:88 pal-1.1:88"

typedef s32 (*ColChkResetFunc)(PlayState*, Collider*);
typedef void (*ColChkApplyFunc)(PlayState*, CollisionCheckContext*, Collider*);
Expand Down
4 changes: 2 additions & 2 deletions src/code/z_kankyo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma increment_block_number "gc-eu:216 gc-eu-mq:216 gc-jp:212 gc-jp-ce:212 gc-jp-mq:212 gc-us:212 gc-us-mq:212" \
"ntsc-1.0:208 ntsc-1.1:208 ntsc-1.2:208 pal-1.0:228 pal-1.1:228"
#pragma increment_block_number "gc-eu:216 gc-eu-mq:216 gc-jp:192 gc-jp-ce:192 gc-jp-mq:192 gc-us:192 gc-us-mq:192" \
"ntsc-1.0:208 ntsc-1.1:208 ntsc-1.2:208 pal-1.0:224 pal-1.1:224"

#include "global.h"
#include "ultra64.h"
Expand Down
32 changes: 32 additions & 0 deletions src/libultra/audio/auxbus.c
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
#include "libaudio.h"
#include "synthInternals.h"

Acmd* alAuxBusPull(void* filter, s16* outp, s32 outCount, s32 sampleOffset, Acmd* p) {
Acmd* ptr = p;
ALAuxBus* m = (ALAuxBus*)filter;
ALFilter** sources = m->sources;
s32 i;

// clear the output buffers here
aClearBuffer(ptr++, AL_AUX_L_OUT, outCount << 1);
aClearBuffer(ptr++, AL_AUX_R_OUT, outCount << 1);

for (i = 0; i < m->sourceCount; i++) {
ptr = sources[i]->handler(sources[i], outp, outCount, sampleOffset, ptr);
}
return ptr;
}

s32 alAuxBusParam(void* filter, s32 paramID, void* param) {
ALAuxBus* m = (ALAuxBus*)filter;
ALFilter** sources = m->sources;

switch (paramID) {
case AL_FILTER_ADD_SOURCE:
sources[m->sourceCount++] = (ALFilter*)param;
break;

default:
break;
}
return 0;
}
10 changes: 10 additions & 0 deletions src/libultra/audio/copy.c
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
#include "libaudio.h"

void alCopy(void* src, void* dest, s32 len) {
s32 i;
u8* s = (u8*)src;
u8* d = (u8*)dest;

for (i = 0; i < len; i++) {
*d++ = *s++;
}
}
263 changes: 263 additions & 0 deletions src/libultra/audio/drvrnew.c
Original file line number Diff line number Diff line change
@@ -1 +1,264 @@
#include "libaudio.h"
#include "synthInternals.h"

// WARNING: THE FOLLOWING CONSTANT MUST BE KEPT IN SYNC WITH SCALING IN MICROCODE!!!
#define SCALE 16384

// the following arrays contain default parameters for a few hopefully useful effects.
#define ms *(((s32)(44.1f)) & ~7)

// clang-format off
static s32 SMALLROOM_PARAMS[2 + 3 * 8] = {
/* sections */ 3,
/* length */ 100 ms,
/* input output fbcoef ffcoef gain rate depth coef */
0, 54 ms, 9830, -9830, 0, 0, 0, 0,
19 ms, 38 ms, 3276, -3276, 0x3FFF, 0, 0, 0,
0, 60 ms, 5000, 0, 0, 0, 0, 0x5000
};

static s32 BIGROOM_PARAMS[2 + 4 * 8] = {
/* sections */ 4,
/* length */ 100 ms,
/* input output fbcoef ffcoef gain rate depth coef */
0, 66 ms, 9830, -9830, 0, 0, 0, 0,
22 ms, 54 ms, 3276, -3276, 0x3FFF, 0, 0, 0,
66 ms, 91 ms, 3276, -3276, 0x3FFF, 0, 0, 0,
0, 94 ms, 8000, 0, 0, 0, 0, 0x5000,
};

static s32 ECHO_PARAMS[2 + 1 * 8] = {
/* sections */ 1,
/* length */ 200 ms,
/* input output fbcoef ffcoef gain rate depth coef */
0, 179 ms, 12000, 0, 0x7FFF, 0, 0, 0,
};

static s32 CHORUS_PARAMS[2 + 1 * 8] = {
/* sections */ 1,
/* length */ 20 ms,
/* input output fbcoef ffcoef gain rate depth coef */
0, 5 ms, 0x4000, 0, 0x7FFF, 7600, 700, 0,
};

static s32 FLANGE_PARAMS[2 + 1 * 8] = {
/* sections */ 1,
/* length */ 20 ms,
/* input output fbcoef ffcoef gain rate depth coef */
0, 5 ms, 0, 0x5FFF, 0x7FFF, 380, 500, 0,
};

static s32 NULL_PARAMS[2 + 1 * 8] = {
/* sections */ 0,
/* length */ 0,
/* input output fbcoef ffcoef gain rate depth coef */
0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on

void _init_lpfilter(ALLowPass* lp) {
s32 i;
s32 temp;
s16 fc;
f64 ffc;
f64 fcoef;

temp = lp->fc * SCALE;
fc = temp >> 15;
lp->fgain = SCALE - fc;

lp->first = true;
for (i = 0; i < 8; i++) {
lp->fcvec.fccoef[i] = 0;
}

lp->fcvec.fccoef[i++] = fc;
fcoef = ffc = (f64)fc / SCALE;

for (; i < 16; i++) {
fcoef *= ffc;
lp->fcvec.fccoef[i] = (s16)(s32)(fcoef * SCALE);
}
}

void alFxNew(ALFx* r, ALSynConfig* c, ALHeap* hp) {
u16 i;
u16 j;
u16 k;
s32* param = NULL;
ALFilter* f = &r->filter;
ALDelay* d;

alFilterNew(f, NULL, alFxParam, AL_FX);
f->handler = alFxPull;
r->paramHdl = (ALSetFXParam)alFxParamHdl;

switch (c->fxType) {
case AL_FX_SMALLROOM:
param = SMALLROOM_PARAMS;
break;

case AL_FX_BIGROOM:
param = BIGROOM_PARAMS;
break;

case AL_FX_ECHO:
param = ECHO_PARAMS;
break;

case AL_FX_CHORUS:
param = CHORUS_PARAMS;
break;

case AL_FX_FLANGE:
param = FLANGE_PARAMS;
break;

case AL_FX_CUSTOM:
param = c->params;
break;

default:
param = NULL_PARAMS;
break;
}

j = 0;

r->section_count = param[j++];
r->length = param[j++];

r->delay = alHeapAlloc(hp, r->section_count, sizeof(ALDelay));
r->base = alHeapAlloc(hp, r->length, sizeof(s16));
r->input = r->base;

for (k = 0; k < r->length; k++) {
r->base[k] = 0;
}

for (i = 0; i < r->section_count; i++) {
d = &r->delay[i];
d->input = param[j++];
d->output = param[j++];
d->fbcoef = param[j++];
d->ffcoef = param[j++];
d->gain = param[j++];

if (param[j] != 0) {
#define RANGE 2.0
d->rsinc = ((((f32)param[j++]) / 1000.0f) * RANGE) / c->outputRate;

// the following constant is derived from:
//
// ratio = 2^(cents/1200)
//
// and therefore for hundredths of a cent
// x
// ln(ratio) = ---------------
// (120,000)/ln(2)
// where
// 120,000/ln(2) = 173123.40...
#define CONVERT 173123.404906676
d->rsgain = (((f32)param[j++]) / CONVERT) * (d->output - d->input);
d->rsval = 1.0f;
d->rsdelta = 0.0f;
d->rs = alHeapAlloc(hp, 1, sizeof(ALResampler));
d->rs->state = alHeapAlloc(hp, 1, sizeof(RESAMPLE_STATE));
d->rs->delta = 0.0f;
d->rs->first = true;
} else {
d->rs = NULL;
j++;
j++;
}

if (param[j] != 0) {
d->lp = alHeapAlloc(hp, 1, sizeof(ALLowPass));
d->lp->fstate = alHeapAlloc(hp, 1, sizeof(POLEF_STATE));
d->lp->fc = param[j++];
_init_lpfilter(d->lp);
} else {
d->lp = NULL;
j++;
}
}
}

void alEnvmixerNew(ALEnvMixer* e, ALHeap* hp) {
alFilterNew(&e->filter, alEnvmixerPull, alEnvmixerParam, AL_ENVMIX);
e->state = alHeapAlloc(hp, 1, sizeof(ENVMIX_STATE));
e->first = true;
e->motion = AL_STOPPED;
e->volume = 1;
e->ltgt = 1;
e->rtgt = 1;
e->cvolL = 1;
e->cvolR = 1;
e->dryamt = 0;
e->wetamt = 0;
e->lratm = 1;
e->lratl = 0;
e->lratm = 1;
e->lratl = 0;
e->delta = 0;
e->segEnd = 0;
e->pan = 0;
e->ctrlList = NULL;
e->ctrlTail = NULL;
e->sources = NULL;
}

void alLoadNew(ALLoadFilter* f, ALDMANew dmaNew, ALHeap* hp) {
s32 i;

// init filter superclass
alFilterNew(&f->filter, alAdpcmPull, alLoadParam, AL_ADPCM);

f->state = alHeapAlloc(hp, 1, sizeof(ADPCM_STATE));
f->lstate = alHeapAlloc(hp, 1, sizeof(ADPCM_STATE));

f->dma = dmaNew(&f->dmaState);

// init the adpcm state
f->lastsam = 0;
f->first = true;
f->memin = 0;
}

void alResampleNew(ALResampler* r, ALHeap* hp) {
alFilterNew(&r->filter, alResamplePull, alResampleParam, AL_RESAMPLE);

// Init resampler state
r->state = alHeapAlloc(hp, 1, sizeof(RESAMPLE_STATE));
r->delta = 0.0f;
r->first = true;
r->motion = AL_STOPPED;
r->ratio = 1.0f;
r->upitch = 0;
r->ctrlList = NULL;
r->ctrlTail = NULL;
// state in the ucode is initialized by the A_INIT flag
}

void alAuxBusNew(ALAuxBus* m, void* sources, s32 maxSources) {
alFilterNew(&m->filter, alAuxBusPull, alAuxBusParam, AL_AUXBUS);
m->sourceCount = 0;
m->maxSources = maxSources;
m->sources = (ALFilter**)sources;
}

void alMainBusNew(ALMainBus* m, void* sources, s32 maxSources) {
alFilterNew(&m->filter, alMainBusPull, alMainBusParam, AL_MAINBUS);
m->sourceCount = 0;
m->maxSources = maxSources;
m->sources = (ALFilter**)sources;
}

void alSaveNew(ALSave* f) {
// init filter superclass
alFilterNew(&f->filter, alSavePull, alSaveParam, AL_SAVE);

// init the save state, which is a virtual dram address
f->dramout = 0;
f->first = true;
}
Loading

0 comments on commit afa0842

Please sign in to comment.