]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug/src/rad.cpp
wwww~
[16.git] / 16 / adplug / adplug / src / rad.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  * rad.cpp - RAD Loader by Simon Peter <dn.tlp@gmx.net>
20  *
21  * BUGS:
22  * some volumes are dropped out
23  */
24
25 #include <cstring>
26 #include "rad.h"
27
28 CPlayer *CradLoader::factory(Copl *newopl)
29 {
30   return new CradLoader(newopl);
31 }
32
33 bool CradLoader::load(const std::string &filename, const CFileProvider &fp)
34 {
35   binistream *f = fp.open(filename); if(!f) return false;
36   char id[16];
37   unsigned char buf,ch,c,b,inp;
38   char bufstr[2] = "\0";
39   unsigned int i,j;
40   unsigned short patofs[32];
41   const unsigned char convfx[16] = {255,1,2,3,255,5,255,255,255,255,20,255,17,0xd,255,19};
42
43   // file validation section
44   f->readString(id, 16); version = f->readInt(1);
45   if(strncmp(id,"RAD by REALiTY!!",16) || version != 0x10)
46     { fp.close(f); return false; }
47
48   // load section
49   radflags = f->readInt(1);
50   if(radflags & 128) {  // description
51     memset(desc,0,80*22);
52     while((buf = f->readInt(1)))
53       if(buf == 1)
54         strcat(desc,"\n");
55       else
56         if(buf >= 2 && buf <= 0x1f)
57           for(i=0;i<buf;i++)
58             strcat(desc," ");
59         else {
60           *bufstr = buf;
61           strcat(desc,bufstr);
62         }
63   }
64   while((buf = f->readInt(1))) {        // instruments
65     buf--;
66     inst[buf].data[2] = f->readInt(1); inst[buf].data[1] = f->readInt(1);
67     inst[buf].data[10] = f->readInt(1); inst[buf].data[9] = f->readInt(1);
68     inst[buf].data[4] = f->readInt(1); inst[buf].data[3] = f->readInt(1);
69     inst[buf].data[6] = f->readInt(1); inst[buf].data[5] = f->readInt(1);
70     inst[buf].data[0] = f->readInt(1);
71     inst[buf].data[8] = f->readInt(1); inst[buf].data[7] = f->readInt(1);
72   }
73   length = f->readInt(1);
74   for(i = 0; i < length; i++) order[i] = f->readInt(1); // orderlist
75   for(i = 0; i < 32; i++) patofs[i] = f->readInt(2);    // pattern offset table
76   init_trackord();              // patterns
77   for(i=0;i<32;i++)
78     if(patofs[i]) {
79       f->seek(patofs[i]);
80       do {
81         buf = f->readInt(1); b = buf & 127;
82         do {
83           ch = f->readInt(1); c = ch & 127;
84           inp = f->readInt(1);
85           tracks[i*9+c][b].note = inp & 127;
86           tracks[i*9+c][b].inst = (inp & 128) >> 3;
87           inp = f->readInt(1);
88           tracks[i*9+c][b].inst += inp >> 4;
89           tracks[i*9+c][b].command = inp & 15;
90           if(inp & 15) {
91             inp = f->readInt(1);
92             tracks[i*9+c][b].param1 = inp / 10;
93             tracks[i*9+c][b].param2 = inp % 10;
94           }
95         } while(!(ch & 128));
96       } while(!(buf & 128));
97     } else
98       memset(trackord[i],0,9*2);
99   fp.close(f);
100
101   // convert replay data
102   for(i=0;i<32*9;i++)   // convert patterns
103     for(j=0;j<64;j++) {
104       if(tracks[i][j].note == 15)
105         tracks[i][j].note = 127;
106       if(tracks[i][j].note > 16 && tracks[i][j].note < 127)
107         tracks[i][j].note -= 4 * (tracks[i][j].note >> 4);
108       if(tracks[i][j].note && tracks[i][j].note < 126)
109         tracks[i][j].note++;
110       tracks[i][j].command = convfx[tracks[i][j].command];
111     }
112   restartpos = 0; initspeed = radflags & 31;
113   bpm = radflags & 64 ? 0 : 50; flags = Decimal;
114
115   rewind(0);
116   return true;
117 }
118
119 float CradLoader::getrefresh()
120 {
121   if(tempo)
122     return (float) (tempo);
123   else
124     return 18.2f;
125 }