Skip to content

Commit

Permalink
[Audio 5/?] Build the Samplebank Table (zeldaret#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thar0 authored Aug 17, 2024
1 parent 56ca236 commit ba5574b
Show file tree
Hide file tree
Showing 24 changed files with 343 additions and 68 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ BUILD_DIR_REPLACE := sed -e 's|$$(BUILD_DIR)|$(BUILD_DIR)|g'
AUDIO_EXTRACT := $(PYTHON) tools/audio_extraction.py
SAMPLECONV := tools/audio/sampleconv/sampleconv
SBC := tools/audio/sbc
ATBLGEN := tools/audio/atblgen

SBCFLAGS := --matching

Expand Down Expand Up @@ -745,6 +746,21 @@ ifeq ($(AUDIO_BUILD_DEBUG),1)
@cmp $(@:.o=.bin) $(patsubst $(BUILD_DIR)/assets/audio/samplebanks/%,$(EXTRACTED_DIR)/baserom_audiotest/audiotable_files/%,$(@:.o=.bin)) && echo "$(<F) OK"
endif

# put together the tables

$(BUILD_DIR)/assets/audio/samplebank_table.h: $(SAMPLEBANK_BUILD_XMLS)
$(ATBLGEN) --banks $@ $^

# build the tables into objects, move data -> rodata

$(BUILD_DIR)/src/audio/tables/samplebank_table.o: src/audio/tables/samplebank_table.c $(BUILD_DIR)/assets/audio/samplebank_table.h
ifneq ($(RUN_CC_CHECK),0)
$(CC_CHECK) $<
endif
$(CC) -c $(CFLAGS) $(MIPS_VERSION) $(OPTFLAGS) -o $(@:.o=.tmp) $<
$(LD) -r -T linker_scripts/audio_table_rodata.ld $(@:.o=.tmp) -o $@
@$(RM) $(@:.o=.tmp)

-include $(DEP_FILES)

# Print target for debugging
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-eu-mq-dbg/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ incbins:
segment: code
vram: 0x80155500
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x80155BF0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80155C70
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-eu-mq/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801108A0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x80110F90
size: 0x80
- name: aspMainData
segment: code
vram: 0x80111010
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-eu/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801108C0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x80110FB0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80111030
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-jp-ce/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801130B0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x801137A0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80113820
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-jp-mq/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801130B0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x801137A0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80113820
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-jp/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801130D0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x801137C0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80113840
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-us-mq/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x80113090
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x80113780
size: 0x80
- name: aspMainData
segment: code
vram: 0x80113800
Expand Down
4 changes: 0 additions & 4 deletions baseroms/gc-us/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ incbins:
segment: code
vram: 0x801130B0
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x801137A0
size: 0x80
- name: aspMainData
segment: code
vram: 0x80113820
Expand Down
4 changes: 0 additions & 4 deletions baseroms/ntsc-1.2/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ incbins:
segment: code
vram: 0x80114220
size: 0x6F0
- name: gSampleBankTable
segment: code
vram: 0x80114910
size: 0x80
- name: aspMainData
segment: code
vram: 0x80114990
Expand Down
3 changes: 0 additions & 3 deletions data/audio_tables.rodata.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ glabel gSequenceFontTable

glabel gSequenceTable
.incbin "incbin/gSequenceTable"

glabel gSampleBankTable
.incbin "incbin/gSampleBankTable"
1 change: 1 addition & 0 deletions include/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
#define UNUSED __attribute__((unused))
#define FALLTHROUGH __attribute__((fallthrough))
#define NORETURN __attribute__((noreturn))
#define NO_REORDER __attribute__((no_reorder))

#endif
2 changes: 1 addition & 1 deletion include/variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ extern s16 gOcarinaSongItemMap[];
extern u8 gSoundFontTable[];
extern u8 gSequenceFontTable[];
extern u8 gSequenceTable[];
extern u8 gSampleBankTable[];
extern AudioTable gSampleBankTable;

extern SaveContext gSaveContext;

Expand Down
23 changes: 19 additions & 4 deletions include/z64audio.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef Z64_AUDIO_H
#define Z64_AUDIO_H

#include "ultra64.h"
#include "sequence.h"

typedef void (*AudioCustomUpdateFunction)(void);


Expand Down Expand Up @@ -110,6 +113,14 @@ typedef enum AudioCacheType {
/* 3 */ CACHE_PERMANENT
} AudioCacheType;

typedef enum AudioCacheLoadType {
/* 0 */ CACHE_LOAD_PERMANENT,
/* 1 */ CACHE_LOAD_PERSISTENT,
/* 2 */ CACHE_LOAD_TEMPORARY,
/* 3 */ CACHE_LOAD_EITHER,
/* 4 */ CACHE_LOAD_EITHER_NOSYNC
} AudioCacheLoadType;

typedef enum AudioLoadStatus {
/* 0 */ LOAD_STATUS_NOT_LOADED, // the entry data is not loaded
/* 1 */ LOAD_STATUS_IN_PROGRESS, // the entry data is being loaded asynchronously
Expand Down Expand Up @@ -807,6 +818,13 @@ typedef struct AudioSlowLoad {
/* 0x4C */ OSIoMesg ioMesg;
} AudioSlowLoad; // size = 0x64

typedef struct AudioTableHeader {
/* 0x00 */ s16 numEntries;
/* 0x02 */ s16 unkMediumParam;
/* 0x04 */ uintptr_t romAddr;
/* 0x08 */ char pad[0x8];
} AudioTableHeader; // size = 0x10

typedef struct AudioTableEntry {
/* 0x00 */ u32 romAddr;
/* 0x04 */ u32 size;
Expand All @@ -818,10 +836,7 @@ typedef struct AudioTableEntry {
} AudioTableEntry; // size = 0x10

typedef struct AudioTable {
/* 0x00 */ s16 numEntries;
/* 0x02 */ s16 unkMediumParam;
/* 0x04 */ u32 romAddr;
/* 0x08 */ char pad[0x8];
/* 0x00 */ AudioTableHeader header;
/* 0x10 */ AudioTableEntry entries[1]; // (dynamic size)
} AudioTable; // size >= 0x20

Expand Down
17 changes: 17 additions & 0 deletions linker_scripts/audio_table_rodata.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
OUTPUT_ARCH (mips)

/* Audio Table Linker Script, maps data into rodata */

SECTIONS {

.rodata :
{
*(.data*)
*(.rodata*)
}

/DISCARD/ :
{
*(*);
}
}
1 change: 1 addition & 0 deletions spec
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ beginseg
include_no_data "$(BUILD_DIR)/src/code/z_game_over.o"
include "$(BUILD_DIR)/src/code/z_construct.o"
include "$(BUILD_DIR)/data/audio_tables.rodata.o"
include "$(BUILD_DIR)/src/audio/tables/samplebank_table.o"
include "$(BUILD_DIR)/data/rsp.text.o"
include "$(BUILD_DIR)/data/rsp.rodata.o"
endseg
Expand Down
6 changes: 3 additions & 3 deletions src/audio/lib/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ void AudioHeap_DiscardSampleCacheEntry(SampleCacheEntry* entry) {
s32 sampleBankId2;
s32 fontId;

numFonts = gAudioCtx.soundFontTable->numEntries;
numFonts = gAudioCtx.soundFontTable->header.numEntries;
for (fontId = 0; fontId < numFonts; fontId++) {
sampleBankId1 = gAudioCtx.soundFontList[fontId].sampleBankId1;
sampleBankId2 = gAudioCtx.soundFontList[fontId].sampleBankId2;
Expand Down Expand Up @@ -1322,7 +1322,7 @@ void AudioHeap_DiscardSampleCaches(void) {
s32 fontId;
s32 j;

numFonts = gAudioCtx.soundFontTable->numEntries;
numFonts = gAudioCtx.soundFontTable->header.numEntries;
for (fontId = 0; fontId < numFonts; fontId++) {
sampleBankId1 = gAudioCtx.soundFontList[fontId].sampleBankId1;
sampleBankId2 = gAudioCtx.soundFontList[fontId].sampleBankId2;
Expand Down Expand Up @@ -1392,7 +1392,7 @@ void AudioHeap_ApplySampleBankCacheInternal(s32 apply, s32 sampleBankId) {
s32 pad[4];

sampleBankTable = gAudioCtx.sampleBankTable;
numFonts = gAudioCtx.soundFontTable->numEntries;
numFonts = gAudioCtx.soundFontTable->header.numEntries;
change.oldAddr = (u32)AudioHeap_SearchCaches(SAMPLE_TABLE, CACHE_EITHER, sampleBankId);
if (change.oldAddr == 0) {
return;
Expand Down
32 changes: 16 additions & 16 deletions src/audio/lib/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ void AudioLoad_SetSampleFontLoadStatus(s32 sampleBankId, s32 loadStatus) {
void AudioLoad_InitTable(AudioTable* table, u32 romAddr, u16 unkMediumParam) {
s32 i;

table->unkMediumParam = unkMediumParam;
table->romAddr = romAddr;
table->header.unkMediumParam = unkMediumParam;
table->header.romAddr = romAddr;

for (i = 0; i < table->numEntries; i++) {
for (i = 0; i < table->header.numEntries; i++) {
if ((table->entries[i].size != 0) && (table->entries[i].medium == MEDIUM_CART)) {
table->entries[i].romAddr += romAddr;
}
Expand Down Expand Up @@ -401,7 +401,7 @@ s32 AudioLoad_SyncLoadSample(Sample* sample, s32 fontId) {

if (sample->medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium((u32)sample->sampleAddr, sampleAddr, sample->size,
gAudioCtx.sampleBankTable->unkMediumParam);
gAudioCtx.sampleBankTable->header.unkMediumParam);
} else {
AudioLoad_SyncDma((u32)sample->sampleAddr, sampleAddr, sample->size, sample->medium);
}
Expand Down Expand Up @@ -707,7 +707,7 @@ void* AudioLoad_SyncLoad(u32 tableType, u32 id, s32* didAllocate) {

*didAllocate = true;
if (medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, (s16)table->unkMediumParam);
AudioLoad_SyncDmaUnkMedium(romAddr, ramAddr, size, (s16)table->header.unkMediumParam);
} else {
AudioLoad_SyncDma(romAddr, ramAddr, size, medium);
}
Expand Down Expand Up @@ -1066,8 +1066,8 @@ void* AudioLoad_AsyncLoadInner(s32 tableType, s32 id, s32 nChunks, s32 retData,
}

if (medium == MEDIUM_UNK) {
AudioLoad_StartAsyncLoadUnkMedium((s16)table->unkMediumParam, devAddr, ramAddr, size, medium, nChunks,
retQueue, MK_ASYNC_MSG(retData, tableType, id, loadStatus));
AudioLoad_StartAsyncLoadUnkMedium((s16)table->header.unkMediumParam, devAddr, ramAddr, size, medium,
nChunks, retQueue, MK_ASYNC_MSG(retData, tableType, id, loadStatus));
} else {
AudioLoad_StartAsyncLoad(devAddr, ramAddr, size, medium, nChunks, retQueue,
MK_ASYNC_MSG(retData, tableType, realId, loadStatus));
Expand Down Expand Up @@ -1215,10 +1215,10 @@ void AudioLoad_Init(void* heap, u32 heapSize) {
// Set audio tables pointers
gAudioCtx.sequenceTable = (AudioTable*)gSequenceTable;
gAudioCtx.soundFontTable = (AudioTable*)gSoundFontTable;
gAudioCtx.sampleBankTable = (AudioTable*)gSampleBankTable;
gAudioCtx.sampleBankTable = &gSampleBankTable;
gAudioCtx.sequenceFontTable = gSequenceFontTable;

gAudioCtx.numSequences = gAudioCtx.sequenceTable->numEntries;
gAudioCtx.numSequences = gAudioCtx.sequenceTable->header.numEntries;

gAudioCtx.specId = 0;
gAudioCtx.resetStatus = 1; // Set reset to immediately initialize the audio heap
Expand All @@ -1229,7 +1229,7 @@ void AudioLoad_Init(void* heap, u32 heapSize) {
AudioLoad_InitTable(gAudioCtx.sequenceTable, (u32)_AudioseqSegmentRomStart, 0);
AudioLoad_InitTable(gAudioCtx.soundFontTable, (u32)_AudiobankSegmentRomStart, 0);
AudioLoad_InitTable(gAudioCtx.sampleBankTable, (u32)_AudiotableSegmentRomStart, 0);
numFonts = gAudioCtx.soundFontTable->numEntries;
numFonts = gAudioCtx.soundFontTable->header.numEntries;
gAudioCtx.soundFontList = AudioHeap_Alloc(&gAudioCtx.initPool, numFonts * sizeof(SoundFont));

for (i = 0; i < numFonts; i++) {
Expand Down Expand Up @@ -1295,7 +1295,7 @@ s32 AudioLoad_SlowLoadSample(s32 fontId, s32 instId, s8* status) {
slowLoad->seqOrFontId = fontId;
slowLoad->instId = instId;
if (slowLoad->medium == MEDIUM_UNK) {
slowLoad->unkMediumParam = gAudioCtx.sampleBankTable->unkMediumParam;
slowLoad->unkMediumParam = gAudioCtx.sampleBankTable->header.unkMediumParam;
}

gAudioCtx.slowLoadPos ^= 1;
Expand Down Expand Up @@ -1439,7 +1439,7 @@ s32 AudioLoad_SlowLoadSeq(s32 seqId, u8* ramAddr, s8* status) {
slowLoad->seqOrFontId = seqId;

if (slowLoad->medium == MEDIUM_UNK) {
slowLoad->unkMediumParam = seqTable->unkMediumParam;
slowLoad->unkMediumParam = seqTable->header.unkMediumParam;
}

gAudioCtx.slowLoadPos ^= 1;
Expand Down Expand Up @@ -1616,7 +1616,7 @@ void AudioLoad_ProcessAsyncLoad(AudioAsyncLoad* asyncLoad, s32 resetStatus) {
if (asyncLoad->bytesRemaining < asyncLoad->chunkSize) {
if (asyncLoad->medium == MEDIUM_UNK) {
AudioLoad_AsyncDmaUnkMedium(asyncLoad->curDevAddr, asyncLoad->curRamAddr, asyncLoad->bytesRemaining,
sampleBankTable->unkMediumParam);
sampleBankTable->header.unkMediumParam);
} else {
AudioLoad_AsyncDma(asyncLoad, asyncLoad->bytesRemaining);
}
Expand All @@ -1626,7 +1626,7 @@ void AudioLoad_ProcessAsyncLoad(AudioAsyncLoad* asyncLoad, s32 resetStatus) {

if (asyncLoad->medium == MEDIUM_UNK) {
AudioLoad_AsyncDmaUnkMedium(asyncLoad->curDevAddr, asyncLoad->curRamAddr, asyncLoad->chunkSize,
sampleBankTable->unkMediumParam);
sampleBankTable->header.unkMediumParam);
} else {
AudioLoad_AsyncDma(asyncLoad, asyncLoad->chunkSize);
}
Expand Down Expand Up @@ -1783,7 +1783,7 @@ void AudioLoad_RelocateFontAndPreloadSamples(s32 fontId, SoundFontData* fontData
case false:
if (sample->medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium((u32)sample->sampleAddr, sampleRamAddr, sample->size,
gAudioCtx.sampleBankTable->unkMediumParam);
gAudioCtx.sampleBankTable->header.unkMediumParam);
sample->sampleAddr = sampleRamAddr;
sample->medium = MEDIUM_RAM;
} else {
Expand Down Expand Up @@ -2039,7 +2039,7 @@ void AudioLoad_PreloadSamplesForFont(s32 fontId, s32 async, SampleBankRelocInfo*
case false:
if (sample->medium == MEDIUM_UNK) {
AudioLoad_SyncDmaUnkMedium((u32)sample->sampleAddr, addr, sample->size,
gAudioCtx.sampleBankTable->unkMediumParam);
gAudioCtx.sampleBankTable->header.unkMediumParam);
sample->sampleAddr = addr;
sample->medium = MEDIUM_RAM;
} else {
Expand Down
Loading

0 comments on commit ba5574b

Please sign in to comment.