Removed some more DEBUG_PRINT statements and fixed padding.

This commit is contained in:
MeltyPlayer
2022-12-06 17:07:14 -06:00
parent 4b98663b59
commit 5faeaff908
4 changed files with 202 additions and 214 deletions
+175 -187
View File
@@ -1,7 +1,6 @@
#pragma once
#include "convUtils.h"
#include "../../debug_print.h"
#include "utils.h"
#include <stdlib.h>
#include <stdio.h>
@@ -22,13 +21,11 @@
unsigned char* gCtlSeqs;
struct seqFile* parse_seqfile(unsigned char* seq){ /* Read SeqFile data */
DEBUG_PRINT("parse_seqfile()");
short revision = read_u16_be(seq);
short bankCount = read_u16_be(seq + 2);
unsigned int size = sizeof(struct seqFile) + (bankCount-1) * sizeof(struct seqObject);
unsigned int size = sizeof(struct seqFile) + (bankCount-1) * sizeof(struct seqObject);
struct seqFile* seqFile = (struct seqFile*)calloc(size, 1);
seqFile->revision = revision;
seqFile->seqCount = bankCount;
@@ -37,21 +34,15 @@ struct seqFile* parse_seqfile(unsigned char* seq){ /* Read SeqFile data */
seqFile->seqArray[i].len = read_u32_be((seq + 4 + i * 8 + 4));
}
DEBUG_PRINT("- revision: %d", revision);
if (revision == TYPE_CTL){
DEBUG_PRINT("- ctl!");
// CTL file, contains instrument and drum data, this is really the only one that needs to be parsed, the rest only needs a header change
gCtlSeqs = (unsigned char*)calloc(0x20B40, 1); // We only really need 0x20AD0 bytes but still
uintptr_t pos = (uintptr_t)gCtlSeqs;
DEBUG_PRINT("- bank count: %d", bankCount);
for (int i = 0; i < bankCount; i++){
DEBUG_PRINT("- ctl bank index: %d", i);
uintptr_t start = pos;
gCtlSeqs = (unsigned char*)calloc(0x20B40, 1); // We only really need 0x20AD0 bytes but still
uintptr_t pos = (uintptr_t)gCtlSeqs;
for (int i = 0; i < bankCount; i++){
uintptr_t start = pos;
struct CTL* ptr = parse_ctl_data(seq+(seqFile->seqArray[i].offset), &pos);
DEBUG_PRINT("- read ptr: %x", ptr);
seqFile->seqArray[i].offset = ptr;
DEBUG_PRINT("- stored offset: %x", seqFile->seqArray[i].offset);
seqFile->seqArray[i].len = (unsigned int)(pos - start);
seqFile->seqArray[i].len = (unsigned int)(pos - start);
}
}else if (revision == TYPE_TBL){
// TBL file, contains raw audio data
@@ -69,32 +60,32 @@ struct seqFile* parse_seqfile(unsigned char* seq){ /* Read SeqFile data */
}
void ctl_free(){
free(gCtlSeqs);
free(gCtlSeqs);
}
void snd_ptrs_to_offsets(struct CSound* snd, uintptr_t ctlData){
struct CSample* smp = snd->sample_addr;
if((uintptr_t)(smp->loop) > ctlData)
smp->loop = (struct CLoop*)((uintptr_t)(smp->loop) - ctlData);
if((uintptr_t)(smp->book) > ctlData)
smp->book = (struct CBook*)((uintptr_t)(smp->book) - ctlData);
snd->sample_addr = (struct CSample*)((uintptr_t)(snd->sample_addr) - ctlData);
struct CSample* smp = snd->sample_addr;
if((uintptr_t)(smp->loop) > ctlData)
smp->loop = (struct CLoop*)((uintptr_t)(smp->loop) - ctlData);
if((uintptr_t)(smp->book) > ctlData)
smp->book = (struct CBook*)((uintptr_t)(smp->book) - ctlData);
snd->sample_addr = (struct CSample*)((uintptr_t)(snd->sample_addr) - ctlData);
}
void ptrs_to_offsets(struct seqFile* ctl){
if (ctl->revision != TYPE_CTL){
if (ctl->revision != TYPE_CTL){
return;
}
for (int i = 0; i < ctl->seqCount; i++){
for (int i = 0; i < ctl->seqCount; i++){
struct CTL* ptr = (struct CTL*)ctl->seqArray[i].offset;
uintptr_t ctlData = (uintptr_t)ptr + 0x10;
uintptr_t ctlData = (uintptr_t)ptr + 0x10;
// find all samples in the CTL file
for (int j = 0; j < ptr->numInstruments; j++){
struct CInstrument* inst = ptr->instrument_pointers[j];
if (inst==0x0)
continue; // null instrument.
inst->env_addr = (struct CEnvelope*)((uintptr_t)inst->env_addr - ctlData);
inst->env_addr = (struct CEnvelope*)((uintptr_t)inst->env_addr - ctlData);
if (inst->sound_hi.sample_addr!=0x0){
snd_ptrs_to_offsets(&(inst->sound_hi), ctlData);
}
@@ -104,38 +95,38 @@ void ptrs_to_offsets(struct seqFile* ctl){
if (inst->sound_lo.sample_addr!=0x0){
snd_ptrs_to_offsets(&(inst->sound_lo), ctlData);
}
ptr->instrument_pointers[j] = (struct CInstrument*)((uintptr_t)(inst) - ctlData);
ptr->instrument_pointers[j] = (struct CInstrument*)((uintptr_t)(inst) - ctlData);
}
if(ptr->numDrums != 0){
for (int j = 0; j < ptr->numDrums; j++){
struct CDrum* drum = ptr->drum_pointers[j];
if (drum==0x0)
continue; // null drum.
drum->env_addr = (struct CEnvelope*)((uintptr_t)drum->env_addr - ctlData);
if (drum->snd.sample_addr!=0x0){
snd_ptrs_to_offsets(&(drum->snd), ctlData);
}
ptr->drum_pointers[j] = (struct CDrum*)((uintptr_t)(drum) - ctlData);
}
ptr->drum_pointers = (struct CDrum**)((uintptr_t)(ptr->drum_pointers) - ctlData);
}
if(ptr->numDrums != 0){
for (int j = 0; j < ptr->numDrums; j++){
struct CDrum* drum = ptr->drum_pointers[j];
if (drum==0x0)
continue; // null drum.
drum->env_addr = (struct CEnvelope*)((uintptr_t)drum->env_addr - ctlData);
if (drum->snd.sample_addr!=0x0){
snd_ptrs_to_offsets(&(drum->snd), ctlData);
}
ptr->drum_pointers[j] = (struct CDrum*)((uintptr_t)(drum) - ctlData);
}
ptr->drum_pointers = (struct CDrum**)((uintptr_t)(ptr->drum_pointers) - ctlData);
}
}
}
struct CLoop* parse_loop(unsigned char* loop, uintptr_t* pos){
uint32_t count = read_u32_be(loop + 8); // variable is signed, but the data is being read as unsigned.
unsigned int size = sizeof(struct CLoop) - 4;
if(count != 0){
size = sizeof(struct CLoop) - 4 + sizeof(short) * 16;
}
struct CLoop* loop_ptr = (struct CLoop*)(*pos);
*pos += size;
*pos = ALIGN16(*pos);
uint32_t count = read_u32_be(loop + 8); // variable is signed, but the data is being read as unsigned.
unsigned int size = sizeof(struct CLoop) - 4;
if(count != 0){
size = sizeof(struct CLoop) - 4 + sizeof(short) * 16;
}
struct CLoop* loop_ptr = (struct CLoop*)(*pos);
*pos += size;
*pos = ALIGN16(*pos);
loop_ptr->start = read_u32_be(loop);
loop_ptr->end = read_u32_be(loop + 4);
loop_ptr->count = count;
loop_ptr->pad = read_u32_be(loop + 12);
loop_ptr->pad = read_u32_be(loop + 12);
if (loop_ptr->count!=0){
for (int i = 0;i<16;i++){
@@ -147,9 +138,9 @@ struct CLoop* parse_loop(unsigned char* loop, uintptr_t* pos){
}
struct CBook* parse_book(unsigned char* book, uintptr_t* pos){
struct CBook* book_ptr = (struct CBook*)(*pos);
*pos += sizeof(struct CBook);
*pos = ALIGN16(*pos);
struct CBook* book_ptr = (struct CBook*)(*pos);
*pos += sizeof(struct CBook);
*pos = ALIGN16(*pos);
book_ptr->order = read_u32_be(book);
book_ptr->npredictors = read_u32_be(book + 4); // both are signed
unsigned char* table_data = book+8;
@@ -160,41 +151,41 @@ struct CBook* parse_book(unsigned char* book, uintptr_t* pos){
}
struct CSample* parse_sample(unsigned char* sample,unsigned char* ctl, uintptr_t* pos){
struct CSample* samp = (struct CSample*)(*pos);
*pos += sizeof(struct CSample);
*pos = ALIGN16(*pos);
struct CSample* samp = (struct CSample*)(*pos);
*pos += sizeof(struct CSample);
*pos = ALIGN16(*pos);
samp->zero=read_u32_be(sample);
samp->addr=read_u32_be(sample+4);
samp->loop=read_u32_be(sample+8);// loop address
samp->book=read_u32_be(sample+12);// book address
samp->sample_size=read_u32_be(sample+16);
samp->book=parse_book(ctl+((uintptr_t)samp->book), pos);
samp->book=parse_book(ctl+((uintptr_t)samp->book), pos);
samp->loop=parse_loop(ctl+((uintptr_t)samp->loop), pos);
return samp;
}
struct CSound* parse_sound(unsigned char* sound,unsigned char* ctl, uintptr_t* pos, uintptr_t sndPos, struct SampleList* samples){
struct CSound* snd = (struct CSound*)(sndPos);
struct CSound* snd = (struct CSound*)(sndPos);
snd->sample_addr=read_u32_be(sound);
snd->tuning = (float)read_f32_be(sound+4);
// if sample_addr is 0 then the sound is null
if (snd->sample_addr!=0){
int smpIndex = -1;
for(int i = 0; i < samples->count; i++){
if(samples->orig_addrs[i] == (uintptr_t)(snd->sample_addr)){
smpIndex = i;
break;
}
}
if(smpIndex < 0){
samples->orig_addrs[samples->count] = (uintptr_t)(snd->sample_addr);
snd->sample_addr = parse_sample(ctl+((uintptr_t)snd->sample_addr),ctl, pos);
samples->addrs[samples->count] = snd->sample_addr;
samples->count++;
} else {
snd->sample_addr = samples->addrs[smpIndex];
}
int smpIndex = -1;
for(int i = 0; i < samples->count; i++){
if(samples->orig_addrs[i] == (uintptr_t)(snd->sample_addr)){
smpIndex = i;
break;
}
}
if(smpIndex < 0){
samples->orig_addrs[samples->count] = (uintptr_t)(snd->sample_addr);
snd->sample_addr = parse_sample(ctl+((uintptr_t)snd->sample_addr),ctl, pos);
samples->addrs[samples->count] = snd->sample_addr;
samples->count++;
} else {
snd->sample_addr = samples->addrs[smpIndex];
}
}
return snd;
}
@@ -211,17 +202,17 @@ struct CDrum* parse_drum(unsigned char* drum,unsigned char* ctl, uintptr_t* pos,
}
struct CEnvelope* parse_envelope(unsigned char* env, uintptr_t* pos, int* size){
int count = 0;
while(1){
unsigned short delay = read_u16_le(env + count * 4);
int count = 0;
while(1){
unsigned short delay = read_u16_le(env + count * 4);
unsigned short arg = read_u16_le(env + count * 4 + 2);
unsigned short delayC = (-delay);
count++;
unsigned short delayC = (-delay);
count++;
if ((1 <= delayC && delayC <= 3) || delay == 0)
break;
}
*size = sizeof(struct CEnvelope) + sizeof(struct delay_arg) * (count-1);
struct CEnvelope* envData = malloc(*size);
}
*size = sizeof(struct CEnvelope) + sizeof(struct delay_arg) * (count-1);
struct CEnvelope* envData = malloc(*size);
for (int i = 0; i < count; i++){
envData->delay_args[i].delay = read_u16_le(env + i * 4);
envData->delay_args[i].arg = read_u16_le(env + i * 4 + 2);
@@ -239,7 +230,7 @@ struct CInstrument* parse_instrument(unsigned char* instrument,unsigned char* ct
inst->sound_lo=*parse_sound(instrument+8,ctl, pos, &(inst->sound_lo), samples);
inst->sound_med=*parse_sound(instrument+16,ctl, pos, &(inst->sound_med), samples);
inst->sound_hi=*parse_sound(instrument+24,ctl, pos, &(inst->sound_hi), samples);
return inst;
}
@@ -249,63 +240,60 @@ struct TBL* parse_tbl_data(unsigned char* tbl){
return tblData;
}
struct SEQ* parse_seq_data(unsigned char* seq){
struct SEQ* parse_seq_data(unsigned char* seq){
struct SEQ* seqData = malloc(sizeof(struct SEQ));
seqData->data = seq;
return seqData;
}
struct CTL* parse_ctl_data(unsigned char* ctlData, uintptr_t* pos){
DEBUG_PRINT("parse_ctl_data()");
DEBUG_PRINT("- ctlData: %x", ctlData);
DEBUG_PRINT("- pos: %x", pos);
int instruments=read_u32_be(ctlData);
unsigned int size = sizeof(struct CTL) + sizeof(struct CInstrument*) * (instruments-1);
struct CTL* ctl = (struct CTL*)(*pos);
*pos += size;
*pos = ALIGN16(*pos);
int instruments=read_u32_be(ctlData);
unsigned int size = sizeof(struct CTL) + sizeof(struct CInstrument*) * (instruments-1);
struct CTL* ctl = (struct CTL*)(*pos);
*pos += size;
*pos = ALIGN16(*pos);
#pragma region Parse CTL header
ctl->numInstruments = read_u32_be(ctlData);
ctl->numDrums = read_u32_be(ctlData + 4);
ctl->shared = read_u32_be(ctlData + 8);
ctl->iso_date = read_u32_be(ctlData + 12);
#pragma endregion
struct SampleList samples = {0};
struct EnvelopeMeta envData[128] = {0};
int envCount = 0;
samples.count = 0;
struct SampleList samples = {0};
struct EnvelopeMeta envData[128] = {0};
int envCount = 0;
samples.count = 0;
// header parsed, now read data
if(ctl->numDrums != 0) {
ctl->drum_pointers= (struct CDrum**)(*pos);
size = sizeof(struct CDrum*) * ctl->numDrums;
*pos += size;
*pos = ALIGN16(*pos);
int drumTablePtr = read_u32_be(ctlData + 16);
for (int i = 0; i < ctl->numDrums; i++){
uint32_t data = read_u32_be(ctlData + drumTablePtr+16 + i * 4);
struct CDrum* d = parse_drum(ctlData+data+16,ctlData+16, pos, &samples);
bool used = 0;
for(int j = 0; j < envCount; j++){
if(envData[j].orig == (uintptr_t)d->env_addr){
used = 1;
break;
}
}
if(used == 0){
int size = 0;
unsigned char* addr = ctlData+((uintptr_t)d->env_addr)+16;
envData[envCount].orig = (uintptr_t)(d->env_addr);
envData[envCount].addr = parse_envelope(addr, pos, &size);
envData[envCount].size = size;
envCount++;
}
ctl->drum_pointers[i] = d;
}
*pos = ALIGN16(*pos);
} else {
ctl->drum_pointers= NULL;
}
if(ctl->numDrums != 0) {
ctl->drum_pointers= (struct CDrum**)(*pos);
size = sizeof(struct CDrum*) * ctl->numDrums;
*pos += size;
*pos = ALIGN16(*pos);
int drumTablePtr = read_u32_be(ctlData + 16);
for (int i = 0; i < ctl->numDrums; i++){
uint32_t data = read_u32_be(ctlData + drumTablePtr+16 + i * 4);
struct CDrum* d = parse_drum(ctlData+data+16,ctlData+16, pos, &samples);
bool used = 0;
for(int j = 0; j < envCount; j++){
if(envData[j].orig == (uintptr_t)d->env_addr){
used = 1;
break;
}
}
if(used == 0){
int size = 0;
unsigned char* addr = ctlData+((uintptr_t)d->env_addr)+16;
envData[envCount].orig = (uintptr_t)(d->env_addr);
envData[envCount].addr = parse_envelope(addr, pos, &size);
envData[envCount].size = size;
envCount++;
}
ctl->drum_pointers[i] = d;
}
*pos = ALIGN16(*pos);
} else {
ctl->drum_pointers= NULL;
}
// parse instrument data
int instTablePtr = 4;
for (int i = 0; i < ctl->numInstruments; i++){
@@ -313,73 +301,73 @@ struct CTL* parse_ctl_data(unsigned char* ctlData, uintptr_t* pos){
if (data == 0)
continue;
struct CInstrument* inst = parse_instrument(ctlData+16+data,ctlData+16, pos, &samples);
bool used = 0;
for(int j = 0; j < envCount; j++){
if(envData[j].orig == (uintptr_t)inst->env_addr){
used = 1;
break;
}
}
if(used == 0){
int size = 0;
unsigned char* addr = ctlData+((uintptr_t)inst->env_addr)+16;
envData[envCount].orig = (uintptr_t)(inst->env_addr);
envData[envCount].addr = parse_envelope(addr, pos, &size);
envData[envCount].size = size;
envCount++;
}
bool used = 0;
for(int j = 0; j < envCount; j++){
if(envData[j].orig == (uintptr_t)inst->env_addr){
used = 1;
break;
}
}
if(used == 0){
int size = 0;
unsigned char* addr = ctlData+((uintptr_t)inst->env_addr)+16;
envData[envCount].orig = (uintptr_t)(inst->env_addr);
envData[envCount].addr = parse_envelope(addr, pos, &size);
envData[envCount].size = size;
envCount++;
}
ctl->instrument_pointers[i] = inst;
}
*pos = ALIGN16(*pos);
// Copy envelopes to ctl
for (int i = 0; i < envCount; i++){
struct CEnvelope* env = envData[i].addr;
*pos = ALIGN16(*pos);
// Copy envelopes to ctl
for (int i = 0; i < envCount; i++){
struct CEnvelope* env = envData[i].addr;
memcpy((uint8_t*)(*pos), env, envData[i].size);
for (int j = 0; j < ctl->numInstruments; j++){
struct CInstrument* inst = ctl->instrument_pointers[j];
if (inst == 0x0)
continue;
if((uintptr_t)(inst->env_addr) == envData[i].orig){
inst->env_addr = (struct CEnvelope*)(*pos);
}
}
for (int j = 0; j < ctl->numDrums; j++){
struct CDrum* drum = ctl->drum_pointers[j];
if (drum == 0x0)
continue;
if((uintptr_t)(drum->env_addr) == envData[i].orig){
drum->env_addr = (struct CEnvelope*)(*pos);
}
}
free(env);
*pos += envData[i].size;
for (int j = 0; j < ctl->numInstruments; j++){
struct CInstrument* inst = ctl->instrument_pointers[j];
if (inst == 0x0)
continue;
if((uintptr_t)(inst->env_addr) == envData[i].orig){
inst->env_addr = (struct CEnvelope*)(*pos);
}
}
for (int j = 0; j < ctl->numDrums; j++){
struct CDrum* drum = ctl->drum_pointers[j];
if (drum == 0x0)
continue;
if((uintptr_t)(drum->env_addr) == envData[i].orig){
drum->env_addr = (struct CEnvelope*)(*pos);
}
}
free(env);
*pos += envData[i].size;
}
*pos = ALIGN16(*pos);
// Copy instruments to ctl
for (int i = 0; i < ctl->numInstruments; i++){
struct CInstrument* inst = ctl->instrument_pointers[i];
if (inst == 0x0)
continue;
*pos = ALIGN16(*pos);
// Copy instruments to ctl
for (int i = 0; i < ctl->numInstruments; i++){
struct CInstrument* inst = ctl->instrument_pointers[i];
if (inst == 0x0)
continue;
memcpy((uint8_t*)(*pos), inst, sizeof(struct CInstrument));
free(inst);
ctl->instrument_pointers[i] = (struct CInstrument*)(*pos);
*pos += sizeof(struct CInstrument);
free(inst);
ctl->instrument_pointers[i] = (struct CInstrument*)(*pos);
*pos += sizeof(struct CInstrument);
}
*pos = ALIGN16(*pos);
// Copy drums to ctl
for (int i = 0; i < ctl->numDrums; i++){
struct CDrum* drum = ctl->drum_pointers[i];
if (drum == 0x0)
continue;
*pos = ALIGN16(*pos);
// Copy drums to ctl
for (int i = 0; i < ctl->numDrums; i++){
struct CDrum* drum = ctl->drum_pointers[i];
if (drum == 0x0)
continue;
memcpy((uint8_t*)(*pos), drum, sizeof(struct CDrum));
free(drum);
ctl->drum_pointers[i] = (struct CDrum*)(*pos);
*pos += sizeof(struct CDrum);
free(drum);
ctl->drum_pointers[i] = (struct CDrum*)(*pos);
*pos += sizeof(struct CDrum);
}
*pos = ALIGN16(*pos);
*pos = ALIGN16(*pos);
//
return ctl;
+5 -5
View File
@@ -144,15 +144,15 @@ SM64_LIB_FN void sm64_audio_init( uint8_t *rom ) {
#define SAMPLES_LOW 528
extern SM64_LIB_FN uint32_t sm64_audio_tick( uint32_t numQueuedSamples, uint32_t numDesiredSamples, int16_t *audio_buffer ) {
update_game_sound();
update_game_sound();
u32 num_audio_samples = numQueuedSamples < numDesiredSamples ? SAMPLES_HIGH : SAMPLES_LOW;
for (int i = 0; i < 2; i++)
{
create_next_audio_buffer(audio_buffer + i * (2 * num_audio_samples), num_audio_samples);
{
create_next_audio_buffer( audio_buffer + i * ( 2 * num_audio_samples ), num_audio_samples );
}
return num_audio_samples;
return num_audio_samples;
}
SM64_LIB_FN void sm64_static_surfaces_load( const struct SM64Surface *surfaceArray, uint32_t numSurfaces )
+14 -14
View File
@@ -5,19 +5,19 @@
#include "decomp/audio/load_dat.h"
extern void load_audio_banks( uint8_t *rom ) {
uint8_t *rom2 = malloc(0x800000);
uint8_t *rom2 = malloc( 0x800000 );
memcpy(rom2, rom, 0x800000);
rom = rom2;
gSoundDataADSR = parse_seqfile(rom+0x57B720); //ctl
gSoundDataRaw = parse_seqfile(rom+0x593560); //tbl
gMusicData = parse_seqfile(rom+0x7B0860);
gBankSetsData = rom+0x7CC621;
memmove(gBankSetsData+0x45,gBankSetsData+0x45-1,0x5B);
gBankSetsData[0x45]=0x00;
ptrs_to_offsets(gSoundDataADSR);
audio_init();
sound_init();
sound_reset(0);
memcpy( rom2, rom, 0x800000 );
rom = rom2;
gSoundDataADSR = parse_seqfile( rom+0x57B720 ); //ctl
gSoundDataRaw = parse_seqfile( rom+0x593560 ); //tbl
gMusicData = parse_seqfile( rom+0x7B0860 );
gBankSetsData = rom+0x7CC621;
memmove( gBankSetsData+0x45,gBankSetsData+0x45-1,0x5B );
gBankSetsData[0x45]=0x00;
ptrs_to_offsets( gSoundDataADSR );
audio_init();
sound_init();
sound_reset( 0 );
}
+8 -8
View File
@@ -6,12 +6,12 @@
SM64PlaySoundFunctionPtr g_play_sound_func = NULL;
extern void play_sound( uint32_t soundBits, f32 *pos ) {
DEBUG_PRINT("$ play_sound(%d) request %d; pos %f %f %f\n", soundBits,sSoundRequestCount,pos[0],pos[1],pos[2]);
sSoundRequests[sSoundRequestCount].soundBits = soundBits;
sSoundRequests[sSoundRequestCount].position = pos;
sSoundRequestCount++;
if ( g_play_sound_func ) {
g_play_sound_func(soundBits, pos);
}
DEBUG_PRINT("$ play_sound(%d) request %d; pos %f %f %f\n", soundBits,sSoundRequestCount,pos[0],pos[1],pos[2]);
sSoundRequests[sSoundRequestCount].soundBits = soundBits;
sSoundRequests[sSoundRequestCount].position = pos;
sSoundRequestCount++;
if ( g_play_sound_func ) {
g_play_sound_func(soundBits, pos);
}
}