Mercurial > audlegacy
annotate Plugins/Input/adplug/core/bmf.cpp @ 1594:44f67f556b60 trunk
[svn] - precision in title format is regarded as character count, not byte count.
author | yaz |
---|---|
date | Thu, 24 Aug 2006 23:08:22 -0700 |
parents | 705d4c089fce |
children |
rev | line source |
---|---|
359 | 1 /* |
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats. | |
1280
6ad7eb96dd26
[svn] Sync with upstream. This adds Westwood ADL format support.
chainsaw
parents:
359
diff
changeset
|
3 * Copyright (C) 1999 - 2003, 2006 Simon Peter, <dn.tlp@gmx.net>, et al. |
359 | 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 | |
1459 | 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
359 | 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 | |
1280
6ad7eb96dd26
[svn] Sync with upstream. This adds Westwood ADL format support.
chainsaw
parents:
359
diff
changeset
|
254 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X\n", |
6ad7eb96dd26
[svn] Sync with upstream. This adds Westwood ADL format support.
chainsaw
parents:
359
diff
changeset
|
255 event.note,event.delay,event.volume,event.instrument, |
6ad7eb96dd26
[svn] Sync with upstream. This adds Westwood ADL format support.
chainsaw
parents:
359
diff
changeset
|
256 event.cmd,event.cmd_data); |
359 | 257 #endif |
258 | |
259 if (event.cmd == 0xFF) | |
260 { | |
261 bmf.channel[i].stream_position = 0xFFFF; | |
262 bmf.active_streams--; | |
263 break; | |
264 } | |
265 else if (event.cmd == 0xFE) | |
266 { | |
267 bmf.channel[i].loop_position = bmf.channel[i].stream_position+1; | |
268 bmf.channel[i].loop_counter = event.cmd_data; | |
269 } | |
270 else if (event.cmd == 0xFD) | |
271 { | |
272 if (bmf.channel[i].loop_counter) | |
273 { | |
274 bmf.channel[i].stream_position = bmf.channel[i].loop_position-1; | |
275 bmf.channel[i].loop_counter--; | |
276 } | |
277 } | |
278 else | |
279 break; | |
280 | |
281 bmf.channel[i].stream_position++; | |
282 } // while (true) | |
283 | |
284 // process normal event | |
285 unsigned short pos = bmf.channel[i].stream_position; | |
286 | |
287 if (pos != 0xFFFF) | |
288 { | |
289 bmf.channel[i].delay = bmf.streams[i][pos].delay; | |
290 | |
291 // command ? | |
292 if (bmf.streams[i][pos].cmd) | |
293 { | |
294 unsigned char cmd = bmf.streams[i][pos].cmd; | |
295 | |
296 // 0x01: Set Modulator Volume | |
297 if (cmd == 0x01) | |
298 { | |
299 unsigned char reg = bmf_adlib_registers[13*i+2]; | |
300 | |
301 opl_write(reg, (adlib[reg] | 0x3F) - bmf.streams[i][pos].cmd_data); | |
302 } | |
303 // 0x10: Set Speed | |
304 else if (cmd == 0x10) | |
305 { | |
306 plr.speed = bmf.streams[i][pos].cmd_data; | |
307 plr.speed_counter = plr.speed; | |
308 } | |
309 } // if (bmf.streams[i][pos].cmd) | |
310 | |
311 // instrument ? | |
312 if (bmf.streams[i][pos].instrument) | |
313 { | |
314 unsigned char ins = bmf.streams[i][pos].instrument-1; | |
315 | |
316 if (bmf.version != BMF1_1) | |
317 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF); | |
318 | |
319 for(int j=0;j<13;j++) | |
320 opl_write(bmf_adlib_registers[i*13+j], bmf.instruments[ins].data[j]); | |
321 } // if (bmf.streams[i][pos].instrument) | |
322 | |
323 // volume ? | |
324 if (bmf.streams[i][pos].volume) | |
325 { | |
326 unsigned char vol = bmf.streams[i][pos].volume-1; | |
327 unsigned char reg = bmf_adlib_registers[13*i+3]; | |
328 | |
329 opl_write(reg, (adlib[reg] | 0x3F) - vol); | |
330 } // if (bmf.streams[i][pos].volume) | |
331 | |
332 // note ? | |
333 if (bmf.streams[i][pos].note) | |
334 { | |
335 unsigned short note = bmf.streams[i][pos].note; | |
336 unsigned short freq = 0; | |
337 | |
338 // mute channel | |
339 opl_write(0xB0+i, adlib[0xB0+i] & 0xDF); | |
340 | |
341 // get frequency | |
342 if (bmf.version == BMF1_1) | |
343 { | |
344 if (note <= 0x60) | |
345 freq = bmf_notes_2[--note % 12]; | |
346 } | |
347 else | |
348 { | |
349 if (note != 0x7F) | |
350 freq = bmf_notes[--note % 12]; | |
351 } | |
352 | |
353 // play note | |
354 if (freq) | |
355 { | |
356 opl_write(0xB0+i, (freq >> 8) | ((note / 12) << 2) | 0x20); | |
357 opl_write(0xA0+i, freq & 0xFF); | |
358 } | |
359 } // if (bmf.streams[i][pos].note) | |
360 | |
361 bmf.channel[i].stream_position++; | |
362 } // if (pos != 0xFFFF) | |
363 | |
364 } // if (!bmf.channel[i].delay) | |
365 | |
366 // is module loop ? | |
367 if (!bmf.active_streams) | |
368 { | |
369 for(int j=0;j<9;j++) | |
370 bmf.channel[j].stream_position = 0; | |
371 | |
372 bmf.active_streams = 9; | |
373 | |
374 plr.looping = 1; | |
375 } | |
376 } | |
377 | |
378 float CxadbmfPlayer::xadplayer_getrefresh() | |
379 { | |
380 return bmf.timer; | |
381 } | |
382 | |
383 std::string CxadbmfPlayer::xadplayer_gettype() | |
384 { | |
385 return std::string("xad: BMF Adlib Tracker"); | |
386 } | |
387 | |
388 std::string CxadbmfPlayer::xadplayer_gettitle() | |
389 { | |
390 return std::string(bmf.title); | |
391 } | |
392 | |
393 std::string CxadbmfPlayer::xadplayer_getauthor() | |
394 { | |
395 return std::string(bmf.author); | |
396 } | |
397 | |
398 unsigned int CxadbmfPlayer::xadplayer_getinstruments() | |
399 { | |
400 return 32; | |
401 } | |
402 | |
403 std::string CxadbmfPlayer::xadplayer_getinstrument(unsigned int i) | |
404 { | |
405 return std::string(bmf.instruments[i].name); | |
406 } | |
407 | |
408 /* -------- Internal Functions ---------------------------- */ | |
409 | |
410 int CxadbmfPlayer::__bmf_convert_stream(unsigned char *stream, int channel) | |
411 { | |
412 #ifdef DEBUG | |
413 AdPlug_LogWrite("channel %02X (note,delay,volume,instrument,command,command_data):\n",channel); | |
414 unsigned char *last = stream; | |
415 #endif | |
416 unsigned char *stream_start = stream; | |
417 | |
418 int pos = 0; | |
419 | |
420 while (true) | |
421 { | |
422 memset(&bmf.streams[channel][pos], 0, sizeof(bmf_event)); | |
423 | |
424 bool is_cmd = false; | |
425 | |
426 if (*stream == 0xFE) | |
427 { | |
428 // 0xFE -> 0xFF: End of Stream | |
429 bmf.streams[channel][pos].cmd = 0xFF; | |
430 | |
431 stream++; | |
432 | |
433 break; | |
434 } | |
435 else if (*stream == 0xFC) | |
436 { | |
437 // 0xFC -> 0xFE xx: Save Loop Position | |
438 bmf.streams[channel][pos].cmd = 0xFE; | |
439 bmf.streams[channel][pos].cmd_data = (*(stream+1) & ((bmf.version == BMF0_9B) ? 0x7F : 0x3F)) - 1; | |
440 | |
441 stream+=2; | |
442 } | |
443 else if (*stream == 0x7D) | |
444 { | |
445 // 0x7D -> 0xFD: Loop Saved Position | |
446 bmf.streams[channel][pos].cmd = 0xFD; | |
447 | |
448 stream++; | |
449 } | |
450 else | |
451 { | |
452 if (*stream & 0x80) | |
453 { | |
454 if (*(stream+1) & 0x80) | |
455 { | |
456 if (*(stream+1) & 0x40) | |
457 { | |
458 // byte0: 1aaaaaaa = NOTE | |
459 bmf.streams[channel][pos].note = *stream & 0x7F; | |
460 // byte1: 11bbbbbb = DELAY | |
461 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F; | |
462 // byte2: cccccccc = COMMAND | |
463 | |
464 stream+=2; | |
465 | |
466 is_cmd = true; | |
467 } | |
468 else | |
469 { | |
470 // byte0: 1aaaaaaa = NOTE | |
471 bmf.streams[channel][pos].note = *stream & 0x7F; | |
472 // byte1: 11bbbbbb = DELAY | |
473 bmf.streams[channel][pos].delay = *(stream+1) & 0x3F; | |
474 | |
475 stream+=2; | |
476 } // if (*(stream+1) & 0x40) | |
477 } | |
478 else | |
479 { | |
480 // byte0: 1aaaaaaa = NOTE | |
481 bmf.streams[channel][pos].note = *stream & 0x7F; | |
482 // byte1: 0bbbbbbb = COMMAND | |
483 | |
484 stream++; | |
485 | |
486 is_cmd = true; | |
487 } // if (*(stream+1) & 0x80) | |
488 } | |
489 else | |
490 { | |
491 // byte0: 0aaaaaaa = NOTE | |
492 bmf.streams[channel][pos].note = *stream & 0x7F; | |
493 | |
494 stream++; | |
495 } // if (*stream & 0x80) | |
496 } // if (*stream == 0xFE) | |
497 | |
498 // is command ? | |
499 if (is_cmd) | |
500 { | |
501 | |
502 /* ALL */ | |
503 | |
504 if ((0x20 <= *stream) && (*stream <= 0x3F)) | |
505 { | |
506 // 0x20 or higher; 0x3F or lower: Set Instrument | |
507 bmf.streams[channel][pos].instrument = *stream - 0x20 + 1; | |
508 | |
509 stream++; | |
510 } | |
511 else if (0x40 <= *stream) | |
512 { | |
513 // 0x40 or higher: Set Volume | |
514 bmf.streams[channel][pos].volume = *stream - 0x40 + 1; | |
515 | |
516 stream++; | |
517 } | |
518 else | |
519 { | |
520 | |
521 /* 0.9b */ | |
522 | |
523 if (bmf.version == BMF0_9B) | |
524 if (*stream < 0x20) | |
525 { | |
526 // 0x1F or lower: ? | |
527 stream++; | |
528 } | |
529 | |
530 /* 1.2 */ | |
531 | |
532 if (bmf.version == BMF1_2) | |
533 if (*stream == 0x01) | |
534 { | |
535 // 0x01: Set Modulator Volume -> 0x01 | |
536 bmf.streams[channel][pos].cmd = 0x01; | |
537 bmf.streams[channel][pos].cmd_data = *(stream+1); | |
538 | |
539 stream+=2; | |
540 } | |
541 else if (*stream == 0x02) | |
542 { | |
543 // 0x02: ? | |
544 stream+=2; | |
545 } | |
546 else if (*stream == 0x03) | |
547 { | |
548 // 0x03: ? | |
549 stream+=2; | |
550 } | |
551 else if (*stream == 0x04) | |
552 { | |
553 // 0x04: Set Speed -> 0x10 | |
554 bmf.streams[channel][pos].cmd = 0x10; | |
555 bmf.streams[channel][pos].cmd_data = *(stream+1); | |
556 | |
557 stream+=2; | |
558 } | |
559 else if (*stream == 0x05) | |
560 { | |
561 // 0x05: Set Carrier Volume (port 380) | |
562 bmf.streams[channel][pos].volume = *(stream+1) + 1; | |
563 | |
564 stream+=2; | |
565 } | |
566 else if (*stream == 0x06) | |
567 { | |
568 // 0x06: Set Carrier Volume (port 382) | |
569 bmf.streams[channel][pos].volume = *(stream+1) + 1; | |
570 | |
571 stream+=2; | |
572 } // if (bmf.version == BMF1_2) | |
573 | |
574 } // if ((0x20 <= *stream) && (*stream <= 0x3F)) | |
575 | |
576 } // if (is_cmd) | |
577 | |
578 #ifdef DEBUG | |
579 AdPlug_LogWrite("%02X %02X %02X %02X %02X %02X <---- ", | |
580 bmf.streams[channel][pos].note, | |
581 bmf.streams[channel][pos].delay, | |
582 bmf.streams[channel][pos].volume, | |
583 bmf.streams[channel][pos].instrument, | |
584 bmf.streams[channel][pos].cmd, | |
585 bmf.streams[channel][pos].cmd_data | |
586 ); | |
587 for(int zz=0;zz<(stream-last);zz++) | |
588 AdPlug_LogWrite("%02X ",last[zz]); | |
589 AdPlug_LogWrite("\n"); | |
590 last=stream; | |
591 #endif | |
592 pos++; | |
593 } // while (true) | |
594 | |
595 return (stream - stream_start); | |
596 } |