2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2004 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 * hsc.cpp - HSC Player by Simon Peter <dn.tlp@gmx.net>
27 /*** public methods **************************************/
29 CPlayer *ChscPlayer::factory(Copl *newopl)
31 return new ChscPlayer(newopl);
34 bool ChscPlayer::load(const std::string &filename, const CFileProvider &fp)
36 binistream *f = fp.open(filename);
39 // file validation section
40 if(!f || !fp.extension(filename, ".hsc") || fp.filesize(f) > 59187) {
41 AdPlug_LogWrite("ChscPlayer::load(\"%s\"): Not a HSC file!\n", filename.c_str());
47 for(i=0;i<128*12;i++) // load instruments
48 *((unsigned char *)instr + i) = f->readInt(1);
49 for (i=0;i<128;i++) { // correct instruments
50 instr[i][2] ^= (instr[i][2] & 0x40) << 1;
51 instr[i][3] ^= (instr[i][3] & 0x40) << 1;
52 instr[i][11] >>= 4; // slide
54 for(i=0;i<51;i++) song[i] = f->readInt(1); // load tracklist
55 for(i=0;i<50*64*9;i++) // load patterns
56 *((char *)patterns + i) = f->readInt(1);
59 rewind(0); // rewind module
63 bool ChscPlayer::update()
66 unsigned char chan,pattnr,note,effect,eff_op,inst,vol,Okt,db;
68 unsigned long pattoff;
70 del--; // player speed handling
72 return !songend; // nothing done
74 if(fadein) // fade-in handling
77 pattnr = song[songpos];
78 if(pattnr == 0xff) { // arrangement handling
79 songend = 1; // set end-flag
81 pattnr = song[songpos];
83 if ((pattnr & 128) && (pattnr <= 0xb1)) { // goto pattern "nr"
84 songpos = song[songpos] & 127;
86 pattnr = song[songpos];
91 for (chan=0;chan<9;chan++) { // handle all channels
92 note = patterns[pattnr][pattoff].note;
93 effect = patterns[pattnr][pattoff].effect;
96 if(note & 128) { // set instrument
97 setinstr(chan,effect);
100 eff_op = effect & 0x0f;
101 inst = channel[chan].inst;
103 channel[chan].slide = 0;
105 switch (effect & 0xf0) { // effect handling
106 case 0: // global effect
107 /* The following fx are unimplemented on purpose:
108 * 02 - Slide Mainvolume up
109 * 03 - Slide Mainvolume down (here: fade in)
110 * 04 - Set Mainvolume to 0
112 * This is because i've never seen any HSC modules using the fx this way.
113 * All modules use the fx the way, i've implemented it.
116 case 1: pattbreak++; break; // jump to next pattern
117 case 3: fadein = 31; break; // fade in (divided by 2)
118 case 5: mode6 = 1; break; // 6 voice mode on
119 case 6: mode6 = 0; break; // 6 voice mode off
123 case 0x10: // manual slides
125 channel[chan].freq += eff_op;
126 channel[chan].slide += eff_op;
128 channel[chan].freq -= eff_op;
129 channel[chan].slide -= eff_op;
132 setfreq(chan,channel[chan].freq);
134 case 0x50: // set percussion instrument (unimplemented)
136 case 0x60: // set feedback
137 opl->write(0xc0 + chan, (instr[channel[chan].inst][8] & 1) + (eff_op << 1));
139 case 0xa0: // set carrier volume
141 opl->write(0x43 + op_table[chan], vol | (instr[channel[chan].inst][2] & ~63));
143 case 0xb0: // set modulator volume
145 if (instr[inst][8] & 1)
146 opl->write(0x40 + op_table[chan], vol | (instr[channel[chan].inst][3] & ~63));
148 opl->write(0x40 + op_table[chan],vol | (instr[inst][3] & ~63));
150 case 0xc0: // set instrument volume
152 opl->write(0x43 + op_table[chan], db | (instr[channel[chan].inst][2] & ~63));
153 if (instr[inst][8] & 1)
154 opl->write(0x40 + op_table[chan], db | (instr[channel[chan].inst][3] & ~63));
156 case 0xd0: pattbreak++; songpos = eff_op; songend = 1; break; // position jump
157 case 0xf0: // set speed
163 if(fadein) // fade-in volume setting
164 setvolume(chan,fadein*2,fadein*2);
166 if(!note) // note handling
170 if ((note == 0x7f-1) || ((note/12) & ~7)) { // pause (7fh)
171 adl_freq[chan] &= ~32;
172 opl->write(0xb0 + chan,adl_freq[chan]);
177 if(mtkmode) // imitate MPU-401 Trakker bug
179 Okt = ((note/12) & 7) << 2;
180 Fnr = note_table[(note % 12)] + instr[inst][11] + channel[chan].slide;
181 channel[chan].freq = Fnr;
182 if(!mode6 || chan < 6)
183 adl_freq[chan] = Okt | 32;
185 adl_freq[chan] = Okt; // never set key for drums
186 opl->write(0xb0 + chan, 0);
189 switch(chan) { // play drums
190 case 6: opl->write(0xbd,bd & ~16); bd |= 48; break; // bass drum
191 case 7: opl->write(0xbd,bd & ~1); bd |= 33; break; // hihat
192 case 8: opl->write(0xbd,bd & ~2); bd |= 34; break; // cymbal
198 del = speed; // player speed-timing
199 if(pattbreak) { // do post-effect handling
200 pattpos=0; // pattern break!
208 pattpos &= 63; // advance in pattern data
216 return !songend; // still playing
219 void ChscPlayer::rewind(int subsong)
224 pattpos = 0; songpos = 0; pattbreak = 0; speed = 2;
225 del = 1; songend = 0; mode6 = 0; bd = 0; fadein = 0;
227 opl->init(); // reset OPL chip
228 opl->write(1,32); opl->write(8,128); opl->write(0xbd,0);
231 setinstr((char) i,(char) i); // init channels
234 unsigned int ChscPlayer::getpatterns()
236 unsigned char poscnt,pattcnt=0;
239 for(poscnt=0;poscnt<51 && song[poscnt] != 0xff;poscnt++)
240 if(song[poscnt] > pattcnt)
241 pattcnt = song[poscnt];
246 unsigned int ChscPlayer::getorders()
248 unsigned char poscnt;
251 for(poscnt=0;poscnt<51;poscnt++)
252 if(song[poscnt] == 0xff)
258 unsigned int ChscPlayer::getinstruments()
260 unsigned char instcnt,instnum=0,i;
264 for(instcnt=0;instcnt<128;instcnt++) {
267 if(instr[instcnt][i])
276 /*** private methods *************************************/
278 void ChscPlayer::setfreq(unsigned char chan, unsigned short freq)
280 adl_freq[chan] = (adl_freq[chan] & ~3) | (freq >> 8);
282 opl->write(0xa0 + chan, freq & 0xff);
283 opl->write(0xb0 + chan, adl_freq[chan]);
286 void ChscPlayer::setvolume(unsigned char chan, int volc, int volm)
288 unsigned char *ins = instr[channel[chan].inst];
289 char op = op_table[chan];
291 opl->write(0x43 + op,volc | (ins[2] & ~63));
292 if (ins[8] & 1) // carrier
293 opl->write(0x40 + op,volm | (ins[3] & ~63));
295 opl->write(0x40 + op, ins[3]); // modulator
298 void ChscPlayer::setinstr(unsigned char chan, unsigned char insnr)
300 unsigned char *ins = instr[insnr];
301 char op = op_table[chan];
303 channel[chan].inst = insnr; // set internal instrument
304 opl->write(0xb0 + chan,0); // stop old note
307 opl->write(0xc0 + chan, ins[8]);
308 opl->write(0x23 + op, ins[0]); // carrier
309 opl->write(0x20 + op, ins[1]); // modulator
310 opl->write(0x63 + op, ins[4]); // bits 0..3 = decay; 4..7 = attack
311 opl->write(0x60 + op, ins[5]);
312 opl->write(0x83 + op, ins[6]); // 0..3 = release; 4..7 = sustain
313 opl->write(0x80 + op, ins[7]);
314 opl->write(0xe3 + op, ins[9]); // bits 0..1 = Wellenform
315 opl->write(0xe0 + op, ins[10]);
316 setvolume(chan, ins[2] & 63, ins[3] & 63);