From 423fec9f79e7be26847a52ca04c85ff0125bd941 Mon Sep 17 00:00:00 2001 From: engineer124 <47598039+engineer124@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:53:56 -0600 Subject: [PATCH] Clean up audio sample counts: bytes, samples, frames (#1289) * Clean up bytes, samples, frames * Improve macro usage * More missed macros * rename macro now that it crosses files * redefine macros in terms of frames * Another use of macro * Fix, it's number of samples, not size * Partial PR Suggestions * Small change * size to length * Correct/Clarify comments * remove comment * More PR suggestions, cleanup * Bad formatting, fixed --- include/z64audio.h | 25 +++- src/code/audio_data.c | 67 ++++++++- src/code/audio_heap.c | 34 ++--- src/code/audio_init_params.c | 2 +- src/code/audio_load.c | 4 +- src/code/audio_synthesis.c | 271 ++++++++++++++++++----------------- 6 files changed, 246 insertions(+), 157 deletions(-) diff --git a/include/z64audio.h b/include/z64audio.h index 0ac1e24eba..4c09eae546 100644 --- a/include/z64audio.h +++ b/include/z64audio.h @@ -23,7 +23,24 @@ #define ADSR_GOTO -2 #define ADSR_RESTART -3 -#define AIBUF_LEN 0x580 +// size of a single sample point +#define SAMPLE_SIZE sizeof(s16) + +// Samples are processed in groups of 16 called a "frame" +#define SAMPLES_PER_FRAME ADPCMFSIZE + +// The length of one left/right channel is 13 frames +#define DMEM_1CH_SIZE (13 * SAMPLES_PER_FRAME * SAMPLE_SIZE) +// Both left and right channels +#define DMEM_2CH_SIZE (2 * DMEM_1CH_SIZE) + +#define AIBUF_LEN (88 * SAMPLES_PER_FRAME) // number of samples +#define AIBUF_SIZE (AIBUF_LEN * SAMPLE_SIZE) // number of bytes + +// Filter sizes +#define FILTER_SIZE (8 * SAMPLE_SIZE) +#define FILTER_BUF_PART1 (8 * SAMPLE_SIZE) +#define FILTER_BUF_PART2 (8 * SAMPLE_SIZE) // Must be the same amount of samples as copied by aDuplicate() (audio microcode) #define WAVE_SAMPLE_COUNT 64 @@ -57,10 +74,10 @@ typedef enum { } SampleMedium; typedef enum { - /* 0 */ CODEC_ADPCM, - /* 1 */ CODEC_S8, + /* 0 */ CODEC_ADPCM, // 16 2-byte samples (32 bytes) compressed into 4-bit samples (8 bytes) + 1 header byte + /* 1 */ CODEC_S8, // 16 2-byte samples (32 bytes) compressed into 8-bit samples (16 bytes) /* 2 */ CODEC_S16_INMEMORY, - /* 3 */ CODEC_SMALL_ADPCM, + /* 3 */ CODEC_SMALL_ADPCM, // 16 2-byte samples (32 bytes) compressed into 2-bit samples (4 bytes) + 1 header byte /* 4 */ CODEC_REVERB, /* 5 */ CODEC_S16 } SampleCodec; diff --git a/src/code/audio_data.c b/src/code/audio_data.c index 679f515097..7aec374940 100644 --- a/src/code/audio_data.c +++ b/src/code/audio_data.c @@ -557,9 +557,70 @@ NoteSubEu gDefaultNoteSub = { }; u16 gHeadsetPanQuantization[64] = { - 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, - 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 30 * SAMPLE_SIZE, + 29 * SAMPLE_SIZE, + 28 * SAMPLE_SIZE, + 27 * SAMPLE_SIZE, + 26 * SAMPLE_SIZE, + 25 * SAMPLE_SIZE, + 24 * SAMPLE_SIZE, + 23 * SAMPLE_SIZE, + 22 * SAMPLE_SIZE, + 21 * SAMPLE_SIZE, + 20 * SAMPLE_SIZE, + 19 * SAMPLE_SIZE, + 18 * SAMPLE_SIZE, + 17 * SAMPLE_SIZE, + 16 * SAMPLE_SIZE, + 15 * SAMPLE_SIZE, + 14 * SAMPLE_SIZE, + 13 * SAMPLE_SIZE, + 12 * SAMPLE_SIZE, + 11 * SAMPLE_SIZE, + 10 * SAMPLE_SIZE, + 9 * SAMPLE_SIZE, + 8 * SAMPLE_SIZE, + 7 * SAMPLE_SIZE, + 6 * SAMPLE_SIZE, + 5 * SAMPLE_SIZE, + 4 * SAMPLE_SIZE, + 3 * SAMPLE_SIZE, + 2 * SAMPLE_SIZE, + 1 * SAMPLE_SIZE, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, }; s32 D_8012FBA4 = 0; diff --git a/src/code/audio_heap.c b/src/code/audio_heap.c index fbce1d5a96..7cdfe086b0 100644 --- a/src/code/audio_heap.c +++ b/src/code/audio_heap.c @@ -961,7 +961,7 @@ void AudioHeap_Init(void) { // Initialize audio binary interface command list buffers for (i = 0; i != 2; i++) { gAudioContext.abiCmdBufs[i] = - AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, gAudioContext.maxAudioCmds * sizeof(u64)); + AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, gAudioContext.maxAudioCmds * sizeof(Acmd)); } // Initialize the decay rate table for adsr @@ -992,9 +992,9 @@ void AudioHeap_Init(void) { reverb->unk_08 = settings->unk_12; reverb->useReverb = 8; reverb->leftRingBuf = - AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * sizeof(s16)); + AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * SAMPLE_SIZE); reverb->rightRingBuf = - AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * sizeof(s16)); + AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, reverb->windowSize * SAMPLE_SIZE); reverb->nextRingBufPos = 0; reverb->unk_20 = 0; reverb->curFrame = 0; @@ -1006,7 +1006,7 @@ void AudioHeap_Init(void) { reverb->tunedSample.tuning = 1.0f; reverb->sample.codec = CODEC_REVERB; reverb->sample.medium = MEDIUM_RAM; - reverb->sample.size = reverb->windowSize * 2; + reverb->sample.size = reverb->windowSize * SAMPLE_SIZE; reverb->sample.sampleAddr = (u8*)reverb->leftRingBuf; reverb->loop.start = 0; reverb->loop.count = 1; @@ -1014,32 +1014,34 @@ void AudioHeap_Init(void) { if (reverb->downsampleRate != 1) { reverb->unk_0E = 0x8000 / reverb->downsampleRate; - reverb->unk_30 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20); - reverb->unk_34 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20); - reverb->unk_38 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20); - reverb->unk_3C = AudioHeap_AllocZeroed(&gAudioContext.miscPool, 0x20); + reverb->unk_30 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, sizeof(RESAMPLE_STATE)); + reverb->unk_34 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, sizeof(RESAMPLE_STATE)); + reverb->unk_38 = AudioHeap_AllocZeroed(&gAudioContext.miscPool, sizeof(RESAMPLE_STATE)); + reverb->unk_3C = AudioHeap_AllocZeroed(&gAudioContext.miscPool, sizeof(RESAMPLE_STATE)); for (j = 0; j < gAudioContext.audioBufferParameters.updatesPerFrame; j++) { - ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, 0x340); + ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, DMEM_2CH_SIZE); reverb->items[0][j].toDownsampleLeft = ramAddr; - reverb->items[0][j].toDownsampleRight = ramAddr + 0x1A0 / sizeof(s16); + reverb->items[0][j].toDownsampleRight = ramAddr + DMEM_1CH_SIZE / SAMPLE_SIZE; - ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, 0x340); + ramAddr = AudioHeap_AllocZeroedAttemptExternal(&gAudioContext.miscPool, DMEM_2CH_SIZE); reverb->items[1][j].toDownsampleLeft = ramAddr; - reverb->items[1][j].toDownsampleRight = ramAddr + 0x1A0 / sizeof(s16); + reverb->items[1][j].toDownsampleRight = ramAddr + DMEM_1CH_SIZE / SAMPLE_SIZE; } } if (settings->lowPassFilterCutoffLeft != 0) { - reverb->filterLeftState = AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 0x40); - reverb->filterLeft = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, 8 * sizeof(s16)); + reverb->filterLeftState = + AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 2 * (FILTER_BUF_PART1 + FILTER_BUF_PART2)); + reverb->filterLeft = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, FILTER_SIZE); AudioHeap_LoadLowPassFilter(reverb->filterLeft, settings->lowPassFilterCutoffLeft); } else { reverb->filterLeft = NULL; } if (settings->lowPassFilterCutoffRight != 0) { - reverb->filterRightState = AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 0x40); - reverb->filterRight = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, 8 * sizeof(s16)); + reverb->filterRightState = + AudioHeap_AllocDmaMemoryZeroed(&gAudioContext.miscPool, 2 * (FILTER_BUF_PART1 + FILTER_BUF_PART2)); + reverb->filterRight = AudioHeap_AllocDmaMemory(&gAudioContext.miscPool, FILTER_SIZE); AudioHeap_LoadLowPassFilter(reverb->filterRight, settings->lowPassFilterCutoffRight); } else { reverb->filterRight = NULL; diff --git a/src/code/audio_init_params.c b/src/code/audio_init_params.c index dc647406ba..28ccec031e 100644 --- a/src/code/audio_init_params.c +++ b/src/code/audio_init_params.c @@ -17,7 +17,7 @@ const s16 D_8014A6C0[] = { #define SFX_SOUNDFONT_2_SIZE 0x17B0 // Sizes of everything on the init pool -#define AI_BUFFERS_SIZE (AIBUF_LEN * sizeof(s16) * ARRAY_COUNT(gAudioContext.aiBuffers)) +#define AI_BUFFERS_SIZE (AIBUF_SIZE * ARRAY_COUNT(gAudioContext.aiBuffers)) #define SOUNDFONT_LIST_SIZE (NUM_SOUNDFONTS * sizeof(SoundFont)) #define PERMANENT_POOL_SIZE (SFX_SEQ_SIZE + SFX_SOUNDFONT_1_SIZE + SFX_SOUNDFONT_2_SIZE) diff --git a/src/code/audio_load.c b/src/code/audio_load.c index 8c77f3beb2..73de884116 100644 --- a/src/code/audio_load.c +++ b/src/code/audio_load.c @@ -1200,8 +1200,8 @@ void AudioLoad_Init(void* heap, u32 heapSize) { AudioHeap_InitMainPools(gAudioHeapInitSizes.initPoolSize); // Initialize the audio interface buffers - for (i = 0; i < 3; i++) { - gAudioContext.aiBuffers[i] = AudioHeap_AllocZeroed(&gAudioContext.initPool, AIBUF_LEN * sizeof(s16)); + for (i = 0; i < ARRAY_COUNT(gAudioContext.aiBuffers); i++) { + gAudioContext.aiBuffers[i] = AudioHeap_AllocZeroed(&gAudioContext.initPool, AIBUF_SIZE); } // Set audio tables pointers diff --git a/src/code/audio_synthesis.c b/src/code/audio_synthesis.c index 2cebdef855..851ae56d9b 100644 --- a/src/code/audio_synthesis.c +++ b/src/code/audio_synthesis.c @@ -1,25 +1,22 @@ #include "ultra64.h" #include "global.h" -#define DEFAULT_LEN_1CH 0x1A0 -#define DEFAULT_LEN_2CH 0x340 - // DMEM Addresses for the RSP #define DMEM_TEMP 0x3C0 #define DMEM_UNCOMPRESSED_NOTE 0x580 #define DMEM_NOTE_PAN_TEMP 0x5C0 -#define DMEM_SCRATCH2 0x760 // = DMEM_TEMP + DEFAULT_LEN_2CH + a bit more +#define DMEM_SCRATCH2 0x760 // = DMEM_TEMP + DMEM_2CH_SIZE + a bit more #define DMEM_COMPRESSED_ADPCM_DATA 0x940 // = DMEM_LEFT_CH #define DMEM_LEFT_CH 0x940 #define DMEM_RIGHT_CH 0xAE0 #define DMEM_WET_TEMP 0x3E0 -#define DMEM_WET_SCRATCH 0x720 // = DMEM_WET_TEMP + DEFAULT_LEN_2CH +#define DMEM_WET_SCRATCH 0x720 // = DMEM_WET_TEMP + DMEM_2CH_SIZE #define DMEM_WET_LEFT_CH 0xC80 -#define DMEM_WET_RIGHT_CH 0xE20 // = DMEM_WET_LEFT_CH + DEFAULT_LEN_1CH +#define DMEM_WET_RIGHT_CH 0xE20 // = DMEM_WET_LEFT_CH + DMEM_1CH_SIZE -Acmd* AudioSynth_LoadRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 length, SynthesisReverb* reverb); -Acmd* AudioSynth_SaveBufferOffset(Acmd* cmd, u16 dmem, u16 offset, s32 length, s16* buf); -Acmd* AudioSynth_SaveRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 length, SynthesisReverb* reverb); +Acmd* AudioSynth_LoadRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 size, SynthesisReverb* reverb); +Acmd* AudioSynth_SaveBufferOffset(Acmd* cmd, u16 dmem, u16 offset, s32 size, s16* buf); +Acmd* AudioSynth_SaveRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 size, SynthesisReverb* reverb); Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updateIndex); Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisState* synthState, s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updateIndex); @@ -28,7 +25,7 @@ Acmd* AudioSynth_NoteApplyHeadsetPanEffects(Acmd* cmd, NoteSubEu* noteSubEu, Not s32 flags, s32 side); Acmd* AudioSynth_ProcessEnvelope(Acmd* cmd, NoteSubEu* noteSubEu, NoteSynthesisState* synthState, s32 aiBufLen, u16 inBuf, s32 headsetPanSettings, s32 flags); -Acmd* AudioSynth_FinalResample(Acmd* cmd, NoteSynthesisState* synthState, s32 count, u16 pitch, u16 inpDmem, +Acmd* AudioSynth_FinalResample(Acmd* cmd, NoteSynthesisState* synthState, s32 size, u16 pitch, u16 inpDmem, s32 resampleFlags); u32 D_801304A0 = _SHIFTL(A_ENVMIXER, 24, 8); @@ -61,14 +58,14 @@ void AudioSynth_InitNextRingBuf(s32 chunkLen, s32 updateIndex, s32 reverbIndex) if (reverb->downsampleRate >= 2) { if (reverb->framesToIgnore == 0) { bufItem = &reverb->items[reverb->curFrame][updateIndex]; - Audio_InvalDCache(bufItem->toDownsampleLeft, DEFAULT_LEN_2CH); + Audio_InvalDCache(bufItem->toDownsampleLeft, DMEM_2CH_SIZE); - for (j = 0, i = 0; i < bufItem->lengthA / 2; j += reverb->downsampleRate, i++) { + for (j = 0, i = 0; i < bufItem->lengthA / (s32)SAMPLE_SIZE; j += reverb->downsampleRate, i++) { reverb->leftRingBuf[bufItem->startPos + i] = bufItem->toDownsampleLeft[j]; reverb->rightRingBuf[bufItem->startPos + i] = bufItem->toDownsampleRight[j]; } - for (i = 0; i < bufItem->lengthB / 2; j += reverb->downsampleRate, i++) { + for (i = 0; i < bufItem->lengthB / (s32)SAMPLE_SIZE; j += reverb->downsampleRate, i++) { reverb->leftRingBuf[i] = bufItem->toDownsampleLeft[j]; reverb->rightRingBuf[i] = bufItem->toDownsampleRight[j]; } @@ -80,14 +77,14 @@ void AudioSynth_InitNextRingBuf(s32 chunkLen, s32 updateIndex, s32 reverbIndex) extraSamples = (numSamples + reverb->nextRingBufPos) - reverb->bufSizePerChan; temp_a0_2 = reverb->nextRingBufPos; if (extraSamples < 0) { - bufItem->lengthA = numSamples * 2; + bufItem->lengthA = numSamples * SAMPLE_SIZE; bufItem->lengthB = 0; bufItem->startPos = reverb->nextRingBufPos; reverb->nextRingBufPos += numSamples; } else { // End of the buffer is reached. Loop back around - bufItem->lengthA = (numSamples - extraSamples) * 2; - bufItem->lengthB = extraSamples * 2; + bufItem->lengthA = (numSamples - extraSamples) * SAMPLE_SIZE; + bufItem->lengthB = extraSamples * SAMPLE_SIZE; bufItem->startPos = reverb->nextRingBufPos; reverb->nextRingBufPos = extraSamples; } @@ -104,13 +101,13 @@ void AudioSynth_InitNextRingBuf(s32 chunkLen, s32 updateIndex, s32 reverbIndex) numSamples = chunkLen / reverb->downsampleRate; extraSamples = (temp_a0_4 + numSamples) - reverb->bufSizePerChan; if (extraSamples < 0) { - bufItem->lengthA = numSamples * 2; + bufItem->lengthA = numSamples * SAMPLE_SIZE; bufItem->lengthB = 0; bufItem->startPos = temp_a0_4; } else { // End of the buffer is reached. Loop back around - bufItem->lengthA = (numSamples - extraSamples) * 2; - bufItem->lengthB = extraSamples * 2; + bufItem->lengthA = (numSamples - extraSamples) * SAMPLE_SIZE; + bufItem->lengthB = extraSamples * SAMPLE_SIZE; bufItem->startPos = temp_a0_4; } bufItem->numSamplesAfterDownsampling = numSamples; @@ -175,7 +172,7 @@ Acmd* AudioSynth_Update(Acmd* cmdStart, s32* cmdCnt, s16* aiStart, s32 aiBufLen) cmdP = AudioSynth_DoOneAudioUpdate(aiBufP, chunkLen, cmdP, gAudioContext.audioBufferParameters.updatesPerFrame - i); aiBufLen -= chunkLen; - aiBufP += chunkLen * 2; + aiBufP += 2 * chunkLen; } for (j = 0; j < gAudioContext.numSynthesisReverbs; j++) { @@ -229,9 +226,9 @@ Acmd* AudioSynth_SaveRingBuffer1AtTemp(Acmd* cmd, SynthesisReverb* reverb, s16 u * Leak some audio from the left reverb channel into the right reverb channel and vice versa (pan) */ Acmd* AudioSynth_LeakReverb(Acmd* cmd, SynthesisReverb* reverb) { - aDMEMMove(cmd++, DMEM_WET_LEFT_CH, DMEM_WET_SCRATCH, DEFAULT_LEN_1CH); - aMix(cmd++, 0x1A, reverb->leakRtl, DMEM_WET_RIGHT_CH, DMEM_WET_LEFT_CH); - aMix(cmd++, 0x1A, reverb->leakLtr, DMEM_WET_SCRATCH, DMEM_WET_RIGHT_CH); + aDMEMMove(cmd++, DMEM_WET_LEFT_CH, DMEM_WET_SCRATCH, DMEM_1CH_SIZE); + aMix(cmd++, DMEM_1CH_SIZE >> 4, reverb->leakRtl, DMEM_WET_RIGHT_CH, DMEM_WET_LEFT_CH); + aMix(cmd++, DMEM_1CH_SIZE >> 4, reverb->leakLtr, DMEM_WET_SCRATCH, DMEM_WET_RIGHT_CH); return cmd; } @@ -240,16 +237,17 @@ Acmd* func_800DB4E4(Acmd* cmd, s32 aiBufLen, SynthesisReverb* reverb, s16 update s16 offsetA; s16 offsetB; - offsetA = (item->startPos & 7) * 2; + offsetA = (item->startPos & 7) * SAMPLE_SIZE; offsetB = ALIGN16(offsetA + item->lengthA); - cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP, item->startPos - (offsetA / 2), DEFAULT_LEN_1CH, reverb); + cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP, item->startPos - (offsetA / (s32)SAMPLE_SIZE), + DMEM_1CH_SIZE, reverb); if (item->lengthB != 0) { // Ring buffer wrapped - cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP + offsetB, 0, DEFAULT_LEN_1CH - offsetB, reverb); + cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP + offsetB, 0, DMEM_1CH_SIZE - offsetB, reverb); } - aSetBuffer(cmd++, 0, DMEM_WET_TEMP + offsetA, DMEM_WET_LEFT_CH, aiBufLen * 2); + aSetBuffer(cmd++, 0, DMEM_WET_TEMP + offsetA, DMEM_WET_LEFT_CH, aiBufLen * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, reverb->unk_0E, reverb->unk_30); - aSetBuffer(cmd++, 0, DMEM_WET_TEMP + DEFAULT_LEN_1CH + offsetA, DMEM_WET_RIGHT_CH, aiBufLen * 2); + aSetBuffer(cmd++, 0, DMEM_WET_TEMP + DMEM_1CH_SIZE + offsetA, DMEM_WET_RIGHT_CH, aiBufLen * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, reverb->unk_0E, reverb->unk_34); return cmd; } @@ -257,7 +255,7 @@ Acmd* func_800DB4E4(Acmd* cmd, s32 aiBufLen, SynthesisReverb* reverb, s16 update Acmd* func_800DB680(Acmd* cmd, SynthesisReverb* reverb, s16 updateIndex) { ReverbRingBufferItem* bufItem = &reverb->items[reverb->curFrame][updateIndex]; - aSetBuffer(cmd++, 0, DMEM_WET_LEFT_CH, DMEM_WET_SCRATCH, bufItem->unk_18 * 2); + aSetBuffer(cmd++, 0, DMEM_WET_LEFT_CH, DMEM_WET_SCRATCH, bufItem->unk_18 * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, bufItem->unk_16, reverb->unk_38); cmd = AudioSynth_SaveBufferOffset(cmd, DMEM_WET_SCRATCH, bufItem->startPos, bufItem->lengthA, reverb->leftRingBuf); @@ -266,7 +264,7 @@ Acmd* func_800DB680(Acmd* cmd, SynthesisReverb* reverb, s16 updateIndex) { cmd = AudioSynth_SaveBufferOffset(cmd, DMEM_WET_SCRATCH + bufItem->lengthA, 0, bufItem->lengthB, reverb->leftRingBuf); } - aSetBuffer(cmd++, 0, DMEM_WET_RIGHT_CH, DMEM_WET_SCRATCH, bufItem->unk_18 * 2); + aSetBuffer(cmd++, 0, DMEM_WET_RIGHT_CH, DMEM_WET_SCRATCH, bufItem->unk_18 * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, bufItem->unk_16, reverb->unk_3C); cmd = AudioSynth_SaveBufferOffset(cmd, DMEM_WET_SCRATCH, bufItem->startPos, bufItem->lengthA, reverb->rightRingBuf); @@ -285,17 +283,18 @@ Acmd* func_800DB828(Acmd* cmd, s32 aiBufLen, SynthesisReverb* reverb, s16 update s16 offsetB; item->unk_14 = (item->unk_18 << 0xF) / aiBufLen; - offsetA = (item->startPos & 7) * 2; + offsetA = (item->startPos & 7) * SAMPLE_SIZE; item->unk_16 = (aiBufLen << 0xF) / item->unk_18; offsetB = ALIGN16(offsetA + item->lengthA); - cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP, item->startPos - (offsetA / 2), DEFAULT_LEN_1CH, reverb); + cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP, item->startPos - (offsetA / (s32)SAMPLE_SIZE), + DMEM_1CH_SIZE, reverb); if (item->lengthB != 0) { // Ring buffer wrapped - cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP + offsetB, 0, DEFAULT_LEN_1CH - offsetB, reverb); + cmd = AudioSynth_LoadRingBufferPart(cmd, DMEM_WET_TEMP + offsetB, 0, DMEM_1CH_SIZE - offsetB, reverb); } - aSetBuffer(cmd++, 0, DMEM_WET_TEMP + offsetA, DMEM_WET_LEFT_CH, aiBufLen * 2); + aSetBuffer(cmd++, 0, DMEM_WET_TEMP + offsetA, DMEM_WET_LEFT_CH, aiBufLen * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, item->unk_14, reverb->unk_30); - aSetBuffer(cmd++, 0, DMEM_WET_TEMP + DEFAULT_LEN_1CH + offsetA, DMEM_WET_RIGHT_CH, aiBufLen * 2); + aSetBuffer(cmd++, 0, DMEM_WET_TEMP + DMEM_1CH_SIZE + offsetA, DMEM_WET_RIGHT_CH, aiBufLen * SAMPLE_SIZE); aResample(cmd++, reverb->resampleFlags, item->unk_14, reverb->unk_34); return cmd; } @@ -303,14 +302,14 @@ Acmd* func_800DB828(Acmd* cmd, s32 aiBufLen, SynthesisReverb* reverb, s16 update /** * Apply a filter (convolution) to each reverb channel. */ -Acmd* AudioSynth_FilterReverb(Acmd* cmd, s32 count, SynthesisReverb* reverb) { +Acmd* AudioSynth_FilterReverb(Acmd* cmd, s32 size, SynthesisReverb* reverb) { if (reverb->filterLeft != NULL) { - aFilter(cmd++, 2, count, reverb->filterLeft); + aFilter(cmd++, 2, size, reverb->filterLeft); aFilter(cmd++, reverb->resampleFlags, DMEM_WET_LEFT_CH, reverb->filterLeftState); } if (reverb->filterRight != NULL) { - aFilter(cmd++, 2, count, reverb->filterRight); + aFilter(cmd++, 2, size, reverb->filterRight); aFilter(cmd++, reverb->resampleFlags, DMEM_WET_RIGHT_CH, reverb->filterRightState); } return cmd; @@ -322,7 +321,7 @@ Acmd* AudioSynth_MaybeMixRingBuffer1(Acmd* cmd, SynthesisReverb* reverb, s32 upd temp_a3 = &gAudioContext.synthesisReverbs[reverb->unk_05]; if (temp_a3->downsampleRate == 1) { cmd = AudioSynth_LoadRingBuffer1AtTemp(cmd, temp_a3, updateIndex); - aMix(cmd++, 0x34, reverb->unk_08, DMEM_WET_LEFT_CH, DMEM_WET_TEMP); + aMix(cmd++, DMEM_2CH_SIZE >> 4, reverb->unk_08, DMEM_WET_LEFT_CH, DMEM_WET_TEMP); cmd = AudioSynth_SaveRingBuffer1AtTemp(cmd, temp_a3, updateIndex); } return cmd; @@ -357,8 +356,8 @@ void func_800DBC10(void) { void func_800DBC18(void) { } -void AudioSynth_SetBuffer(Acmd* cmd, s32 flags, s32 dmemIn, s32 dmemOut, s32 count) { - aSetBuffer(cmd, flags, dmemIn, dmemOut, count); +void AudioSynth_SetBuffer(Acmd* cmd, s32 flags, s32 dmemIn, s32 dmemOut, u32 size) { + aSetBuffer(cmd, flags, dmemIn, dmemOut, size); } void func_800DBC54(void) { @@ -368,9 +367,9 @@ void func_800DBC5C(void) { } // possible fake match? -void AudioSynth_DMemMove(Acmd* cmd, s32 dmemIn, s32 dmemOut, s32 count) { +void AudioSynth_DMemMove(Acmd* cmd, s32 dmemIn, s32 dmemOut, u32 size) { cmd->words.w0 = _SHIFTL(A_DMEMMOVE, 24, 8) | _SHIFTL(dmemIn, 0, 24); - cmd->words.w1 = _SHIFTL(dmemOut, 16, 16) | _SHIFTL(count, 0, 16); + cmd->words.w1 = _SHIFTL(dmemOut, 16, 16) | _SHIFTL(size, 0, 16); } void func_800DBC90(void) { @@ -385,8 +384,8 @@ void func_800DBCA0(void) { void func_800DBCA8(void) { } -void AudioSynth_InterL(Acmd* cmd, s32 dmemIn, s32 dmemOut, s32 count) { - cmd->words.w0 = _SHIFTL(A_INTERL, 24, 8) | _SHIFTL(count, 0, 16); +void AudioSynth_InterL(Acmd* cmd, s32 dmemIn, s32 dmemOut, s32 numSamples) { + cmd->words.w0 = _SHIFTL(A_INTERL, 24, 8) | _SHIFTL(numSamples, 0, 16); cmd->words.w1 = _SHIFTL(dmemIn, 16, 16) | _SHIFTL(dmemOut, 0, 16); } @@ -423,13 +422,13 @@ void AudioSynth_S8Dec(Acmd* cmd, s32 flags, s16* state) { aS8Dec(cmd, flags, state); } -void AudioSynth_HiLoGain(Acmd* cmd, s32 gain, s32 dmemIn, s32 dmemOut, s32 count) { - cmd->words.w0 = _SHIFTL(A_HILOGAIN, 24, 8) | _SHIFTL(gain, 16, 8) | _SHIFTL(count, 0, 16); +void AudioSynth_HiLoGain(Acmd* cmd, s32 gain, s32 dmemIn, s32 dmemOut, s32 size) { + cmd->words.w0 = _SHIFTL(A_HILOGAIN, 24, 8) | _SHIFTL(gain, 16, 8) | _SHIFTL(size, 0, 16); cmd->words.w1 = _SHIFTL(dmemIn, 16, 16) | _SHIFTL(dmemOut, 0, 16); } -void AudioSynth_UnkCmd19(Acmd* cmd, s32 arg1, s32 arg2, s32 arg3, s32 arg4) { - cmd->words.w0 = _SHIFTL(A_UNK19, 24, 8) | _SHIFTL(arg4, 16, 8) | _SHIFTL(arg3, 0, 16); +void AudioSynth_UnkCmd19(Acmd* cmd, s32 arg1, s32 arg2, s32 size, s32 arg4) { + cmd->words.w0 = _SHIFTL(A_UNK19, 24, 8) | _SHIFTL(arg4, 16, 8) | _SHIFTL(size, 0, 16); cmd->words.w1 = _SHIFTL(arg1, 16, 16) | _SHIFTL(arg2, 0, 16); } @@ -445,8 +444,8 @@ void func_800DBE28(void) { void func_800DBE30(void) { } -void AudioSynth_UnkCmd3(Acmd* cmd, s32 arg1, s32 arg2, s32 arg3) { - cmd->words.w0 = _SHIFTL(A_UNK3, 24, 8) | _SHIFTL(arg3, 0, 16); +void AudioSynth_UnkCmd3(Acmd* cmd, s32 arg1, s32 arg2, s32 size) { + cmd->words.w0 = _SHIFTL(A_UNK3, 24, 8) | _SHIFTL(size, 0, 16); cmd->words.w1 = _SHIFTL(arg1, 16, 16) | _SHIFTL(arg2, 0, 16); } @@ -459,12 +458,12 @@ void func_800DBE64(void) { void func_800DBE6C(void) { } -void AudioSynth_LoadFilter(Acmd* cmd, s32 flags, s32 countOrBuf, void* addr) { - aFilter(cmd, flags, countOrBuf, addr); +void AudioSynth_LoadFilterBuffer(Acmd* cmd, s32 flags, s32 buf, void* addr) { + aFilter(cmd, flags, buf, addr); } -void AudioSynth_LoadFilterCount(Acmd* cmd, s32 count, void* addr) { - aFilter(cmd, 2, count, addr); +void AudioSynth_LoadFilterSize(Acmd* cmd, s32 size, void* addr) { + aFilter(cmd, 2, size, addr); } Acmd* AudioSynth_LoadRingBuffer1(Acmd* cmd, s32 aiBufLen, SynthesisReverb* reverb, s16 updateIndex) { @@ -492,20 +491,20 @@ Acmd* AudioSynth_LoadRingBuffer2(Acmd* cmd, s32 aiBufLen, SynthesisReverb* rever return cmd; } -Acmd* AudioSynth_LoadRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 length, SynthesisReverb* reverb) { - aLoadBuffer(cmd++, &reverb->leftRingBuf[startPos], dmem, length); - aLoadBuffer(cmd++, &reverb->rightRingBuf[startPos], dmem + DEFAULT_LEN_1CH, length); +Acmd* AudioSynth_LoadRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 size, SynthesisReverb* reverb) { + aLoadBuffer(cmd++, &reverb->leftRingBuf[startPos], dmem, size); + aLoadBuffer(cmd++, &reverb->rightRingBuf[startPos], dmem + DMEM_1CH_SIZE, size); return cmd; } -Acmd* AudioSynth_SaveRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 length, SynthesisReverb* reverb) { - aSaveBuffer(cmd++, dmem, &reverb->leftRingBuf[startPos], length); - aSaveBuffer(cmd++, dmem + DEFAULT_LEN_1CH, &reverb->rightRingBuf[startPos], length); +Acmd* AudioSynth_SaveRingBufferPart(Acmd* cmd, u16 dmem, u16 startPos, s32 size, SynthesisReverb* reverb) { + aSaveBuffer(cmd++, dmem, &reverb->leftRingBuf[startPos], size); + aSaveBuffer(cmd++, dmem + DMEM_1CH_SIZE, &reverb->rightRingBuf[startPos], size); return cmd; } -Acmd* AudioSynth_SaveBufferOffset(Acmd* cmd, u16 dmem, u16 offset, s32 length, s16* buf) { - aSaveBuffer(cmd++, dmem, &buf[offset], length); +Acmd* AudioSynth_SaveBufferOffset(Acmd* cmd, u16 dmem, u16 offset, s32 size, s16* buf) { + aSaveBuffer(cmd++, dmem, &buf[offset], size); return cmd; } @@ -549,7 +548,7 @@ Acmd* AudioSynth_SaveReverbSamples(Acmd* cmd, SynthesisReverb* reverb, s16 updat } else { // Downsampling is done later by CPU when RSP is done, therefore we need to have // double buffering. Left and right buffers are adjacent in memory. - AudioSynth_SaveBuffer(cmd++, DMEM_WET_LEFT_CH, DEFAULT_LEN_2CH, + AudioSynth_SaveBuffer(cmd++, DMEM_WET_LEFT_CH, DMEM_2CH_SIZE, reverb->items[reverb->curFrame][updateIndex].toDownsampleLeft); } @@ -606,7 +605,7 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat } } - aClearBuffer(cmd++, DMEM_LEFT_CH, DEFAULT_LEN_2CH); + aClearBuffer(cmd++, DMEM_LEFT_CH, DMEM_2CH_SIZE); i = 0; for (reverbIndex = 0; reverbIndex < gAudioContext.numSynthesisReverbs; reverbIndex++) { @@ -620,15 +619,15 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat // Mixes reverb sample into the main dry channel // reverb->volume is always set to 0x7FFF (audio spec), and DMEM_LEFT_CH is cleared before the loop. // So for the first iteration, this is essentially a DMEMmove from DMEM_WET_LEFT_CH to DMEM_LEFT_CH - aMix(cmd++, DEFAULT_LEN_2CH >> 4, reverb->volume, DMEM_WET_LEFT_CH, DMEM_LEFT_CH); + aMix(cmd++, DMEM_2CH_SIZE >> 4, reverb->volume, DMEM_WET_LEFT_CH, DMEM_LEFT_CH); unk14 = reverb->unk_14; if (unk14) { - aDMEMMove(cmd++, DMEM_WET_LEFT_CH, DMEM_WET_TEMP, DEFAULT_LEN_2CH); + aDMEMMove(cmd++, DMEM_WET_LEFT_CH, DMEM_WET_TEMP, DMEM_2CH_SIZE); } // Decays reverb over time. The (+ 0x8000) here is -100% - aMix(cmd++, DEFAULT_LEN_2CH >> 4, reverb->decayRatio + 0x8000, DMEM_WET_LEFT_CH, DMEM_WET_LEFT_CH); + aMix(cmd++, DMEM_2CH_SIZE >> 4, reverb->decayRatio + 0x8000, DMEM_WET_LEFT_CH, DMEM_WET_LEFT_CH); // Leak reverb between the left and right channels if (reverb->leakRtl != 0 || reverb->leakLtr != 0) { @@ -642,7 +641,7 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat cmd = AudioSynth_MaybeMixRingBuffer1(cmd, reverb, updateIndex); } cmd = AudioSynth_MaybeLoadRingBuffer2(cmd, aiBufLen, reverb, updateIndex); - aMix(cmd++, 0x34, reverb->unk_16, DMEM_WET_TEMP, DMEM_WET_LEFT_CH); + aMix(cmd++, DMEM_2CH_SIZE >> 4, reverb->unk_16, DMEM_WET_TEMP, DMEM_WET_LEFT_CH); } } @@ -660,7 +659,7 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat if (useReverb) { if (reverb->filterLeft != NULL || reverb->filterRight != NULL) { - cmd = AudioSynth_FilterReverb(cmd, aiBufLen * 2, reverb); + cmd = AudioSynth_FilterReverb(cmd, aiBufLen * SAMPLE_SIZE, reverb); } // Saves the wet channel sample from DMEM (DMEM_WET_LEFT_CH) into RDRAM (ringBuffer) for future use @@ -682,9 +681,8 @@ Acmd* AudioSynth_DoOneAudioUpdate(s16* aiBuf, s32 aiBufLen, Acmd* cmd, s32 updat i++; } - updateIndex = aiBufLen * 2; - aInterleave(cmd++, DMEM_TEMP, DMEM_LEFT_CH, DMEM_RIGHT_CH, updateIndex); - aSaveBuffer(cmd++, DMEM_TEMP, aiBuf, updateIndex * 2); + aInterleave(cmd++, DMEM_TEMP, DMEM_LEFT_CH, DMEM_RIGHT_CH, 2 * aiBufLen); + aSaveBuffer(cmd++, DMEM_TEMP, aiBuf, 2 * (aiBufLen * (s32)SAMPLE_SIZE)); return cmd; } @@ -710,7 +708,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS s32 nSamplesToDecode; u32 sampleAddr; u32 samplesLenFixedPoint; - s32 samplesLenAdjusted; + s32 samplesLenAdjusted; // seems to be used as both bytes and numSamples? s32 nSamplesProcessed; s32 loopEndPos; s32 nSamplesToProcess; @@ -780,7 +778,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS if (noteSubEu->bitField1.isSyntheticWave) { cmd = AudioSynth_LoadWaveSamples(cmd, noteSubEu, synthState, numSamplesToLoad); - sampleDmemBeforeResampling = DMEM_UNCOMPRESSED_NOTE + (synthState->samplePosInt * (s32)sizeof(s16)); + sampleDmemBeforeResampling = DMEM_UNCOMPRESSED_NOTE + (synthState->samplePosInt * (s32)SAMPLE_SIZE); synthState->samplePosInt += numSamplesToLoad; } else { sample = noteSubEu->tunedSample->sample; @@ -804,6 +802,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS if (sample->codec == CODEC_ADPCM || sample->codec == CODEC_SMALL_ADPCM) { if (gAudioContext.curLoadedBook != sample->book->book) { u32 nEntries; + switch (bookOffset) { case 1: gAudioContext.curLoadedBook = &D_8012FBA8[1]; @@ -817,7 +816,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS if (1) {} if (1) {} if (1) {} - nEntries = 16 * sample->book->order * sample->book->numPredictors; + nEntries = SAMPLES_PER_FRAME * sample->book->order * sample->book->numPredictors; aLoadADPCM(cmd++, nEntries, gAudioContext.curLoadedBook); } } @@ -832,13 +831,14 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS nSamplesToProcess = samplesLenAdjusted - nSamplesProcessed; if (nFirstFrameSamplesToIgnore == 0 && !synthState->restart) { - nFirstFrameSamplesToIgnore = 16; + nFirstFrameSamplesToIgnore = SAMPLES_PER_FRAME; } - nSamplesInFirstFrame = 16 - nFirstFrameSamplesToIgnore; + nSamplesInFirstFrame = SAMPLES_PER_FRAME - nFirstFrameSamplesToIgnore; if (nSamplesToProcess < nSamplesUntilLoopEnd) { - nFramesToDecode = (s32)(nSamplesToProcess - nSamplesInFirstFrame + 15) / 16; - nSamplesToDecode = nFramesToDecode * 16; + nFramesToDecode = + (s32)(nSamplesToProcess - nSamplesInFirstFrame + SAMPLES_PER_FRAME - 1) / SAMPLES_PER_FRAME; + nSamplesToDecode = nFramesToDecode * SAMPLES_PER_FRAME; nTrailingSamplesToIgnore = nSamplesInFirstFrame + nSamplesToDecode - nSamplesToProcess; } else { nSamplesToDecode = nSamplesUntilLoopEnd - nSamplesInFirstFrame; @@ -847,7 +847,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS nSamplesToDecode = 0; nSamplesInFirstFrame = nSamplesUntilLoopEnd; } - nFramesToDecode = (nSamplesToDecode + 15) / 16; + nFramesToDecode = (nSamplesToDecode + SAMPLES_PER_FRAME - 1) / SAMPLES_PER_FRAME; if (loopInfo->count != 0) { // Loop around and restart restart = true; @@ -858,25 +858,29 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS switch (sample->codec) { case CODEC_ADPCM: + // 16 2-byte samples (32 bytes) compressed into 4-bit samples (8 bytes) + 1 header byte frameSize = 9; - skipInitialSamples = 16; + skipInitialSamples = SAMPLES_PER_FRAME; sampleDataStart = 0; break; case CODEC_SMALL_ADPCM: + // 16 2-byte samples (32 bytes) compressed into 2-bit samples (4 bytes) + 1 header byte frameSize = 5; - skipInitialSamples = 16; + skipInitialSamples = SAMPLES_PER_FRAME; sampleDataStart = 0; break; case CODEC_S8: + // 16 2-byte samples (32 bytes) compressed into 8-bit samples (16 bytes) frameSize = 16; - skipInitialSamples = 16; + skipInitialSamples = SAMPLES_PER_FRAME; sampleDataStart = 0; break; case CODEC_S16_INMEMORY: - AudioSynth_ClearBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, (samplesLenAdjusted * 2) + 0x20); + AudioSynth_ClearBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, + (samplesLenAdjusted + SAMPLES_PER_FRAME) * SAMPLE_SIZE); flags = A_CONTINUE; skipBytes = 0; nSamplesProcessed = samplesLenAdjusted; @@ -884,7 +888,8 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS goto skip; case CODEC_S16: - AudioSynth_ClearBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, (samplesLenAdjusted * 2) + 0x20); + AudioSynth_ClearBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, + (samplesLenAdjusted + SAMPLES_PER_FRAME) * SAMPLE_SIZE); flags = A_CONTINUE; skipBytes = 0; nSamplesProcessed = samplesLenAdjusted; @@ -896,7 +901,8 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS } if (nFramesToDecode != 0) { - frameIndex = (synthState->samplePosInt + skipInitialSamples - nFirstFrameSamplesToIgnore) / 16; + frameIndex = (synthState->samplePosInt + skipInitialSamples - nFirstFrameSamplesToIgnore) / + SAMPLES_PER_FRAME; sampleDataOffset = frameIndex * frameSize; if (sample->medium == MEDIUM_RAM) { sampleData = (u8*)(sampleDataStart + sampleDataOffset + sampleAddr); @@ -904,8 +910,8 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS return cmd; } else { sampleData = AudioLoad_DmaSampleData(sampleDataStart + sampleDataOffset + sampleAddr, - ALIGN16((nFramesToDecode * frameSize) + 0x10), flags, - &synthState->sampleDmaIndex, sample->medium); + ALIGN16((nFramesToDecode * frameSize) + SAMPLES_PER_FRAME), + flags, &synthState->sampleDmaIndex, sample->medium); } if (sampleData == NULL) { @@ -913,7 +919,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS } sampleDataStartPad = (u32)sampleData & 0xF; - aligned = ALIGN16((nFramesToDecode * frameSize) + 16); + aligned = ALIGN16((nFramesToDecode * frameSize) + SAMPLES_PER_FRAME); addr = DMEM_COMPRESSED_ADPCM_DATA - aligned; aLoadBuffer(cmd++, sampleData - sampleDataStartPad, addr, aligned); } else { @@ -930,59 +936,59 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS nSamplesInThisIteration = nSamplesToDecode + nSamplesInFirstFrame - nTrailingSamplesToIgnore; if (nSamplesProcessed == 0) { if (1) {} - skipBytes = nFirstFrameSamplesToIgnore * 2; + skipBytes = nFirstFrameSamplesToIgnore * SAMPLE_SIZE; } else { - phi_s4 = ALIGN16(s5 + 16); + phi_s4 = ALIGN16(s5 + 8 * SAMPLE_SIZE); } switch (sample->codec) { case CODEC_ADPCM: - aligned = ALIGN16((nFramesToDecode * frameSize) + 0x10); + aligned = ALIGN16((nFramesToDecode * frameSize) + SAMPLES_PER_FRAME); addr = DMEM_COMPRESSED_ADPCM_DATA - aligned; aSetBuffer(cmd++, 0, addr + sampleDataStartPad, DMEM_UNCOMPRESSED_NOTE + phi_s4, - nSamplesToDecode * 2); + nSamplesToDecode * SAMPLE_SIZE); aADPCMdec(cmd++, flags, synthState->synthesisBuffers->adpcmdecState); break; case CODEC_SMALL_ADPCM: - aligned = ALIGN16((nFramesToDecode * frameSize) + 0x10); + aligned = ALIGN16((nFramesToDecode * frameSize) + SAMPLES_PER_FRAME); addr = DMEM_COMPRESSED_ADPCM_DATA - aligned; aSetBuffer(cmd++, 0, addr + sampleDataStartPad, DMEM_UNCOMPRESSED_NOTE + phi_s4, - nSamplesToDecode * 2); + nSamplesToDecode * SAMPLE_SIZE); aADPCMdec(cmd++, flags | 4, synthState->synthesisBuffers->adpcmdecState); break; case CODEC_S8: - aligned = ALIGN16((nFramesToDecode * frameSize) + 0x10); + aligned = ALIGN16((nFramesToDecode * frameSize) + SAMPLES_PER_FRAME); addr = DMEM_COMPRESSED_ADPCM_DATA - aligned; AudioSynth_SetBuffer(cmd++, 0, addr + sampleDataStartPad, DMEM_UNCOMPRESSED_NOTE + phi_s4, - nSamplesToDecode * 2); + nSamplesToDecode * SAMPLE_SIZE); AudioSynth_S8Dec(cmd++, flags, synthState->synthesisBuffers->adpcmdecState); break; } if (nSamplesProcessed != 0) { - aDMEMMove(cmd++, DMEM_UNCOMPRESSED_NOTE + phi_s4 + (nFirstFrameSamplesToIgnore * 2), - DMEM_UNCOMPRESSED_NOTE + s5, nSamplesInThisIteration * 2); + aDMEMMove(cmd++, DMEM_UNCOMPRESSED_NOTE + phi_s4 + (nFirstFrameSamplesToIgnore * SAMPLE_SIZE), + DMEM_UNCOMPRESSED_NOTE + s5, nSamplesInThisIteration * SAMPLE_SIZE); } nSamplesProcessed += nSamplesInThisIteration; switch (flags) { case A_INIT: - skipBytes = 0x20; - s5 = (nSamplesToDecode + 0x10) * 2; + skipBytes = SAMPLES_PER_FRAME * SAMPLE_SIZE; + s5 = (nSamplesToDecode + SAMPLES_PER_FRAME) * SAMPLE_SIZE; break; case A_LOOP: - s5 = nSamplesInThisIteration * 2 + s5; + s5 = nSamplesInThisIteration * SAMPLE_SIZE + s5; break; default: if (s5 != 0) { - s5 = nSamplesInThisIteration * 2 + s5; + s5 = nSamplesInThisIteration * SAMPLE_SIZE + s5; } else { - s5 = (nFirstFrameSamplesToIgnore + nSamplesInThisIteration) * 2; + s5 = (nFirstFrameSamplesToIgnore + nSamplesInThisIteration) * SAMPLE_SIZE; } break; } @@ -992,7 +998,7 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS skip: if (noteFinished) { AudioSynth_ClearBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE + s5, - (samplesLenAdjusted - nSamplesProcessed) * 2); + (samplesLenAdjusted - nSamplesProcessed) * SAMPLE_SIZE); finished = true; note->noteSubEu.bitField0.finished = true; func_800DB2C0(updateIndex, noteIndex); @@ -1015,19 +1021,21 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS case 2: switch (curPart) { case 0: - AudioSynth_InterL(cmd++, DMEM_UNCOMPRESSED_NOTE + skipBytes, DMEM_TEMP + 0x20, + AudioSynth_InterL(cmd++, DMEM_UNCOMPRESSED_NOTE + skipBytes, + DMEM_TEMP + (SAMPLES_PER_FRAME * SAMPLE_SIZE), ALIGN8(samplesLenAdjusted / 2)); resampledTempLen = samplesLenAdjusted; - sampleDmemBeforeResampling = DMEM_TEMP + 0x20; + sampleDmemBeforeResampling = DMEM_TEMP + (SAMPLES_PER_FRAME * SAMPLE_SIZE); if (finished) { AudioSynth_ClearBuffer(cmd++, sampleDmemBeforeResampling + resampledTempLen, - samplesLenAdjusted + 0x10); + samplesLenAdjusted + SAMPLES_PER_FRAME); } break; case 1: AudioSynth_InterL(cmd++, DMEM_UNCOMPRESSED_NOTE + skipBytes, - DMEM_TEMP + 0x20 + resampledTempLen, ALIGN8(samplesLenAdjusted / 2)); + DMEM_TEMP + (SAMPLES_PER_FRAME * SAMPLE_SIZE) + resampledTempLen, + ALIGN8(samplesLenAdjusted / 2)); break; } } @@ -1043,14 +1051,14 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS flags = A_INIT; } - cmd = AudioSynth_FinalResample(cmd, synthState, aiBufLen * 2, resamplingRateFixedPoint, sampleDmemBeforeResampling, - flags); + cmd = AudioSynth_FinalResample(cmd, synthState, aiBufLen * (s32)SAMPLE_SIZE, resamplingRateFixedPoint, + sampleDmemBeforeResampling, flags); if (bookOffset == 3) { - AudioSynth_UnkCmd19(cmd++, DMEM_TEMP, DMEM_TEMP, aiBufLen * 2, 0); + AudioSynth_UnkCmd19(cmd++, DMEM_TEMP, DMEM_TEMP, aiBufLen * SAMPLE_SIZE, 0); } if (bookOffset == 2) { - AudioSynth_UnkCmd3(cmd++, DMEM_TEMP, DMEM_TEMP, aiBufLen * 2); + AudioSynth_UnkCmd3(cmd++, DMEM_TEMP, DMEM_TEMP, aiBufLen * SAMPLE_SIZE); } gain = noteSubEu->gain; @@ -1059,20 +1067,20 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS if (gain < 0x10) { gain = 0x10; } - AudioSynth_HiLoGain(cmd++, gain, DMEM_TEMP, 0, (aiBufLen * 2) + 0x20); + AudioSynth_HiLoGain(cmd++, gain, DMEM_TEMP, 0, (aiBufLen + SAMPLES_PER_FRAME) * SAMPLE_SIZE); } filter = noteSubEu->filter; if (filter != 0) { - AudioSynth_LoadFilterCount(cmd++, aiBufLen * 2, filter); - AudioSynth_LoadFilter(cmd++, flags, DMEM_TEMP, synthState->synthesisBuffers->mixEnvelopeState); + AudioSynth_LoadFilterSize(cmd++, aiBufLen * SAMPLE_SIZE, filter); + AudioSynth_LoadFilterBuffer(cmd++, flags, DMEM_TEMP, synthState->synthesisBuffers->mixEnvelopeState); } unk7 = noteSubEu->unk_07; unkE = noteSubEu->unk_0E; buf = &synthState->synthesisBuffers->panSamplesBuffer[0x18]; if (unk7 != 0 && noteSubEu->unk_0E != 0) { - AudioSynth_DMemMove(cmd++, DMEM_TEMP, DMEM_SCRATCH2, aiBufLen * 2); + AudioSynth_DMemMove(cmd++, DMEM_TEMP, DMEM_SCRATCH2, aiBufLen * SAMPLE_SIZE); thing = DMEM_SCRATCH2 - unk7; if (synthState->unk_1A != 0) { AudioSynth_ClearBuffer(cmd++, thing, unk7); @@ -1080,9 +1088,9 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS } else { AudioSynth_LoadBuffer(cmd++, thing, unk7, buf); } - AudioSynth_SaveBuffer(cmd++, DMEM_TEMP + (aiBufLen * 2) - unk7, unk7, buf); - AudioSynth_Mix(cmd++, (aiBufLen * 2) >> 4, unkE, DMEM_SCRATCH2, thing); - AudioSynth_DMemMove(cmd++, thing, DMEM_TEMP, aiBufLen * 2); + AudioSynth_SaveBuffer(cmd++, DMEM_TEMP + (aiBufLen * SAMPLE_SIZE) - unk7, unk7, buf); + AudioSynth_Mix(cmd++, (aiBufLen * (s32)SAMPLE_SIZE) >> 4, unkE, DMEM_SCRATCH2, thing); + AudioSynth_DMemMove(cmd++, thing, DMEM_TEMP, aiBufLen * SAMPLE_SIZE); } else { synthState->unk_1A = 1; } @@ -1099,17 +1107,18 @@ Acmd* AudioSynth_ProcessNote(s32 noteIndex, NoteSubEu* noteSubEu, NoteSynthesisS if (!(flags & A_INIT)) { flags = A_CONTINUE; } - cmd = AudioSynth_NoteApplyHeadsetPanEffects(cmd, noteSubEu, synthState, aiBufLen * 2, flags, side); + cmd = + AudioSynth_NoteApplyHeadsetPanEffects(cmd, noteSubEu, synthState, aiBufLen * (s32)SAMPLE_SIZE, flags, side); } return cmd; } -Acmd* AudioSynth_FinalResample(Acmd* cmd, NoteSynthesisState* synthState, s32 count, u16 pitch, u16 inpDmem, +Acmd* AudioSynth_FinalResample(Acmd* cmd, NoteSynthesisState* synthState, s32 size, u16 pitch, u16 inpDmem, s32 resampleFlags) { if (pitch == 0) { - AudioSynth_ClearBuffer(cmd++, DMEM_TEMP, count); + AudioSynth_ClearBuffer(cmd++, DMEM_TEMP, size); } else { - aSetBuffer(cmd++, 0, inpDmem, DMEM_TEMP, count); + aSetBuffer(cmd++, 0, inpDmem, DMEM_TEMP, size); aResample(cmd++, resampleFlags, pitch, synthState->synthesisBuffers->finalResampleState); } return cmd; @@ -1163,7 +1172,7 @@ Acmd* AudioSynth_ProcessEnvelope(Acmd* cmd, NoteSubEu* noteSubEu, NoteSynthesisS synthState->curVolRight = curVolRight + (rampRight * (aiBufLen >> 3)); if (noteSubEu->bitField1.usesHeadsetPanEffects2) { - AudioSynth_ClearBuffer(cmd++, DMEM_NOTE_PAN_TEMP, DEFAULT_LEN_1CH); + AudioSynth_ClearBuffer(cmd++, DMEM_NOTE_PAN_TEMP, DMEM_1CH_SIZE); AudioSynth_EnvSetup1(cmd++, phi_t1 * 2, rampReverb, rampLeft, rampRight); AudioSynth_EnvSetup2(cmd++, curVolLeft, curVolRight); switch (headsetPanSettings) { @@ -1198,13 +1207,13 @@ Acmd* AudioSynth_LoadWaveSamples(Acmd* cmd, NoteSubEu* noteSubEu, NoteSynthesisS if (noteSubEu->bitField1.bookOffset != 0) { // Move the noise wave (that reads compiled assembly as samples) from ram to dmem - AudioSynth_LoadBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, ALIGN16(numSamplesToLoad * sizeof(s16)), gWaveSamples[8]); + AudioSynth_LoadBuffer(cmd++, DMEM_UNCOMPRESSED_NOTE, ALIGN16(numSamplesToLoad * SAMPLE_SIZE), gWaveSamples[8]); // Offset the address for the samples read by gWaveSamples[8] to the next set of samples - gWaveSamples[8] += numSamplesToLoad * sizeof(s16); + gWaveSamples[8] += numSamplesToLoad * SAMPLE_SIZE; return cmd; } else { // Move the synthetic wave from ram to dmem - aLoadBuffer(cmd++, noteSubEu->waveSampleAddr, DMEM_UNCOMPRESSED_NOTE, WAVE_SAMPLE_COUNT * sizeof(s16)); + aLoadBuffer(cmd++, noteSubEu->waveSampleAddr, DMEM_UNCOMPRESSED_NOTE, WAVE_SAMPLE_COUNT * SAMPLE_SIZE); // If the harmonic changes, map the offset in the wave from one harmonic to another for continuity if (harmonicIndexCurAndPrev != 0) { @@ -1222,10 +1231,10 @@ Acmd* AudioSynth_LoadWaveSamples(Acmd* cmd, NoteSubEu* noteSubEu, NoteSynthesisS // Duplicate (copy) the WAVE_SAMPLE_COUNT samples as many times as needed to reach numSamplesToLoad. // (numSamplesToLoad - numSamplesAvail) is the number of samples missing. // Divide by WAVE_SAMPLE_COUNT, rounding up, to get the amount of duplicates - numDuplicates = ((numSamplesToLoad - numSamplesAvail + (WAVE_SAMPLE_COUNT - 1)) / WAVE_SAMPLE_COUNT); + numDuplicates = ((numSamplesToLoad - numSamplesAvail + WAVE_SAMPLE_COUNT - 1) / WAVE_SAMPLE_COUNT); if (numDuplicates != 0) { aDuplicate(cmd++, numDuplicates, DMEM_UNCOMPRESSED_NOTE, - DMEM_UNCOMPRESSED_NOTE + (WAVE_SAMPLE_COUNT * sizeof(s16))); + DMEM_UNCOMPRESSED_NOTE + (WAVE_SAMPLE_COUNT * SAMPLE_SIZE)); } } synthState->samplePosInt = samplePosInt;