2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2003, 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 * [xad] BMF player, by Riven the Mage <riven@ok.ru>
25 file(s) : GAMESNET.COM
26 type : GamesNet advertising intro
27 tune : by (?)The Brain [Razor 1911]
28 player : ver.0.9b by Hammer
31 type : Ford Knox BBStro
32 tune : by The Brain [Razor 1911]
34 comment : in original player at 9th channel the feedback adlib register is not C8 but C6.
38 tune : by The Brain [Razor 1911]
40 comment : inaccurate replaying, because constant outport; in original player it can be 380 or 382.
47 const unsigned char CxadbmfPlayer::bmf_adlib_registers[117] =
49 0x20, 0x23, 0x40, 0x43, 0x60, 0x63, 0x80, 0x83, 0xA0, 0xB0, 0xC0, 0xE0, 0xE3,
50 0x21, 0x24, 0x41, 0x44, 0x61, 0x64, 0x81, 0x84, 0xA1, 0xB1, 0xC1, 0xE1, 0xE4,
51 0x22, 0x25, 0x42, 0x45, 0x62, 0x65, 0x82, 0x85, 0xA2, 0xB2, 0xC2, 0xE2, 0xE5,
52 0x28, 0x2B, 0x48, 0x4B, 0x68, 0x6B, 0x88, 0x8B, 0xA3, 0xB3, 0xC3, 0xE8, 0xEB,
53 0x29, 0x2C, 0x49, 0x4C, 0x69, 0x6C, 0x89, 0x8C, 0xA4, 0xB4, 0xC4, 0xE9, 0xEC,
54 0x2A, 0x2D, 0x4A, 0x4D, 0x6A, 0x6D, 0x8A, 0x8D, 0xA5, 0xB5, 0xC5, 0xEA, 0xED,
55 0x30, 0x33, 0x50, 0x53, 0x70, 0x73, 0x90, 0x93, 0xA6, 0xB6, 0xC6, 0xF0, 0xF3,
56 0x31, 0x34, 0x51, 0x54, 0x71, 0x74, 0x91, 0x94, 0xA7, 0xB7, 0xC7, 0xF1, 0xF4,
57 0x32, 0x35, 0x52, 0x55, 0x72, 0x75, 0x92, 0x95, 0xA8, 0xB8, 0xC8, 0xF2, 0xF5
60 const unsigned short CxadbmfPlayer::bmf_notes[12] =
62 0x157, 0x16B, 0x181, 0x198, 0x1B0, 0x1CA, 0x1E5, 0x202, 0x220, 0x241, 0x263, 0x287
66 const unsigned short CxadbmfPlayer::bmf_notes_2[12] =
68 0x159, 0x16D, 0x183, 0x19A, 0x1B2, 0x1CC, 0x1E8, 0x205, 0x223, 0x244, 0x267, 0x28B
71 const unsigned char CxadbmfPlayer::bmf_default_instrument[13] =
73 0x01, 0x01, 0x3F, 0x3F, 0x00, 0x00, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00
76 CPlayer *CxadbmfPlayer::factory(Copl *newopl)
78 return new CxadbmfPlayer(newopl);
81 bool CxadbmfPlayer::xadplayer_load()
83 unsigned short ptr = 0;
90 AdPlug_LogWrite("\nbmf_load():\n\n");
92 if (!strncmp((char *)&tune[0],"BMF1.2",6))
97 else if (!strncmp((char *)&tune[0],"BMF1.1",6))
104 bmf.version = BMF0_9B;
108 // copy title & author
109 if (bmf.version > BMF0_9B)
113 strncpy(bmf.title,(char *)&tune[ptr],36);
115 while (tune[ptr]) { ptr++; }
118 strncpy(bmf.author,(char *)&tune[ptr],36);
120 while (tune[ptr]) { ptr++; }
125 strncpy(bmf.title,xad.title,36);
126 strncpy(bmf.author,xad.author,36);
130 if (bmf.version > BMF0_9B)
131 bmf.speed = tune[ptr++];
133 bmf.speed = ((tune[ptr++] << 8) / 3) >> 8; // strange, yeh ?
136 if (bmf.version > BMF0_9B)
138 unsigned long iflags = (tune[ptr] << 24) | (tune[ptr+1] << 16) | (tune[ptr+2] << 8) | tune[ptr+3];
142 if (iflags & (1 << (31-i)))
144 strcpy(bmf.instruments[i].name, (char *)&tune[ptr]);
145 memcpy(bmf.instruments[i].data, &tune[ptr+11], 13);
150 bmf.instruments[i].name[0] = 0;
152 if (bmf.version == BMF1_1)
153 for(int j=0;j<13;j++)
154 bmf.instruments[i].data[j] = bmf_default_instrument[j];
156 for(int j=0;j<13;j++)
157 bmf.instruments[i].data[j] = 0;
166 bmf.instruments[i].name[0] = 0;
167 memcpy(bmf.instruments[tune[ptr]].data, &tune[ptr+2],13); // bug no.1 (no instrument-table-end detection)
173 if (bmf.version > BMF0_9B)
175 unsigned long sflags = (tune[ptr] << 24) | (tune[ptr+1] << 16) | (tune[ptr+2] << 8) | tune[ptr+3];
179 if (sflags & (1 << (31-i)))
180 ptr+=__bmf_convert_stream(&tune[ptr],i);
182 bmf.streams[i][0].cmd = 0xFF;
186 for(i=0;i<tune[5];i++)
187 ptr+=__bmf_convert_stream(&tune[ptr],i);
189 for(i=tune[5];i<9;i++)
190 bmf.streams[i][0].cmd = 0xFF;
196 void CxadbmfPlayer::xadplayer_rewind(int subsong)
202 bmf.channel[i].stream_position = 0;
203 bmf.channel[i].delay = 0;
204 bmf.channel[i].loop_position = 0;
205 bmf.channel[i].loop_counter = 0;
208 plr.speed = bmf.speed;
210 AdPlug_LogWrite("speed: %x\n",plr.speed);
213 bmf.active_streams = 9;
215 // OPL initialization
216 if (bmf.version > BMF0_9B)
218 opl_write(0x01, 0x20);
221 if (bmf.version == BMF1_1)
224 opl_write(bmf_adlib_registers[13*i+j], bmf_default_instrument[j]);
226 else if (bmf.version == BMF1_2)
227 for(i=0x20; i<0x100; i++)
228 opl_write(i,0xFF); // very interesting, really!
233 opl_write(0x08, 0x00);
234 opl_write(0xBD, 0xC0);
237 void CxadbmfPlayer::xadplayer_update()
240 if (bmf.channel[i].stream_position != 0xFFFF)
241 if (bmf.channel[i].delay)
242 bmf.channel[i].delay--;
246 AdPlug_LogWrite("channel %02X:\n", i);
250 // process so-called cross-events
253 memcpy(&event, &bmf.streams[i][bmf.channel[i].stream_position], sizeof(bmf_event));
255 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X\n",
256 event.note,event.delay,event.volume,event.instrument,
257 event.cmd,event.cmd_data);
260 if (event.cmd == 0xFF)
262 bmf.channel[i].stream_position = 0xFFFF;
263 bmf.active_streams--;
266 else if (event.cmd == 0xFE)
268 bmf.channel[i].loop_position = bmf.channel[i].stream_position+1;
269 bmf.channel[i].loop_counter = event.cmd_data;
271 else if (event.cmd == 0xFD)
273 if (bmf.channel[i].loop_counter)
275 bmf.channel[i].stream_position = bmf.channel[i].loop_position-1;
276 bmf.channel[i].loop_counter--;
282 bmf.channel[i].stream_position++;
285 // process normal event
286 unsigned short pos = bmf.channel[i].stream_position;
290 bmf.channel[i].delay = bmf.streams[i][pos].delay;
293 if (bmf.streams[i][pos].cmd)
295 unsigned char cmd = bmf.streams[i][pos].cmd;
297 // 0x01: Set Modulator Volume
300 unsigned char reg = bmf_adlib_registers[13*i+2];
302 opl_write(reg, (adlib[reg] | 0x3F) - bmf.streams[i][pos].cmd_data);
305 else if (cmd == 0x10)
307 plr.speed = bmf.streams[i][pos].cmd_data;
308 plr.speed_counter = plr.speed;
310 } // if (bmf.streams[i][pos].cmd)
313 if (bmf.streams[i][pos].instrument)
315 unsigned char ins = bmf.streams[i][pos].instrument-1;
317 if (bmf.version != BMF1_1)
318 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF);
320 for(int j=0;j<13;j++)
321 opl_write(bmf_adlib_registers[i*13+j], bmf.instruments[ins].data[j]);
322 } // if (bmf.streams[i][pos].instrument)
325 if (bmf.streams[i][pos].volume)
327 unsigned char vol = bmf.streams[i][pos].volume-1;
328 unsigned char reg = bmf_adlib_registers[13*i+3];
330 opl_write(reg, (adlib[reg] | 0x3F) - vol);
331 } // if (bmf.streams[i][pos].volume)
334 if (bmf.streams[i][pos].note)
336 unsigned short note = bmf.streams[i][pos].note;
337 unsigned short freq = 0;
340 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF);
343 if (bmf.version == BMF1_1)
346 freq = bmf_notes_2[--note % 12];
351 freq = bmf_notes[--note % 12];
357 opl_write(0xB0+i, (freq >> 8) | ((note / 12) << 2) | 0x20);
358 opl_write(0xA0+i, freq & 0xFF);
360 } // if (bmf.streams[i][pos].note)
362 bmf.channel[i].stream_position++;
363 } // if (pos != 0xFFFF)
365 } // if (!bmf.channel[i].delay)
368 if (!bmf.active_streams)
371 bmf.channel[j].stream_position = 0;
373 bmf.active_streams = 9;
379 float CxadbmfPlayer::xadplayer_getrefresh()
384 std::string CxadbmfPlayer::xadplayer_gettype()
386 return std::string("xad: BMF Adlib Tracker");
389 std::string CxadbmfPlayer::xadplayer_gettitle()
391 return std::string(bmf.title);
394 std::string CxadbmfPlayer::xadplayer_getauthor()
396 return std::string(bmf.author);
399 unsigned int CxadbmfPlayer::xadplayer_getinstruments()
404 std::string CxadbmfPlayer::xadplayer_getinstrument(unsigned int i)
406 return std::string(bmf.instruments[i].name);
409 /* -------- Internal Functions ---------------------------- */
411 int CxadbmfPlayer::__bmf_convert_stream(unsigned char *stream, int channel)
414 AdPlug_LogWrite("channel %02X (note,delay,volume,instrument,command,command_data):\n",channel);
415 unsigned char *last = stream;
417 unsigned char *stream_start = stream;
423 memset(&bmf.streams[channel][pos], 0, sizeof(bmf_event));
429 // 0xFE -> 0xFF: End of Stream
430 bmf.streams[channel][pos].cmd = 0xFF;
436 else if (*stream == 0xFC)
438 // 0xFC -> 0xFE xx: Save Loop Position
439 bmf.streams[channel][pos].cmd = 0xFE;
440 bmf.streams[channel][pos].cmd_data = (*(stream+1) & ((bmf.version == BMF0_9B) ? 0x7F : 0x3F)) - 1;
444 else if (*stream == 0x7D)
446 // 0x7D -> 0xFD: Loop Saved Position
447 bmf.streams[channel][pos].cmd = 0xFD;
455 if (*(stream+1) & 0x80)
457 if (*(stream+1) & 0x40)
459 // byte0: 1aaaaaaa = NOTE
460 bmf.streams[channel][pos].note = *stream & 0x7F;
461 // byte1: 11bbbbbb = DELAY
462 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F;
463 // byte2: cccccccc = COMMAND
471 // byte0: 1aaaaaaa = NOTE
472 bmf.streams[channel][pos].note = *stream & 0x7F;
473 // byte1: 11bbbbbb = DELAY
474 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F;
477 } // if (*(stream+1) & 0x40)
481 // byte0: 1aaaaaaa = NOTE
482 bmf.streams[channel][pos].note = *stream & 0x7F;
483 // byte1: 0bbbbbbb = COMMAND
488 } // if (*(stream+1) & 0x80)
492 // byte0: 0aaaaaaa = NOTE
493 bmf.streams[channel][pos].note = *stream & 0x7F;
496 } // if (*stream & 0x80)
497 } // if (*stream == 0xFE)
505 if ((0x20 <= *stream) && (*stream <= 0x3F))
507 // 0x20 or higher; 0x3F or lower: Set Instrument
508 bmf.streams[channel][pos].instrument = *stream - 0x20 + 1;
512 else if (0x40 <= *stream)
514 // 0x40 or higher: Set Volume
515 bmf.streams[channel][pos].volume = *stream - 0x40 + 1;
524 if (bmf.version == BMF0_9B)
533 if (bmf.version == BMF1_2)
536 // 0x01: Set Modulator Volume -> 0x01
537 bmf.streams[channel][pos].cmd = 0x01;
538 bmf.streams[channel][pos].cmd_data = *(stream+1);
542 else if (*stream == 0x02)
547 else if (*stream == 0x03)
552 else if (*stream == 0x04)
554 // 0x04: Set Speed -> 0x10
555 bmf.streams[channel][pos].cmd = 0x10;
556 bmf.streams[channel][pos].cmd_data = *(stream+1);
560 else if (*stream == 0x05)
562 // 0x05: Set Carrier Volume (port 380)
563 bmf.streams[channel][pos].volume = *(stream+1) + 1;
567 else if (*stream == 0x06)
569 // 0x06: Set Carrier Volume (port 382)
570 bmf.streams[channel][pos].volume = *(stream+1) + 1;
573 } // if (bmf.version == BMF1_2)
575 } // if ((0x20 <= *stream) && (*stream <= 0x3F))
580 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X <---- ",
581 bmf.streams[channel][pos].note,
582 bmf.streams[channel][pos].delay,
583 bmf.streams[channel][pos].volume,
584 bmf.streams[channel][pos].instrument,
585 bmf.streams[channel][pos].cmd,
586 bmf.streams[channel][pos].cmd_data
588 for(int zz=0;zz<(stream-last);zz++)
589 AdPlug_LogWrite("%02X ",last[zz]);
590 AdPlug_LogWrite("\n");
596 return (stream - stream_start);