|
359
|
1 /*
|
|
|
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
|
|
|
3 * Copyright (C) 1999 - 2003 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 * sa2.cpp - SAdT2 Loader by Simon Peter <dn.tlp@gmx.net>
|
|
|
20 * SAdT Loader by Mamiya <mamiya@users.sourceforge.net>
|
|
|
21 */
|
|
|
22
|
|
|
23 #include <stdio.h>
|
|
|
24
|
|
|
25 #include "sa2.h"
|
|
|
26 #include "debug.h"
|
|
|
27
|
|
|
28 CPlayer *Csa2Loader::factory(Copl *newopl)
|
|
|
29 {
|
|
|
30 return new Csa2Loader(newopl);
|
|
|
31 }
|
|
|
32
|
|
|
33 bool Csa2Loader::load(const std::string &filename, const CFileProvider &fp)
|
|
|
34 {
|
|
|
35 binistream *f = fp.open(filename); if(!f) return false;
|
|
|
36 struct {
|
|
|
37 unsigned char data[11],arpstart,arpspeed,arppos,arpspdcnt;
|
|
|
38 } insts;
|
|
|
39 unsigned char buf;
|
|
|
40 int i,j, k, notedis = 0;
|
|
|
41 const unsigned char convfx[16] = {0,1,2,3,4,5,6,255,8,255,10,11,12,13,255,15};
|
|
|
42 unsigned char sat_type;
|
|
|
43 enum SAT_TYPE {
|
|
|
44 HAS_ARPEGIOLIST = (1 << 7),
|
|
|
45 HAS_V7PATTERNS = (1 << 6),
|
|
|
46 HAS_ACTIVECHANNELS = (1 << 5),
|
|
|
47 HAS_TRACKORDER = (1 << 4),
|
|
|
48 HAS_ARPEGIO = (1 << 3),
|
|
|
49 HAS_OLDBPM = (1 << 2),
|
|
|
50 HAS_OLDPATTERNS = (1 << 1),
|
|
|
51 HAS_UNKNOWN127 = (1 << 0)
|
|
|
52 };
|
|
|
53
|
|
|
54 // read header
|
|
|
55 f->readString(header.sadt, 4);
|
|
|
56 header.version = f->readInt(1);
|
|
|
57
|
|
|
58 // file validation section
|
|
|
59 if(strncmp(header.sadt,"SAdT",4)) { fp.close(f); return false; }
|
|
|
60 switch(header.version) {
|
|
|
61 case 1:
|
|
|
62 notedis = +0x18;
|
|
|
63 sat_type = HAS_UNKNOWN127 | HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
64 break;
|
|
|
65 case 2:
|
|
|
66 notedis = +0x18;
|
|
|
67 sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
68 break;
|
|
|
69 case 3:
|
|
|
70 notedis = +0x0c;
|
|
|
71 sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
72 break;
|
|
|
73 case 4:
|
|
|
74 notedis = +0x0c;
|
|
|
75 sat_type = HAS_ARPEGIO | HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
76 break;
|
|
|
77 case 5:
|
|
|
78 notedis = +0x0c;
|
|
|
79 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
80 break;
|
|
|
81 case 6:
|
|
|
82 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
|
|
|
83 break;
|
|
|
84 case 7:
|
|
|
85 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_V7PATTERNS;
|
|
|
86 break;
|
|
|
87 case 8:
|
|
|
88 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER;
|
|
|
89 break;
|
|
|
90 case 9:
|
|
|
91 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER | HAS_ACTIVECHANNELS;
|
|
|
92 break;
|
|
|
93 default: /* unknown */
|
|
|
94 fp.close(f);
|
|
|
95 return false;
|
|
|
96 }
|
|
|
97
|
|
|
98 // load section
|
|
|
99 // instruments
|
|
|
100 for(i = 0; i < 31; i++) {
|
|
|
101 if(sat_type & HAS_ARPEGIO) {
|
|
|
102 for(j = 0; j < 11; j++) insts.data[j] = f->readInt(1);
|
|
|
103 insts.arpstart = f->readInt(1);
|
|
|
104 insts.arpspeed = f->readInt(1);
|
|
|
105 insts.arppos = f->readInt(1);
|
|
|
106 insts.arpspdcnt = f->readInt(1);
|
|
|
107 inst[i].arpstart = insts.arpstart;
|
|
|
108 inst[i].arpspeed = insts.arpspeed;
|
|
|
109 inst[i].arppos = insts.arppos;
|
|
|
110 inst[i].arpspdcnt = insts.arpspdcnt;
|
|
|
111 } else {
|
|
|
112 for(j = 0; j < 11; j++) insts.data[j] = f->readInt(1);
|
|
|
113 inst[i].arpstart = 0;
|
|
|
114 inst[i].arpspeed = 0;
|
|
|
115 inst[i].arppos = 0;
|
|
|
116 inst[i].arpspdcnt = 0;
|
|
|
117 }
|
|
|
118 for(j=0;j<11;j++)
|
|
|
119 inst[i].data[j] = insts.data[j];
|
|
|
120 inst[i].misc = 0;
|
|
|
121 inst[i].slide = 0;
|
|
|
122 }
|
|
|
123
|
|
|
124 // instrument names
|
|
|
125 for(i = 0; i < 29; i++) f->readString(instname[i], 17);
|
|
|
126
|
|
|
127 f->ignore(3); // dummy bytes
|
|
|
128 for(i = 0; i < 128; i++) order[i] = f->readInt(1); // pattern orders
|
|
|
129 if(sat_type & HAS_UNKNOWN127) f->ignore(127);
|
|
|
130
|
|
|
131 // infos
|
|
|
132 nop = f->readInt(2); length = f->readInt(1); restartpos = f->readInt(1);
|
|
|
133
|
|
|
134 // bpm
|
|
|
135 bpm = f->readInt(2);
|
|
|
136 if(sat_type & HAS_OLDBPM) {
|
|
|
137 bpm = bpm * 125 / 50; // cps -> bpm
|
|
|
138 }
|
|
|
139
|
|
|
140 if(sat_type & HAS_ARPEGIOLIST) {
|
|
|
141 init_specialarp();
|
|
|
142 for(i = 0; i < 256; i++) arplist[i] = f->readInt(1); // arpeggio list
|
|
|
143 for(i = 0; i < 256; i++) arpcmd[i] = f->readInt(1); // arpeggio commands
|
|
|
144 }
|
|
|
145
|
|
|
146 for(i=0;i<64;i++) { // track orders
|
|
|
147 for(j=0;j<9;j++) {
|
|
|
148 if(sat_type & HAS_TRACKORDER)
|
|
|
149 trackord[i][j] = f->readInt(1);
|
|
|
150 else
|
|
|
151 {
|
|
|
152 trackord[i][j] = i * 9 + j;
|
|
|
153 }
|
|
|
154 }
|
|
|
155 }
|
|
|
156
|
|
|
157 if(sat_type & HAS_ACTIVECHANNELS)
|
|
|
158 activechan = f->readInt(2); // active channels
|
|
|
159 else
|
|
|
160 activechan = 0xffff;
|
|
|
161
|
|
|
162 AdPlug_LogWrite("Csa2Loader::load(\"%s\"): sat_type = %x, nop = %d, "
|
|
|
163 "length = %d, restartpos = %d, activechan = %x, bpm = %d\n",
|
|
|
164 filename.c_str(), sat_type, nop, length, restartpos, activechan, bpm);
|
|
|
165
|
|
|
166 // track data
|
|
|
167 if(sat_type & HAS_OLDPATTERNS) {
|
|
|
168 i = 0;
|
|
|
169 while(!f->ateof()) {
|
|
|
170 for(j=0;j<64;j++) {
|
|
|
171 for(k=0;k<9;k++) {
|
|
|
172 buf = f->readInt(1);
|
|
|
173 tracks[i+k][j].note = buf ? (buf + notedis) : 0;
|
|
|
174 tracks[i+k][j].inst = f->readInt(1);
|
|
|
175 tracks[i+k][j].command = convfx[f->readInt(1) & 0xf];
|
|
|
176 tracks[i+k][j].param1 = f->readInt(1);
|
|
|
177 tracks[i+k][j].param2 = f->readInt(1);
|
|
|
178 }
|
|
|
179 }
|
|
|
180 i+=9;
|
|
|
181 }
|
|
|
182 } else
|
|
|
183 if(sat_type & HAS_V7PATTERNS) {
|
|
|
184 i = 0;
|
|
|
185 while(!f->ateof()) {
|
|
|
186 for(j=0;j<64;j++) {
|
|
|
187 for(k=0;k<9;k++) {
|
|
|
188 buf = f->readInt(1);
|
|
|
189 tracks[i+k][j].note = buf >> 1;
|
|
|
190 tracks[i+k][j].inst = (buf & 1) << 4;
|
|
|
191 buf = f->readInt(1);
|
|
|
192 tracks[i+k][j].inst += buf >> 4;
|
|
|
193 tracks[i+k][j].command = convfx[buf & 0x0f];
|
|
|
194 buf = f->readInt(1);
|
|
|
195 tracks[i+k][j].param1 = buf >> 4;
|
|
|
196 tracks[i+k][j].param2 = buf & 0x0f;
|
|
|
197 }
|
|
|
198 }
|
|
|
199 i+=9;
|
|
|
200 }
|
|
|
201 } else {
|
|
|
202 i = 0;
|
|
|
203 while(!f->ateof()) {
|
|
|
204 for(j=0;j<64;j++) {
|
|
|
205 buf = f->readInt(1);
|
|
|
206 tracks[i][j].note = buf >> 1;
|
|
|
207 tracks[i][j].inst = (buf & 1) << 4;
|
|
|
208 buf = f->readInt(1);
|
|
|
209 tracks[i][j].inst += buf >> 4;
|
|
|
210 tracks[i][j].command = convfx[buf & 0x0f];
|
|
|
211 buf = f->readInt(1);
|
|
|
212 tracks[i][j].param1 = buf >> 4;
|
|
|
213 tracks[i][j].param2 = buf & 0x0f;
|
|
|
214 }
|
|
|
215 i++;
|
|
|
216 }
|
|
|
217 }
|
|
|
218 fp.close(f);
|
|
|
219
|
|
|
220 // fix instrument names
|
|
|
221 for(i=0;i<29;i++)
|
|
|
222 for(j=0;j<17;j++)
|
|
|
223 if(!instname[i][j])
|
|
|
224 instname[i][j] = ' ';
|
|
|
225
|
|
|
226 rewind(0); // rewind module
|
|
|
227 return true;
|
|
|
228 }
|
|
|
229
|
|
|
230 std::string Csa2Loader::gettype()
|
|
|
231 {
|
|
|
232 char tmpstr[40];
|
|
|
233
|
|
|
234 sprintf(tmpstr,"Surprise! Adlib Tracker 2 (version %d)",header.version);
|
|
|
235 return std::string(tmpstr);
|
|
|
236 }
|
|
|
237
|
|
|
238 std::string Csa2Loader::gettitle()
|
|
|
239 {
|
|
|
240 char bufinst[29*17],buf[18];
|
|
|
241 int i,ptr;
|
|
|
242
|
|
|
243 // parse instrument names for song name
|
|
|
244 memset(bufinst,'\0',29*17);
|
|
|
245 for(i=0;i<29;i++) {
|
|
|
246 buf[16] = ' '; buf[17] = '\0';
|
|
|
247 memcpy(buf,instname[i]+1,16);
|
|
|
248 for(ptr=16;ptr>0;ptr--)
|
|
|
249 if(buf[ptr] == ' ')
|
|
|
250 buf[ptr] = '\0';
|
|
|
251 else {
|
|
|
252 if(ptr<16)
|
|
|
253 buf[ptr+1] = ' ';
|
|
|
254 break;
|
|
|
255 }
|
|
|
256 strcat(bufinst,buf);
|
|
|
257 }
|
|
|
258
|
|
|
259 if(strchr(bufinst,'"'))
|
|
|
260 return std::string(bufinst,strchr(bufinst,'"')-bufinst+1,strrchr(bufinst,'"')-strchr(bufinst,'"')-1);
|
|
|
261 else
|
|
|
262 return std::string();
|
|
|
263 }
|