]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/adplug-2.2.1/src/fmc.cpp
Please enter the commit message for your changes. Lines starting
[16.git] / 16 / adplug / adplug-2.2.1 / src / fmc.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   fmc.cpp - FMC Loader by Riven the Mage <riven@ok.ru>
20 */
21
22 #include <cstring>
23 #include "fmc.h"
24
25 /* -------- Public Methods -------------------------------- */
26
27 CPlayer *CfmcLoader::factory(Copl *newopl)
28 {
29   return new CfmcLoader(newopl);
30 }
31
32 bool CfmcLoader::load(const std::string &filename, const CFileProvider &fp)
33 {
34   binistream *f = fp.open(filename); if(!f) return false;
35   const unsigned char conv_fx[16] = {0,1,2,3,4,8,255,255,255,255,26,11,12,13,14,15};
36
37   int i,j,k,t=0;
38
39   // read header
40   f->readString(header.id, 4);
41   f->readString(header.title, 21);
42   header.numchan = f->readInt(1);
43
44   // 'FMC!' - signed ?
45   if (strncmp(header.id,"FMC!",4)) { fp.close(f); return false; }
46
47   // init CmodPlayer
48   realloc_instruments(32);
49   realloc_order(256);
50   realloc_patterns(64,64,header.numchan);
51   init_trackord();
52
53   // load order
54   for(i = 0; i < 256; i++) order[i] = f->readInt(1);
55
56   f->ignore(2);
57
58   // load instruments
59   for(i = 0; i < 32; i++) {
60     instruments[i].synthesis = f->readInt(1);
61     instruments[i].feedback = f->readInt(1);
62
63     instruments[i].mod_attack = f->readInt(1);
64     instruments[i].mod_decay = f->readInt(1);
65     instruments[i].mod_sustain = f->readInt(1);
66     instruments[i].mod_release = f->readInt(1);
67     instruments[i].mod_volume = f->readInt(1);
68     instruments[i].mod_ksl = f->readInt(1);
69     instruments[i].mod_freq_multi = f->readInt(1);
70     instruments[i].mod_waveform = f->readInt(1);
71     instruments[i].mod_sustain_sound = f->readInt(1);
72     instruments[i].mod_ksr = f->readInt(1);
73     instruments[i].mod_vibrato = f->readInt(1);
74     instruments[i].mod_tremolo = f->readInt(1);
75
76     instruments[i].car_attack = f->readInt(1);
77     instruments[i].car_decay = f->readInt(1);
78     instruments[i].car_sustain = f->readInt(1);
79     instruments[i].car_release = f->readInt(1);
80     instruments[i].car_volume = f->readInt(1);
81     instruments[i].car_ksl = f->readInt(1);
82     instruments[i].car_freq_multi = f->readInt(1);
83     instruments[i].car_waveform = f->readInt(1);
84     instruments[i].car_sustain_sound = f->readInt(1);
85     instruments[i].car_ksr = f->readInt(1);
86     instruments[i].car_vibrato = f->readInt(1);
87     instruments[i].car_tremolo = f->readInt(1);
88
89     instruments[i].pitch_shift = f->readInt(1);
90
91     f->readString(instruments[i].name, 21);
92   }
93
94   // load tracks
95   for (i=0;i<64;i++)
96     {
97       if(f->ateof()) break;
98
99       for (j=0;j<header.numchan;j++)
100         {
101           for (k=0;k<64;k++)
102             {
103               fmc_event event;
104
105               // read event
106               event.byte0 = f->readInt(1);
107               event.byte1 = f->readInt(1);
108               event.byte2 = f->readInt(1);
109
110               // convert event
111               tracks[t][k].note = event.byte0 & 0x7F;
112               tracks[t][k].inst = ((event.byte0 & 0x80) >> 3) + (event.byte1 >> 4) + 1;
113               tracks[t][k].command = conv_fx[event.byte1 & 0x0F];
114               tracks[t][k].param1 = event.byte2 >> 4;
115               tracks[t][k].param2 = event.byte2 & 0x0F;
116
117               // fix effects
118               if (tracks[t][k].command == 0x0E)   // 0x0E (14): Retrig
119                 tracks[t][k].param1 = 3;
120               if (tracks[t][k].command == 0x1A) { // 0x1A (26): Volume Slide
121                 if (tracks[t][k].param1 > tracks[t][k].param2)
122                   {
123                     tracks[t][k].param1 -= tracks[t][k].param2;
124                     tracks[t][k].param2 = 0;
125                   }
126                 else
127                   {
128                     tracks[t][k].param2 -= tracks[t][k].param1;
129                     tracks[t][k].param1 = 0;
130                   }
131               }
132             }
133
134           t++;
135         }
136     }
137   fp.close(f);
138
139   // convert instruments
140   for (i=0;i<31;i++)
141     buildinst(i);
142
143   // order length
144   for (i=0;i<256;i++)
145     {
146       if (order[i] >= 0xFE)
147         {
148           length = i;
149           break;
150         }
151     }
152
153   // data for Protracker
154   activechan = (0xffffffff >> (32 - header.numchan)) << (32 - header.numchan);
155   nop = t / header.numchan;
156   restartpos = 0;
157
158   // flags
159   flags = Faust;
160
161   rewind(0);
162
163   return true;
164 }
165
166 float CfmcLoader::getrefresh()
167 {
168   return 50.0f;
169 }
170
171 std::string CfmcLoader::gettype()
172 {
173   return std::string("Faust Music Creator");
174 }
175
176 std::string CfmcLoader::gettitle()
177 {
178   return std::string(header.title);
179 }
180
181 std::string CfmcLoader::getinstrument(unsigned int n)
182 {
183   return std::string(instruments[n].name);
184 }
185
186 unsigned int CfmcLoader::getinstruments()
187 {
188   return 32;
189 }
190
191 /* -------- Private Methods ------------------------------- */
192
193 void CfmcLoader::buildinst(unsigned char i)
194 {
195   inst[i].data[0]   = ((instruments[i].synthesis & 1) ^ 1);
196   inst[i].data[0]  |= ((instruments[i].feedback & 7) << 1);
197
198   inst[i].data[3]   = ((instruments[i].mod_attack & 15) << 4);
199   inst[i].data[3]  |=  (instruments[i].mod_decay & 15);
200   inst[i].data[5]   = ((15 - (instruments[i].mod_sustain & 15)) << 4);
201   inst[i].data[5]  |=  (instruments[i].mod_release & 15);
202   inst[i].data[9]   =  (63 - (instruments[i].mod_volume & 63));
203   inst[i].data[9]  |= ((instruments[i].mod_ksl & 3) << 6);
204   inst[i].data[1]   =  (instruments[i].mod_freq_multi & 15);
205   inst[i].data[7]   =  (instruments[i].mod_waveform & 3);
206   inst[i].data[1]  |= ((instruments[i].mod_sustain_sound & 1) << 5);
207   inst[i].data[1]  |= ((instruments[i].mod_ksr & 1) << 4);
208   inst[i].data[1]  |= ((instruments[i].mod_vibrato & 1) << 6);
209   inst[i].data[1]  |= ((instruments[i].mod_tremolo & 1) << 7);
210
211   inst[i].data[4]   = ((instruments[i].car_attack & 15) << 4);
212   inst[i].data[4]  |=  (instruments[i].car_decay & 15);
213   inst[i].data[6]   = ((15 - (instruments[i].car_sustain & 15)) << 4);
214   inst[i].data[6]  |=  (instruments[i].car_release & 15);
215   inst[i].data[10]  =  (63 - (instruments[i].car_volume & 63));
216   inst[i].data[10] |= ((instruments[i].car_ksl & 3) << 6);
217   inst[i].data[2]   =  (instruments[i].car_freq_multi & 15);
218   inst[i].data[8]   =  (instruments[i].car_waveform & 3);
219   inst[i].data[2]  |= ((instruments[i].car_sustain_sound & 1) << 5);
220   inst[i].data[2]  |= ((instruments[i].car_ksr & 1) << 4);
221   inst[i].data[2]  |= ((instruments[i].car_vibrato & 1) << 6);
222   inst[i].data[2]  |= ((instruments[i].car_tremolo & 1) << 7);
223
224   inst[i].slide     =   instruments[i].pitch_shift;
225 }