diff src/modplug/load_mod.cxx @ 2337:107c1fed3d92

Port Schism modplug core.
author "Tony Vroon <chainsaw@gentoo.org>"
date Thu, 24 Jan 2008 12:05:59 +0000
parents 6907fc39b53f
children
line wrap: on
line diff
--- a/src/modplug/load_mod.cxx	Wed Jan 23 19:37:05 2008 +0100
+++ b/src/modplug/load_mod.cxx	Thu Jan 24 12:05:59 2008 +0000
@@ -110,7 +110,7 @@
 	case CMD_PATTERNBREAK:		command = 0x0D; param = ((param / 10) << 4) | (param % 10); break;
 	case CMD_MODCMDEX:			command = 0x0E; break;
 	case CMD_SPEED:				command = 0x0F; if (param > 0x20) param = 0x20; break;
-	case CMD_TEMPO:				if (param > 0x20) { command = 0x0F; break; }
+	case CMD_TEMPO:				if (param > 0x20) { command = 0x0F; break; } return 0;
 	case CMD_GLOBALVOLUME:		command = 'G' - 55; break;
 	case CMD_GLOBALVOLSLIDE:	command = 'H' - 55; break;
 	case CMD_KEYOFF:			command = 'K' - 55; break;
@@ -192,7 +192,7 @@
 	 || (IsMagic(s, "M&K!")) || (IsMagic(s, "N.T."))) m_nChannels = 4; else
 	if ((IsMagic(s, "CD81")) || (IsMagic(s, "OKTA"))) m_nChannels = 8; else
 	if ((s[0]=='F') && (s[1]=='L') && (s[2]=='T') && (s[3]>='4') && (s[3]<='9')) m_nChannels = s[3] - '0'; else
-	if ((s[0]>='2') && (s[0]<='9') && (s[1]=='C') && (s[2]=='H') && (s[3]=='N')) m_nChannels = s[0] - '0'; else
+	if ((s[0]>='4') && (s[0]<='9') && (s[1]=='C') && (s[2]=='H') && (s[3]=='N')) m_nChannels = s[0] - '0'; else
 	if ((s[0]=='1') && (s[1]>='0') && (s[1]<='9') && (s[2]=='C') && (s[3]=='H')) m_nChannels = s[1] - '0' + 10; else
 	if ((s[0]=='2') && (s[1]>='0') && (s[1]<='9') && (s[2]=='C') && (s[3]=='H')) m_nChannels = s[1] - '0' + 20; else
 	if ((s[0]=='3') && (s[1]>='0') && (s[1]<='2') && (s[2]=='C') && (s[3]=='H')) m_nChannels = s[1] - '0' + 30; else
@@ -228,19 +228,13 @@
 		}
 		psmp->nLoopStart = loopstart;
 		psmp->nLoopEnd = loopstart + looplen;
