2 Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 Copyright (C) 1999 - 2006 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 dtm.cpp - DTM loader by Riven the Mage <riven@ok.ru>
22 NOTE: Panning (Ex) effect is ignored.
28 /* -------- Public Methods -------------------------------- */
30 CPlayer *CdtmLoader::factory(Copl *newopl)
32 return new CdtmLoader(newopl);
35 bool CdtmLoader::load(const std::string &filename, const CFileProvider &fp)
37 binistream *f = fp.open(filename); if(!f) return false;
38 const unsigned char conv_inst[11] = { 2,1,10,9,4,3,6,5,0,8,7 };
39 const unsigned short conv_note[12] = { 0x16B, 0x181, 0x198, 0x1B0, 0x1CA, 0x1E5, 0x202, 0x220, 0x241, 0x263, 0x287, 0x2AE };
43 f->readString(header.id, 12);
44 header.version = f->readInt(1);
45 f->readString(header.title, 20); f->readString(header.author, 20);
46 header.numpat = f->readInt(1); header.numinst = f->readInt(1);
48 // signature exists ? good version ?
49 if(memcmp(header.id,"DeFy DTM ",9) || header.version != 0x10)
50 { fp.close (f); return false; }
62 unsigned char bufstr_length = f->readInt(1);
64 if(bufstr_length > 80) {
72 f->readString(bufstr,bufstr_length);
74 for (j=0;j<bufstr_length;j++)
78 bufstr[bufstr_length] = 0;
87 realloc_instruments(header.numinst);
89 realloc_patterns(header.numpat,64,9);
90 init_notetable(conv_note);
94 for (i=0;i<header.numinst;i++)
96 unsigned char name_length = f->readInt(1);
99 f->readString(instruments[i].name, name_length);
101 instruments[i].name[name_length] = 0;
103 for(j = 0; j < 12; j++)
104 instruments[i].data[j] = f->readInt(1);
107 inst[i].data[conv_inst[j]] = instruments[i].data[j];
111 for(i = 0; i < 100; i++) order[i] = f->readInt(1);
115 unsigned char *pattern = new unsigned char [0x480];
120 unsigned short packed_length;
122 packed_length = f->readInt(2);
124 unsigned char *packed_pattern = new unsigned char [packed_length];
126 for(j = 0; j < packed_length; j++)
127 packed_pattern[j] = f->readInt(1);
129 long unpacked_length = unpack_pattern(packed_pattern,packed_length,pattern,0x480);
131 delete [] packed_pattern;
133 if (!unpacked_length)
145 dtm_event *event = (dtm_event *)&pattern[(k*9+j)*2];
148 if (event->byte0 == 0x80)
150 if (event->byte1 <= 0x80)
151 tracks[t][k].inst = event->byte1 + 1;
157 tracks[t][k].note = event->byte0;
159 if ((event->byte0 != 0) && (event->byte0 != 127))
163 switch (event->byte1 >> 4)
165 case 0x0: // pattern break
166 if ((event->byte1 & 15) == 1)
167 tracks[t][k].command = 13;
170 case 0x1: // freq. slide up
171 tracks[t][k].command = 28;
172 tracks[t][k].param1 = event->byte1 & 15;
175 case 0x2: // freq. slide down
176 tracks[t][k].command = 28;
177 tracks[t][k].param2 = event->byte1 & 15;
180 case 0xA: // set carrier volume
181 case 0xC: // set instrument volume
182 tracks[t][k].command = 22;
183 tracks[t][k].param1 = (0x3F - (event->byte1 & 15)) >> 4;
184 tracks[t][k].param2 = (0x3F - (event->byte1 & 15)) & 15;
187 case 0xB: // set modulator volume
188 tracks[t][k].command = 21;
189 tracks[t][k].param1 = (0x3F - (event->byte1 & 15)) >> 4;
190 tracks[t][k].param2 = (0x3F - (event->byte1 & 15)) & 15;
193 case 0xE: // set panning
196 case 0xF: // set speed
197 tracks[t][k].command = 13;
198 tracks[t][k].param2 = event->byte1 & 15;
214 if (order[i] >= 0x80)
218 if (order[i] == 0xFF)
221 restartpos = order[i] - 0x80;
235 void CdtmLoader::rewind(int subsong)
237 CmodPlayer::rewind(subsong);
239 // default instruments
240 for (int i=0;i<9;i++)
244 channel[i].vol1 = 63 - (inst[i].data[10] & 63);
245 channel[i].vol2 = 63 - (inst[i].data[9] & 63);
249 float CdtmLoader::getrefresh()
254 std::string CdtmLoader::gettype()
256 return std::string("DeFy Adlib Tracker");
259 std::string CdtmLoader::gettitle()
261 return std::string(header.title);
264 std::string CdtmLoader::getauthor()
266 return std::string(header.author);
269 std::string CdtmLoader::getdesc()
271 return std::string(desc);
274 std::string CdtmLoader::getinstrument(unsigned int n)
276 return std::string(instruments[n].name);
279 unsigned int CdtmLoader::getinstruments()
281 return header.numinst;
284 /* -------- Private Methods ------------------------------- */
286 long CdtmLoader::unpack_pattern(unsigned char *ibuf, long ilen, unsigned char *obuf, long olen)
288 unsigned char *input = ibuf;
289 unsigned char *output = obuf;
291 long input_length = 0;
292 long output_length = 0;
294 unsigned char repeat_byte, repeat_counter;
297 while (input_length < ilen)
299 repeat_byte = input[input_length++];
301 if ((repeat_byte & 0xF0) == 0xD0)
303 repeat_counter = repeat_byte & 15;
304 repeat_byte = input[input_length++];
309 for (int i=0;i<repeat_counter;i++)
311 if (output_length < olen)
312 output[output_length++] = repeat_byte;
316 return output_length;