ref: af91213d67a744cf532548803c4c150eef5cb7c1
parent: 15897be81442299876efd399c7b14f8110e65879
author: Olav Sørensen <[email protected]>
date: Mon Apr 27 16:52:12 EDT 2020
Emergency crashfix in v1.22 (when saving XMs) And some more silly divison->bitshift stuff that I should not really do anymore.
--- a/src/ft2_module_saver.c
+++ b/src/ft2_module_saver.c
@@ -19,7 +19,8 @@
static SDL_Thread *thread;
-static uint16_t packPatt(uint8_t *pattPtr, uint16_t numRows);
+static uint8_t packedPattData[65536];
+static uint16_t packPatt(uint8_t *writePtr, uint8_t *pattPtr, uint16_t numRows);
static const char modSig[32][5] =
{
@@ -29,14 +30,9 @@
"25CH", "26CH", "27CH", "28CH", "29CH", "30CH", "31CH", "32CH"
};
-// ft2_replayer.c
-extern const uint16_t amigaPeriod[12*8];
-
bool saveXM(UNICHAR *filenameU)
{
- uint8_t *pattPtr;
int16_t ap, ai, i, j, k, a;
- uint16_t b, c;
size_t result;
songHeaderTyp h;
patternHeaderTyp ph;
@@ -123,19 +119,11 @@
}
else
{
- c = packPatt((uint8_t *)patt[i], pattLens[i]);
- b = pattLens[i] * TRACK_WIDTH;
- ph.dataLen = c;
+ ph.dataLen = packPatt(packedPattData, (uint8_t *)patt[i], pattLens[i]);
result = fwrite(&ph, ph.patternHeaderSize, 1, f);
- result += fwrite(patt[i], ph.dataLen, 1, f);
+ result += fwrite(packedPattData, ph.dataLen, 1, f);
- pattPtr = (uint8_t *)patt[i];
-
- memcpy(&pattPtr[b-c], patt[i], c);
- unpackPatt(pattPtr, b - c, pattLens[i], song.antChn);
- clearUnusedChannels(patt[i], pattLens[i], song.antChn);
-
if (result != 2) // write was not OK
{
fclose(f);
@@ -399,15 +387,15 @@
{
smp = &instr[i]->samp[0];
- l1 = smp->len / 2;
- l2 = smp->repS / 2;
- l3 = smp->repL / 2;
+ l1 = smp->len >> 1;
+ l2 = smp->repS >> 1;
+ l3 = smp->repL >> 1;
if (smp->typ & 16)
{
- l1 /= 2;
- l2 /= 2;
- l3 /= 2;
+ l1 >>= 1;
+ l2 >>= 1;
+ l3 >>= 1;
}
if (l1 > 32767)
@@ -543,7 +531,7 @@
restoreSample(smp);
- l1 = smp->len / 2;
+ l1 = smp->len >> 1;
if (smp->typ & 16) // 16-bit sample (convert to 8-bit)
{
if (l1 > 65534)
@@ -580,8 +568,9 @@
if (l1 > 32767)
l1 = 32767;
- l1 *= 2;
+ l1 <<= 1;
+
if (fwrite(smp->pek, 1, l1, f) != (size_t)l1)
{
fixSample(smp);
@@ -640,9 +629,9 @@
SDL_DetachThread(thread);
}
-static uint16_t packPatt(uint8_t *pattPtr, uint16_t numRows)
+static uint16_t packPatt(uint8_t *writePtr, uint8_t *pattPtr, uint16_t numRows)
{
- uint8_t bytes[5], packBits, *writePtr, *firstBytePtr;
+ uint8_t bytes[5], packBits, *firstBytePtr;
uint16_t totalPackLen;
totalPackLen = 0;
@@ -650,7 +639,6 @@
if (pattPtr == NULL)
return 0;
- writePtr = pattPtr;
for (uint16_t row = 0; row < numRows; row++)
{
for (uint16_t chn = 0; chn < song.antChn; chn++)
@@ -692,7 +680,7 @@
totalPackLen += (uint16_t)(writePtr - firstBytePtr); // bytes writen
}
- // skip unused channels
+ // skip unused channels (unpacked patterns always have 32 channels)
pattPtr += sizeof (tonTyp) * (MAX_VOICES - song.antChn);
}