2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2008 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 * sa2.cpp - SAdT2 Loader by Simon Peter <dn.tlp@gmx.net>
20 * SAdT Loader by Mamiya <mamiya@users.sourceforge.net>
30 CPlayer *Csa2Loader::factory(Copl *newopl)
32 return new Csa2Loader(newopl);
35 bool Csa2Loader::load(const std::string &filename, const CFileProvider &fp)
37 binistream *f = fp.open(filename); if(!f) return false;
39 unsigned char data[11],arpstart,arpspeed,arppos,arpspdcnt;
42 int i,j, k, notedis = 0;
43 const unsigned char convfx[16] = {0,1,2,3,4,5,6,255,8,255,10,11,12,13,255,15};
44 unsigned char sat_type;
46 HAS_ARPEGIOLIST = (1 << 7),
47 HAS_V7PATTERNS = (1 << 6),
48 HAS_ACTIVECHANNELS = (1 << 5),
49 HAS_TRACKORDER = (1 << 4),
50 HAS_ARPEGIO = (1 << 3),
51 HAS_OLDBPM = (1 << 2),
52 HAS_OLDPATTERNS = (1 << 1),
53 HAS_UNKNOWN127 = (1 << 0)
57 f->readString(header.sadt, 4);
58 header.version = f->readInt(1);
60 // file validation section
61 if(strncmp(header.sadt,"SAdT",4)) { fp.close(f); return false; }
62 switch(header.version) {
65 sat_type = HAS_UNKNOWN127 | HAS_OLDPATTERNS | HAS_OLDBPM;
69 sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
73 sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
77 sat_type = HAS_ARPEGIO | HAS_OLDPATTERNS | HAS_OLDBPM;
81 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
84 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
87 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_V7PATTERNS;
90 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER;
93 sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER | HAS_ACTIVECHANNELS;
95 default: /* unknown */
102 for(i = 0; i < 31; i++) {
103 if(sat_type & HAS_ARPEGIO) {
104 for(j = 0; j < 11; j++) insts.data[j] = f->readInt(1);
105 insts.arpstart = f->readInt(1);
106 insts.arpspeed = f->readInt(1);
107 insts.arppos = f->readInt(1);
108 insts.arpspdcnt = f->readInt(1);
109 inst[i].arpstart = insts.arpstart;
110 inst[i].arpspeed = insts.arpspeed;
111 inst[i].arppos = insts.arppos;
112 inst[i].arpspdcnt = insts.arpspdcnt;
114 for(j = 0; j < 11; j++) insts.data[j] = f->readInt(1);
115 inst[i].arpstart = 0;
116 inst[i].arpspeed = 0;
118 inst[i].arpspdcnt = 0;
121 inst[i].data[j] = insts.data[j];
127 for(i = 0; i < 29; i++) f->readString(instname[i], 17);
129 f->ignore(3); // dummy bytes
130 for(i = 0; i < 128; i++) order[i] = f->readInt(1); // pattern orders
131 if(sat_type & HAS_UNKNOWN127) f->ignore(127);
134 nop = f->readInt(2); length = f->readInt(1); restartpos = f->readInt(1);
138 if(sat_type & HAS_OLDBPM) {
139 bpm = bpm * 125 / 50; // cps -> bpm
142 if(sat_type & HAS_ARPEGIOLIST) {
144 for(i = 0; i < 256; i++) arplist[i] = f->readInt(1); // arpeggio list
145 for(i = 0; i < 256; i++) arpcmd[i] = f->readInt(1); // arpeggio commands
148 for(i=0;i<64;i++) { // track orders
150 if(sat_type & HAS_TRACKORDER)
151 trackord[i][j] = f->readInt(1);
154 trackord[i][j] = i * 9 + j;
159 if(sat_type & HAS_ACTIVECHANNELS)
160 activechan = f->readInt(2) << 16; // active channels
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);
167 if(sat_type & HAS_OLDPATTERNS) {
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);
183 if(sat_type & HAS_V7PATTERNS) {
189 tracks[i+k][j].note = buf >> 1;
190 tracks[i+k][j].inst = (buf & 1) << 4;
192 tracks[i+k][j].inst += buf >> 4;
193 tracks[i+k][j].command = convfx[buf & 0x0f];
195 tracks[i+k][j].param1 = buf >> 4;
196 tracks[i+k][j].param2 = buf & 0x0f;
206 tracks[i][j].note = buf >> 1;
207 tracks[i][j].inst = (buf & 1) << 4;
209 tracks[i][j].inst += buf >> 4;
210 tracks[i][j].command = convfx[buf & 0x0f];
212 tracks[i][j].param1 = buf >> 4;
213 tracks[i][j].param2 = buf & 0x0f;
220 // fix instrument names
224 instname[i][j] = ' ';
226 rewind(0); // rewind module
230 std::string Csa2Loader::gettype()
234 sprintf(tmpstr,"Surprise! Adlib Tracker 2 (version %d)",header.version);
235 return std::string(tmpstr);
238 std::string Csa2Loader::gettitle()
240 char bufinst[29*17],buf[18];
243 // parse instrument names for song name
244 memset(bufinst,'\0',29*17);
246 buf[16] = ' '; buf[17] = '\0';
247 memcpy(buf,instname[i]+1,16);
248 for(ptr=16;ptr>0;ptr--)
259 if(strchr(bufinst,'"'))
260 return std::string(bufinst,strchr(bufinst,'"')-bufinst+1,strrchr(bufinst,'"')-strchr(bufinst,'"')-1);
262 return std::string();