From 8b3f318f60d8ad945c7b5c316bc5de1f20bb4c8a Mon Sep 17 00:00:00 2001 From: Andy Sloane Date: Fri, 20 Nov 2015 17:28:39 -0800 Subject: [PATCH] [#19] handle loops of length 0 also more robust way to find sample offsets to load in the file --- xm.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/xm.js b/xm.js index b6dcfeb..dbc89bc 100644 --- a/xm.js +++ b/xm.js @@ -787,8 +787,8 @@ function load(arrayBuf) { // FIXME: ignoring keymaps for now and assuming 1 sample / instrument // var keymap = getarray(dv, idx+0x21); var samphdrsiz = dv.getUint32(idx+0x1d, true); - console.log("hdrsiz %d; instrument %d: '%s' %d samples, samphdrsiz %d", - hdrsiz, i, instname, nsamp, samphdrsiz); + console.log("hdrsiz %d; instrument %s: '%s' %d samples, samphdrsiz %d", + hdrsiz, (i+1).toString(16), instname, nsamp, samphdrsiz); idx += hdrsiz; var totalsamples = 0; var samps = []; @@ -802,7 +802,10 @@ function load(arrayBuf) { var samppan = dv.getUint8(idx+15); var sampnote = dv.getInt8(idx+16); var sampname = getstring(dv, idx+18, 22); - var sampleoffset = idx + nsamp * samphdrsiz + totalsamples; + var sampleoffset = totalsamples; + if (samplooplen === 0) { + samptype &= ~3; + } console.log("sample %d: len %d name '%s' loop %d/%d vol %d offset %s", j, samplen, sampname, samploop, samplooplen, sampvol, sampleoffset.toString(16)); console.log(" type %d note %s(%d) finetune %d pan %d", @@ -816,24 +819,26 @@ function load(arrayBuf) { 'len': samplen, 'loop': samploop, 'looplen': samplooplen, 'note': sampnote, 'fine': sampfinetune, 'pan': samppan, 'type': samptype, 'vol': sampvol, - 'sampledata': ConvertSample( - new Uint8Array(arrayBuf, sampleoffset, samplen), samptype & 16), + 'fileoffset': sampleoffset }; // length / pointers are all specified in bytes; fixup for 16-bit samples - if (samptype & 16) { + samps.push(samp); + idx += samphdrsiz; + totalsamples += samplen; + } + for (j = 0; j < nsamp; j++) { + var samp = samps[j]; + samp.sampledata = ConvertSample( + new Uint8Array(arrayBuf, idx + samp.fileoffset, samp.len), samp.type & 16); + if (samp.type & 16) { samp.len /= 2; samp.loop /= 2; samp.looplen /= 2; } - // unroll short loops and any pingpong loops if ((samp.type & 3) && (samp.looplen < 2048 || (samp.type & 2))) { UnrollSampleLoop(samp); } - samps.push(samp); - - idx += samphdrsiz; - totalsamples += samplen; } idx += totalsamples; inst.samplemap = samplemap;