2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2007 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 * amd.cpp - AMD Loader by Simon Peter <dn.tlp@gmx.net>
27 CPlayer *CamdLoader::factory(Copl *newopl)
29 return new CamdLoader(newopl);
32 bool CamdLoader::load(const std::string &filename, const CFileProvider &fp)
34 binistream *f = fp.open(filename); if(!f) return false;
37 unsigned char version;
39 int i, j, k, t, numtrax, maxi = 0;
40 unsigned char buf, buf2, buf3;
41 const unsigned char convfx[10] = {0,1,2,9,17,11,13,18,3,14};
42 const unsigned char convvol[64] = {
43 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 9, 0xa, 0xa, 0xb,
44 0xc, 0xc, 0xd, 0xe, 0xe, 0xf, 0x10, 0x10, 0x11, 0x12, 0x13, 0x14, 0x14,
45 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21,
46 0x22, 0x23, 0x25, 0x26, 0x28, 0x29, 0x2b, 0x2d, 0x2e, 0x30, 0x32, 0x35,
47 0x37, 0x3a, 0x3c, 0x3f
50 // file validation section
51 if(fp.filesize(f) < 1072) { fp.close(f); return false; }
52 f->seek(1062); f->readString(header.id, 9);
53 header.version = f->readInt(1);
54 if(strncmp(header.id, "<o\xefQU\xeeRoR", 9) &&
55 strncmp(header.id, "MaDoKaN96", 9)) { fp.close(f); return false; }
58 memset(inst, 0, sizeof(inst));
60 f->readString(songname, sizeof(songname));
61 f->readString(author, sizeof(author));
62 for(i = 0; i < 26; i++) {
63 f->readString(instname[i], 23);
64 for(j = 0; j < 11; j++) inst[i].data[j] = f->readInt(1);
66 length = f->readInt(1); nop = f->readInt(1) + 1;
67 for(i=0;i<128;i++) order[i] = f->readInt(1);
68 f->seek(10, binio::Add);
69 if(header.version == 0x10) { // unpacked module
72 trackord[i/9][i%9] = i+1;
78 tracks[i][j].param2 = (buf&127) % 10;
79 tracks[i][j].param1 = (buf&127) / 10;
81 tracks[i][j].inst = buf >> 4;
82 tracks[i][j].command = buf & 0x0f;
84 if(buf >> 4) // fix bug in AMD save routine
85 tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
87 tracks[i][j].note = 0;
88 tracks[i][j].inst += (buf & 1) << 4;
92 } else { // packed module
95 trackord[i][j] = f->readInt(2) + 1;
96 numtrax = f->readInt(2);
97 for(k=0;k<numtrax;k++) {
99 if(i > 575) i = 575; // fix corrupted modules
100 maxi = (i + 1 > maxi ? i + 1 : maxi);
105 for(t = j; t < j + (buf & 127) && t < 64; t++) {
106 tracks[i][t].command = 0;
107 tracks[i][t].inst = 0;
108 tracks[i][t].note = 0;
109 tracks[i][t].param1 = 0;
110 tracks[i][t].param2 = 0;
115 tracks[i][j].param2 = buf % 10;
116 tracks[i][j].param1 = buf / 10;
118 tracks[i][j].inst = buf >> 4;
119 tracks[i][j].command = buf & 0x0f;
121 if(buf >> 4) // fix bug in AMD save routine
122 tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
124 tracks[i][j].note = 0;
125 tracks[i][j].inst += (buf & 1) << 4;
132 // convert to protracker replay data
133 bpm = 50; restartpos = 0; flags = Decimal;
134 for(i=0;i<26;i++) { // convert instruments
135 buf = inst[i].data[0];
136 buf2 = inst[i].data[1];
137 inst[i].data[0] = inst[i].data[10];
138 inst[i].data[1] = buf;
139 buf = inst[i].data[2];
140 inst[i].data[2] = inst[i].data[5];
141 buf3 = inst[i].data[3];
142 inst[i].data[3] = buf;
143 buf = inst[i].data[4];
144 inst[i].data[4] = inst[i].data[7];
145 inst[i].data[5] = buf3;
146 buf3 = inst[i].data[6];
147 inst[i].data[6] = inst[i].data[8];
148 inst[i].data[7] = buf;
149 inst[i].data[8] = inst[i].data[9];
150 inst[i].data[9] = buf2;
151 inst[i].data[10] = buf3;
152 for(j=0;j<23;j++) // convert names
153 if(instname[i][j] == '\xff')
154 instname[i][j] = '\x20';
156 for(i=0;i<maxi;i++) // convert patterns
158 tracks[i][j].command = convfx[tracks[i][j].command];
160 if(tracks[i][j].command == 14) {
161 if(tracks[i][j].param1 == 2) {
162 tracks[i][j].command = 10;
163 tracks[i][j].param1 = tracks[i][j].param2;
164 tracks[i][j].param2 = 0;
167 if(tracks[i][j].param1 == 3) {
168 tracks[i][j].command = 10;
169 tracks[i][j].param1 = 0;
174 if(tracks[i][j].command == 17) {
175 int vol = convvol[tracks[i][j].param1 * 10 + tracks[i][j].param2];
177 if(vol > 63) vol = 63;
178 tracks[i][j].param1 = vol / 10;
179 tracks[i][j].param2 = vol % 10;
187 float CamdLoader::getrefresh()
190 return (float) (tempo);