OSDN Git Service

Enable 64 bit IO support on MinGW
[timidity41/timidity41.git] / timidity / rcp.c
1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24 /* rcp.c - written by Masanao Izumo <mo@goice.co.jp> */
25
26 #include <stdio.h>
27 #ifndef NO_STRING_H
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #endif
32 #include "timidity.h"
33 #include "common.h"
34 #include "instrum.h"
35 #include "playmidi.h"
36 #include "readmidi.h"
37 #include "controls.h"
38
39 #define RCP_MAXCHANNELS 32
40 /* #define RCP_LOOP_CONT_LIMIT 16 */
41 #define RCP_LOOP_TIME_LIMIT 600
42
43 struct NoteList
44 {
45     int32 gate;                 /* Note length */
46     int   ch;                   /* channel */
47     int   note;                 /* Note number */
48     struct NoteList *next;      /* next note */
49 };
50
51 struct RCPNoteTracer
52 {
53     int gfmt;                   /*! RCP format (1 if G36 or G18) */
54     int32 at;                   /*! current time */
55     int32 tempo;                /*! current tempo (sync with current_tempo) */
56     int32 tempo_to;             /*! tempo gradate to */
57     int tempo_grade;            /*! tempo gradation slope */
58     int tempo_step;             /*! tempo gradation step */
59     struct NoteList *notes;     /*! note list */
60     MBlockList pool;            /*! memory pool for notes */
61     struct NoteList *freelist;  /*! free note list */
62 };
63
64 #define SETMIDIEVENT(e, at, t, ch, pa, pb) \
65     { (e).time = (at); (e).type = (t); \
66       (e).channel = (uint8)(ch); (e).a = (uint8)(pa); (e).b = (uint8)(pb); }
67
68 #define MIDIEVENT(at, t, ch, pa, pb) \
69     { MidiEvent event; SETMIDIEVENT(event, at, t, ch, pa, pb); \
70       readmidi_add_event(&event); }
71
72 static int read_rcp_track(struct timidity_file *tf, int trackno, int gfmt);
73 static int preprocess_sysex(uint8* ex, int ch, int gt, int vel);
74
75 /* Note Tracer */
76 static void ntr_init(struct RCPNoteTracer *ntr, int gfmt, int32 at);
77 static void ntr_end(struct RCPNoteTracer *ntr);
78 static void ntr_incr(struct RCPNoteTracer *ntr, int step);
79 static void ntr_note_on(struct RCPNoteTracer *ntr,
80                         int ch, int note, int velo, int gate);
81 static void ntr_wait_all_off(struct RCPNoteTracer *ntr);
82 #define ntr_at(ntr) ((ntr).at)
83
84 #define USER_EXCLUSIVE_LENGTH 24
85 #define MAX_EXCLUSIVE_LENGTH 1024
86 static uint8 user_exclusive_data[8][USER_EXCLUSIVE_LENGTH];
87 static int32 init_tempo;
88 static int32 init_keysig;
89 static int play_bias;
90
91 #define TEMPO_GRADATION_SKIP            2
92 #define TEMPO_GRADATION_GRADE           600
93
94 int read_rcp_file(struct timidity_file *tf, char *magic0, char *fn)
95 {
96     char buff[361], *p;
97     int ntrack, timebase1, timebase2, i, len, gfmt;
98
99     strncpy(buff, magic0, 4);
100     if(tf_read(buff + 4, 1, 32-4, tf) != 32-4)
101         return 1;
102     len = strlen(fn);
103     if(strncmp(buff, "RCM-PC98V2.0(C)COME ON MUSIC", 28) == 0)
104     {
105         /* RCP or R36 */
106         gfmt = 0;
107         if(check_file_extension(fn, ".r36", 1))
108             current_file_info->file_type = IS_R36_FILE;
109         else
110             current_file_info->file_type = IS_RCP_FILE;
111     }
112     else if(strncmp(buff, "COME ON MUSIC RECOMPOSER RCP3.0", 31) == 0)
113     {
114         /* G36 or G18 */
115         gfmt = 1;
116         if(check_file_extension(fn, ".g18", 1))
117             current_file_info->file_type = IS_G18_FILE;
118         else
119             current_file_info->file_type = IS_G36_FILE;
120     }
121     else
122         return 1;
123
124     /* title */
125     if(tf_read(buff, 1, 64, tf) != 64)
126         return 1;
127     if(current_file_info->seq_name == NULL)
128     {
129         buff[64] = '\0';
130         for(len = 63; len >= 0; len--)
131         {
132             if(buff[len] == ' ')
133                 buff[len] = '\0';
134             else if(buff[len] != '\0')
135                 break;
136         }
137
138         len = SAFE_CONVERT_LENGTH(len + 1);
139         p = (char *)new_segment(&tmpbuffer, len);
140         code_convert(buff, p, len, NULL, NULL);
141         current_file_info->seq_name = (char *)safe_strdup(p);
142         reuse_mblock(&tmpbuffer);
143     }
144     current_file_info->format = 1;
145
146     if(!gfmt) /* RCP or R36 */
147     {
148         if(tf_read(buff, 1, 336, tf) != 336)
149             return 1;
150 #ifndef __BORLANDC__
151         buff[336] = '\0';
152         for(len = 335; len >= 0; len--)
153         {
154             if(buff[len] == ' ')
155                 buff[len] = '\0';
156             else if(buff[len] != '\0')
157                 break;
158         }
159         len = SAFE_CONVERT_LENGTH(len + 1);
160
161         p = (char *)new_segment(&tmpbuffer, len);
162         code_convert(buff, p, len, NULL, NULL);
163         ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "Memo: %s", p);
164         reuse_mblock(&tmpbuffer);
165 #else
166         buff[336] = '\0';
167         {
168         char tmp[30];
169         int i;
170         for(i=0;i<336;i+=28)
171         {
172         strncpy(tmp,buff+i,28);
173         tmp[28] = '\0';
174         for(len=28; len>=0; len--)
175         {
176             if(tmp[len] == ' ')
177                         tmp[len] = '\0';
178             else if(tmp[len] != '\0')
179                         break;
180         }
181         if(tmp[0]=='\0')
182                 continue;
183         len = SAFE_CONVERT_LENGTH(len + 1);
184
185         p = (char *)new_segment(&tmpbuffer, len);
186         code_convert(tmp, p, len, NULL, NULL);
187         ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "Memo: %s", p);
188         reuse_mblock(&tmpbuffer);
189         }
190         }
191 #endif
192
193         skip(tf, 16);           /* 0x40 */
194
195         timebase1 = tf_getc(tf);
196         init_tempo = tf_getc(tf); /* tempo */
197         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Tempo %d", init_tempo);
198         if(init_tempo < 8 || init_tempo > 250)
199             init_tempo = 120;
200
201         /* Time Signature: numerator, denominator, Key Signature */
202         current_file_info->time_sig_n = tf_getc(tf);
203         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Time signature(n) %d",
204                   current_file_info->time_sig_n);
205         current_file_info->time_sig_d = tf_getc(tf);
206         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Time signature(d) %d",
207                   current_file_info->time_sig_d);
208         init_keysig = tf_getc(tf);
209         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Key signature %d", init_keysig);
210         if (init_keysig < 0 || init_keysig >= 32)
211                 init_keysig = 0;
212
213         play_bias = (int)(signed char)tf_getc(tf);
214         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Play bias %d", play_bias);
215         if(play_bias < -36 || play_bias > 36)
216             play_bias = 0;
217
218         skip(tf, 12);           /* cm6 */
219         skip(tf, 4);            /* reserved */
220         skip(tf, 12);           /* gsd */
221         skip(tf, 4);            /* reserved */
222
223         if((ntrack = tf_getc(tf)) == EOF)
224             return 1;
225         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Number of tracks %d", ntrack);
226         if(ntrack != 18 && ntrack != 36)
227             ntrack = 18;
228         timebase2 = tf_getc(tf);
229         skip(tf, 14);           /* reserved */
230         skip(tf, 16);           /*  */
231
232         skip(tf, 32 * (14 + 2)); /* rhythm definition */
233     }
234     else /* G36 or G18 */
235     {
236         skip(tf, 64);   /* reserved */
237
238         /* memo */
239         if(tf_read(buff, 1, 360, tf) != 360)
240             return 1;
241         buff[360] = '\0';
242         for(len = 359; len >= 0; len--)
243         {
244             if(buff[len] == ' ')
245                 buff[len] = '\0';
246             else if(buff[len] != '\0')
247                 break;
248         }
249         len = SAFE_CONVERT_LENGTH(len + 1);
250         p = (char *)new_segment(&tmpbuffer, len);
251         code_convert(buff, p, len, NULL, NULL);
252         ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "Memo: %s", p);
253         reuse_mblock(&tmpbuffer);
254
255         /* Number of tracks */
256         ntrack = tf_getc(tf);
257         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Number of tracks %d", ntrack);
258         if(ntrack != 18 && ntrack != 36)
259             ntrack = 18;
260         skip(tf, 1);
261         timebase1 = tf_getc(tf);
262         timebase2 = tf_getc(tf);
263         init_tempo = tf_getc(tf); /* tempo */
264         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Tempo %d", init_tempo);
265         if(init_tempo < 8 || init_tempo > 250)
266             init_tempo = 120;
267         skip(tf, 1); /* ?? */
268
269         /* Time Signature: numerator, denominator, Key Signature */
270         current_file_info->time_sig_n = tf_getc(tf);
271         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Time signature(n) %d",
272                   current_file_info->time_sig_n);
273         current_file_info->time_sig_d = tf_getc(tf);
274         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Time signature(n) %d",
275                   current_file_info->time_sig_d);
276         init_keysig = tf_getc(tf);
277         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Key signature %d", init_keysig);
278         if (init_keysig < 0 || init_keysig >= 32)
279                 init_keysig = 0;
280
281         play_bias = (int)(signed char)tf_getc(tf);
282         ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Play bias %d", play_bias);
283         if(play_bias < -36 || play_bias > 36)
284             play_bias = 0;
285         skip(tf, 6 + 16 + 112); /* reserved */
286         skip(tf, 12);           /* gsd */
287         skip(tf, 4);
288         skip(tf, 12);           /* gsd */
289         skip(tf, 4);
290         skip(tf, 12);           /* cm6 */
291         skip(tf, 4);
292         skip(tf, 80);           /* reserved */
293         skip(tf, 128 * (14 + 2)); /* rhythm definition */
294     }
295
296     /* SysEx data */
297     for(i = 0; i < 8; i++)
298     {
299         int mid;
300         skip(tf, 24);   /* memo */
301         if(tf_read(user_exclusive_data[i], 1, USER_EXCLUSIVE_LENGTH, tf)
302            != USER_EXCLUSIVE_LENGTH)
303             return 1;
304         mid = user_exclusive_data[i][0];
305         if(mid > 0 && mid < 0x7e &&
306            (current_file_info->mid == 0 || current_file_info->mid >= 0x7e))
307             current_file_info->mid = mid;
308     }
309
310     current_file_info->divisions = (timebase1 | (timebase2 << 8));
311     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "divisions %d",
312               current_file_info->divisions);
313     current_file_info->format = 1;
314     current_file_info->tracks = ntrack;
315
316     if(!IS_URL_SEEK_SAFE(tf->url))
317         if((tf->url = url_cache_open(tf->url, 1)) == NULL)
318             return 1;
319
320     for(i = 0; i < ntrack; i++)
321         if(read_rcp_track(tf, i, gfmt))
322         {
323             if(ignore_midi_error)
324                 return 0;
325             return 1;
326         }
327     return 0;
328 }
329
330 static void rcp_tempo_set(int32 at, int32 tempo)
331 {
332     int lo, mid, hi;
333     
334     lo = (tempo & 0xff);
335     mid = ((tempo >> 8) & 0xff);
336     hi = ((tempo >> 16) & 0xff);
337     MIDIEVENT(at, ME_TEMPO, lo, hi, mid);
338 }
339
340 static int32 rcp_tempo_change(struct RCPNoteTracer *ntr, int a, int b)
341 {
342     int32 tempo;
343     
344     tempo = (int32)((uint32)60000000 * 64 / (init_tempo * a));  /* 6*10^7 / (ini*a/64) */
345     ntr->tempo_grade = b;
346     if (b != 0)
347     {
348         ntr->tempo_to = tempo;
349         ntr->tempo_step = 0;
350         ntr->tempo_grade *= TEMPO_GRADATION_GRADE * TEMPO_GRADATION_SKIP;
351         tempo = ntr->tempo;     /* unchanged */
352     }
353     else
354     {
355         ntr->tempo = tempo;
356         rcp_tempo_set(ntr_at(*ntr), tempo);
357     }
358     return tempo;
359 }
360
361 static void rcp_timesig_change(int32 at)
362 {
363         int n, d;
364         
365         n = current_file_info->time_sig_n;
366         d = current_file_info->time_sig_d;
367         MIDIEVENT(at, ME_TIMESIG, 0, n, d);
368 }
369
370 static void rcp_keysig_change(int32 at, int32 key)
371 {
372         int8 sf, mi;
373         
374         if (key < 8)
375                 sf = key, mi = 0;
376         else if (key < 16)
377                 sf = 8 - key, mi = 0;
378         else if (key < 24)
379                 sf = key - 16, mi = 1;
380         else
381                 sf = 24 - key, mi = 1;
382         MIDIEVENT(at, ME_KEYSIG, 0, sf, mi);
383 }
384
385 static char *rcp_cmd_name(int cmd)
386 {
387     if(cmd < 0x80)
388     {
389         static char name[16];
390         sprintf(name, "NoteOn %d", cmd);
391         return name;
392     }
393
394     switch(cmd)
395     {
396       case 0x90: return "UserExclusive0";
397       case 0x91: return "UserExclusive1";
398       case 0x92: return "UserExclusive2";
399       case 0x93: return "UserExclusive3";
400       case 0x94: return "UserExclusive4";
401       case 0x95: return "UserExclusive5";
402       case 0x96: return "UserExclusive6";
403       case 0x97: return "UserExclusive7";
404       case 0x98: return "ChannelExclusive";
405       case 0xc0: return "DX7 function";
406       case 0xc1: return "DX parameter";
407       case 0xc2: return "DX RERF";
408       case 0xc3: return "TX function";
409       case 0xc5: return "FB-01 P parameter";
410       case 0xc6: return "FB-01 S System";
411       case 0xc7: return "TX81Z V VCED";
412       case 0xc8: return "TX81Z A ACED";
413       case 0xc9: return "TX81Z P PCED";
414       case 0xca: return "TX81Z S System";
415       case 0xcb: return "TX81Z E EFFECT";
416       case 0xcc: return "DX7-2 R REMOTE SW";
417       case 0xcd: return "DX7-2 A ACED";
418       case 0xce: return "DX7-2 P PCED";
419       case 0xcf: return "TX802 P PCED";
420       case 0xd0: return "YamahaBase";
421       case 0xd1: return "YamahaPara";
422       case 0xd2: return "YamahaDevice";
423       case 0xd3: return "XGPara";
424       case 0xdc: return "MKS-7";
425       case 0xdd: return "RolandBase";
426       case 0xde: return "RolandPara";
427       case 0xdf: return "RolandDevice";
428       case 0xe1: return "BnkLPrg";
429       case 0xe2: return "Bank&ProgCng";
430       case 0xe5: return "KeyScan";
431       case 0xe6: return "ChChange";
432       case 0xe7: return "TempoChange";
433       case 0xea: return "ChannelAfterTouch";
434       case 0xeb: return "ControlChange";
435       case 0xec: return "ProgChange";
436       case 0xed: return "AfterTouch";
437       case 0xee: return "PitchBend";
438       case 0xf5: return "KeyChange";
439       case 0xf6: return "Comment";
440       case 0xf7: return "2ndEvent";
441       case 0xf8: return "LoopEnd";
442       case 0xf9: return "LoopStart";
443       case 0xfc: return "SameMeasure";
444       case 0xfd: return "MeasureEnd";
445       case 0xfe: return "EndOfTrack";
446     }
447     return "Unknown";
448 }
449
450 static int rcp_parse_sysex_event(int32 at, uint8 *val, int32 len)
451 {
452     MidiEvent ev, evm[260];
453     int ne, i;
454
455     if(len == 0) {return 0;}
456
457     if(parse_sysex_event(val, len, &ev))
458     {
459         ev.time = at;
460         readmidi_add_event(&ev);
461     }
462         if ((ne = parse_sysex_event_multi(val, len, evm)) > 0)
463                 for (i = 0; i < ne; i++) {
464                         evm[i].time = at;
465                         readmidi_add_event(&evm[i]);
466                 }
467     
468     return 0;
469 }
470
471 #define MAX_STACK_DEPTH 16
472 static int read_rcp_track(struct timidity_file *tf, int trackno, int gfmt)
473 {
474     int32 current_tempo;
475     int size;
476     int ch;
477     long last_point, cmdlen;
478     int key_offset;
479     struct RCPNoteTracer ntr;
480     struct
481     {
482         long start_at;          /* loop start time */
483         long loop_start;        /* loop start seek point */
484         int  count;             /* loop count */
485     } stack[MAX_STACK_DEPTH];
486     int sp;
487     int i, len;
488     uint8 sysex[MAX_EXCLUSIVE_LENGTH];
489
490     int roland_base_init = 0;
491     int roland_dev_init = 0;
492     int roland_base_addr0 = 0;
493     int roland_base_addr1 = 0;
494     int roland_device_id = 0x10;
495     int roland_model_id = 0x16;
496
497     int yamaha_base_init = 0;
498     int yamaha_dev_init = 0;
499     int yamaha_base_addr0 = 0;
500     int yamaha_base_addr1 = 0;
501     int yamaha_device_id = 0x10;        /* ??? */
502     int yamaha_model_id = 0x16;         /* ??? */
503
504     unsigned char cs;           /* check sum */
505
506     long same_measure = -1;     /* Meassure to stack pointer */
507     long track_top, data_top;
508     int time_offset;
509     char buff[64], *p;
510     int ret = 0;
511
512     /*
513      * Read Track Header
514      */
515
516     track_top = tf_tell(tf);
517     ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY, "Track top: %d", track_top);
518
519     size = tf_getc(tf);
520     size |= (tf_getc(tf) << 8);
521
522     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Track size %d", size);
523     last_point = size + tf_tell(tf) - 2;
524
525     if(gfmt)
526     {
527         skip(tf, 2);
528         cmdlen = 6;
529     }
530     else
531         cmdlen = 4;
532
533     i = tf_getc(tf);            /* Track Number */
534     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Track number %d", i);
535     skip(tf, 1);                /* Rhythm */
536
537     if((ch = tf_getc(tf)) == 0xff) /* No playing */
538     {
539         tf_seek(tf, last_point, SEEK_SET);
540         return 0;
541     }
542
543     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Track channel %d", ch);
544
545     if(ch >= RCP_MAXCHANNELS)
546         ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
547                   "RCP: Invalid channel: %d", ch);
548
549     /* Key offset */
550     key_offset = tf_getc(tf);
551     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Key offset %d", key_offset);
552     if(key_offset > 64)
553         key_offset -= 128;
554     key_offset += play_bias;
555
556     /* Time offset */
557     time_offset = (int)(signed char)tf_getc(tf);
558     ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Time offset %d", time_offset);
559     if(time_offset < -99 || 99 < time_offset)
560     {
561         ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
562                   "RCP: Invalid time offset: %d", time_offset);
563         if(time_offset < -99)
564             time_offset = -99;
565         else
566             time_offset = 99;
567     }
568
569     i = tf_getc(tf);            /* mode */
570     if(i == 1)
571     {
572         /* Mute */
573         tf_seek(tf, last_point, SEEK_SET);
574         return 0;
575     }
576
577     /* Comment */
578     tf_read(buff, 1, 36, tf);
579     buff[36] = '\0';
580     for(len = 35; len >= 0; len--)
581     {
582         if(buff[len] == ' ')
583             buff[len] = '\0';
584         else if(buff[len] != '\0')
585             break;
586     }
587     len = SAFE_CONVERT_LENGTH(len+1);
588     p = (char *)new_segment(&tmpbuffer, len);
589     code_convert(buff, p, len, NULL, NULL);
590     if(*p)
591         ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "RCP Track name: %s", p);
592     reuse_mblock(&tmpbuffer);
593
594     /*
595      * End of Track Header
596      */
597
598     sp = 0;
599     ntr.tempo = current_tempo = 60000000 / init_tempo;
600     ntr_init(&ntr, gfmt, readmidi_set_track(trackno, 1));
601     if(trackno == 0 && init_tempo != 120)
602         ntr.tempo = current_tempo = rcp_tempo_change(&ntr, 64, 0);
603         if (trackno == 0) {
604                 rcp_timesig_change(ntr_at(ntr));
605                 rcp_keysig_change(ntr_at(ntr), init_keysig);
606         }
607     ntr_incr(&ntr, time_offset);
608
609     data_top = tf_tell(tf);
610     while(last_point >= tf_tell(tf) + cmdlen)
611     {
612         int cmd, a, b, step, gate;
613         int st1, gt1, st2, gt2;
614
615         if(readmidi_error_flag)
616         {
617             ret = -1;
618             break;
619         }
620
621         if(!gfmt)
622         {
623             cmd = tf_getc(tf);
624             step = tf_getc(tf);
625             gate = a = tf_getc(tf);
626             if((b = tf_getc(tf)) == EOF)
627             {
628                 ret = -1;
629                 break;
630             }
631             st1 = gt1 = st2 = gt2 = 0;
632
633             if(ctl->verbosity >= VERB_DEBUG_SILLY)
634             {
635                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
636                           "[%d] %d %s: ch=%d step=%d a=%d b=%d sp=%d",
637                           tf_tell(tf) - 4 - track_top,
638                           ntr_at(ntr), rcp_cmd_name(cmd), ch, step, a, b, sp);
639             }
640         }
641         else
642         {
643             cmd = tf_getc(tf);
644             b = tf_getc(tf);
645             st1 = tf_getc(tf);
646             st2 = tf_getc(tf);
647             a = gt1 = tf_getc(tf);
648             if((gt2 = tf_getc(tf)) == EOF)
649             {
650                 ret = -1;
651                 break;
652             }
653
654             step = st1 + (st2 << 8);
655             gate = gt1 + (gt2 << 8);
656
657             if(ctl->verbosity >= VERB_DEBUG_SILLY)
658             {
659                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
660                           "[%d] %d %s: ch=%d step=%d gate=%d a=%d b=%d sp=%d",
661                           tf_tell(tf) - 6 - track_top,
662                           ntr_at(ntr), rcp_cmd_name(cmd), ch,
663                           step, gate, a, b, sp);
664             }
665         }
666
667         if(cmd < 0x80)          /* Note event */
668         {
669             if(gate > 0)
670             {
671                 int note = cmd + key_offset;
672                 ntr_note_on(&ntr, ch, note & 0x7f, b, gate);
673             }
674             ntr_incr(&ntr, step);
675             continue;
676         }
677
678         /* command */
679         switch(cmd)
680         {
681           case 0x90:    /* User Exclusive 1 */
682           case 0x91:    /* User Exclusive 2 */
683           case 0x92:    /* User Exclusive 3 */
684           case 0x93:    /* User Exclusive 4 */
685           case 0x94:    /* User Exclusive 5 */
686           case 0x95:    /* User Exclusive 6 */
687           case 0x96:    /* User Exclusive 7 */
688           case 0x97:    /* User Exclusive 8 */
689             memcpy(sysex, user_exclusive_data[cmd - 0x90],
690                    USER_EXCLUSIVE_LENGTH);
691             sysex[USER_EXCLUSIVE_LENGTH] = 0xf7;
692             len = preprocess_sysex(sysex, ch, a, b);
693             rcp_parse_sysex_event(ntr_at(ntr), sysex, len);
694             ntr_incr(&ntr, step);
695             break;
696
697           case 0x98:    /* Channel Exclusive */
698             len = 0;
699             while(tf_getc(tf) == 0xf7 && len + 6 < sizeof(sysex))
700             {
701                 if(gfmt)
702                 {
703                     if(tf_read(sysex + len, 1, 5, tf) != 5)
704                     {
705                         ret = -1;
706                         goto end_of_track;
707                     }
708                     len += 5;
709                 }
710                 else
711                 {
712                     tf_getc(tf); /* 0x00 */
713                     if(tf_read(sysex + len, 1, 2, tf) != 2)
714                     {
715                         ret = -1;
716                         goto end_of_track;
717                     }
718                     len += 2;
719                 }
720             }
721             tf_seek(tf, -1, SEEK_CUR);
722             sysex[len] = 0xf7;
723             len = preprocess_sysex(sysex, ch, a, b);
724             rcp_parse_sysex_event(ntr_at(ntr), sysex, len);
725             ntr_incr(&ntr, step);
726             break;
727
728           case 0xd0:    /* Yamaha base */
729             a &= 0x7f;
730             b &= 0x7f;
731             yamaha_base_addr0 = a;
732             yamaha_base_addr1 = b;
733             yamaha_base_init = 1;
734             ntr_incr(&ntr, step);
735             break;
736
737           case 0xd1:    /* Yamaha para */
738             a &= 0x7f;
739             b &= 0x7f;
740             if(!yamaha_base_init)
741                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
742                           "YamPara used before initializing YamBase");
743             if(!yamaha_dev_init)
744                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
745                           "YamPara used before initializing YamDev#");
746             yamaha_base_init = yamaha_dev_init = 1;
747             sysex[0] = 0x43;
748             sysex[1] = yamaha_device_id;
749             sysex[2] = yamaha_model_id;
750             sysex[3] = 0x12;
751             cs = 0;
752             cs += sysex[4] = yamaha_base_addr0;
753             cs += sysex[5] = yamaha_base_addr1;
754             cs += sysex[6] = a;
755             cs += sysex[7] = b;
756             sysex[8] = 128 - (cs & 0x7f);
757             sysex[9] = 0xf7;
758                 rcp_parse_sysex_event(ntr_at(ntr), sysex, 10);
759             ntr_incr(&ntr, step);
760             break;
761
762           case 0xd2:    /* Yamaha device */
763             a &= 0x7f;
764             b &= 0x7f;
765             yamaha_device_id = a;
766             yamaha_model_id = b;
767             yamaha_dev_init = 1;
768             ntr_incr(&ntr, step);
769             break;
770
771           case 0xd3:    /* XG para */
772             /* ?? */
773             a &= 0x7f;
774             b &= 0x7f;
775             sysex[0] = 0x43;
776             sysex[1] = yamaha_device_id;
777             sysex[2] = yamaha_model_id;
778             sysex[3] = 0x12;
779             cs = 0;
780             cs += sysex[4] = yamaha_base_addr0;
781             cs += sysex[5] = yamaha_base_addr1;
782             cs += sysex[6] = a;
783             cs += sysex[7] = b;
784             sysex[8] = 128 - (cs & 0x7f);
785             sysex[9] = 0xf7;
786                 rcp_parse_sysex_event(ntr_at(ntr), sysex, 10);
787             ntr_incr(&ntr, step);
788             break;
789
790           case 0xdd:    /* Roland base */
791             a &= 0x7f;
792             b &= 0x7f;
793             roland_base_addr0 = a;
794             roland_base_addr1 = b;
795             roland_base_init = 1;
796             ntr_incr(&ntr, step);
797             break;
798
799           case 0xde:    /* Roland para */
800             a &= 0x7f;
801             b &= 0x7f;
802             if(!roland_base_init)
803                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
804                           "RolPara used before initializing RolBase");
805             if(!roland_dev_init)
806                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
807                           "RolPara used before initializing RolDev#");
808             roland_base_init = roland_dev_init = 1;
809             sysex[0] = 0x41;
810             sysex[1] = roland_device_id;
811             sysex[2] = roland_model_id;
812             sysex[3] = 0x12;
813             cs = 0;
814             cs += sysex[4] = roland_base_addr0;
815             cs += sysex[5] = roland_base_addr1;
816             cs += sysex[6] = a;
817             cs += sysex[7] = b;
818             sysex[8] = 128 - (cs & 0x7f);
819             sysex[9] = 0xf7;
820                 rcp_parse_sysex_event(ntr_at(ntr), sysex, 10);
821             ntr_incr(&ntr, step);
822             break;
823
824           case 0xdf:    /* Roland device */
825             a &= 0x7f;
826             b &= 0x7f;
827             roland_device_id = a;
828             roland_model_id = b;
829             roland_dev_init = 1;
830             ntr_incr(&ntr, step);
831             break;
832
833           case 0xe1:    /* BnkLPrg */
834             ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
835                       "BnkLPrg is not supported: 0x%02x 0x%02x", a, b);
836             ntr_incr(&ntr, step);
837             break;
838
839           case 0xe2:    /* bank & program change */
840             readmidi_add_ctl_event(ntr_at(ntr), ch, 0, b); /*Change MSB Bank*/
841             MIDIEVENT(ntr_at(ntr), ME_PROGRAM, ch, a & 0x7f, 0);
842             ntr_incr(&ntr, step);
843             break;
844
845           case 0xe5:    /* key scan */
846 #if 0
847             switch(a)
848             {
849               case 12: /* suspend playing */
850               case 18: /* increase play bias */
851               case 23: /* stop playing */
852               case 32: /* show main screen */
853               case 33: /* show 11th track */
854               case 34: /* show 12th track */
855               case 35: /* show 13th track */
856               case 36: /* show 14th track */
857               case 37: /* show 15th track */
858               case 38: /* show 16th track */
859               case 39: /* show 17th track */
860               case 40: /* show 18th track */
861               case 48: /* show 10th track */
862               case 49: /* show 1st track */
863               case 50: /* show 2nd track */
864               case 51: /* show 3rd track */
865               case 52: /* show 4th track */
866               case 53: /* show 5th track */
867               case 54: /* show 6th track */
868               case 55: /* show 7th track */
869               case 56: /* show 8th track */
870               case 57: /* show 9th track */
871               case 61: /* mute 1st track */
872                 break;
873             }
874 #endif
875             ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
876                       "Key scan 0x%02x 0x%02x is not supported", a, b);
877             ntr_incr(&ntr, step);
878             break;
879
880           case 0xe6:    /* MIDI channel change */
881             ch = a - 1;
882             if(ch == 0) /* ##?? */
883                 ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "MIDI channel off");
884             else if(ch >= RCP_MAXCHANNELS)
885                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
886                           "RCP: Invalid channel: %d", ch);
887             ntr_incr(&ntr, step);
888             break;
889
890           case 0xe7:    /* tempo change */
891             if(a == 0)
892             {
893                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
894                           "Invalid tempo change\n");
895                 a = 64;
896             }
897             current_tempo = rcp_tempo_change(&ntr, a, b);
898             ntr_incr(&ntr, step);
899             break;
900
901           case 0xea:    /* channel after touch (channel pressure) */
902             a &= 0x7f;
903             MIDIEVENT(ntr_at(ntr), ME_CHANNEL_PRESSURE, ch, a, 0);
904             ntr_incr(&ntr, step);
905             break;
906
907           case 0xeb:    /* control change */
908             readmidi_add_ctl_event(ntr_at(ntr), ch, a, b);
909             ntr_incr(&ntr, step);
910             break;
911
912           case 0xec:    /* program change */
913             a &= 0x7f;
914             MIDIEVENT(ntr_at(ntr), ME_PROGRAM, ch, a, 0);
915             ntr_incr(&ntr, step);
916             break;
917
918           case 0xed:    /* after touch polyphonic (polyphonic key pressure) */
919             a &= 0x7f;
920             b &= 0x7f;
921             MIDIEVENT(ntr_at(ntr), ME_KEYPRESSURE, ch, a, b);
922             ntr_incr(&ntr, step);
923             break;
924
925           case 0xee:    /* pitch bend */
926             a &= 0x7f;
927             b &= 0x7f;
928             MIDIEVENT(ntr_at(ntr), ME_PITCHWHEEL, ch, a, b);
929             ntr_incr(&ntr, step);
930             break;
931
932         case 0xf5:      /* key change */
933                 if (step < 0 || step >= 32) {
934                         ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, "Invalid key change\n");
935                         step = 0;
936                 }
937                 rcp_keysig_change(ntr_at(ntr), step);
938                 break;
939
940           case 0xf6:    /* comment */
941             len = 0;
942             if(!gfmt)
943             {
944                 buff[len++] = a;
945                 buff[len++] = b;
946             }
947             else
948             {
949                 buff[len++] = b;
950                 buff[len++] = st1;
951                 buff[len++] = st2;
952                 buff[len++] = gt1;
953                 buff[len++] = gt2;
954             }
955
956             while(tf_getc(tf) == 0xf7 && len + 6 < sizeof(buff))
957             {
958                 if(gfmt)
959                 {
960                     if(tf_read(buff + len, 1, 5, tf) != 5)
961                     {
962                         ret = -1;
963                         goto end_of_track;
964                     }
965                     len += 5;
966                 }
967                 else
968                 {
969                     tf_getc(tf); /* 0x00 */
970                     if(tf_read(buff + len, 1, 2, tf) != 2)
971                     {
972                         ret = -1;
973                         goto end_of_track;
974                     }
975                     len += 2;
976                 }
977             }
978             tf_seek(tf, -1, SEEK_CUR);
979
980             if(ctl->verbosity >= VERB_VERBOSE || opt_trace_text_meta_event)
981             {
982                 buff[len] = '\0';
983                 for(len--; len >= 0; len--)
984                 {
985                     if(buff[len] == ' ')
986                         buff[len] = '\0';
987                     else if(buff[len] != '\0')
988                         break;
989                 }
990
991                 if(opt_trace_text_meta_event)
992                 {
993                     MidiEvent ev;
994                     if(readmidi_make_string_event(ME_TEXT, buff, &ev, 1)
995                        != NULL)
996                     {
997                         ev.time = ntr_at(ntr);
998                         readmidi_add_event(&ev);
999                     }
1000                 }
1001                 else if(ctl->verbosity >= VERB_VERBOSE)
1002                 {
1003                     len = SAFE_CONVERT_LENGTH(len+1);
1004                     p = (char *)new_segment(&tmpbuffer, len);
1005                     code_convert(buff, p, len, NULL, NULL);
1006                     ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "Comment: %s", p);
1007                     reuse_mblock(&tmpbuffer);
1008                 }
1009             }
1010             break;
1011
1012           case 0xf7:    /* 2nd Event */
1013             ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1014                       "Something's wrong: 2nd Event is appeared");
1015             break;
1016
1017           case 0xf8:    /* loop end */
1018             if(ctl->verbosity >= VERB_DEBUG_SILLY)
1019                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1020                           "Loop end at %d",
1021                           tf_tell(tf) - track_top - cmdlen);
1022             if(sp == 0)
1023                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1024                           "Misplaced loop end (ch=%d)", ch);
1025             else if(sp > MAX_STACK_DEPTH)
1026                 sp--;           /* through deeply loop */
1027             else
1028             {
1029                 int top;
1030
1031                 top = sp - 1;
1032
1033                 if(same_measure == top)
1034                 {
1035                     sp = same_measure;
1036                     tf_seek(tf, stack[sp].loop_start, SEEK_SET);
1037                     same_measure = -1;
1038                     goto break_loop_end;
1039                 }
1040
1041                 if((!gfmt && step == 0xff) || (gfmt && step == 0xffff))
1042                 {
1043                     sp = top;
1044                     goto break_loop_end;
1045                 }
1046
1047                 if(stack[top].count >= step)
1048                     sp = top;
1049 #ifdef RCP_LOOP_CONT_LIMIT
1050                 else if(stack[top].count >= RCP_LOOP_CONT_LIMIT)
1051                 {
1052                     ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1053                               "Loop limit exceeded (ch=%d)", ch);
1054                     sp = top;
1055                 }
1056 #endif /* RCP_LOOP_CONT_LIMIT */
1057 #ifdef RCP_LOOP_TIME_LIMIT
1058                 else if((current_tempo * DIV_1000000) *
1059                         (ntr_at(ntr) - stack[top].start_at)
1060                         / current_file_info->divisions
1061                         > RCP_LOOP_TIME_LIMIT)
1062                 {
1063                     ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1064                               "Loop time exceeded (ch=%d)", ch);
1065                     sp = top;
1066                 }
1067 #endif /* RCP_LOOP_TIME_LIMIT */
1068                 else
1069                 {
1070                     stack[top].count++;
1071                     ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1072                               "Jump to %d (cnt=%d)",
1073                               stack[top].loop_start - track_top,
1074                               stack[top].count);
1075                     tf_seek(tf, stack[top].loop_start, SEEK_SET);
1076                 }
1077             }
1078           break_loop_end:
1079             break;
1080
1081           case 0xf9:    /* loop start */
1082 #if 1
1083             if(!gfmt && step == 0xff) /* ##?? */
1084                 continue;
1085 #endif
1086             if(ctl->verbosity >= VERB_DEBUG_SILLY)
1087                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1088                           "Loop start at %d",
1089                           tf_tell(tf) - track_top - cmdlen);
1090
1091             if(sp >= MAX_STACK_DEPTH)
1092                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1093                           "Too deeply nested %d (ch=%d, Ignored this loop)",
1094                           ch, sp);
1095             else
1096             {
1097                 stack[sp].start_at = ntr_at(ntr);
1098                 stack[sp].loop_start = tf_tell(tf);
1099                 stack[sp].count = 1;
1100             }
1101             sp++;
1102             break;
1103
1104           case 0xfc:    /* same measure */
1105             if(ctl->verbosity >= VERB_DEBUG_SILLY)
1106                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1107                           "Same measure at %d",
1108                           tf_tell(tf) - track_top - cmdlen);
1109             if(sp >= MAX_STACK_DEPTH)
1110                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1111                           "Too deeply nested %d (ch=%d)", sp, ch);
1112             else
1113             {
1114                 long jmp, nextflag;
1115
1116                 if(same_measure >= 0)
1117                 {
1118                     sp = same_measure;
1119                     tf_seek(tf, stack[sp].loop_start, SEEK_SET);
1120                     same_measure = -1;
1121                     goto break_same_measure;
1122                 }
1123
1124                 nextflag = 0;
1125
1126               next_same_measure:
1127                 if(!gfmt)
1128                 {
1129                     jmp = (long)a | ((long)b << 8);
1130
1131                     if(jmp & 0x03) /* ##?? */
1132                     {
1133                         /* What do these two bits mean? */
1134                         /* Clear them here. */
1135                         ctl->cmsg(CMSG_WARNING, VERB_DEBUG,
1136                                   "Jump %d is changed to %d",
1137                                   jmp, jmp & ~3);
1138                         jmp &= ~3;              /* jmp=(jmp/4)*4 */
1139                     }
1140                 }
1141                 else
1142                 {
1143 #if 0
1144                     if(b != 0)
1145                     {
1146                         /* ##?? */
1147                     }
1148 #endif
1149                     jmp = gate;
1150                     if(current_file_info->file_type == IS_G36_FILE)
1151                         jmp = jmp * 6 - 242;
1152                     else
1153                         jmp -= (jmp + 2) % 6; /* (jmp+2)%6==0 */
1154                 }
1155
1156                 if(jmp < data_top - track_top ||
1157                    jmp >= last_point - track_top)
1158                 {
1159                     ctl->cmsg(CMSG_WARNING, VERB_NOISY,
1160                               "RCP Invalid same measure: %d (ch=%d)", jmp, ch);
1161                     break;
1162                 }
1163
1164                 if(nextflag == 0)
1165                 {
1166                     stack[sp].start_at = ntr_at(ntr);
1167                     stack[sp].loop_start = tf_tell(tf);
1168                     same_measure = sp;
1169                     sp++;
1170                 }
1171
1172                 ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY, "Jump to %d", jmp);
1173                 tf_seek(tf, track_top + jmp, SEEK_SET);
1174
1175                 if(tf_getc(tf) == 0xfc)
1176                 {
1177                     nextflag = 1;
1178                     if(!gfmt)
1179                     {
1180                         tf_getc(tf); /* step */
1181                         a = tf_getc(tf);
1182                         b = tf_getc(tf);
1183                     }
1184                     else
1185                     {
1186                         b = tf_getc(tf);
1187                         tf_getc(tf); /* st1 */
1188                         tf_getc(tf); /* st2 */
1189                         a = gt1 = tf_getc(tf);
1190                         gate = gt1 + (gt2 << 8);
1191                     }
1192                     goto next_same_measure;
1193                 }
1194                 tf_seek(tf, -1, SEEK_CUR);
1195             }
1196           break_same_measure:
1197             break;
1198
1199           case 0xfd:    /* measure end */
1200             if(same_measure >= 0)
1201             {
1202                 sp = same_measure;
1203                 tf_seek(tf, stack[sp].loop_start, SEEK_SET);
1204                 same_measure = -1;
1205             }
1206             break;
1207
1208           case 0xfe:    /* end of track */
1209             goto end_of_track;
1210
1211           case 0xc0:    /* DX7 function */
1212           case 0xc1:    /* DX parameter */
1213           case 0xc2:    /* DX RERF */
1214           case 0xc3:    /* TX function */
1215           case 0xc5:    /* FB-01 P parameter */
1216           case 0xc6:    /* FB-01 S System */
1217           case 0xc7:    /* TX81Z V VCED */
1218           case 0xc8:    /* TX81Z A ACED */
1219           case 0xc9:    /* TX81Z P PCED */
1220           case 0xca:    /* TX81Z S System */
1221           case 0xcb:    /* TX81Z E EFFECT */
1222           case 0xcc:    /* DX7-2 R REMOTE SW */
1223           case 0xcd:    /* DX7-2 A ACED */
1224           case 0xce:    /* DX7-2 P PCED */
1225           case 0xcf:    /* TX802 P PCED */
1226           case 0xdc:    /* MKS-7 */
1227             if(!gfmt)
1228                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1229                           "RCP %s is not unsupported: "
1230                           "0x%02x 0x%02x 0x%02x 0x%02x (ch=%d)",
1231                           rcp_cmd_name(cmd), cmd, step, a, b, ch);
1232             else
1233                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1234                           "RCP %s is not unsupported: "
1235                           "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x (ch=%d)",
1236                           rcp_cmd_name(cmd), cmd, b, st1, st2, gt1, gt2, ch);
1237             ntr_incr(&ntr, step);
1238             break;
1239           default:
1240             if(!gfmt)
1241             {
1242                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1243                           "RCP Unknown Command: 0x%02x 0x%02x 0x%02x 0x%02x "
1244                           "(ch=%d)",
1245                           cmd, step, a, b, ch);
1246                 /* ##?? */
1247                 if(cmd == 0xe9 && step == 0xf0 && a == 0xe0 && b == 0x80)
1248                 {
1249                     ctl->cmsg(CMSG_ERROR, VERB_VERBOSE,
1250                               "RCP e9 f0 e0 80 is end of track ??");
1251                     goto end_of_track;
1252                 }
1253             }
1254             else
1255                 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1256                           "RCP Unknown Command: "
1257                           "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x (ch=%d)",
1258                           cmd, b, st1, st2, gt1, gt2, ch);
1259 #if 0
1260             ntr_incr(&ntr, step);
1261 #endif
1262             break;
1263         }
1264     }
1265
1266   end_of_track:
1267     /* All note off */
1268     ntr_wait_all_off(&ntr);
1269     ntr_end(&ntr);
1270     tf_seek(tf, last_point, SEEK_SET);
1271     return ret;
1272 }
1273
1274 static void ntr_init(struct RCPNoteTracer *ntr, int gfmt, int32 at)
1275 {
1276     memset(ntr, 0, sizeof(*ntr));
1277     init_mblock(&ntr->pool);
1278     ntr->gfmt = gfmt;
1279     ntr->at = at;
1280     ntr->tempo_grade = 0;
1281 }
1282
1283 static void ntr_end(struct RCPNoteTracer *ntr)
1284 {
1285     reuse_mblock(&ntr->pool);
1286 }
1287
1288 static void ntr_add(struct RCPNoteTracer *ntr, int ch, int note, int gate)
1289 {
1290     struct NoteList *p;
1291
1292     if(ntr->freelist)
1293     {
1294         p = ntr->freelist;
1295         ntr->freelist = ntr->freelist->next;
1296     }
1297     else
1298         p = (struct NoteList *)new_segment(&ntr->pool,
1299                                            sizeof(struct NoteList));
1300     p->gate = gate;
1301     p->ch = ch;
1302     p->note = note;
1303     p->next = ntr->notes;
1304     ntr->notes = p;
1305 }
1306
1307 static void ntr_note_on(struct RCPNoteTracer *ntr,
1308                         int ch, int note, int velo, int gate)
1309 {
1310     struct NoteList *p;
1311
1312     for(p = ntr->notes; p != NULL; p = p->next)
1313         if(p->ch == ch && p->note == note)
1314         {
1315             p->gate = gate;
1316             return;
1317         }
1318
1319     MIDIEVENT(ntr->at, ME_NOTEON, ch, note, velo);
1320     ntr_add(ntr, ch, note, gate);
1321 }
1322
1323 static void rcp_tempo_gradate(struct RCPNoteTracer *ntr, int step)
1324 {
1325     int tempo_grade, tempo_step, tempo, diff, sign, at;
1326     
1327     if (step <= 0 || (tempo_grade = ntr->tempo_grade) == 0)
1328         return;
1329     tempo_step = ntr->tempo_step - step;
1330     if (tempo_step <= 0)
1331     {
1332         tempo = ntr->tempo;
1333         diff = ntr->tempo_to - tempo;
1334         sign = (diff < 0) ? -1 : 1;
1335         diff *= sign;   /* abs(diff) */
1336         at = ntr_at(*ntr);
1337         while (tempo_step <= 0 && diff != 0)
1338         {
1339             if (tempo_grade > diff)
1340                 tempo_grade = diff;
1341             tempo += sign * tempo_grade;
1342             diff -= tempo_grade;
1343             rcp_tempo_set(at, tempo);
1344             at += TEMPO_GRADATION_SKIP;
1345             tempo_step += TEMPO_GRADATION_SKIP;
1346         }
1347         ntr->tempo = tempo;
1348         if (diff == 0)
1349             ntr->tempo_grade = 0;
1350     }
1351     ntr->tempo_step = tempo_step;
1352 }
1353
1354 static void ntr_incr(struct RCPNoteTracer *ntr, int step)
1355 {
1356     if(step < 0) {
1357         struct NoteList *p;
1358         ntr->at += step;
1359         for(p = ntr->notes; p != NULL; p = p->next)
1360             p->gate -= step;
1361         return;
1362     }
1363
1364     rcp_tempo_gradate(ntr, step);
1365
1366     while(step >= 0)
1367     {
1368         int32 mingate;
1369         struct NoteList *p, *q;
1370
1371         if(ntr->notes == NULL)
1372         {
1373             ntr->at += step;
1374             return;
1375         }
1376
1377         q = NULL;
1378         p = ntr->notes;
1379         mingate = step;
1380         while(p)
1381         {
1382             struct NoteList *next;
1383
1384             next = p->next;
1385             if(p->gate == 0)
1386             {
1387                 if(ctl->verbosity >= VERB_DEBUG_SILLY)
1388                     ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1389                               "NoteOff %d at %d", p->note, ntr->at);
1390                 MIDIEVENT(ntr->at, ME_NOTEOFF, p->ch, p->note, 0);
1391                 p->next = ntr->freelist;
1392                 ntr->freelist = p;
1393             }
1394             else
1395             {
1396                 if(mingate > p->gate)
1397                     mingate = p->gate;
1398                 p->next = q;
1399                 q = p;
1400             }
1401             p = next;
1402         }
1403         ntr->notes = q;
1404
1405         if(step == 0)
1406             return;
1407
1408         step -= mingate;
1409         ntr->at += mingate;
1410
1411         for(p = ntr->notes; p != NULL; p = p->next)
1412             p->gate -= mingate;
1413     }
1414 }
1415
1416 static void ntr_wait_all_off(struct RCPNoteTracer *ntr)
1417 {
1418     while(ntr->notes)
1419     {
1420         int32 mingate;
1421         struct NoteList *p, *q;
1422
1423         mingate = 256;
1424         q = NULL;
1425         p = ntr->notes;
1426         while(p)
1427         {
1428             struct NoteList *next;
1429
1430             next = p->next;
1431             if(p->gate == 0)
1432             {
1433                 if(ctl->verbosity >= VERB_DEBUG_SILLY)
1434                     ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
1435                               "NoteOff %d", p->note);
1436                 MIDIEVENT(ntr->at, ME_NOTEOFF, p->ch, p->note, 0);
1437                 p->next = ntr->freelist;
1438                 ntr->freelist = p;
1439             }
1440             else
1441             {
1442                 if(mingate > p->gate)
1443                     mingate = p->gate;
1444                 p->next = q;
1445                 q = p;
1446             }
1447             p = next;
1448         }
1449         ntr->notes = q;
1450         for(p = ntr->notes; p != NULL; p = p->next)
1451             p->gate -= mingate;
1452         rcp_tempo_gradate(ntr, mingate);
1453         ntr->at += mingate;
1454     }
1455 }
1456
1457 static int preprocess_sysex(uint8 *ex, int ch, int gt, int vel)
1458 {
1459     uint8 cs = 0;               /* check sum */
1460     int len = 0;
1461     int i;
1462
1463     for(i = 0; i < MAX_EXCLUSIVE_LENGTH && ex[i] != 0xf7; i++)
1464     {
1465         switch(ex[i])
1466         {
1467           case 0x80: /* gt */
1468             cs += ex[len++] = gt;
1469             break;
1470           case 0x81: /* ve */
1471             cs += ex[len++] = vel;
1472             break;
1473           case 0x82: /* ch */
1474             cs += ex[len++] = ch;
1475             break;
1476           case 0x83: /* (Clear Sum) */
1477 #if 0
1478             if(ex[0] == 0x41 && len == 3 &&
1479                ex[len - 2] == 0x10 && ex[len - 1] == 0x12) /* ##?? */
1480             {
1481                 ex[len - 1] = 0x42;
1482                 ex[len++] = 0x12;
1483             }
1484 #endif
1485             cs = 0;
1486             break;
1487           case 0x84: /* (Send Sum) */
1488             ex[len++] = 128 - (cs & 0x7f);
1489             break;
1490           default:
1491             cs += ex[len++] = ex[i];
1492             break;
1493         }
1494     }
1495     ex[len++] = 0xf7;
1496     return len;
1497 }