]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug/src/sa2.cpp
renamed: 16/adplug/adplug-2.2.1/.DS_Store -> 16/adplug/adplug/.DS_Store
[16.git] / 16 / adplug / adplug / src / sa2.cpp
1 /*
2  * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3  * Copyright (C) 1999 - 2008 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 <cstring>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "sa2.h"
28 #include "debug.h"
29
30 CPlayer *Csa2Loader::factory(Copl *newopl)
31 {
32   return new Csa2Loader(newopl);
33 }
34
35 bool Csa2Loader::load(const std::string &filename, const CFileProvider &fp)
36 {
37   binistream *f = fp.open(filename); if(!f) return false;
38   struct {
39     unsigned char data[11],arpstart,arpspeed,arppos,arpspdcnt;
40   } insts;
41   unsigned char buf;
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;
45   enum 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)
54   };
55
56   // read header
57   f->readString(header.sadt, 4);
58   header.version = f->readInt(1);
59
60   // file validation section
61   if(strncmp(header.sadt,"SAdT",4)) { fp.close(f); return false; }
62   switch(header.version) {
63   case 1:
64     notedis = +0x18;
65     sat_type = HAS_UNKNOWN127 | HAS_OLDPATTERNS | HAS_OLDBPM;
66     break;
67   case 2:
68     notedis = +0x18;
69     sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
70     break;
71   case 3:
72     notedis = +0x0c;
73     sat_type = HAS_OLDPATTERNS | HAS_OLDBPM;
74     break;
75   case 4:
76     notedis = +0x0c;
77     sat_type = HAS_ARPEGIO | HAS_OLDPATTERNS | HAS_OLDBPM;
78     break;
79   case 5:
80     notedis = +0x0c;
81     sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
82     break;
83   case 6:
84     sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_OLDPATTERNS | HAS_OLDBPM;
85     break;
86   case 7:
87     sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_V7PATTERNS;
88     break;
89   case 8:
90     sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER;
91     break;
92   case 9:
93     sat_type = HAS_ARPEGIO | HAS_ARPEGIOLIST | HAS_TRACKORDER | HAS_ACTIVECHANNELS;
94     break;
95   default:      /* unknown */
96     fp.close(f);
97     return false;
98   }
99
100   // load section
101   // instruments
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;
113     } else {
114       for(j = 0; j < 11; j++) insts.data[j] = f->readInt(1);
115       inst[i].arpstart = 0;
116       inst[i].arpspeed = 0;
117       inst[i].arppos = 0;
118       inst[i].arpspdcnt = 0;
119     }
120     for(j=0;j<11;j++)
121       inst[i].data[j] = insts.data[j];
122     inst[i].misc = 0;
123     inst[i].slide = 0;
124   }
125
126   // instrument names
127   for(i = 0; i < 29; i++) f->readString(instname[i], 17);
128
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);
132
133   // infos
134   nop = f->readInt(2); length = f->readInt(1); restartpos = f->readInt(1);
135
136   // bpm
137   bpm = f->readInt(2);
138   if(sat_type & HAS_OLDBPM) {
139     bpm = bpm * 125 / 50;               // cps -> bpm
140   }
141
142   if(sat_type & HAS_ARPEGIOLIST) {
143     init_specialarp();
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
146   }
147
148   for(i=0;i<64;i++) {                           // track orders
149     for(j=0;j<9;j++) {
150       if(sat_type & HAS_TRACKORDER)
151         trackord[i][j] = f->readInt(1);
152       else
153         {
154           trackord[i][j] = i * 9 + j;
155         }
156     }
157   }
158
159   if(sat_type & HAS_ACTIVECHANNELS)
160     activechan = f->readInt(2) << 16;           // active channels
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 }