comparison Plugins/Input/adplug/core/fmc.cpp @ 359:8df427a314a8 trunk

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