]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug/src/amd.cpp
wwww~
[16.git] / 16 / adplug / adplug / src / amd.cpp
1 /*
2  * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3  * Copyright (C) 1999 - 2007 Simon Peter, <dn.tlp@gmx.net>, et al.
4  * 
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.
9  * 
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.
14  * 
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
18  *
19  * amd.cpp - AMD Loader by Simon Peter <dn.tlp@gmx.net>
20  */
21
22 #include <string.h>
23
24 #include "amd.h"
25 #include "debug.h"
26
27 CPlayer *CamdLoader::factory(Copl *newopl)
28 {
29   return new CamdLoader(newopl);
30 }
31
32 bool CamdLoader::load(const std::string &filename, const CFileProvider &fp)
33 {
34   binistream *f = fp.open(filename); if(!f) return false;
35   struct {
36     char id[9];
37     unsigned char version;
38   } header;
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
48   };
49
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; }
56
57   // load section
58   memset(inst, 0, sizeof(inst));
59   f->seek(0);
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);
65   }
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
70     maxi = nop * 9;
71     for(i=0;i<64*9;i++)
72       trackord[i/9][i%9] = i+1;
73     t = 0;
74     while(!f->ateof()) {
75       for(j=0;j<64;j++)
76         for(i=t;i<t+9;i++) {
77           buf = f->readInt(1);
78           tracks[i][j].param2 = (buf&127) % 10;
79           tracks[i][j].param1 = (buf&127) / 10;
80           buf = f->readInt(1);
81           tracks[i][j].inst = buf >> 4;
82           tracks[i][j].command = buf & 0x0f;
83           buf = f->readInt(1);
84           if(buf >> 4)  // fix bug in AMD save routine
85             tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
86           else
87             tracks[i][j].note = 0;
88           tracks[i][j].inst += (buf & 1) << 4;
89         }
90       t += 9;
91     }
92   } else {                      // packed module
93     for(i=0;i<nop;i++)
94       for(j=0;j<9;j++)
95         trackord[i][j] = f->readInt(2) + 1;
96     numtrax = f->readInt(2);
97     for(k=0;k<numtrax;k++) {
98       i = f->readInt(2);
99       if(i > 575) i = 575;      // fix corrupted modules
100       maxi = (i + 1 > maxi ? i + 1 : maxi);
101       j = 0;
102       do {
103         buf = f->readInt(1);
104         if(buf & 128) {
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;
111           }
112           j += buf & 127;
113           continue;
114         }
115         tracks[i][j].param2 = buf % 10;
116         tracks[i][j].param1 = buf / 10;
117         buf = f->readInt(1);
118         tracks[i][j].inst = buf >> 4;
119         tracks[i][j].command = buf & 0x0f;
120         buf = f->readInt(1);
121         if(buf >> 4)    // fix bug in AMD save routine
122           tracks[i][j].note = ((buf & 14) >> 1) * 12 + (buf >> 4);
123         else
124           tracks[i][j].note = 0;
125         tracks[i][j].inst += (buf & 1) << 4;
126         j++;
127       } while(j<64);
128     }
129   }
130   fp.close(f);
131
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';
155   }
156   for(i=0;i<maxi;i++)   // convert patterns
157     for(j=0;j<64;j++) {
158       tracks[i][j].command = convfx[tracks[i][j].command];
159       // extended 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;
165         }
166
167         if(tracks[i][j].param1 == 3) {
168           tracks[i][j].command = 10;
169           tracks[i][j].param1 = 0;
170         }
171       }
172
173       // fix volume
174       if(tracks[i][j].command == 17) {
175         int vol = convvol[tracks[i][j].param1 * 10 + tracks[i][j].param2];
176
177         if(vol > 63) vol = 63;
178         tracks[i][j].param1 = vol / 10;
179         tracks[i][j].param2 = vol % 10;
180       }
181     }
182
183   rewind(0);
184   return true;
185 }
186
187 float CamdLoader::getrefresh()
188 {
189   if(tempo)
190     return (float) (tempo);
191   else
192     return 18.2f;
193 }