-		if (psmp->nLength < 4) psmp->nLength = 0;
+		if (psmp->nLength < 2) psmp->nLength = 0;
 		if (psmp->nLength)
 		{
 			UINT derr = 0;
 			if (psmp->nLoopStart >= psmp->nLength) { psmp->nLoopStart = psmp->nLength-1; derr|=1; }
 			if (psmp->nLoopEnd > psmp->nLength) { psmp->nLoopEnd = psmp->nLength; derr |= 1; }
 			if (psmp->nLoopStart > psmp->nLoopEnd) derr |= 1;
-			if ((psmp->nLoopStart > psmp->nLoopEnd) || (psmp->nLoopEnd <= 8)
-			 || (psmp->nLoopEnd - psmp->nLoopStart <= 4))
-			{
-				psmp->nLoopStart = 0;
-				psmp->nLoopEnd = 0;
-			}
 			if (psmp->nLoopEnd > psmp->nLoopStart)
 			{
 				psmp->uFlags |= CHN_LOOP;
@@ -306,11 +300,10 @@
 	for (UINT ich=0; ich<m_nChannels; ich++)
 	{
 		ChnSettings[ich].nVolume = 64;
-		if (gdwSoundSetup & SNDMIX_MAXDEFAULTPAN)
-			ChnSettings[ich].nPan = (((ich&3)==1) || ((ich&3)==2)) ? 256 : 0;
-		else
-			ChnSettings[ich].nPan = (((ich&3)==1) || ((ich&3)==2)) ? 0xC0 : 0x40;
+		ChnSettings[ich].nPan = (((ich&3)==1) || ((ich&3)==2)) ? 256 : 0;
 	}
+	m_nStereoSeparation = (gdwSoundSetup & SNDMIX_MAXDEFAULTPAN) ? 128 : 64;
+	
 	// Reading channels
 	for (UINT ipat=0; ipat<nbp; ipat++)
 	{
@@ -318,6 +311,7 @@
 		{
 			if ((Patterns[ipat] = AllocatePattern(64, m_nChannels)) == NULL) break;
 			PatternSize[ipat] = 64;
+			PatternAllocSize[ipat] = 64;
 			if (dwMemPos + m_nChannels*256 >= dwMemLength) break;
 			MODCOMMAND *m = Patterns[ipat];
 			LPCBYTE p = lpStream + dwMemPos;
@@ -362,137 +356,3 @@
 }
 
 
-#ifndef MODPLUG_NO_FILESAVE
-#pragma warning(disable:4100)
-
-BOOL CSoundFile::SaveMod(LPCSTR lpszFileName, UINT nPacking)
-//----------------------------------------------------------
-{
-	BYTE insmap[32];
-	UINT inslen[32];
-	BYTE bTab[32];
-	BYTE ord[128];
-	FILE *f;
-
-	if ((!m_nChannels) || (!lpszFileName)) return FALSE;
-	if ((f = fopen(lpszFileName, "wb")) == NULL) return FALSE;
-	memset(ord, 0, sizeof(ord));
-	memset(inslen, 0, sizeof(inslen));
-	if (m_nInstruments)
-	{
-		memset(insmap, 0, sizeof(insmap));
-		for (UINT i=1; i<32; i++) if (Headers[i])
-		{
-			for (UINT j=0; j<128; j++) if (Headers[i]->Keyboard[j])
-			{
-				insmap[i] = Headers[i]->Keyboard[j];
-				break;
-			}
-		}
-	} else
-	{
-		for (UINT i=0; i<32; i++) insmap[i] = (BYTE)i;
-	}
-	// Writing song name
-	fwrite(m_szNames, 20, 1, f);
-	// Writing instrument definition
-	for (UINT iins=1; iins<=31; iins++)
-	{
-		MODINSTRUMENT *pins = &Ins[insmap[iins]];
-		memcpy(bTab, m_szNames[iins],22);
-		inslen[iins] = pins->nLength;
-		if (inslen[iins] > 0x1fff0) inslen[iins] = 0x1fff0;
-		bTab[22] = inslen[iins] >> 9;
-		bTab[23] = inslen[iins] >> 1;
-		if (pins->RelativeTone < 0) bTab[24] = 0x08; else
-		if (pins->RelativeTone > 0) bTab[24] = 0x07; else
-		bTab[24] = (BYTE)XM2MODFineTune(pins->nFineTune);
-		bTab[25] = pins->nVolume >> 2;
-		bTab[26] = pins->nLoopStart >> 9;
-		bTab[27] = pins->nLoopStart >> 1;
-		bTab[28] = (pins->nLoopEnd - pins->nLoopStart) >> 9;
-		bTab[29] = (pins->nLoopEnd - pins->nLoopStart) >> 1;
-		fwrite(bTab, 30, 1, f);
-	}
-	// Writing number of patterns
-	UINT nbp=0, norders=128;
-	for (UINT iord=0; iord<128; iord++)
-	{
-		if (Order[iord] == 0xFF)
-		{
-			norders = iord;
-			break;
-		}
-		if ((Order[iord] < 0x80) && (nbp<=Order[iord])) nbp = Order[iord]+1;
-	}
-	bTab[0] = norders;
-	bTab[1] = m_nRestartPos;
-	fwrite(bTab, 2, 1, f);
-	// Writing pattern list
-	if (norders) memcpy(ord, Order, norders);
-	fwrite(ord, 128, 1, f);
-	// Writing signature
-	if (m_nChannels == 4)
-		lstrcpy((LPSTR)&bTab, "M.K.");
-	else
-		wsprintf((LPSTR)&bTab, "%luCHN", m_nChannels);
-	fwrite(bTab, 4, 1, f);
-	// Writing patterns
-	for (UINT ipat=0; ipat<nbp; ipat++) if (Patterns[ipat])
-	{
-		BYTE s[64*4];
-		MODCOMMAND *m = Patterns[ipat];
-		for (UINT i=0; i<64; i++) if (i < PatternSize[ipat])
-		{
-			LPBYTE p=s;
-			for (UINT c=0; c<m_nChannels; c++,p+=4,m++)
-			{
-				UINT param = ModSaveCommand(m, FALSE);
-				UINT command = param >> 8;
-				param &= 0xFF;
-				if (command > 0x0F) command = param = 0;
-				if ((m->vol >= 0x10) && (m->vol <= 0x50) && (!command) && (!param)) { command = 0x0C; param = m->vol - 0x10; }
-				UINT period = m->note;
-				if (period)
-				{
-					if (period < 37) period = 37;
-					period -= 37;
-					if (period >= 6*12) period = 6*12-1;
-					period = ProTrackerPeriodTable[period];
-				}
-				UINT instr = (m->instr > 31) ? 0 : m->instr;
-				p[0] = ((period >> 8) & 0x0F) | (instr & 0x10);
-				p[1] = period & 0xFF;
-				p[2] = ((instr & 0x0F) << 4) | (command & 0x0F);
-				p[3] = param;
-			}
-			fwrite(s, m_nChannels, 4, f);
-		} else
-		{
-			memset(s, 0, m_nChannels*4);
-			fwrite(s, m_nChannels, 4, f);
-		}
-	}
-	// Writing instruments
-	for (UINT ismpd=1; ismpd<=31; ismpd++) if (inslen[ismpd])
-	{
-		MODINSTRUMENT *pins = &Ins[insmap[ismpd]];
-		UINT flags = RS_PCM8S;
-#ifndef NO_PACKING
-		if (!(pins->uFlags & (CHN_16BIT|CHN_STEREO)))
-		{
-			if ((nPacking) && (CanPackSample((char *)pins->pSample, inslen[ismpd], nPacking)))
-			{
-				fwrite("ADPCM", 1, 5, f);
-				flags = RS_ADPCM4;
-			}
-		}
-#endif
-		WriteSample(f, pins, flags, inslen[ismpd]);
-	}
-	fclose(f);
-	return TRUE;
-}
-
-#pragma warning(default:4100)
-#endif // MODPLUG_NO_FILESAVE