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

[svn] Adlib synthesizer (AdPlug) support.
author chainsaw
date Fri, 30 Dec 2005 16:31:39 -0800
parents
children 6ad7eb96dd26
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 * [xad] BMF player, by Riven the Mage <riven@ok.ru>
20 */
21
22 /*
23 - discovery -
24
25 file(s) : GAMESNET.COM
26 type : GamesNet advertising intro
27 tune : by (?)The Brain [Razor 1911]
28 player : ver.0.9b by Hammer
29
30 file(s) : 2FAST4U.COM
31 type : Ford Knox BBStro
32 tune : by The Brain [Razor 1911]
33 player : ver.1.1 by ?
34 comment : in original player at 9th channel the feedback adlib register is not C8 but C6.
35
36 file(s) : DATURA.COM
37 type : Datura BBStro
38 tune : by The Brain [Razor 1911]
39 player : ver.1.2 by ?
40 comment : inaccurate replaying, because constant outport; in original player it can be 380 or 382.
41 */
42
43 #include "bmf.h"
44 #include "debug.h"
45
46 const unsigned char CxadbmfPlayer::bmf_adlib_registers[117] =
47 {
48 0x20, 0x23, 0x40, 0x43, 0x60, 0x63, 0x80, 0x83, 0xA0, 0xB0, 0xC0, 0xE0, 0xE3,
49 0x21, 0x24, 0x41, 0x44, 0x61, 0x64, 0x81, 0x84, 0xA1, 0xB1, 0xC1, 0xE1, 0xE4,
50 0x22, 0x25, 0x42, 0x45, 0x62, 0x65, 0x82, 0x85, 0xA2, 0xB2, 0xC2, 0xE2, 0xE5,
51 0x28, 0x2B, 0x48, 0x4B, 0x68, 0x6B, 0x88, 0x8B, 0xA3, 0xB3, 0xC3, 0xE8, 0xEB,
52 0x29, 0x2C, 0x49, 0x4C, 0x69, 0x6C, 0x89, 0x8C, 0xA4, 0xB4, 0xC4, 0xE9, 0xEC,
53 0x2A, 0x2D, 0x4A, 0x4D, 0x6A, 0x6D, 0x8A, 0x8D, 0xA5, 0xB5, 0xC5, 0xEA, 0xED,
54 0x30, 0x33, 0x50, 0x53, 0x70, 0x73, 0x90, 0x93, 0xA6, 0xB6, 0xC6, 0xF0, 0xF3,
55 0x31, 0x34, 0x51, 0x54, 0x71, 0x74, 0x91, 0x94, 0xA7, 0xB7, 0xC7, 0xF1, 0xF4,
56 0x32, 0x35, 0x52, 0x55, 0x72, 0x75, 0x92, 0x95, 0xA8, 0xB8, 0xC8, 0xF2, 0xF5
57 };
58
59 const unsigned short CxadbmfPlayer::bmf_notes[12] =
60 {
61 0x157, 0x16B, 0x181, 0x198, 0x1B0, 0x1CA, 0x1E5, 0x202, 0x220, 0x241, 0x263, 0x287
62 };
63
64 /* for 1.1 */
65 const unsigned short CxadbmfPlayer::bmf_notes_2[12] =
66 {
67 0x159, 0x16D, 0x183, 0x19A, 0x1B2, 0x1CC, 0x1E8, 0x205, 0x223, 0x244, 0x267, 0x28B
68 };
69
70 const unsigned char CxadbmfPlayer::bmf_default_instrument[13] =
71 {
72 0x01, 0x01, 0x3F, 0x3F, 0x00, 0x00, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00
73 };
74
75 CPlayer *CxadbmfPlayer::factory(Copl *newopl)
76 {
77 return new CxadbmfPlayer(newopl);
78 }
79
80 bool CxadbmfPlayer::xadplayer_load()
81 {
82 unsigned short ptr = 0;
83 int i;
84
85 if(xad.fmt != BMF)
86 return false;
87
88 #ifdef DEBUG
89 AdPlug_LogWrite("\nbmf_load():\n\n");
90 #endif
91 if (!strncmp((char *)&tune[0],"BMF1.2",6))
92 {
93 bmf.version = BMF1_2;
94 bmf.timer = 70.0f;
95 }
96 else if (!strncmp((char *)&tune[0],"BMF1.1",6))
97 {
98 bmf.version = BMF1_1;
99 bmf.timer = 60.0f;
100 }
101 else
102 {
103 bmf.version = BMF0_9B;
104 bmf.timer = 18.2f;
105 }
106
107 // copy title & author
108 if (bmf.version > BMF0_9B)
109 {
110 ptr = 6;
111
112 strncpy(bmf.title,(char *)&tune[ptr],36);
113
114 while (tune[ptr]) { ptr++; }
115 ptr++;
116
117 strncpy(bmf.author,(char *)&tune[ptr],36);
118
119 while (tune[ptr]) { ptr++; }
120 ptr++;
121 }
122 else
123 {
124 strncpy(bmf.title,xad.title,36);
125 strncpy(bmf.author,xad.author,36);
126 }
127
128 // speed
129 if (bmf.version > BMF0_9B)
130 bmf.speed = tune[ptr++];
131 else
132 bmf.speed = ((tune[ptr++] << 8) / 3) >> 8; // strange, yeh ?
133
134 // load instruments
135 if (bmf.version > BMF0_9B)
136 {
137 unsigned long iflags = (tune[ptr] << 24) | (tune[ptr+1] << 16) | (tune[ptr+2] << 8) | tune[ptr+3];
138 ptr+=4;
139
140 for(i=0;i<32;i++)
141 if (iflags & (1 << (31-i)))
142 {
143 strcpy(bmf.instruments[i].name, (char *)&tune[ptr]);
144 memcpy(bmf.instruments[i].data, &tune[ptr+11], 13);
145 ptr += 24;
146 }
147 else
148 {
149 bmf.instruments[i].name[0] = 0;
150
151 if (bmf.version == BMF1_1)
152 for(int j=0;j<13;j++)
153 bmf.instruments[i].data[j] = bmf_default_instrument[j];
154 else
155 for(int j=0;j<13;j++)
156 bmf.instruments[i].data[j] = 0;
157 }
158 }
159 else
160 {
161 ptr = 6;
162
163 for(i=0;i<32;i++)
164 {
165 bmf.instruments[i].name[0] = 0;
166 memcpy(bmf.instruments[tune[ptr]].data, &tune[ptr+2],13); // bug no.1 (no instrument-table-end detection)
167 ptr+=15;
168 }
169 }
170
171 // load streams
172 if (bmf.version > BMF0_9B)
173 {
174 unsigned long sflags = (tune[ptr] << 24) | (tune[ptr+1] << 16) | (tune[ptr+2] << 8) | tune[ptr+3];
175 ptr+=4;
176
177 for(i=0;i<9;i++)
178 if (sflags & (1 << (31-i)))
179 ptr+=__bmf_convert_stream(&tune[ptr],i);
180 else
181 bmf.streams[i][0].cmd = 0xFF;
182 }
183 else
184 {
185 for(i=0;i<tune[5];i++)
186 ptr+=__bmf_convert_stream(&tune[ptr],i);
187
188 for(i=tune[5];i<9;i++)
189 bmf.streams[i][0].cmd = 0xFF;
190 }
191
192 return true;
193 }
194
195 void CxadbmfPlayer::xadplayer_rewind(int subsong)
196 {
197 int i,j;
198
199 for(i=0; i<9; i++)
200 {
201 bmf.channel[i].stream_position = 0;
202 bmf.channel[i].delay = 0;
203 bmf.channel[i].loop_position = 0;
204 bmf.channel[i].loop_counter = 0;
205 }
206
207 plr.speed = bmf.speed;
208 #ifdef DEBUG
209 AdPlug_LogWrite("speed: %x\n",plr.speed);
210 #endif
211
212 bmf.active_streams = 9;
213
214 // OPL initialization
215 if (bmf.version > BMF0_9B)
216 {
217 opl_write(0x01, 0x20);
218
219 /* 1.1 */
220 if (bmf.version == BMF1_1)
221 for(i=0;i<9;i++)
222 for(j=0;j<13;j++)
223 opl_write(bmf_adlib_registers[13*i+j], bmf_default_instrument[j]);
224 /* 1.2 */
225 else if (bmf.version == BMF1_2)
226 for(i=0x20; i<0x100; i++)
227 opl_write(i,0xFF); // very interesting, really!
228 }
229
230 /* ALL */
231
232 opl_write(0x08, 0x00);
233 opl_write(0xBD, 0xC0);
234 }
235
236 void CxadbmfPlayer::xadplayer_update()
237 {
238 for(int i=0;i<9;i++)
239 if (bmf.channel[i].stream_position != 0xFFFF)
240 if (bmf.channel[i].delay)
241 bmf.channel[i].delay--;
242 else
243 {
244 #ifdef DEBUG
245 AdPlug_LogWrite("channel %02X:\n", i);
246 #endif
247 bmf_event event;
248
249 // process so-called cross-events
250 while (true)
251 {
252 memcpy(&event, &bmf.streams[i][bmf.channel[i].stream_position], sizeof(bmf_event));
253 #ifdef DEBUG
254 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X\n",event.note,event.delay,event.volume,event.instrument,event.cmd,event.cmd_data );
255 #endif
256
257 if (event.cmd == 0xFF)
258 {
259 bmf.channel[i].stream_position = 0xFFFF;
260 bmf.active_streams--;
261 break;
262 }
263 else if (event.cmd == 0xFE)
264 {
265 bmf.channel[i].loop_position = bmf.channel[i].stream_position+1;
266 bmf.channel[i].loop_counter = event.cmd_data;
267 }
268 else if (event.cmd == 0xFD)
269 {
270 if (bmf.channel[i].loop_counter)
271 {
272 bmf.channel[i].stream_position = bmf.channel[i].loop_position-1;
273 bmf.channel[i].loop_counter--;
274 }
275 }
276 else
277 break;
278
279 bmf.channel[i].stream_position++;
280 } // while (true)
281
282 // process normal event
283 unsigned short pos = bmf.channel[i].stream_position;
284
285 if (pos != 0xFFFF)
286 {
287 bmf.channel[i].delay = bmf.streams[i][pos].delay;
288
289 // command ?
290 if (bmf.streams[i][pos].cmd)
291 {
292 unsigned char cmd = bmf.streams[i][pos].cmd;
293
294 // 0x01: Set Modulator Volume
295 if (cmd == 0x01)
296 {
297 unsigned char reg = bmf_adlib_registers[13*i+2];
298
299 opl_write(reg, (adlib[reg] | 0x3F) - bmf.streams[i][pos].cmd_data);
300 }
301 // 0x10: Set Speed
302 else if (cmd == 0x10)
303 {
304 plr.speed = bmf.streams[i][pos].cmd_data;
305 plr.speed_counter = plr.speed;
306 }
307 } // if (bmf.streams[i][pos].cmd)
308
309 // instrument ?
310 if (bmf.streams[i][pos].instrument)
311 {
312 unsigned char ins = bmf.streams[i][pos].instrument-1;
313
314 if (bmf.version != BMF1_1)
315 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF);
316
317 for(int j=0;j<13;j++)
318 opl_write(bmf_adlib_registers[i*13+j], bmf.instruments[ins].data[j]);
319 } // if (bmf.streams[i][pos].instrument)
320
321 // volume ?
322 if (bmf.streams[i][pos].volume)
323 {
324 unsigned char vol = bmf.streams[i][pos].volume-1;
325 unsigned char reg = bmf_adlib_registers[13*i+3];
326
327 opl_write(reg, (adlib[reg] | 0x3F) - vol);
328 } // if (bmf.streams[i][pos].volume)
329
330 // note ?
331 if (bmf.streams[i][pos].note)
332 {
333 unsigned short note = bmf.streams[i][pos].note;
334 unsigned short freq = 0;
335
336 // mute channel
337 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF);
338
339 // get frequency
340 if (bmf.version == BMF1_1)
341 {
342 if (note <= 0x60)
343 freq = bmf_notes_2[--note % 12];
344 }
345 else
346 {
347 if (note != 0x7F)
348 freq = bmf_notes[--note % 12];
349 }
350
351 // play note
352 if (freq)
353 {
354 opl_write(0xB0+i, (freq >> 8) | ((note / 12) << 2) | 0x20);
355 opl_write(0xA0+i, freq & 0xFF);
356 }
357 } // if (bmf.streams[i][pos].note)
358
359 bmf.channel[i].stream_position++;
360 } // if (pos != 0xFFFF)
361
362 } // if (!bmf.channel[i].delay)
363
364 // is module loop ?
365 if (!bmf.active_streams)
366 {
367 for(int j=0;j<9;j++)
368 bmf.channel[j].stream_position = 0;
369
370 bmf.active_streams = 9;
371
372 plr.looping = 1;
373 }
374 }
375
376 float CxadbmfPlayer::xadplayer_getrefresh()
377 {
378 return bmf.timer;
379 }
380
381 std::string CxadbmfPlayer::xadplayer_gettype()
382 {
383 return std::string("xad: BMF Adlib Tracker");
384 }
385
386 std::string CxadbmfPlayer::xadplayer_gettitle()
387 {
388 return std::string(bmf.title);
389 }
390
391 std::string CxadbmfPlayer::xadplayer_getauthor()
392 {
393 return std::string(bmf.author);
394 }
395
396 unsigned int CxadbmfPlayer::xadplayer_getinstruments()
397 {
398 return 32;
399 }
400
401 std::string CxadbmfPlayer::xadplayer_getinstrument(unsigned int i)
402 {
403 return std::string(bmf.instruments[i].name);
404 }
405
406 /* -------- Internal Functions ---------------------------- */
407
408 int CxadbmfPlayer::__bmf_convert_stream(unsigned char *stream, int channel)
409 {
410 #ifdef DEBUG
411 AdPlug_LogWrite("channel %02X (note,delay,volume,instrument,command,command_data):\n",channel);
412 unsigned char *last = stream;
413 #endif
414 unsigned char *stream_start = stream;
415
416 int pos = 0;
417
418 while (true)
419 {
420 memset(&bmf.streams[channel][pos], 0, sizeof(bmf_event));
421
422 bool is_cmd = false;
423
424 if (*stream == 0xFE)
425 {
426 // 0xFE -> 0xFF: End of Stream
427 bmf.streams[channel][pos].cmd = 0xFF;
428
429 stream++;
430
431 break;
432 }
433 else if (*stream == 0xFC)
434 {
435 // 0xFC -> 0xFE xx: Save Loop Position
436 bmf.streams[channel][pos].cmd = 0xFE;
437 bmf.streams[channel][pos].cmd_data = (*(stream+1) & ((bmf.version == BMF0_9B) ? 0x7F : 0x3F)) - 1;
438
439 stream+=2;
440 }
441 else if (*stream == 0x7D)
442 {
443 // 0x7D -> 0xFD: Loop Saved Position
444 bmf.streams[channel][pos].cmd = 0xFD;
445
446 stream++;
447 }
448 else
449 {
450 if (*stream & 0x80)
451 {
452 if (*(stream+1) & 0x80)
453 {
454 if (*(stream+1) & 0x40)
455 {
456 // byte0: 1aaaaaaa = NOTE
457 bmf.streams[channel][pos].note = *stream & 0x7F;
458 // byte1: 11bbbbbb = DELAY
459 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F;
460 // byte2: cccccccc = COMMAND
461
462 stream+=2;
463
464 is_cmd = true;
465 }
466 else
467 {
468 // byte0: 1aaaaaaa = NOTE
469 bmf.streams[channel][pos].note = *stream & 0x7F;
470 // byte1: 11bbbbbb = DELAY
471 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F;
472
473 stream+=2;
474 } // if (*(stream+1) & 0x40)
475 }
476 else
477 {
478 // byte0: 1aaaaaaa = NOTE
479 bmf.streams[channel][pos].note = *stream & 0x7F;
480 // byte1: 0bbbbbbb = COMMAND
481
482 stream++;
483
484 is_cmd = true;
485 } // if (*(stream+1) & 0x80)
486 }
487 else
488 {
489 // byte0: 0aaaaaaa = NOTE
490 bmf.streams[channel][pos].note = *stream & 0x7F;
491
492 stream++;
493 } // if (*stream & 0x80)
494 } // if (*stream == 0xFE)
495
496 // is command ?
497 if (is_cmd)
498 {
499
500 /* ALL */
501
502 if ((0x20 <= *stream) && (*stream <= 0x3F))
503 {
504 // 0x20 or higher; 0x3F or lower: Set Instrument
505 bmf.streams[channel][pos].instrument = *stream - 0x20 + 1;
506
507 stream++;
508 }
509 else if (0x40 <= *stream)
510 {
511 // 0x40 or higher: Set Volume
512 bmf.streams[channel][pos].volume = *stream - 0x40 + 1;
513
514 stream++;
515 }
516 else
517 {
518
519 /* 0.9b */
520
521 if (bmf.version == BMF0_9B)
522 if (*stream < 0x20)
523 {
524 // 0x1F or lower: ?
525 stream++;
526 }
527
528 /* 1.2 */
529
530 if (bmf.version == BMF1_2)
531 if (*stream == 0x01)
532 {
533 // 0x01: Set Modulator Volume -> 0x01
534 bmf.streams[channel][pos].cmd = 0x01;
535 bmf.streams[channel][pos].cmd_data = *(stream+1);
536
537 stream+=2;
538 }
539 else if (*stream == 0x02)
540 {
541 // 0x02: ?
542 stream+=2;
543 }
544 else if (*stream == 0x03)
545 {
546 // 0x03: ?
547 stream+=2;
548 }
549 else if (*stream == 0x04)
550 {
551 // 0x04: Set Speed -> 0x10
552 bmf.streams[channel][pos].cmd = 0x10;
553 bmf.streams[channel][pos].cmd_data = *(stream+1);
554
555 stream+=2;
556 }
557 else if (*stream == 0x05)
558 {
559 // 0x05: Set Carrier Volume (port 380)
560 bmf.streams[channel][pos].volume = *(stream+1) + 1;
561
562 stream+=2;
563 }
564 else if (*stream == 0x06)
565 {
566 // 0x06: Set Carrier Volume (port 382)
567 bmf.streams[channel][pos].volume = *(stream+1) + 1;
568
569 stream+=2;
570 } // if (bmf.version == BMF1_2)
571
572 } // if ((0x20 <= *stream) && (*stream <= 0x3F))
573
574 } // if (is_cmd)
575
576 #ifdef DEBUG
577 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X <---- ",
578 bmf.streams[channel][pos].note,
579 bmf.streams[channel][pos].delay,
580 bmf.streams[channel][pos].volume,
581 bmf.streams[channel][pos].instrument,
582 bmf.streams[channel][pos].cmd,
583 bmf.streams[channel][pos].cmd_data
584 );
585 for(int zz=0;zz<(stream-last);zz++)
586 AdPlug_LogWrite("%02X ",last[zz]);
587 AdPlug_LogWrite("\n");
588 last=stream;
589 #endif
590 pos++;
591 } // while (true)
592
593 return (stream - stream_start);
594 }