2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * mtk.cpp - MPU-401 Trakker Loader by Simon Peter (dn.tlp@gmx.net)
25 /*** public methods **************************************/
27 CPlayer *CmtkLoader::factory(Copl *newopl)
29 return new CmtkLoader(newopl);
32 bool CmtkLoader::load(const std::string &filename, const CFileProvider &fp)
34 binistream *f = fp.open(filename); if(!f) return false;
37 unsigned short crc,size;
40 char songname[34],composername[34],instname[0x80][34];
41 unsigned char insts[0x80][12],order[0x80],dummy,patterns[0x32][0x40][9];
43 unsigned char *cmp,*org;
45 unsigned long cmpsize,cmpptr=0,orgptr=0;
46 unsigned short ctrlbits=0,ctrlmask=0,cmd,cnt,offs;
49 f->readString(header.id, 18);
50 header.crc = f->readInt(2);
51 header.size = f->readInt(2);
53 // file validation section
54 if(strncmp(header.id,"mpu401tr\x92kk\xeer@data",18))
55 { fp.close(f); return false; }
58 cmpsize = fp.filesize(f) - 22;
59 cmp = new unsigned char[cmpsize];
60 org = new unsigned char[header.size];
61 for(i = 0; i < cmpsize; i++) cmp[i] = f->readInt(1);
64 while(cmpptr < cmpsize) { // decompress
67 ctrlbits = cmp[cmpptr] + (cmp[cmpptr + 1] << 8);
71 if(!(ctrlbits & ctrlmask)) { // uncompressed data
72 if(orgptr >= header.size)
75 org[orgptr] = cmp[cmpptr];
81 cmd = (cmp[cmpptr] >> 4) & 0x0f;
82 cnt = cmp[cmpptr] & 0x0f;
86 if(orgptr + cnt > header.size) goto err;
88 memset(&org[orgptr],cmp[cmpptr],cnt);
89 cmpptr++; orgptr += cnt;
93 if(orgptr + cnt > header.size) goto err;
94 cnt += (cmp[cmpptr] << 4) + 19;
95 memset(&org[orgptr],cmp[++cmpptr],cnt);
96 cmpptr++; orgptr += cnt;
100 if(orgptr + cnt > header.size) goto err;
101 offs = (cnt+3) + (cmp[cmpptr] << 4);
102 cnt = cmp[++cmpptr] + 16; cmpptr++;
103 memcpy(&org[orgptr],&org[orgptr - offs],cnt);
108 if(orgptr + cmd > header.size) goto err;
109 offs = (cnt+3) + (cmp[cmpptr++] << 4);
110 memcpy(&org[orgptr],&org[orgptr-offs],cmd);
116 data = (struct mtkdata *) org;
118 // convert to HSC replay data
119 memset(title,0,34); strncpy(title,data->songname+1,33);
120 memset(composer,0,34); strncpy(composer,data->composername+1,33);
121 memset(instname,0,0x80*34);
123 strncpy(instname[i],data->instname[i]+1,33);
124 memcpy(instr,data->insts,0x80 * 12);
125 memcpy(song,data->order,0x80);
126 memcpy(patterns,data->patterns,header.size-6084);
127 for (i=0;i<128;i++) { // correct instruments
128 instr[i][2] ^= (instr[i][2] & 0x40) << 1;
129 instr[i][3] ^= (instr[i][3] & 0x40) << 1;
130 instr[i][11] >>= 4; // make unsigned