static int process_info(int size, SFInfo *sf, FILE *fd) { sf->infopos = ftell(fd); sf->infosize = size; /* parse the buffer */ while (size > 0) { SFChunk chunk; /* read a sub chunk */ READCHUNK(chunk, fd); size -= 8; if (feof(fd)) return -1; switch (chunkid(chunk.id)) { case IFIL_ID: /* soundfont file version */ READW(sf->version, fd); READW(sf->minorversion, fd); break; case INAM_ID: /* name of the font */ sf->sf_name = (char*)safe_malloc(chunk.size + 1); fread(sf->sf_name, 1, chunk.size, fd); sf->sf_name[chunk.size] = 0; break; default: FSKIP(chunk.size, fd); break; } size -= chunk.size; } return 0; }
static void load_sample_info(int size, SFInfo *sf, FILE *fd) { int i; int in_rom; /* the record size depends on the soundfont version */ if (sf->version > 1) { /* SF2 includes sample name and other infos */ sf->nsamples = size / 46; sf->sample = NEW(SFSampleInfo, sf->nsamples); } else { /* SBK; sample name may be read already */ int nsamples = size / 16; if (sf->sample == NULL) { sf->nsamples = nsamples; sf->sample = NEW(SFSampleInfo, sf->nsamples); } else if (sf->nsamples != nsamples) { #if 0 fprintf(stderr, "*** different # of infos ?? (%d : %d)\n", sf->nsamples, nsamples); FSKIP(size, fd); return; #endif /* overwrite it */ sf->nsamples = nsamples; } } in_rom = 1; /* data may start from ROM samples */ for (i = 0; i < sf->nsamples; i++) { if (sf->version > 1) /* SF2 only */ READSTR(sf->sample[i].name, fd); READDW(sf->sample[i].startsample, fd); READDW(sf->sample[i].endsample, fd); READDW(sf->sample[i].startloop, fd); READDW(sf->sample[i].endloop, fd); if (sf->version > 1) { /* SF2 only */ READDW(sf->sample[i].samplerate, fd); READB(sf->sample[i].originalPitch, fd); READB(sf->sample[i].pitchCorrection, fd); READW(sf->sample[i].samplelink, fd); READW(sf->sample[i].sampletype, fd); } else { /* for SBK; set missing infos */ sf->sample[i].samplerate = 44100; sf->sample[i].originalPitch = 60; sf->sample[i].pitchCorrection = 0; sf->sample[i].samplelink = 0; /* the first RAM data starts from address 0 */ if (sf->sample[i].startsample == 0) in_rom = 0; if (in_rom) sf->sample[i].sampletype = 0x8001; else sf->sample[i].sampletype = 1; } } }
void Instruments::load_sample_info(int size, SFInfo *sf, struct timidity_file *fd) { int i; int in_rom; /* the record size depends on the soundfont version */ if (sf->version > 1) { /* SF2 includes sample name and other infos */ sf->nsamples = size / 46; sf->sample = NEW(SFSampleInfo, sf->nsamples); } else { /* SBK; sample name may be read already */ int nsamples = size / 16; if (sf->sample == NULL) { sf->nsamples = nsamples; sf->sample = NEW(SFSampleInfo, sf->nsamples); } else if (sf->nsamples != nsamples) { /* overwrite it */ sf->nsamples = nsamples; } } in_rom = 1; /* data may start from ROM samples */ for (i = 0; i < sf->nsamples; i++) { if (sf->version > 1) /* SF2 only */ READSTR(sf->sample[i].name, fd); READDW((uint32_t *)&sf->sample[i].startsample, fd); READDW((uint32_t *)&sf->sample[i].endsample, fd); READDW((uint32_t *)&sf->sample[i].startloop, fd); READDW((uint32_t *)&sf->sample[i].endloop, fd); if (sf->version > 1) { /* SF2 only */ READDW((uint32_t *)&sf->sample[i].samplerate, fd); READB(sf->sample[i].originalPitch, fd); READB(sf->sample[i].pitchCorrection, fd); READW(&sf->sample[i].samplelink, fd); READW(&sf->sample[i].sampletype, fd); } else { /* for SBK; set missing infos */ sf->sample[i].samplerate = 44100; sf->sample[i].originalPitch = 60; sf->sample[i].pitchCorrection = 0; sf->sample[i].samplelink = 0; /* the first RAM data starts from address 0 */ if (sf->sample[i].startsample == 0) in_rom = 0; if (in_rom) sf->sample[i].sampletype = 0x8001; else sf->sample[i].sampletype = 1; } } }
static void load_gen(int size, SFBags *bagp, FILE *fd) { int i; size /= 4; bagp->gen = NEW(SFGenRec, size); for (i = 0; i < size; i++) { READW(bagp->gen[i].oper, fd); READW(bagp->gen[i].amount, fd); } bagp->ngens = size; }
void Instruments::load_gen(int size, SFBags *bagp, struct timidity_file *fd) { int i; size /= 4; bagp->gen = NEW(SFGenRec, size); for (i = 0; i < size; i++) { READW((uint16_t *)&bagp->gen[i].oper, fd); READW((uint16_t *)&bagp->gen[i].amount, fd); } bagp->ngens = size; }
static void load_preset_header(int size, SFInfo *sf, FILE *fd) { int i; sf->npresets = size / 38; sf->preset = NEW(SFPresetHdr, sf->npresets); for (i = 0; i < sf->npresets; i++) { READSTR(sf->preset[i].hdr.name, fd); READW(sf->preset[i].preset, fd); READW(sf->preset[i].bank, fd); READW(sf->preset[i].hdr.bagNdx, fd); SKIPDW(fd); /* lib; ignored*/ SKIPDW(fd); /* genre; ignored */ SKIPDW(fd); /* morph; ignored */ /* initialize layer table; it'll be parsed later */ sf->preset[i].hdr.nlayers = 0; sf->preset[i].hdr.layer = NULL; } }
int Instruments::process_info(int size, SFInfo *sf, struct timidity_file *fd) { sf->infopos = tf_tell(fd); sf->infosize = size; /* parse the buffer */ while (size > 0) { SFChunk chunk; /* read a sub chunk */ if (READCHUNK(&chunk, fd) <= 0) return -1; size -= 8; ctl_cmsg(CMSG_INFO, VERB_DEBUG, " %c%c%c%c:", chunk.id[0], chunk.id[1], chunk.id[2], chunk.id[3]); switch (chunkid(chunk.id)) { case IFIL_ID: /* soundfont file version */ READW(&sf->version, fd); READW(&sf->minorversion, fd); ctl_cmsg(CMSG_INFO, VERB_DEBUG, " version %d, minor %d", sf->version, sf->minorversion); break; case INAM_ID: /* name of the font */ sf->sf_name = (char*)safe_malloc(chunk.size + 1); tf_read(sf->sf_name, 1, chunk.size, fd); sf->sf_name[chunk.size] = 0; ctl_cmsg(CMSG_INFO, VERB_DEBUG, " name %s", sf->sf_name); break; default: FSKIP(chunk.size, fd); break; } size -= chunk.size; } return 0; }
static void load_bag(int size, SFBags *bagp, FILE *fd) { int i; size /= 4; bagp->bag = NEW(uint16, size); for (i = 0; i < size; i++) { READW(bagp->bag[i], fd); SKIPW(fd); /* mod; ignored */ } bagp->nbags = size; }
void Instruments::load_bag(int size, SFBags *bagp, struct timidity_file *fd) { int i; size /= 4; bagp->bag = NEW(uint16_t, size); for (i = 0; i < size; i++) { READW(&bagp->bag[i], fd); SKIPW(fd); /* mod; ignored */ } bagp->nbags = size; }
static void load_inst_header(int size, SFInfo *sf, FILE *fd) { int i; sf->ninsts = size / 22; sf->inst = NEW(SFInstHdr, sf->ninsts); for (i = 0; i < sf->ninsts; i++) { READSTR(sf->inst[i].hdr.name, fd); READW(sf->inst[i].hdr.bagNdx, fd); /* iniitialize layer table; it'll be parsed later */ sf->inst[i].hdr.nlayers = 0; sf->inst[i].hdr.layer = NULL; } }
void Instruments::load_inst_header(int size, SFInfo *sf, struct timidity_file *fd) { int i; sf->ninsts = size / 22; sf->inst = NEW(SFInstHdr, sf->ninsts); for (i = 0; i < sf->ninsts; i++) { READSTR(sf->inst[i].hdr.name, fd); READW(&sf->inst[i].hdr.bagNdx, fd); /* iniitialize layer table; it'll be parsed later */ sf->inst[i].hdr.nlayers = 0; sf->inst[i].hdr.layer = NULL; ctl_cmsg(CMSG_INFO, VERB_DEBUG, " InstHdr %d (%s) bagNdx=%d", i, sf->inst[i].hdr.name, sf->inst[i].hdr.bagNdx); } }
// gpio helper functions void configure_pad_mode(u32 gpio_num) { u32 padconf_address = 0; u32 padconf_val = 0; // error validation if (!is_valid_gpio_number(gpio_num)) { return; } // lookup padconf address and current padconf padconf_address = gpio_padconf_map[gpio_num]; padconf_val = READW(padconf_address); // only update to gpio non-input mode if necessary if (padconf_val != 0x0003) { WRITEW(0x0003, padconf_address); } }
int CRlePack::LoadFile( const char* a_pcFilename, int a_iNumColors ) { FILE* f; f = fopen( a_pcFilename, "rb" ); if (f==NULL) { debug( "Can't open file '%s'.\n", a_pcFilename ); return -1; } fseek( f, 0, SEEK_END ); long iFileSize = ftell ( f ); p->m_pData = malloc( iFileSize ); if ( NULL == p->m_pData ) { fclose( f ); return -1; } fseek( f, 0, SEEK_SET ); int iRead = fread( p->m_pData, 1, iFileSize, f ); fclose( f ); p->m_iColorCount = gamescreen->format->BitsPerPixel == 8 ? a_iNumColors : 256; if ( iFileSize != iRead ) { debug( "Warning CRlePack(): iFileSize=%d, iRead=%d\n", iFileSize, iRead ); } struct SHeader { char acDummy[8]; Uint32 iDatacount; } *poHeader = (SHeader*) p->m_pData; ChangeEndian32( poHeader->iDatacount ); debug( "File '%s' contains %d entries.\n", a_pcFilename, poHeader->iDatacount ); if (poHeader->iDatacount>MAXDATACOUNT) poHeader->iDatacount = MAXDATACOUNT; // Sanity p->m_iArraysize = poHeader->iDatacount; p->m_pSprites = new SRleSprite*[ poHeader->iDatacount ]; char* pcNext = ((char*)p->m_pData) + sizeof(SHeader); char* pcEnd = ((char*)p->m_pData) + iFileSize; while ( pcNext < pcEnd - 4 ) { if ( 0 == strncmp( pcNext, "prop", 4 ) ) { struct SProperty { char acName[4]; Uint32 iSize; } *poProperty = (SProperty*) (pcNext+4); ChangeEndian32( poProperty->iSize ); pcNext += 4 + sizeof(SProperty) + poProperty->iSize; } else if ( 0 == strncmp( pcNext, "RLE ", 4 ) ) { struct SRLE { SRleSprite oSprite; } *poRle = (SRLE*) (pcNext+10); poRle->oSprite.color_depth = ConvertEndian16(poRle->oSprite.color_depth); poRle->oSprite.w = ConvertEndian16(poRle->oSprite.w); poRle->oSprite.h = ConvertEndian16(poRle->oSprite.h); poRle->oSprite.size = ConvertEndian32(poRle->oSprite.size); p->m_pSprites[p->m_iCount] = &(poRle->oSprite); p->m_iCount++; pcNext += 10 + sizeof( SRLE ) + poRle->oSprite.size; } else if ( 0 == strncmp( pcNext, "PAL ", 4 ) ) { struct SPAL { Uint32 iLength1; Uint32 iLength; SDL_Color aoColors[256]; } *poPal = (SPAL*) (pcNext+4); ChangeEndian32( poPal->iLength ); int iNumColors = poPal->iLength>1024 ? 1024 : poPal->iLength; iNumColors /= 4; for (int i=0; i< iNumColors; i++) { p->m_aoPalette[i].r = poPal->aoColors[i].r*4; p->m_aoPalette[i].g = poPal->aoColors[i].g*4; p->m_aoPalette[i].b = poPal->aoColors[i].b*4; p->m_aoPalette[i].unused = 0; p->m_aoTintedPalette[i] = p->m_aoPalette[i]; } pcNext += 4 + 8 + poPal->iLength; } else { struct SUnknown { Uint32 iSize; } *poUnknown = (SUnknown*) (pcNext+4); ChangeEndian32( poUnknown->iSize ); debug( "Unknown: '%4s', size: %d\n", pcNext, poUnknown->iSize ); pcNext += 4 + sizeof(SUnknown) + poUnknown->iSize; } } return p->m_iCount; #if 0 // # // # Here's the old implementation or read, left here for reference only. // # int datacount; #define READDW(I) { \ unsigned char data[4]; \ fread( data, 4, 1, f ); \ (I) = (data[0]<<24) + (data[1]<<16) + (data[2]<<8) + data[3]; } #define READW(I) { \ unsigned char data[2]; \ fread( data, 2, 1, f ); \ (I) = (data[0]<<8) + data[1]; } #define READCH(S,C) { \ fread( S, C, 1, f ); S[C] = 0; } fseek( f, 8, SEEK_SET ); // Skip header READDW( datacount ); debug( "File '%s' contains %d entries.\n", filename, datacount ); if (datacount>500) datacount = 500; // Sanity p->arraysize = datacount; p->sprites = new SRleSprite*[ datacount ]; while( (!feof(f)) && (!ferror(f)) && (datacount>0) ) { char s[10]; READCH( s, 4 ); if ( !strcmp( s, "prop" )) // Found a property { fseek( f, 4, SEEK_CUR ); unsigned int propsize; READDW( propsize ); fseek( f, propsize, SEEK_CUR ); } else if (!strcmp( s, "RLE " )) // Found an SRleSprite { datacount--; unsigned int length, bpp, width, height, size; READDW( length ); READDW( length ); READW( bpp ); READW( width ); READW( height ); READDW( size ); SRleSprite* sprite = (SRleSprite*) malloc( sizeof(SRleSprite) + size ); p->sprites[ p->count ] = sprite; (p->count)++; sprite->w = width; sprite->h = height; sprite->color_depth = bpp; sprite->size = size; fread( sprite->dat, 1, size, f ); } else if (!strcmp( s, "PAL ")) // Found a palette { datacount--; unsigned int length, pallength; READDW( length ); READDW( length ); pallength = length>1024 ? 1024 : length; pallength /= 4; for (unsigned int i=0; i< pallength; i++) { char c[4]; fread( c, 4, 1, f ); p->palette[i].r = c[0]*4; p->palette[i].g = c[1]*4; p->palette[i].b = c[2]*4; p->palette[i].unused = 0; } fseek( f, length - pallength*4, SEEK_CUR ); } else // Found something else { debug( "Unknown: %s.", s ); datacount--; unsigned int length; READDW( length ); READDW( length ); fseek( f, length, SEEK_CUR ); } } fclose( f ); #endif }