-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmfi.c
More file actions
executable file
·623 lines (586 loc) · 20.1 KB
/
mfi.c
File metadata and controls
executable file
·623 lines (586 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
mfi.c (Melody Format for i-mode)
by Kentaro Sato <kentaro@ranvis.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "timidity.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "controls.h"
#include <stdlib.h>
#define SETMIDIEVENT(e, at, t, ch, pa, pb) \
{ (e).time = (at); (e).type = (t); \
(e).channel = (uint8)(ch); (e).a = (uint8)(pa); (e).b = (uint8)(pb); }
#define MIDIEVENT(at, t, ch, pa, pb) \
{ MidiEvent event; SETMIDIEVENT(event, at, t, ch, pa, pb); \
readmidi_add_event(&event); }
#ifdef LITTLE_ENDIAN
#define BE_FCC(type) ((uint32)XCHG_LONG_CONST(type))
#else /* BIG ENDIAN */
#define BE_FCC(type) ((uint32)BE_LONG(type))
#endif
/*
Note: Comparing MFi FCC (Four Character Code)
In MFi, FCC has the same bit length as 4-byte-integer such as dataLength,
so loading and comparing it like an integer should be portable enough.
*/
#define MAPBITS(effInt, effBits, mapBits) (((effInt) << ((mapBits) - (effBits))) | ((effInt) >> ((effBits) - ((mapBits) - (effBits)))))
#define MAPBITS2(effInt, effBits, mapBits) (((effInt) << ((mapBits) - (effBits))) | (((effInt) << ((mapBits) - (effBits))) >> (effBits)) | (((effInt) << ((mapBits) - (effBits))) >> ((effBits) * 2)))
#define MFI_DEBUG_PREFIX "MFi "
#define MFI_DEBUG_MORE_VERBOSE 0
#define MFI_DEBUG_NOTE_EVENT 0
#define MFI_DEBUG_NOTE_EVENT_S 0
#define MFI_DEBUG_CTL_DATA 0
#define MFI_DEBUG_UNKNOWN_CTL_DATA 1
#define POS_DS(pos) ((pos) / 48), ((pos) % 48)
#if MFI_DEBUG_MORE_VERBOSE
#undef VERB_DEBUG
#undef VERB_NOISY
#define VERB_DEBUG VERB_NORMAL
#define VERB_NOISY VERB_NORMAL
#endif
#if MFI_DEBUG_NOTE_EVENT
#define NOTE_EVENT_DEBUGSTR(channel, note, octave, velocity, duration) \
ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %s%d@%d %d(%d.%02d)", POS_DS(pos), (channel)+1, note, octave, velocity, duration, POS_DS(pos + duration))
#define NOTE_EVENT_POSSIBLE_SLUR_DEBUGSTR() /* empty */
#else
#define NOTE_EVENT_DEBUGSTR(channel, note, octave, velocity, duration) /* empty */
#define NOTE_EVENT_POSSIBLE_SLUR_DEBUGSTR() /* empty */
#endif
#if MFI_DEBUG_NOTE_EVENT_S
#define NOTE_BUF_EV_DEBUGSTR(channel, time, note, octave, velocity, offtime) \
ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "<< %d [%02d] %s%d@%d %d", time, (channel)+1, note, octave, velocity, offtime)
#else
#define NOTE_BUF_EV_DEBUGSTR(channel, time, note, octave, velocity, offtime) /* empty */
#endif
#if MFI_DEBUG_CTL_DATA
#define EX_DATA_DEBUGSTR1(exname, channel, paramstr, p1) \
ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %02X(%s) : " paramstr, POS_DS(pos), (channel)+1, data[2], exname, p1)
#define EX_NCDATA_DEBUGSTR1(exname, paramstr, p1) \
ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [--] %02X(%s) : " paramstr, POS_DS(pos), data[2], exname, p1)
#define EX_DATA_DEBUGSTR2(exname, channel, paramstr, p1, p2) \
ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %02X(%s) : " paramstr, POS_DS(pos), (channel)+1, data[2], exname, p1, p2)
#else
#define EX_DATA_DEBUGSTR1(exname, channel, paramstr, p1) /* empty */
#define EX_NCDATA_DEBUGSTR1(exname, channel, p1) /* empty */
#define EX_DATA_DEBUGSTR2(exname, channel, paramstr, p1, p2) /* empty */
#endif
#if MFI_DEBUG_UNKNOWN_CTL_DATA
#define EX_UNKNOWN_DATA_DEBUGSTR() \
ctl->cmsg(CMSG_WARNING, VERB_NOISY, MFI_DEBUG_PREFIX "%d.%02d %02X (not implemented) : %02X", POS_DS(pos), data[2], data[3])
#define EX_UNKNOWN_EXT_DATA_DEBUGSTR(len) \
ctl->cmsg(CMSG_WARNING, VERB_NOISY, MFI_DEBUG_PREFIX "%d.%02d %02X (not implemented) : %04X", POS_DS(pos), data[2], len)
#else
#define EX_UNKNOWN_DATA_DEBUGSTR() /* empty */
#define EX_UNKNOWN_EXT_DATA_DEBUGSTR(len) /* empty */
#endif
typedef struct timidity_file timidity_file;
static int tf_read_beint16(int *, timidity_file *);
static int tf_read_beint32(int *, timidity_file *);
static int read_mfi_information(int, int *, int *, int *, timidity_file *);
static int read_mfi_track(int, int, int, int, int, timidity_file *);
int read_mfi_file(timidity_file *tf)
{
int length, dataLength, infoLength, dataType, mfiVersion, noteType, extStDLength;
uint32 type;
uint8 numTracks;
int i;
if (tf_read_beint32(&dataLength, tf) != 1
|| tf_read_beint16(&infoLength, tf) != 1
|| tf_read_beint16(&dataType, tf) != 1
|| tf_read(&numTracks, 1, 1, tf) != 1)
return 1;
infoLength -= 2 + 1;
if (dataType == 0x0202)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "MFi Type 2.2 may not be playable."); /* I'm not sure :-) */
return 1;
}
if (numTracks == 0)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "MFi contains no track.");
return 1;
}
ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "MFi Tracks: %d", numTracks);
if (numTracks > MAX_CHANNELS / 4)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Too many tracks, last %d track(s) are ignored.", numTracks - (MAX_CHANNELS / 4));
numTracks = MAX_CHANNELS / 4;
}
current_file_info->divisions = 48;
current_file_info->format = 1;
current_file_info->tracks = numTracks;
current_file_info->file_type = IS_MFI_FILE;
noteType = extStDLength = 0;
if (read_mfi_information(infoLength, &mfiVersion, ¬eType, &extStDLength, tf) != 0)
return 1;
for(i = 0; i < numTracks; i++)
{
if (tf_read(&type, 4, 1, tf) != 1
|| tf_read_beint32(&length, tf) != 1)
return 1;
if (type != BE_FCC(0x74726163 /*trac*/))
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown track signature.");
return 1;
}
if (read_mfi_track(i, length, mfiVersion, noteType, extStDLength, tf) != 0)
return 1;
}
return 0;
}
static int read_mfi_information(int infoLength, int *mfiVersion, int *noteType, int *extStDLength, timidity_file *tf)
{
int length;
uint32 type;
uint8 byteData;
char buf[512];
*mfiVersion = 1;
while (infoLength > 0)
{
if (infoLength < 4 + 2)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Odd information length.");
return 1;
}
infoLength -= 6;
if (tf_read(&type, 4, 1, tf) != 1
|| tf_read_beint16(&length, tf) != 1)
return 1;
if (length == 0)
continue;
if (length > infoLength)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Total information length was too small.");
return 1;
}
infoLength -= length;
switch(type)
{
case BE_FCC(0x7469746C /*titl*/): { /* title */
char *title;
if (current_file_info->seq_name == NULL)
goto skip_info_data;
title = safe_malloc(length + 1);
if (tf_read(title, length, 1, tf) != 1)
{
free(title);
return 1;
}
title[length] = '\0';
current_file_info->seq_name = title;
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Title: %s", title);
} break;
case BE_FCC(0x736F7263 /*sorc*/): { /* source */
const char *srcInfo;
if (length != 1)
goto skip_info_data;
if (tf_read(&byteData, 1, 1, tf) != 1)
return 1;
switch (byteData >> 1)
{
case 0x00: srcInfo = "network"; break;
case 0x01: srcInfo = "manual"; break;
case 0x02: srcInfo = "external"; break;
default: srcInfo = "unknown";
}
ctl->cmsg(CMSG_INFO, VERB_NOISY, "Source: %s%s", srcInfo, (byteData & 1) ? ", copyrighted" : "");
} break;
case BE_FCC(0x76657273 /*vers*/): /* version (unused) */
if (tf_read(&type, 4, 1, tf) != 1)
return 1;
switch(type)
{
case BE_FCC(0x30313030 /*0100*/): *mfiVersion = 1; break;
case BE_FCC(0x30323030 /*0200*/): *mfiVersion = 2; break;
case BE_FCC(0x30333030 /*0300*/): *mfiVersion = 3; break;
default:
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown MFi version.");
//return 1;
}
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "MFi Version: %d", *mfiVersion);
/*
info/controls which are marked as '(MFi*)' are only executable when
mfiVersion == * or mfiVersion >= *.
*/
break;
case BE_FCC(0x64617465 /*date*/): { /* created date */
char created[9];
if (length != 8)
goto skip_info_data;
if (tf_read(created, 8, 1, tf) != 1)
return 1;
created[8] = '\0';
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Date: %.4s-%.2s-%.2s", created, &created[4], &created[6]);
} break;
case BE_FCC(0x636F7079 /*copy*/): { /* copyright */
int lengthToRead;
lengthToRead = length >= sizeof buf ? sizeof buf - 1 : length;
if (lengthToRead > 0 && tf_read(buf, lengthToRead, 1, tf) != 1)
return 1;
buf[lengthToRead] = '\0';
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Copyright: %s", buf);
if (lengthToRead < length && tf_seek(tf, length - lengthToRead, SEEK_CUR) == -1)
return 1;
} break;
/* case BE_FCC(0x70726F74 /-*prot*-/):
goto skip_info_data; */
case BE_FCC(0x6E6F7465 /*note*/): /* note (MFi2) */
if (length != 2 || tf_read_beint16(noteType, tf) != 1)
return 1;
if (*noteType > 1)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown note information.");
return 1;
}
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Note Type: %d", *noteType);
break;
case BE_FCC(0x65787374 /*exst*/): /* extended status (MFi2) */
if (length != 2 || tf_read_beint16(extStDLength, tf) != 1)
return 1;
if (*extStDLength != 0)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown extended status information. (%d)");
return 1;
}
break;
default:
skip_info_data:
if (tf_seek(tf, length, SEEK_CUR) == -1)
return 1;
}
}
return 0;
}
char *get_mfi_file_title(timidity_file *tf)
{
int length, dataLength, infoLength, dataType;
uint32 type;
uint8 numTracks;
if (tf_read_beint32(&dataLength, tf) != 1
|| tf_read_beint16(&infoLength, tf) != 1
|| tf_read_beint16(&dataType, tf) != 1
|| dataType == 0x0202
|| tf_read(&numTracks, 1, 1, tf) != 1)
return NULL;
infoLength -= 2 + 1;
while (infoLength >= 4 + 2)
{
infoLength -= 6;
if (tf_read(&type, 4, 1, tf) != 1
|| tf_read_beint16(&length, tf) != 1)
return NULL;
if (length > infoLength)
break;
infoLength -= length;
if (type == BE_FCC(0x7469746C /*titl*/))
{
char *title;
if (length == 0)
return NULL;
if ((title = malloc(length + 1)) == NULL)
break;
if (tf_read(title, length, 1, tf) != 1)
{
free(title);
break;
}
title[length] = '\0';
return title;
}
else if (length != 0 && tf_seek(tf, length, SEEK_CUR) == -1)
break;
}
return NULL;
}
typedef struct LastNoteInfo {
int on, off, note, velocity;
} LastNoteInfo;
#define NO_LAST_NOTE_INFO -1
#define LASTNOTEINFO_HAS_DATA(lni) ((lni).on != NO_LAST_NOTE_INFO)
#define SEND_LASTNOTEINFO(lni, ch) if (LASTNOTEINFO_HAS_DATA((lni)[ch])) SendLastNoteInfo(lni, ch);
#define SEND_AND_CLEAR_LASTNOTEINFO(lni, ch) if (LASTNOTEINFO_HAS_DATA((lni)[ch])) { SendLastNoteInfo(lni, ch); (lni)[ch].on = NO_LAST_NOTE_INFO; }
void StoreLastNoteInfo(LastNoteInfo *info, int channel, int time, int duration, int note, int velocity)
{
info[channel].on = time;
info[channel].off = time + duration;
info[channel].note = note;
info[channel].velocity = velocity;
}
void SendLastNoteInfo(const LastNoteInfo *info, int channel)
{
NOTE_BUF_EV_DEBUGSTR(channel, info[channel].on, note_name[info[channel].note % 12], info[channel].note / 12, info[channel].velocity, info[channel].off);
MIDIEVENT(info[channel].on, ME_NOTEON, channel, info[channel].note, info[channel].velocity);
MIDIEVENT(info[channel].off, ME_NOTEOFF, channel, info[channel].note, 0);
}
#define CHECK_AND_READ_FROM_FILE(ptr, readLen) do { \
if ((length) < (readLen) || tf_read(ptr, readLen, 1, tf) != 1) { \
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Odd track length."); \
return 1; \
} \
length -= readLen; \
} while(0)
static int read_mfi_track(int trackNo, int length, int mfiVersion, int noteType, int extStDLength, timidity_file *tf)
{
uint8 data[5];
int i, pos, note, velocity, dataLength;
uint8 instruments[MAX_CHANNELS];
int channelMap[4];
LastNoteInfo lastNotes[MAX_CHANNELS];
readmidi_set_track(trackNo, 1);
pos = 0;
velocity = 0x7F;
for(i = 0; i < 4; i++)
channelMap[i] = (trackNo * 4) + i;
dataLength = (noteType == 1) ? 4 : 3;
data[3] = 0; /* initialize for debugging purpose */
for(i = 0; i < MAX_CHANNELS; i++)
lastNotes[i].on = NO_LAST_NOTE_INFO;
while (length > 0)
{
CHECK_AND_READ_FROM_FILE(data, dataLength);
pos += data[0];
if (data[1] != 0xFF) /* note */
{
int channel;
channel = channelMap[data[1] >> 6];
note = 0x48 - 0x1B + (data[1] & 0x3F);
if (dataLength >= 4)
{
velocity = ((data[3] & 0xFC) >> 1) | (data[3] >> 7); /* abcdefgh -> 0abcdefa */
if (data[3] & 0x2) /* sign */
data[3] |= ~0x01;
else
data[3] &= 0x01;
note += ((int8)data[3]) * 12; /* octave shift */
}
if (LASTNOTEINFO_HAS_DATA(lastNotes[channel]))
{
if (lastNotes[channel].off <= pos
|| note != lastNotes[channel].note)
{
SendLastNoteInfo(lastNotes, channel);
StoreLastNoteInfo(lastNotes, channel, pos, data[2], note, velocity);
}
#if 0
else if (note != lastNotes[channel].note) /* possible slur */
{
if (lastNotes[channel].on == pos) /* may be a chord */
{
SendLastNoteInfo(lastNotes, channel);
StoreLastNoteInfo(lastNotes, channel, pos, data[2], note, velocity);
}
else /* slur */
{
/* not implemented */
}
}
#endif
else /* tie, what if the velocity isn't the same? :-) */
lastNotes[channel].off = pos + data[2];
}
else
StoreLastNoteInfo(lastNotes, channel, pos, data[2], note, velocity);
NOTE_EVENT_DEBUGSTR(channel, note_name[note % 12], note / 12, velocity, data[2]);
}
else /* controls */
{
if (dataLength == 3)
CHECK_AND_READ_FROM_FILE(&data[3], 1);
if ((data[2] & 0xF0) == 0xC0) /* tempo */
{
int timebase, tempo;
timebase = data[2] & 0xF;
if ((timebase & 0x7) == 0x7)
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Undefined tempo timebase.");
else
{
if (timebase & 0x8)
timebase = 15 << (timebase & 0x7);
else
timebase = 6 << (timebase & 0x7);
tempo = 48 * (1000000 * 60 / data[3] / timebase);
MIDIEVENT(pos, ME_TEMPO, tempo & 0xFF, (tempo >> 16) & 0xFF, (tempo >> 8) & 0xFF);
}
}
else if ((data[2] & 0xF0) == 0xF0) /* extended controls */
{
int extLength, channel;
uint8 extData[512];
CHECK_AND_READ_FROM_FILE(&data[4], 1);
extLength = (data[3] << 8) | data[4];
if (extLength <= sizeof extData)
{
CHECK_AND_READ_FROM_FILE(extData, extLength);
switch(data[2] & 0xF)
{
/* case 0x0: /-* modify envelope (MFi1) */
case 0x1: /* vibrato (MFi1) */
if (mfiVersion == 1 && extData[0] == 0x01 && extData[1] & 0x01)
{
channel = channelMap[extData[1] >> 5];
MIDIEVENT(pos, ME_MODULATION_WHEEL, channel, (extData[2] & 0xC0) ? 64 : 0, 0)
}
break;
/*case 0xF: /-* system exclusive (MFi2 or MFi3) */
default:
EX_UNKNOWN_EXT_DATA_DEBUGSTR(extLength);
}
}
else
{
if (tf_seek(tf, extLength, SEEK_CUR) == -1)
return 1;
EX_UNKNOWN_EXT_DATA_DEBUGSTR(extLength);
length -= extLength;
}
}
else
{
int part, channel, value;
#define GET_PART_AND_CHANNEL(p, c) (p) = data[3] >> 6; (c) = channelMap[p]
switch(data[2])
{
case 0xB0: /* master volume */
value = MAPBITS2(data[3] & 0x7F, 7, 16);
MIDIEVENT(pos, ME_MASTER_VOLUME, 0, value & 0xFF, value >> 8);
EX_NCDATA_DEBUGSTR1("Master Volume", "%d", value);
break;
case 0xBA: /* set drum channel flag */
channel = (data[3] >> 3) & 0xF;
value = data[3] & 0x1;
MIDIEVENT(pos, ME_DRUMPART, channel, value, 0);
EX_DATA_DEBUGSTR1("Drum Flag", channel, "%d", value);
break;
case 0xD0: /* music begin/end */
/* ignored */
break;
/* case 0xDD: /-* loop begin/end */
case 0xDE: /* nop */
break;
case 0xDF: /* end-of-track */
if (length != 0)
{
ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Premature end-of-track (%d)", length);
length = 0;
}
break;
case 0xE0: /* program change */
GET_PART_AND_CHANNEL(part, channel);
SEND_AND_CLEAR_LASTNOTEINFO(lastNotes, channel);
/*MIDIEVENT(pos, ME_DRUMPART, channel, 0, 0);*/
instruments[channel] = (instruments[channel] & 0x40) | (data[3] & 0x3F);
MIDIEVENT(pos, ME_PROGRAM, channel, instruments[channel], 0);
EX_DATA_DEBUGSTR1("Program Change", channel, "%d", instruments[channel]);
break;
case 0xE1: /* pre program change */
GET_PART_AND_CHANNEL(part, channel);
instruments[channel] = (data[3] & 0x1) << 6;
EX_DATA_DEBUGSTR1("Pre Program Change", channel, "%d", instruments[channel]);
break;
case 0xE2: /* volume */
GET_PART_AND_CHANNEL(part, channel);
value = MAPBITS(data[3] & 0x3F, 6, 7);
MIDIEVENT(pos, ME_MAINVOLUME, channel, value, 0);
EX_DATA_DEBUGSTR1("Volume", channel, "%d", value);
break;
case 0xE3: /* pan */
GET_PART_AND_CHANNEL(part, channel);
value = MAPBITS(data[3] & 0x3F, 6, 7);
MIDIEVENT(pos, ME_PAN, channel, value, 0);
EX_DATA_DEBUGSTR1("Pan", channel, "%d", value);
break;
case 0xE4: /* pitch bend (MFi3) */
if (mfiVersion >= 3)
{
GET_PART_AND_CHANNEL(part, channel);
value = MAPBITS2(data[3] & 0x3F, 6, 14);
MIDIEVENT(pos, ME_PITCHWHEEL, channel, value & 0x7F, value >> 7);
EX_DATA_DEBUGSTR1("Pitch Bend", channel, "%d", value);
}
break;
case 0xE5: /* map part to channel */
part = data[3] >> 6;
SEND_AND_CLEAR_LASTNOTEINFO(lastNotes, channelMap[part]);
channelMap[part] = data[3] & 0xF;
SEND_AND_CLEAR_LASTNOTEINFO(lastNotes, channelMap[part]);
EX_DATA_DEBUGSTR1("Map Channel", part, "%d", channelMap[part]);
break;
case 0xE6: /* expression */
GET_PART_AND_CHANNEL(part, channel);
value = data[3] & 0x3F;
if (value & 0x20)
value = 64 - (((value ^ 0x3F) + 1) << 1);
else if (value & 0x10)
value = 64 + 1 + (value << 1);
else
value = 64 + (value << 1);
MIDIEVENT(pos, ME_EXPRESSION, channel, value, 0);
EX_DATA_DEBUGSTR1("Expression", channel, "%d", value);
break;
case 0xE7: /* pitch bend range (MFi3) */
if (mfiVersion >= 3)
{
GET_PART_AND_CHANNEL(part, channel);
value = data[3] & 0x3F;
MIDIEVENT(pos, ME_RPN_MSB, channel, 0, 0);
MIDIEVENT(pos, ME_RPN_LSB, channel, 0, 0);
MIDIEVENT(pos, ME_DATA_ENTRY_MSB, channel, value, 0);
EX_DATA_DEBUGSTR1("Pitch Bend Range", channel, "%d", value);
}
break;
case 0xEA: /* vibrato (MFi3) */
if (mfiVersion >= 3)
{
GET_PART_AND_CHANNEL(part, channel);
value = MAPBITS(data[3] & 0x3F, 6, 7);
MIDIEVENT(pos, ME_MODULATION_WHEEL, channel, value, 0);
EX_DATA_DEBUGSTR1("Vibrato", channel, "%d", value);
}
break;
default:
EX_UNKNOWN_DATA_DEBUGSTR();
}
}
}
}
for(i = 0; i < MAX_CHANNELS; i++)
{
SEND_LASTNOTEINFO(lastNotes, i);
}
return 0;
}
static int tf_read_beint16(int *value, timidity_file *tf)
{
uint16 value_;
if (tf_read(&value_, 2, 1, tf) != 1)
return 0;
*value = BE_SHORT(value_);
return 1;
}
static int tf_read_beint32(int *value, timidity_file *tf)
{
uint32 value_;
if (tf_read(&value_, 4, 1, tf) != 1)
return 0;
*value = BE_LONG(value_);
return 1;
}