Mercurial > audlegacy
comparison Plugins/Input/adplug/core/mid.cpp @ 359:8df427a314a8 trunk
[svn] Adlib synthesizer (AdPlug) support.
author | chainsaw |
---|---|
date | Fri, 30 Dec 2005 16:31:39 -0800 |
parents | |
children |
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 * | |
20 * MIDI & MIDI-like file player - Last Update: 8/16/2000 | |
21 * by Phil Hassey - www.imitationpickles.org | |
22 * philhassey@hotmail.com | |
23 * | |
24 * Can play the following | |
25 * .LAA - a raw save of a Lucas Arts Adlib music | |
26 * or | |
27 * a raw save of a LucasFilm Adlib music | |
28 * .MID - a "midi" save of a Lucas Arts Adlib music | |
29 * - or general MIDI files | |
30 * .CMF - Creative Music Format | |
31 * .SCI - the sierra "midi" format. | |
32 * Files must be in the form | |
33 * xxxNAME.sci | |
34 * So that the loader can load the right patch file: | |
35 * xxxPATCH.003 (patch.003 must be saved from the | |
36 * sierra resource from each game.) | |
37 * | |
38 * 6/2/2000: v1.0 relased by phil hassey | |
39 * Status: LAA is almost perfect | |
40 * - some volumes are a bit off (intrument too quiet) | |
41 * MID is fine (who wants to listen to MIDI vid adlib anyway) | |
42 * CMF is okay (still needs the adlib rythm mode implemented | |
43 * for real) | |
44 * 6/6/2000: | |
45 * Status: SCI: there are two SCI formats, orginal and advanced. | |
46 * original: (Found in SCI/EGA Sierra Adventures) | |
47 * played almost perfectly, I believe | |
48 * there is one mistake in the instrument | |
49 * loader that causes some sounds to | |
50 * not be quite right. Most sounds are fine. | |
51 * advanced: (Found in SCI/VGA Sierra Adventures) | |
52 * These are multi-track files. (Thus the | |
53 * player had to be modified to work with | |
54 * them.) This works fine. | |
55 * There are also multiple tunes in each file. | |
56 * I think some of them are supposed to be | |
57 * played at the same time, but I'm not sure | |
58 * when. | |
59 * 8/16/200: | |
60 * Status: LAA: now EGA and VGA lucas games work pretty well | |
61 * | |
62 * Other acknowledgements: | |
63 * Allegro - for the midi instruments and the midi volume table | |
64 * SCUMM Revisited - for getting the .LAA / .MIDs out of those | |
65 * LucasArts files. | |
66 * FreeSCI - for some information on the sci music files | |
67 * SD - the SCI Decoder (to get all .sci out of the Sierra files) | |
68 */ | |
69 | |
70 #include <stdlib.h> | |
71 #include <stdio.h> | |
72 #include <math.h> | |
73 #include <string.h> | |
74 #include "mid.h" | |
75 #include "mididata.h" | |
76 | |
77 /*#define TESTING*/ | |
78 #ifdef TESTING | |
79 #define midiprintf printf | |
80 #else | |
81 void CmidPlayer::midiprintf(char *format, ...) | |
82 { | |
83 } | |
84 #endif | |
85 | |
86 CPlayer *CmidPlayer::factory(Copl *newopl) | |
87 { | |
88 return new CmidPlayer(newopl); | |
89 } | |
90 | |
91 unsigned char CmidPlayer::datalook(long pos) | |
92 { | |
93 if (pos<0 || pos >= flen) return(0); | |
94 return(data[pos]); | |
95 } | |
96 | |
97 unsigned long CmidPlayer::getnexti(unsigned long num) | |
98 { | |
99 unsigned long v=0; | |
100 unsigned long i; | |
101 | |
102 for (i=0; i<num; i++) | |
103 { | |
104 v+=(datalook(pos)<<(8*i)); pos++; | |
105 } | |
106 return(v); | |
107 } | |
108 | |
109 unsigned long CmidPlayer::getnext(unsigned long num) | |
110 { | |
111 unsigned long v=0; | |
112 unsigned long i; | |
113 | |
114 for (i=0; i<num; i++) | |
115 { | |
116 v<<=8; | |
117 v+=datalook(pos); pos++; | |
118 } | |
119 return(v); | |
120 } | |
121 | |
122 unsigned long CmidPlayer::getval() | |
123 { | |
124 int v=0; | |
125 unsigned char b; | |
126 | |
127 b=(unsigned char)getnext(1); | |
128 v=b&0x7f; | |
129 while ((b&0x80) !=0) | |
130 { | |
131 b=(unsigned char)getnext(1); | |
132 v = (v << 7) + (b & 0x7F); | |
133 } | |
134 return(v); | |
135 } | |
136 | |
137 #define LUCAS_STYLE 1 | |
138 #define CMF_STYLE 2 | |
139 #define MIDI_STYLE 4 | |
140 #define SIERRA_STYLE 8 | |
141 | |
142 #define ADLIB_MELODIC 0 | |
143 #define ADLIB_RYTHM 1 | |
144 | |
145 bool CmidPlayer::load_sierra_ins(const std::string &fname, const CFileProvider &fp) | |
146 { | |
147 long i,j,k,l; | |
148 unsigned char ins[28]; | |
149 char *pfilename; | |
150 binistream *f; | |
151 | |
152 pfilename = (char *)malloc(fname.length()+9); | |
153 strcpy(pfilename,fname.c_str()); | |
154 j=0; | |
155 for(i=strlen(pfilename)-1; i >= 0; i--) | |
156 if(pfilename[i] == '/' || pfilename[i] == '\\') { | |
157 j = i+1; | |
158 break; | |
159 } | |
160 sprintf(pfilename+j+3,"patch.003"); | |
161 | |
162 f = fp.open(pfilename); | |
163 free(pfilename); | |
164 if(!f) return false; | |
165 | |
166 f->ignore(2); | |
167 stins = 0; | |
168 for (i=0; i<2; i++) | |
169 { | |
170 for (k=0; k<48; k++) | |
171 { | |
172 l=i*48+k; | |
173 midiprintf ("\n%2d: ",l); | |
174 for (j=0; j<28; j++) | |
175 ins[j] = f->readInt(1); | |
176 | |
177 myinsbank[l][0]= | |
178 (ins[9]*0x80) + (ins[10]*0x40) + | |
179 (ins[5]*0x20) + (ins[11]*0x10) + | |
180 ins[1]; //1=ins5 | |
181 myinsbank[l][1]= | |
182 (ins[22]*0x80) + (ins[23]*0x40) + | |
183 (ins[18]*0x20) + (ins[24]*0x10) + | |
184 ins[14]; //1=ins18 | |
185 | |
186 myinsbank[l][2]=(ins[0]<<6)+ins[8]; | |
187 myinsbank[l][3]=(ins[13]<<6)+ins[21]; | |
188 | |
189 myinsbank[l][4]=(ins[3]<<4)+ins[6]; | |
190 myinsbank[l][5]=(ins[16]<<4)+ins[19]; | |
191 myinsbank[l][6]=(ins[4]<<4)+ins[7]; | |
192 myinsbank[l][7]=(ins[17]<<4)+ins[20]; | |
193 | |
194 myinsbank[l][8]=ins[26]; | |
195 myinsbank[l][9]=ins[27]; | |
196 | |
197 myinsbank[l][10]=((ins[2]<<1))+(1-(ins[12]&1)); | |
198 //(ins[12] ? 0:1)+((ins[2]<<1)); | |
199 | |
200 for (j=0; j<11; j++) | |
201 midiprintf ("%02X ",myinsbank[l][j]); | |
202 stins++; | |
203 } | |
204 f->ignore(2); | |
205 } | |
206 | |
207 fp.close(f); | |
208 memcpy(smyinsbank, myinsbank, 128 * 16); | |
209 return true; | |
210 } | |
211 | |
212 void CmidPlayer::sierra_next_section() | |
213 { | |
214 int i,j; | |
215 | |
216 for (i=0; i<16; i++) | |
217 track[i].on=0; | |
218 | |
219 midiprintf("\n\nnext adv sierra section:\n"); | |
220 | |
221 pos=sierra_pos; | |
222 i=0;j=0; | |
223 while (i!=0xff) | |
224 { | |
225 getnext(1); | |
226 curtrack=j; j++; | |
227 track[curtrack].on=1; | |
228 track[curtrack].spos = getnext(1); | |
229 track[curtrack].spos += (getnext(1) << 8) + 4; //4 best usually +3? not 0,1,2 or 5 | |
230 // track[curtrack].spos=getnext(1)+(getnext(1)<<8)+4; // dynamite!: doesn't optimize correctly!! | |
231 track[curtrack].tend=flen; //0xFC will kill it | |
232 track[curtrack].iwait=0; | |
233 track[curtrack].pv=0; | |
234 midiprintf ("track %d starts at %lx\n",curtrack,track[curtrack].spos); | |
235 | |
236 getnext(2); | |
237 i=getnext(1); | |
238 } | |
239 getnext(2); | |
240 deltas=0x20; | |
241 sierra_pos=pos; | |
242 //getch(); | |
243 | |
244 fwait=0; | |
245 doing=1; | |
246 } | |
247 | |
248 #define FILE_LUCAS 1 | |
249 #define FILE_MIDI 2 | |
250 #define FILE_CMF 3 | |
251 #define FILE_SIERRA 4 | |
252 #define FILE_ADVSIERRA 5 | |
253 #define FILE_OLDLUCAS 6 | |
254 | |
255 bool CmidPlayer::load(const std::string &filename, const CFileProvider &fp) | |
256 { | |
257 binistream *f = fp.open(filename); if(!f) return false; | |
258 int good; | |
259 unsigned char s[6]; | |
260 | |
261 f->readString((char *)s, 6); | |
262 good=0; | |
263 subsongs=0; | |
264 switch(s[0]) | |
265 { | |
266 case 'A': | |
267 if (s[1]=='D' && s[2]=='L') good=FILE_LUCAS; | |
268 break; | |
269 case 'M': | |
270 if (s[1]=='T' && s[2]=='h' && s[3]=='d') good=FILE_MIDI; | |
271 break; | |
272 case 'C': | |
273 if (s[1]=='T' && s[2]=='M' && s[3]=='F') good=FILE_CMF; | |
274 break; | |
275 case 0x84: | |
276 if (s[1]==0x00 && load_sierra_ins(filename, fp)) | |
277 if (s[2]==0xf0) | |
278 good=FILE_ADVSIERRA; | |
279 else | |
280 good=FILE_SIERRA; | |
281 break; | |
282 default: | |
283 if (s[4]=='A' && s[5]=='D') good=FILE_OLDLUCAS; | |
284 break; | |
285 } | |
286 | |
287 if (good!=0) | |
288 subsongs=1; | |
289 else { | |
290 fp.close(f); | |
291 return false; | |
292 } | |
293 | |
294 type=good; | |
295 f->seek(0); | |
296 flen = fp.filesize(f); | |
297 data = new unsigned char [flen]; | |
298 f->readString((char *)data, flen); | |
299 | |
300 fp.close(f); | |
301 rewind(0); | |
302 return true; | |
303 } | |
304 | |
305 void CmidPlayer::midi_write_adlib(unsigned int r, unsigned char v) | |
306 { | |
307 opl->write(r,v); | |
308 adlib_data[r]=v; | |
309 } | |
310 | |
311 unsigned char adlib_opadd[] = {0x00 ,0x01 ,0x02 ,0x08 ,0x09 ,0x0A ,0x10 ,0x11 ,0x12}; | |
312 int ops[] = {0x20,0x20,0x40,0x40,0x60,0x60,0x80,0x80,0xe0,0xe0,0xc0}; | |
313 | |
314 void CmidPlayer::midi_fm_instrument(int voice, unsigned char *inst) | |
315 { | |
316 if ((adlib_style&SIERRA_STYLE)!=0) | |
317 midi_write_adlib(0xbd,0); //just gotta make sure this happens.. | |
318 //'cause who knows when it'll be | |
319 //reset otherwise. | |
320 | |
321 | |
322 midi_write_adlib(0x20+adlib_opadd[voice],inst[0]); | |
323 midi_write_adlib(0x23+adlib_opadd[voice],inst[1]); | |
324 | |
325 if ((adlib_style&LUCAS_STYLE)!=0) | |
326 { | |
327 midi_write_adlib(0x43+adlib_opadd[voice],0x3f); | |
328 if ((inst[10] & 1)==0) | |
329 midi_write_adlib(0x40+adlib_opadd[voice],inst[2]); | |
330 else | |
331 midi_write_adlib(0x40+adlib_opadd[voice],0x3f); | |
332 } | |
333 else | |
334 { | |
335 if ((adlib_style&SIERRA_STYLE)!=0) | |
336 { | |
337 midi_write_adlib(0x40+adlib_opadd[voice],inst[2]); | |
338 midi_write_adlib(0x43+adlib_opadd[voice],inst[3]); | |
339 } | |
340 else | |
341 { | |
342 midi_write_adlib(0x40+adlib_opadd[voice],inst[2]); | |
343 if ((inst[10] & 1)==0) | |
344 midi_write_adlib(0x43+adlib_opadd[voice],inst[3]); | |
345 else | |
346 midi_write_adlib(0x43+adlib_opadd[voice],0); | |
347 } | |
348 } | |
349 | |
350 midi_write_adlib(0x60+adlib_opadd[voice],inst[4]); | |
351 midi_write_adlib(0x63+adlib_opadd[voice],inst[5]); | |
352 midi_write_adlib(0x80+adlib_opadd[voice],inst[6]); | |
353 midi_write_adlib(0x83+adlib_opadd[voice],inst[7]); | |
354 midi_write_adlib(0xe0+adlib_opadd[voice],inst[8]); | |
355 midi_write_adlib(0xe3+adlib_opadd[voice],inst[9]); | |
356 | |
357 midi_write_adlib(0xc0+voice,inst[10]); | |
358 } | |
359 | |
360 void CmidPlayer::midi_fm_volume(int voice, int volume) | |
361 { | |
362 int vol; | |
363 | |
364 if ((adlib_style&SIERRA_STYLE)==0) //sierra likes it loud! | |
365 { | |
366 vol=volume>>2; | |
367 | |
368 if ((adlib_style&LUCAS_STYLE)!=0) | |
369 { | |
370 if ((adlib_data[0xc0+voice]&1)==1) | |
371 midi_write_adlib(0x40+adlib_opadd[voice], (unsigned | |
372 char)((63-vol) | | |
373 (adlib_data[0x40+adlib_opadd[voice]]&0xc0))); | |
374 midi_write_adlib(0x43+adlib_opadd[voice], (unsigned char)((63-vol) | | |
375 (adlib_data[0x43+adlib_opadd[voice]]&0xc0))); | |
376 } | |
377 else | |
378 { | |
379 if ((adlib_data[0xc0+voice]&1)==1) | |
380 midi_write_adlib(0x40+adlib_opadd[voice], (unsigned | |
381 char)((63-vol) | | |
382 (adlib_data[0x40+adlib_opadd[voice]]&0xc0))); | |
383 midi_write_adlib(0x43+adlib_opadd[voice], (unsigned char)((63-vol) | | |
384 (adlib_data[0x43+adlib_opadd[voice]]&0xc0))); | |
385 } | |
386 } | |
387 } | |
388 | |
389 | |
390 int fnums[] = { | |
391 0x16b,0x181,0x198,0x1b0,0x1ca,0x1e5,0x202,0x220,0x241,0x263,0x287,0x2ae | |
392 }; | |
393 | |
394 void CmidPlayer::midi_fm_playnote(int voice, int note, int volume) | |
395 { | |
396 int freq=fnums[note%12]; | |
397 int oct=note/12; | |
398 int c; | |
399 | |
400 midi_fm_volume(voice,volume); | |
401 midi_write_adlib(0xa0+voice,(unsigned char)(freq&0xff)); | |
402 | |
403 c=((freq&0x300) >> 8)+(oct<<2) + (1<<5); | |
404 midi_write_adlib(0xb0+voice,(unsigned char)c); | |
405 } | |
406 | |
407 void CmidPlayer::midi_fm_endnote(int voice) | |
408 { | |
409 //midi_fm_volume(voice,0); | |
410 //midi_write_adlib(0xb0+voice,0); | |
411 | |
412 midi_write_adlib(0xb0+voice,(unsigned char)(adlib_data[0xb0+voice]&(255-32))); | |
413 } | |
414 | |
415 void CmidPlayer::midi_fm_reset() | |
416 { | |
417 int i; | |
418 for (i=0; i<256; i++) | |
419 midi_write_adlib(i,0); | |
420 | |
421 midi_write_adlib(0x01, 0x20); | |
422 midi_write_adlib(0xBD,0xc0); | |
423 } | |
424 | |
425 bool CmidPlayer::update() | |
426 { | |
427 long w,v,note,vel,ctrl,nv,x,l,lnum; | |
428 int i=0,j,c; | |
429 int on,onl; | |
430 unsigned char ins[11]; | |
431 int ret; | |
432 | |
433 if (doing == 1) | |
434 { | |
435 // just get the first wait and ignore it :> | |
436 for (curtrack=0; curtrack<16; curtrack++) | |
437 if (track[curtrack].on) | |
438 { | |
439 pos=track[curtrack].pos; | |
440 if (type != FILE_SIERRA && type !=FILE_ADVSIERRA) | |
441 track[curtrack].iwait+=getval(); | |
442 else | |
443 track[curtrack].iwait+=getnext(1); | |
444 track[curtrack].pos=pos; | |
445 } | |
446 doing=0; | |
447 } | |
448 | |
449 iwait=0; | |
450 ret=1; | |
451 | |
452 while (iwait==0 && ret==1) | |
453 { | |
454 for (curtrack=0; curtrack<16; curtrack++) | |
455 if (track[curtrack].on && track[curtrack].iwait==0 && | |
456 track[curtrack].pos < track[curtrack].tend) | |
457 { | |
458 pos=track[curtrack].pos; | |
459 | |
460 v=getnext(1); | |
461 | |
462 // This is to do implied MIDI events. | |
463 if (v<0x80) {v=track[curtrack].pv; pos--;} | |
464 track[curtrack].pv=(unsigned char)v; | |
465 | |
466 c=v&0x0f; | |
467 midiprintf ("[%2X]",v); | |
468 switch(v&0xf0) | |
469 { | |
470 case 0x80: /*note off*/ | |
471 note=getnext(1); vel=getnext(1); | |
472 for (i=0; i<9; i++) | |
473 if (chp[i][0]==c && chp[i][1]==note) | |
474 { | |
475 midi_fm_endnote(i); | |
476 chp[i][0]=-1; | |
477 } | |
478 break; | |
479 case 0x90: /*note on*/ | |
480 // doing=0; | |
481 note=getnext(1); vel=getnext(1); | |
482 | |
483 if (ch[c].on!=0) | |
484 { | |
485 for (i=0; i<9; i++) | |
486 chp[i][2]++; | |
487 | |
488 j=0; | |
489 on=-1;onl=0; | |
490 for (i=0; i<9; i++) | |
491 if (chp[i][0]==-1 && chp[i][2]>onl) | |
492 { onl=chp[i][2]; on=i; j=1; } | |
493 | |
494 if (on==-1) | |
495 { | |
496 onl=0; | |
497 for (i=0; i<9; i++) | |
498 if (chp[i][2]>onl) | |
499 { onl=chp[i][2]; on=i; } | |
500 } | |
501 | |
502 if (j==0) | |
503 midi_fm_endnote(on); | |
504 | |
505 if (vel!=0 && ch[c].inum>=0 && ch[c].inum<128) | |
506 { | |
507 if (adlib_mode == ADLIB_MELODIC || c < 12) | |
508 midi_fm_instrument(on,ch[c].ins); | |
509 else | |
510 { | |
511 //the following fails to be effective | |
512 //at doing rythm sounds .. (cmf blah) | |
513 ins[0]=ins[1]=ch[c].ins[0]; | |
514 ins[2]=ins[3]=ch[c].ins[2]; | |
515 ins[4]=ins[5]=ch[c].ins[4]; | |
516 ins[6]=ins[7]=ch[c].ins[6]; | |
517 ins[8]=ins[9]=ch[c].ins[8]; | |
518 ins[10]=ch[c].ins[10]|1; | |
519 midi_fm_instrument(on,ins); | |
520 } | |
521 | |
522 if ((adlib_style&MIDI_STYLE)!=0) | |
523 { | |
524 nv=((ch[c].vol*vel)/128); | |
525 if ((adlib_style&LUCAS_STYLE)!=0) | |
526 nv*=2; | |
527 if (nv>127) nv=127; | |
528 nv=my_midi_fm_vol_table[nv]; | |
529 if ((adlib_style&LUCAS_STYLE)!=0) | |
530 nv=(int)((float)sqrt((float)nv)*11); | |
531 } | |
532 else | |
533 { | |
534 nv=vel; | |
535 } | |
536 | |
537 midi_fm_playnote(on,note+ch[c].nshift,nv*2); | |
538 chp[on][0]=c; | |
539 chp[on][1]=note; | |
540 chp[on][2]=0; | |
541 } | |
542 else | |
543 { | |
544 if (vel==0) //same code as end note | |
545 { | |
546 for (i=0; i<9; i++) | |
547 if (chp[i][0]==c && chp[i][1]==note) | |
548 { | |
549 // midi_fm_volume(i,0); // really end the note | |
550 midi_fm_endnote(i); | |
551 chp[i][0]=-1; | |
552 } | |
553 } | |
554 else | |
555 { // i forget what this is for. | |
556 chp[on][0]=-1; | |
557 chp[on][2]=0; | |
558 } | |
559 } | |
560 midiprintf(" [%d:%d:%d:%d]\n",c,ch[c].inum,note,vel); | |
561 } | |
562 else | |
563 midiprintf ("off"); | |
564 break; | |
565 case 0xa0: /*key after touch */ | |
566 note=getnext(1); vel=getnext(1); | |
567 /* //this might all be good | |
568 for (i=0; i<9; i++) | |
569 if (chp[i][0]==c & chp[i][1]==note) | |
570 | |
571 midi_fm_playnote(i,note+cnote[c],my_midi_fm_vol_table[(cvols[c]*vel)/128]*2); | |
572 */ | |
573 break; | |
574 case 0xb0: /*control change .. pitch bend? */ | |
575 ctrl=getnext(1); vel=getnext(1); | |
576 | |
577 switch(ctrl) | |
578 { | |
579 case 0x07: | |
580 midiprintf ("(pb:%d: %d %d)",c,ctrl,vel); | |
581 ch[c].vol=vel; | |
582 midiprintf("vol"); | |
583 break; | |
584 case 0x67: | |
585 midiprintf ("\n\nhere:%d\n\n",vel); | |
586 if ((adlib_style&CMF_STYLE)!=0) | |
587 adlib_mode=vel; // this gets faked - no mode change | |
588 break; | |
589 } | |
590 break; | |
591 case 0xc0: /*patch change*/ | |
592 x=getnext(1); | |
593 ch[c].inum=x; | |
594 for (j=0; j<11; j++) | |
595 ch[c].ins[j]=myinsbank[ch[c].inum][j]; | |
596 break; | |
597 case 0xd0: /*chanel touch*/ | |
598 x=getnext(1); | |
599 break; | |
600 case 0xe0: /*pitch wheel*/ | |
601 x=getnext(1); | |
602 x=getnext(1); | |
603 break; | |
604 case 0xf0: | |
605 switch(v) | |
606 { | |
607 case 0xf0: | |
608 case 0xf7: /*sysex*/ | |
609 l=getval(); | |
610 if (datalook(pos+l)==0xf7) | |
611 i=1; | |
612 midiprintf("{%d}",l); | |
613 midiprintf("\n"); | |
614 | |
615 if (datalook(pos)==0x7d && | |
616 datalook(pos+1)==0x10 && | |
617 datalook(pos+2)<16) | |
618 { | |
619 adlib_style=LUCAS_STYLE|MIDI_STYLE; | |
620 for (i=0; i<l; i++) | |
621 { | |
622 midiprintf ("%x ",datalook(pos+i)); | |
623 if ((i-3)%10 == 0) midiprintf("\n"); | |
624 } | |
625 midiprintf ("\n"); | |
626 getnext(1); | |
627 getnext(1); | |
628 c=getnext(1); | |
629 getnext(1); | |
630 | |
631 // getnext(22); //temp | |
632 ch[c].ins[0]=(unsigned char)((getnext(1)<<4)+getnext(1)); | |
633 ch[c].ins[2]=(unsigned char)(0xff-(((getnext(1)<<4)+getnext(1))&0x3f)); | |
634 ch[c].ins[4]=(unsigned char)(0xff-((getnext(1)<<4)+getnext(1))); | |
635 ch[c].ins[6]=(unsigned char)(0xff-((getnext(1)<<4)+getnext(1))); | |
636 ch[c].ins[8]=(unsigned char)((getnext(1)<<4)+getnext(1)); | |
637 | |
638 ch[c].ins[1]=(unsigned char)((getnext(1)<<4)+getnext(1)); | |
639 ch[c].ins[3]=(unsigned char)(0xff-(((getnext(1)<<4)+getnext(1))&0x3f)); | |
640 ch[c].ins[5]=(unsigned char)(0xff-((getnext(1)<<4)+getnext(1))); | |
641 ch[c].ins[7]=(unsigned char)(0xff-((getnext(1)<<4)+getnext(1))); | |
642 ch[c].ins[9]=(unsigned char)((getnext(1)<<4)+getnext(1)); | |
643 | |
644 i=(getnext(1)<<4)+getnext(1); | |
645 ch[c].ins[10]=i; | |
646 | |
647 //if ((i&1)==1) ch[c].ins[10]=1; | |
648 | |
649 midiprintf ("\n%d: ",c); | |
650 for (i=0; i<11; i++) | |
651 midiprintf ("%2X ",ch[c].ins[i]); | |
652 getnext(l-26); | |
653 } | |
654 else | |
655 { | |
656 midiprintf("\n"); | |
657 for (j=0; j<l; j++) | |
658 midiprintf ("%2X ",getnext(1)); | |
659 } | |
660 | |
661 midiprintf("\n"); | |
662 if(i==1) | |
663 getnext(1); | |
664 break; | |
665 case 0xf1: | |
666 break; | |
667 case 0xf2: | |
668 getnext(2); | |
669 break; | |
670 case 0xf3: | |
671 getnext(1); | |
672 break; | |
673 case 0xf4: | |
674 break; | |
675 case 0xf5: | |
676 break; | |
677 case 0xf6: /*something*/ | |
678 case 0xf8: | |
679 case 0xfa: | |
680 case 0xfb: | |
681 case 0xfc: | |
682 //this ends the track for sierra. | |
683 if (type == FILE_SIERRA || | |
684 type == FILE_ADVSIERRA) | |
685 { | |
686 track[curtrack].tend=pos; | |
687 midiprintf ("endmark: %ld -- %lx\n",pos,pos); | |
688 } | |
689 break; | |
690 case 0xfe: | |
691 break; | |
692 case 0xfd: | |
693 break; | |
694 case 0xff: | |
695 v=getnext(1); | |
696 l=getval(); | |
697 midiprintf ("\n"); | |
698 midiprintf("{%X_%X}",v,l); | |
699 if (v==0x51) | |
700 { | |
701 lnum=getnext(l); | |
702 msqtr=lnum; /*set tempo*/ | |
703 midiprintf ("(qtr=%ld)",msqtr); | |
704 } | |
705 else | |
706 { | |
707 for (i=0; i<l; i++) | |
708 midiprintf ("%2X ",getnext(1)); | |
709 } | |
710 break; | |
711 } | |
712 break; | |
713 default: midiprintf("!",v); /* if we get down here, a error occurred */ | |
714 break; | |
715 } | |
716 | |
717 if (pos < track[curtrack].tend) | |
718 { | |
719 if (type != FILE_SIERRA && type !=FILE_ADVSIERRA) | |
720 w=getval(); | |
721 else | |
722 w=getnext(1); | |
723 track[curtrack].iwait=w; | |
724 /* | |
725 if (w!=0) | |
726 { | |
727 midiprintf("\n<%d>",w); | |
728 f = | |
729 ((float)w/(float)deltas)*((float)msqtr/(float)1000000); | |
730 if (doing==1) f=0; //not playing yet. don't wait yet | |
731 } | |
732 */ | |
733 } | |
734 else | |
735 track[curtrack].iwait=0; | |
736 | |
737 track[curtrack].pos=pos; | |
738 } | |
739 | |
740 | |
741 ret=0; //end of song. | |
742 iwait=0; | |
743 for (curtrack=0; curtrack<16; curtrack++) | |
744 if (track[curtrack].on == 1 && | |
745 track[curtrack].pos < track[curtrack].tend) | |
746 ret=1; //not yet.. | |
747 | |
748 if (ret==1) | |
749 { | |
750 iwait=0xffffff; // bigger than any wait can be! | |
751 for (curtrack=0; curtrack<16; curtrack++) | |
752 if (track[curtrack].on == 1 && | |
753 track[curtrack].pos < track[curtrack].tend && | |
754 track[curtrack].iwait < iwait) | |
755 iwait=track[curtrack].iwait; | |
756 } | |
757 } | |
758 | |
759 | |
760 if (iwait !=0 && ret==1) | |
761 { | |
762 for (curtrack=0; curtrack<16; curtrack++) | |
763 if (track[curtrack].on) | |
764 track[curtrack].iwait-=iwait; | |
765 | |
766 | |
767 fwait=1.0f/(((float)iwait/(float)deltas)*((float)msqtr/(float)1000000)); | |
768 } | |
769 else | |
770 fwait=50; // 1/50th of a second | |
771 | |
772 midiprintf ("\n"); | |
773 for (i=0; i<16; i++) | |
774 if (track[i].on) | |
775 if (track[i].pos < track[i].tend) | |
776 midiprintf ("<%d>",track[i].iwait); | |
777 else | |
778 midiprintf("stop"); | |
779 | |
780 /* | |
781 if (ret==0 && type==FILE_ADVSIERRA) | |
782 if (datalook(sierra_pos-2)!=0xff) | |
783 { | |
784 midiprintf ("next sectoin!"); | |
785 sierra_next_section(p); | |
786 fwait=50; | |
787 ret=1; | |
788 } | |
789 */ | |
790 | |
791 if(ret) | |
792 return true; | |
793 else | |
794 return false; | |
795 } | |
796 | |
797 float CmidPlayer::getrefresh() | |
798 { | |
799 return (fwait > 0.01f ? fwait : 0.01f); | |
800 } | |
801 | |
802 void CmidPlayer::rewind(int subsong) | |
803 { | |
804 long i,j,n,m,l; | |
805 long o_sierra_pos; | |
806 unsigned char ins[16]; | |
807 | |
808 pos=0; tins=0; | |
809 adlib_style=MIDI_STYLE|CMF_STYLE; | |
810 adlib_mode=ADLIB_MELODIC; | |
811 for (i=0; i<128; i++) | |
812 for (j=0; j<16; j++) | |
813 myinsbank[i][j]=midi_fm_instruments[i][j]; | |
814 for (i=0; i<16; i++) | |
815 { | |
816 ch[i].inum=0; | |
817 for (j=0; j<11; j++) | |
818 ch[i].ins[j]=myinsbank[ch[i].inum][j]; | |
819 ch[i].vol=127; | |
820 ch[i].nshift=-25; | |
821 ch[i].on=1; | |
822 } | |
823 | |
824 /* General init */ | |
825 for (i=0; i<9; i++) | |
826 { | |
827 chp[i][0]=-1; | |
828 chp[i][2]=0; | |
829 } | |
830 | |
831 deltas=250; // just a number, not a standard | |
832 msqtr=500000; | |
833 fwait=123; // gotta be a small thing.. sorta like nothing | |
834 iwait=0; | |
835 | |
836 subsongs=1; | |
837 | |
838 for (i=0; i<16; i++) | |
839 { | |
840 track[i].tend=0; | |
841 track[i].spos=0; | |
842 track[i].pos=0; | |
843 track[i].iwait=0; | |
844 track[i].on=0; | |
845 track[i].pv=0; | |
846 } | |
847 curtrack=0; | |
848 | |
849 /* specific to file-type init */ | |
850 | |
851 pos=0; | |
852 i=getnext(1); | |
853 switch(type) | |
854 { | |
855 case FILE_LUCAS: | |
856 getnext(24); //skip junk and get to the midi. | |
857 adlib_style=LUCAS_STYLE|MIDI_STYLE; | |
858 //note: no break, we go right into midi headers... | |
859 case FILE_MIDI: | |
860 if (type != FILE_LUCAS) | |
861 tins=128; | |
862 getnext(11); /*skip header*/ | |
863 deltas=getnext(2); | |
864 midiprintf ("deltas:%ld\n",deltas); | |
865 getnext(4); | |
866 | |
867 curtrack=0; | |
868 track[curtrack].on=1; | |
869 track[curtrack].tend=getnext(4); | |
870 track[curtrack].spos=pos; | |
871 midiprintf ("tracklen:%ld\n",track[curtrack].tend); | |
872 break; | |
873 case FILE_CMF: | |
874 getnext(3); // ctmf | |
875 getnexti(2); //version | |
876 n=getnexti(2); // instrument offset | |
877 m=getnexti(2); // music offset | |
878 deltas=getnexti(2); //ticks/qtr note | |
879 msqtr=1000000/getnexti(2)*deltas; | |
880 //the stuff in the cmf is click ticks per second.. | |
881 | |
882 i=getnexti(2); | |
883 if(i) title = (char *)data+i; | |
884 i=getnexti(2); | |
885 if(i) author = (char *)data+i; | |
886 i=getnexti(2); | |
887 if(i) remarks = (char *)data+i; | |
888 | |
889 getnext(16); // channel in use table .. | |
890 i=getnexti(2); // num instr | |
891 if (i>128) i=128; // to ward of bad numbers... | |
892 getnexti(2); //basic tempo | |
893 | |
894 midiprintf("\nioff:%d\nmoff%d\ndeltas:%ld\nmsqtr:%ld\nnumi:%d\n", | |
895 n,m,deltas,msqtr,i); | |
896 pos=n; // jump to instruments | |
897 tins=i; | |
898 for (j=0; j<i; j++) | |
899 { | |
900 midiprintf ("\n%d: ",j); | |
901 for (l=0; l<16; l++) | |
902 { | |
903 myinsbank[j][l]=(unsigned char)getnext(1); | |
904 midiprintf ("%2X ",myinsbank[j][l]); | |
905 } | |
906 } | |
907 | |
908 for (i=0; i<16; i++) | |
909 ch[i].nshift=-13; | |
910 | |
911 adlib_style=CMF_STYLE; | |
912 | |
913 curtrack=0; | |
914 track[curtrack].on=1; | |
915 track[curtrack].tend=flen; // music until the end of the file | |
916 track[curtrack].spos=m; //jump to midi music | |
917 break; | |
918 case FILE_OLDLUCAS: | |
919 msqtr=250000; | |
920 pos=9; | |
921 deltas=getnext(1); | |
922 | |
923 i=8; | |
924 pos=0x19; // jump to instruments | |
925 tins=i; | |
926 for (j=0; j<i; j++) | |
927 { | |
928 midiprintf ("\n%d: ",j); | |
929 for (l=0; l<16; l++) | |
930 ins[l]=(unsigned char)getnext(1); | |
931 | |
932 myinsbank[j][10]=ins[2]; | |
933 myinsbank[j][0]=ins[3]; | |
934 myinsbank[j][2]=ins[4]; | |
935 myinsbank[j][4]=ins[5]; | |
936 myinsbank[j][6]=ins[6]; | |
937 myinsbank[j][8]=ins[7]; | |
938 myinsbank[j][1]=ins[8]; | |
939 myinsbank[j][3]=ins[9]; | |
940 myinsbank[j][5]=ins[10]; | |
941 myinsbank[j][7]=ins[11]; | |
942 myinsbank[j][9]=ins[12]; | |
943 | |
944 for (l=0; l<11; l++) | |
945 midiprintf ("%2X ",myinsbank[j][l]); | |
946 } | |
947 | |
948 for (i=0; i<16; i++) | |
949 { | |
950 if (i<tins) | |
951 { | |
952 ch[i].inum=i; | |
953 for (j=0; j<11; j++) | |
954 ch[i].ins[j]=myinsbank[ch[i].inum][j]; | |
955 } | |
956 } | |
957 | |
958 adlib_style=LUCAS_STYLE|MIDI_STYLE; | |
959 | |
960 curtrack=0; | |
961 track[curtrack].on=1; | |
962 track[curtrack].tend=flen; // music until the end of the file | |
963 track[curtrack].spos=0x98; //jump to midi music | |
964 break; | |
965 case FILE_ADVSIERRA: | |
966 memcpy(myinsbank, smyinsbank, 128 * 16); | |
967 tins = stins; | |
968 deltas=0x20; | |
969 getnext(11); //worthless empty space and "stuff" :) | |
970 | |
971 o_sierra_pos=sierra_pos=pos; | |
972 sierra_next_section(); | |
973 while (datalook(sierra_pos-2)!=0xff) | |
974 { | |
975 sierra_next_section(); | |
976 subsongs++; | |
977 } | |
978 | |
979 if (subsong < 0 || subsong >= subsongs) subsong=0; | |
980 | |
981 sierra_pos=o_sierra_pos; | |
982 sierra_next_section(); | |
983 i=0; | |
984 while (i != subsong) | |
985 { | |
986 sierra_next_section(); | |
987 i++; | |
988 } | |
989 | |
990 adlib_style=SIERRA_STYLE|MIDI_STYLE; //advanced sierra tunes use volume | |
991 break; | |
992 case FILE_SIERRA: | |
993 memcpy(myinsbank, smyinsbank, 128 * 16); | |
994 tins = stins; | |
995 getnext(2); | |
996 deltas=0x20; | |
997 | |
998 curtrack=0; | |
999 track[curtrack].on=1; | |
1000 track[curtrack].tend=flen; // music until the end of the file | |
1001 | |
1002 for (i=0; i<16; i++) | |
1003 { | |
1004 ch[i].nshift=-13; | |
1005 ch[i].on=getnext(1); | |
1006 ch[i].inum=getnext(1); | |
1007 for (j=0; j<11; j++) | |
1008 ch[i].ins[j]=myinsbank[ch[i].inum][j]; | |
1009 } | |
1010 | |
1011 track[curtrack].spos=pos; | |
1012 adlib_style=SIERRA_STYLE|MIDI_STYLE; | |
1013 break; | |
1014 } | |
1015 | |
1016 | |
1017 /* sprintf(info,"%s\r\nTicks/Quarter Note: %ld\r\n",info,deltas); | |
1018 sprintf(info,"%sms/Quarter Note: %ld",info,msqtr); */ | |
1019 | |
1020 for (i=0; i<16; i++) | |
1021 if (track[i].on) | |
1022 { | |
1023 track[i].pos=track[i].spos; | |
1024 track[i].pv=0; | |
1025 track[i].iwait=0; | |
1026 } | |
1027 | |
1028 doing=1; | |
1029 opl->init(); | |
1030 } | |
1031 | |
1032 std::string CmidPlayer::gettype() | |
1033 { | |
1034 switch(type) { | |
1035 case FILE_LUCAS: | |
1036 return std::string("LucasArts AdLib MIDI"); | |
1037 case FILE_MIDI: | |
1038 return std::string("General MIDI"); | |
1039 case FILE_CMF: | |
1040 return std::string("Creative Music Format (CMF MIDI)"); | |
1041 case FILE_OLDLUCAS: | |
1042 return std::string("Lucasfilm Adlib MIDI"); | |
1043 case FILE_ADVSIERRA: | |
1044 return std::string("Sierra On-Line VGA MIDI"); | |
1045 case FILE_SIERRA: | |
1046 return std::string("Sierra On-Line EGA MIDI"); | |
1047 default: | |
1048 return std::string("MIDI unknown"); | |
1049 } | |
1050 } |