# HG changeset patch # User Matti Hamalainen # Date 1425734593 -7200 # Node ID 377c8a603d21717f657fb3fa1e5f0badd8ee92ba # Parent e44a2b7abe0734402128ddef0f573e98b5d19214 Cleanups in JSSMOD writing. diff -r e44a2b7abe07 -r 377c8a603d21 tools/xm2jss.c --- a/tools/xm2jss.c Sat Mar 07 14:37:24 2015 +0200 +++ b/tools/xm2jss.c Sat Mar 07 15:23:13 2015 +0200 @@ -443,58 +443,77 @@ /* Save a JSSMOD file */ -int jssSaveJSSMOD(FILE *outFile, JSSModule *m, int patMode, int flags8, int flags16) +int jssSaveJSSMOD(FILE *outFile, JSSModule *module, int patMode, int flags8, int flags16) { JSSMODHeader jssH; int i, pattern, order, instr, totalSize; - const size_t patBufSize = 64*1024; // 64kB pattern buffer + const size_t patBufSize = 256*1024; // 256kB pattern buffer Uint8 *patBuf; // Check the module - if (m == NULL) - JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, "Module pointer was NULL\n"); + if (module == NULL) + JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, + "Module pointer was NULL\n"); - if (m->nchannels < 1 || m->npatterns < 1 || m->norders < 1 || - m->nchannels > jsetMaxChannels || - m->npatterns > jsetMaxPatterns || - m->norders > jsetMaxOrders) + if (module->nchannels < 1 || module->npatterns < 1 || module->norders < 1 || + module->nchannels > jsetMaxChannels || + module->npatterns > jsetMaxPatterns || + module->norders > jsetMaxOrders) JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n", - m->nchannels, m->npatterns, m->norders); + module->nchannels, module->npatterns, module->norders); // Create the JSSMOD header jssH.idMagic[0] = 'J'; jssH.idMagic[1] = 'M'; jssH.idVersion = JSSMOD_VERSION; - jssH.defFlags = m->defFlags; - jssH.intVersion = m->intVersion; - jssH.norders = m->norders; - jssH.npatterns = m->npatterns; - jssH.nextInstruments = m->nextInstruments; - jssH.ninstruments = m->ninstruments; - jssH.defRestartPos = m->defRestartPos; + jssH.defFlags = module->defFlags; + jssH.intVersion = module->intVersion; + jssH.norders = module->norders; + jssH.npatterns = module->npatterns; + jssH.nextInstruments = module->nextInstruments; + jssH.ninstruments = module->ninstruments; + jssH.defRestartPos = module->defRestartPos; - jssH.nchannels = m->nchannels; - jssH.defSpeed = m->defSpeed; - jssH.defTempo = m->defTempo; + jssH.nchannels = module->nchannels; + jssH.defSpeed = module->defSpeed; + jssH.defTempo = module->defTempo; jssH.patMode = patMode; // Write header + if (!dm_fwrite_str(outFile, jssH.idMagic, sizeof(jssH.idMagic)) || + !dm_fwrite_byte(outFile, jssH.idVersion) || + + !dm_fwrite_le16(outFile, jssH.defFlags) || + !dm_fwrite_le16(outFile, jssH.intVersion) || + !dm_fwrite_le16(outFile, jssH.norders) || + !dm_fwrite_le16(outFile, jssH.npatterns) || + !dm_fwrite_le16(outFile, jssH.nextInstruments) || + !dm_fwrite_le16(outFile, jssH.ninstruments) || + !dm_fwrite_le16(outFile, jssH.defRestartPos) || + + !dm_fwrite_byte(outFile, jssH.nchannels) || + !dm_fwrite_byte(outFile, jssH.defSpeed) || + !dm_fwrite_byte(outFile, jssH.defTempo) || + !dm_fwrite_byte(outFile, jssH.patMode)) + JSSERROR(DMERR_FWRITE, DMERR_FWRITE, + "Could not write JSSMOD header!\n"); + totalSize = sizeof(jssH); - if (fwrite(&jssH, sizeof(jssH), 1, outFile) != 1) - JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD header!\n"); - dmMsg(1," * JSSMOD-header 0x%04x, %d bytes.\n", JSSMOD_VERSION, totalSize); // Write orders list - for (totalSize = order = 0; order < m->norders; order++) + for (totalSize = order = 0; order < module->norders; order++) { - int tmp = m->orderList[order]; - if (tmp == jsetNotSet || tmp > m->npatterns) + int tmp = module->orderList[order]; + if (tmp != jsetNotSet && tmp > module->npatterns) JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, "Orderlist entry #%d has invalid value %d.\n", order, tmp); + if (tmp == jsetNotSet) + tmp = 0xffff; + if (!dm_fwrite_le16(outFile, tmp)) JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD orders list entry #%d (%d).\n", @@ -504,7 +523,7 @@ } dmMsg(1," * %d item orders list, %d bytes.\n", - m->norders, totalSize); + module->norders, totalSize); // Allocate pattern compression buffer if ((patBuf = dmMalloc(patBufSize)) == NULL) @@ -512,10 +531,10 @@ "Error allocating memory for pattern compression buffer.\n"); // Convert and write patterns - for (totalSize = pattern = 0; pattern < m->npatterns; pattern++) - if (m->patterns[pattern] != NULL) + for (totalSize = pattern = 0; pattern < module->npatterns; pattern++) + if (module->patterns[pattern] != NULL) { - JSSPattern *pat = m->patterns[pattern]; + JSSPattern *pat = module->patterns[pattern]; JSSMODPattern patHead; size_t finalSize = 0; @@ -577,16 +596,17 @@ "Pattern #%d was NULL.\n", pattern); dmFree(patBuf); - dmMsg(1," * %d patterns, %d bytes.\n", m->npatterns, totalSize); + dmMsg(1," * %d patterns, %d bytes.\n", module->npatterns, totalSize); // Write extended instruments - for (totalSize = instr = 0; instr < m->nextInstruments; instr++) - if (m->extInstruments[instr] != NULL) + for (totalSize = instr = 0; instr < module->nextInstruments; instr++) + if (module->extInstruments[instr] != NULL) { - JSSExtInstrument *einst = m->extInstruments[instr]; + JSSExtInstrument *einst = module->extInstruments[instr]; JSSMODExtInstrument jssE; dmMemset(&jssE, 0, sizeof(jssE)); + // Create header jssE.nsamples = einst->nsamples; for (i = 0; i < jsetNNotes; i++) @@ -609,35 +629,31 @@ "Could not write JSSMOD extended instrument #%d to file!\n", instr); } else JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Extended instrument #%d NULL!\n", instr); - dmMsg(1," * %d Extended Instruments, %d bytes.\n", m->nextInstruments, totalSize); + dmMsg(1," * %d Extended Instruments, %d bytes.\n", module->nextInstruments, totalSize); // Write sample instrument headers - for (totalSize = instr = 0; instr < m->ninstruments; instr++) + for (totalSize = instr = 0; instr < module->ninstruments; instr++) + if (module->instruments[instr] != NULL) { + JSSInstrument *inst = module->instruments[instr]; JSSMODInstrument jssI; - JSSInstrument *inst = m->instruments[instr]; dmMemset(&jssI, 0, sizeof(jssI)); // Create header - if (inst != NULL) - { - jssI.size = inst->size; - jssI.loopS = inst->loopS; - jssI.loopE = inst->loopE; - jssI.volume = inst->volume; - jssI.flags = inst->flags; - jssI.C4BaseSpeed = inst->C4BaseSpeed; - jssI.ERelNote = inst->ERelNote; - jssI.EFineTune = inst->EFineTune; - jssI.EPanning = inst->EPanning; - jssI.convFlags = (inst->flags & jsf16bit) ? flags16 : flags8; - if (inst->data != NULL) - jssI.convFlags |= jsampHasData; - } - else - JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Instrument #%d NULL!\n", instr); + jssI.size = inst->size; + jssI.loopS = inst->loopS; + jssI.loopE = inst->loopE; + jssI.volume = inst->volume; + jssI.flags = inst->flags; + jssI.C4BaseSpeed = inst->C4BaseSpeed; + jssI.ERelNote = inst->ERelNote; + jssI.EFineTune = inst->EFineTune; + jssI.EPanning = inst->EPanning; + jssI.convFlags = (inst->flags & jsf16bit) ? flags16 : flags8; + if (inst->data != NULL) + jssI.convFlags |= jsampHasData; // Write to file totalSize += sizeof(jssI); @@ -645,34 +661,38 @@ JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD instrument #%d to file!\n", instr); } - dmMsg(1," * %d Instrument headers, %d bytes.\n", m->ninstruments, totalSize); + else + JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Instrument #%d NULL!\n", instr); + + dmMsg(1," * %d Instrument headers, %d bytes.\n", + module->ninstruments, totalSize); // Write sample data - for (totalSize = instr = 0; instr < m->ninstruments; instr++) - if (m->instruments[instr]) + for (totalSize = instr = 0; instr < module->ninstruments; instr++) { - JSSInstrument *inst = m->instruments[instr]; - if (inst->data != NULL) + JSSInstrument *inst = module->instruments[instr]; + if (inst != NULL && inst->data != NULL) { - size_t res; + size_t bsize = inst->size; if (inst->flags & jsf16bit) { + bsize *= sizeof(Uint16); jssEncodeSample16(inst->data, inst->size, flags16); - res = fwrite(inst->data, sizeof(Uint16), inst->size, outFile); } else { + bsize *= sizeof(Uint8); jssEncodeSample8(inst->data, inst->size, flags8); - res = fwrite(inst->data, sizeof(Uint8), inst->size, outFile); } - totalSize += inst->size; - if (res != (size_t) inst->size) + if (fwrite(inst->data, sizeof(Uint16), bsize, outFile) != bsize) JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD sample #%d to file!\n", instr); + + totalSize += bsize; } } - dmMsg(1," * %d samples, %d bytes.\n", m->ninstruments, totalSize); + dmMsg(1," * %d samples, %d bytes.\n", module->ninstruments, totalSize); return DMERR_OK; }