-
Notifications
You must be signed in to change notification settings - Fork 259
/
fluid_midi.c
2740 lines (2335 loc) · 69.7 KB
/
fluid_midi.c
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FluidSynth - A Software Synthesizer
*
* Copyright (C) 2003 Peter Hanappe and others.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "fluid_midi.h"
#include "fluid_sys.h"
#include "fluid_synth.h"
#include "fluid_settings.h"
static int fluid_midi_event_length(unsigned char event);
static int fluid_isasciistring(char *s);
static long fluid_getlength(const unsigned char *s);
/* Read the entire contents of a file into memory, allocating enough memory
* for the file, and returning the length and the buffer.
* Note: This rewinds the file to the start before reading.
* Returns NULL if there was an error reading or allocating memory.
*/
typedef FILE *fluid_file;
static char *fluid_file_read_full(fluid_file fp, size_t *length);
static void fluid_midi_event_set_sysex_LOCAL(fluid_midi_event_t *evt, int type, void *data, int size, int dynamic);
static void fluid_midi_event_get_sysex_LOCAL(fluid_midi_event_t *evt, void **data, int *size);
#define READ_FULL_INITIAL_BUFLEN 1024
static fluid_track_t *new_fluid_track(int num);
static void delete_fluid_track(fluid_track_t *track);
static int fluid_track_set_name(fluid_track_t *track, char *name);
static int fluid_track_add_event(fluid_track_t *track, fluid_midi_event_t *evt);
static fluid_midi_event_t *fluid_track_next_event(fluid_track_t *track);
static int fluid_track_get_duration(fluid_track_t *track);
static int fluid_track_reset(fluid_track_t *track);
static int fluid_player_add_track(fluid_player_t *player, fluid_track_t *track);
static int fluid_player_callback(void *data, unsigned int msec);
static int fluid_player_reset(fluid_player_t *player);
static int fluid_player_load(fluid_player_t *player, fluid_playlist_item *item);
static void fluid_player_advancefile(fluid_player_t *player);
static void fluid_player_playlist_load(fluid_player_t *player, unsigned int msec);
static void fluid_player_update_tempo(fluid_player_t *player);
static fluid_midi_file *new_fluid_midi_file(const char *buffer, size_t length);
static void delete_fluid_midi_file(fluid_midi_file *mf);
static int fluid_midi_file_read_mthd(fluid_midi_file *midifile);
static int fluid_midi_file_load_tracks(fluid_midi_file *midifile, fluid_player_t *player);
static int fluid_midi_file_read_track(fluid_midi_file *mf, fluid_player_t *player, int num);
static int fluid_midi_file_read_event(fluid_midi_file *mf, fluid_track_t *track);
static int fluid_midi_file_read_varlen(fluid_midi_file *mf);
static int fluid_midi_file_getc(fluid_midi_file *mf);
static int fluid_midi_file_push(fluid_midi_file *mf, int c);
static int fluid_midi_file_read(fluid_midi_file *mf, void *buf, int len);
static int fluid_midi_file_skip(fluid_midi_file *mf, int len);
static int fluid_midi_file_eof(fluid_midi_file *mf);
static int fluid_midi_file_read_tracklen(fluid_midi_file *mf);
static int fluid_midi_file_eot(fluid_midi_file *mf);
static int fluid_midi_file_get_division(fluid_midi_file *midifile);
/***************************************************************
*
* MIDIFILE
*/
/**
* Check if a file is a MIDI file.
* @param filename Path to the file to check
* @return TRUE if it could be a MIDI file, FALSE otherwise
*
* The current implementation only checks for the "MThd" header in the file.
* It is useful only to distinguish between SoundFont and MIDI files.
*/
int fluid_is_midifile(const char *filename)
{
FILE *fp;
uint32_t id;
int retcode = FALSE;
do
{
if((fp = fluid_file_open(filename, NULL)) == NULL)
{
return retcode;
}
if(FLUID_FREAD(&id, sizeof(id), 1, fp) != 1)
{
break;
}
retcode = (id == FLUID_FOURCC('M', 'T', 'h', 'd'));
}
while(0);
FLUID_FCLOSE(fp);
return retcode;
}
/**
* Return a new MIDI file handle for parsing an already-loaded MIDI file.
* @internal
* @param buffer Pointer to full contents of MIDI file (borrows the pointer).
* The caller must not free buffer until after the fluid_midi_file is deleted.
* @param length Size of the buffer in bytes.
* @return New MIDI file handle or NULL on error.
*/
fluid_midi_file *
new_fluid_midi_file(const char *buffer, size_t length)
{
fluid_midi_file *mf;
mf = FLUID_NEW(fluid_midi_file);
if(mf == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
return NULL;
}
FLUID_MEMSET(mf, 0, sizeof(fluid_midi_file));
mf->c = -1;
mf->running_status = -1;
mf->buffer = buffer;
mf->buf_len = length;
mf->buf_pos = 0;
mf->eof = FALSE;
if(fluid_midi_file_read_mthd(mf) != FLUID_OK)
{
FLUID_FREE(mf);
return NULL;
}
return mf;
}
static char *
fluid_file_read_full(fluid_file fp, size_t *length)
{
size_t buflen;
char *buffer;
size_t n;
/* Work out the length of the file in advance */
if(FLUID_FSEEK(fp, 0, SEEK_END) != 0)
{
FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
return NULL;
}
buflen = ftell(fp);
if(FLUID_FSEEK(fp, 0, SEEK_SET) != 0)
{
FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
return NULL;
}
FLUID_LOG(FLUID_DBG, "File load: Allocating %lu bytes", (unsigned long)buflen);
buffer = FLUID_MALLOC(buflen);
if(buffer == NULL)
{
FLUID_LOG(FLUID_PANIC, "Out of memory");
return NULL;
}
n = FLUID_FREAD(buffer, 1, buflen, fp);
if(n != buflen)
{
FLUID_LOG(FLUID_ERR, "Only read %lu bytes; expected %lu", (unsigned long)n,
(unsigned long)buflen);
FLUID_FREE(buffer);
return NULL;
};
*length = n;
return buffer;
}
/**
* Delete a MIDI file handle.
* @internal
* @param mf MIDI file handle to close and free.
*/
void
delete_fluid_midi_file(fluid_midi_file *mf)
{
fluid_return_if_fail(mf != NULL);
FLUID_FREE(mf);
}
/*
* Gets the next byte in a MIDI file, taking into account previous running status.
*
* returns -1 if EOF or read error
*/
int
fluid_midi_file_getc(fluid_midi_file *mf)
{
unsigned char c;
if(mf->c >= 0)
{
c = mf->c;
mf->c = -1;
}
else
{
if(mf->buf_pos >= mf->buf_len)
{
mf->eof = TRUE;
return -1;
}
c = mf->buffer[mf->buf_pos++];
mf->trackpos++;
}
return (int) c;
}
/*
* Saves a byte to be returned the next time fluid_midi_file_getc() is called,
* when it is necessary according to running status.
*/
int
fluid_midi_file_push(fluid_midi_file *mf, int c)
{
mf->c = c;
return FLUID_OK;
}
/*
* fluid_midi_file_read
*/
int
fluid_midi_file_read(fluid_midi_file *mf, void *buf, int len)
{
int num = len < mf->buf_len - mf->buf_pos
? len : mf->buf_len - mf->buf_pos;
if(num != len)
{
mf->eof = TRUE;
}
if(num < 0)
{
num = 0;
}
/* Note: Read bytes, even if there aren't enough, but only increment
* trackpos if successful (emulates old behaviour of fluid_midi_file_read)
*/
FLUID_MEMCPY(buf, mf->buffer + mf->buf_pos, num);
mf->buf_pos += num;
if(num == len)
{
mf->trackpos += num;
}
#if DEBUG
else
{
FLUID_LOG(FLUID_DBG, "Could not read the requested number of bytes");
}
#endif
return (num != len) ? FLUID_FAILED : FLUID_OK;
}
/*
* fluid_midi_file_skip
*/
int
fluid_midi_file_skip(fluid_midi_file *mf, int skip)
{
int new_pos = mf->buf_pos + skip;
/* Mimic the behaviour of fseek: Error to seek past the start of file, but
* OK to seek past end (this just puts it into the EOF state). */
if(new_pos < 0)
{
FLUID_LOG(FLUID_ERR, "Failed to seek position in file");
return FLUID_FAILED;
}
/* Clear the EOF flag, even if moved past the end of the file (this is
* consistent with the behaviour of fseek). */
mf->eof = FALSE;
mf->buf_pos = new_pos;
return FLUID_OK;
}
/*
* fluid_midi_file_eof
*/
int fluid_midi_file_eof(fluid_midi_file *mf)
{
/* Note: This does not simply test whether the file read pointer is past
* the end of the file. It mimics the behaviour of feof by actually
* testing the stateful EOF condition, which is set to TRUE if getc or
* fread have attempted to read past the end (but not if they have
* precisely reached the end), but reset to FALSE upon a successful seek.
*/
return mf->eof;
}
/*
* fluid_midi_file_read_mthd
*/
int
fluid_midi_file_read_mthd(fluid_midi_file *mf)
{
char mthd[14];
if(fluid_midi_file_read(mf, mthd, sizeof(mthd)) != FLUID_OK)
{
return FLUID_FAILED;
}
if((FLUID_STRNCMP(mthd, "MThd", 4) != 0) || (mthd[7] != 6)
|| (mthd[9] > 2))
{
FLUID_LOG(FLUID_ERR,
"Doesn't look like a MIDI file: invalid MThd header");
return FLUID_FAILED;
}
mf->type = mthd[9];
mf->ntracks = (unsigned) mthd[11];
mf->ntracks += (unsigned int)(mthd[10]) << 16;
if((signed char)mthd[12] < 0)
{
mf->uses_smpte = 1;
mf->smpte_fps = -(signed char)mthd[12];
mf->smpte_res = (unsigned) mthd[13];
FLUID_LOG(FLUID_ERR, "File uses SMPTE timing -- Not implemented yet");
return FLUID_FAILED;
}
else
{
mf->uses_smpte = 0;
mf->division = ((unsigned)mthd[12] << 8) | ((unsigned)mthd[13] & 0xff);
FLUID_LOG(FLUID_DBG, "Division=%d", mf->division);
}
return FLUID_OK;
}
/*
* fluid_midi_file_load_tracks
*/
int
fluid_midi_file_load_tracks(fluid_midi_file *mf, fluid_player_t *player)
{
int i;
for(i = 0; i < mf->ntracks; i++)
{
if(fluid_midi_file_read_track(mf, player, i) != FLUID_OK)
{
return FLUID_FAILED;
}
}
return FLUID_OK;
}
/*
* fluid_isasciistring
*/
int
fluid_isasciistring(char *s)
{
/* From ctype.h */
#define fluid_isascii(c) (((c) & ~0x7f) == 0)
size_t i, len = FLUID_STRLEN(s);
for(i = 0; i < len; i++)
{
if(!fluid_isascii(s[i]))
{
return 0;
}
}
return 1;
#undef fluid_isascii
}
/*
* fluid_getlength
*/
long
fluid_getlength(const unsigned char *s)
{
long i = 0;
i = s[3] | (s[2] << 8) | (s[1] << 16) | (s[0] << 24);
return i;
}
/*
* fluid_midi_file_read_tracklen
*/
int
fluid_midi_file_read_tracklen(fluid_midi_file *mf)
{
unsigned char length[5];
if(fluid_midi_file_read(mf, length, 4) != FLUID_OK)
{
return FLUID_FAILED;
}
mf->tracklen = fluid_getlength(length);
mf->trackpos = 0;
mf->eot = 0;
return FLUID_OK;
}
/*
* fluid_midi_file_eot
*/
int
fluid_midi_file_eot(fluid_midi_file *mf)
{
#if DEBUG
if(mf->trackpos > mf->tracklen)
{
printf("track overrun: %d > %d\n", mf->trackpos, mf->tracklen);
}
#endif
return mf->eot || (mf->trackpos >= mf->tracklen);
}
/*
* fluid_midi_file_read_track
*/
int
fluid_midi_file_read_track(fluid_midi_file *mf, fluid_player_t *player, int num)
{
fluid_track_t *track;
unsigned char id[5], length[5];
int found_track = 0;
int skip;
if(fluid_midi_file_read(mf, id, 4) != FLUID_OK)
{
return FLUID_FAILED;
}
id[4] = '\0';
mf->dtime = 0;
while(!found_track)
{
if(fluid_isasciistring((char *) id) == 0)
{
FLUID_LOG(FLUID_ERR,
"An non-ascii track header found, corrupt file");
return FLUID_FAILED;
}
else if(FLUID_STRCMP((char *) id, "MTrk") == 0)
{
found_track = 1;
if(fluid_midi_file_read_tracklen(mf) != FLUID_OK)
{
return FLUID_FAILED;
}
track = new_fluid_track(num);
if(track == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
return FLUID_FAILED;
}
while(!fluid_midi_file_eot(mf))
{
if(fluid_midi_file_read_event(mf, track) != FLUID_OK)
{
delete_fluid_track(track);
return FLUID_FAILED;
}
}
/* Skip remaining track data, if any */
if(mf->trackpos < mf->tracklen)
{
if(fluid_midi_file_skip(mf, mf->tracklen - mf->trackpos) != FLUID_OK)
{
delete_fluid_track(track);
return FLUID_FAILED;
}
}
if(fluid_player_add_track(player, track) != FLUID_OK)
{
delete_fluid_track(track);
return FLUID_FAILED;
}
}
else
{
found_track = 0;
if(fluid_midi_file_read(mf, length, 4) != FLUID_OK)
{
return FLUID_FAILED;
}
skip = fluid_getlength(length);
/* fseek(mf->fp, skip, SEEK_CUR); */
if(fluid_midi_file_skip(mf, skip) != FLUID_OK)
{
return FLUID_FAILED;
}
}
}
if(fluid_midi_file_eof(mf))
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
return FLUID_OK;
}
/*
* fluid_midi_file_read_varlen
*/
int
fluid_midi_file_read_varlen(fluid_midi_file *mf)
{
int i;
int c;
mf->varlen = 0;
for(i = 0;; i++)
{
if(i == 4)
{
FLUID_LOG(FLUID_ERR, "Invalid variable length number");
return FLUID_FAILED;
}
c = fluid_midi_file_getc(mf);
if(c < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
if(c & 0x80)
{
mf->varlen |= (int)(c & 0x7F);
mf->varlen <<= 7;
}
else
{
mf->varlen += c;
break;
}
}
return FLUID_OK;
}
/*
* fluid_midi_file_read_event
*/
int
fluid_midi_file_read_event(fluid_midi_file *mf, fluid_track_t *track)
{
int status;
int type;
int tempo;
unsigned char *metadata = NULL;
unsigned char *dyn_buf = NULL;
unsigned char static_buf[256];
int nominator, denominator, clocks, notes;
fluid_midi_event_t *evt;
int channel = 0;
int param1 = 0;
int param2 = 0;
int size;
/* read the delta-time of the event */
if(fluid_midi_file_read_varlen(mf) != FLUID_OK)
{
return FLUID_FAILED;
}
mf->dtime += mf->varlen;
/* read the status byte */
status = fluid_midi_file_getc(mf);
if(status < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
/* not a valid status byte: use the running status instead */
if((status & 0x80) == 0)
{
if((mf->running_status & 0x80) == 0)
{
FLUID_LOG(FLUID_ERR, "Undefined status and invalid running status");
return FLUID_FAILED;
}
fluid_midi_file_push(mf, status);
status = mf->running_status;
}
/* check what message we have */
mf->running_status = status;
if(status == MIDI_SYSEX) /* system exclusif */
{
/* read the length of the message */
if(fluid_midi_file_read_varlen(mf) != FLUID_OK)
{
return FLUID_FAILED;
}
if(mf->varlen)
{
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
__LINE__, mf->varlen);
metadata = FLUID_MALLOC(mf->varlen + 1);
if(metadata == NULL)
{
FLUID_LOG(FLUID_PANIC, "Out of memory");
return FLUID_FAILED;
}
/* read the data of the message */
if(fluid_midi_file_read(mf, metadata, mf->varlen) != FLUID_OK)
{
FLUID_FREE(metadata);
return FLUID_FAILED;
}
evt = new_fluid_midi_event();
if(evt == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
FLUID_FREE(metadata);
return FLUID_FAILED;
}
evt->dtime = mf->dtime;
size = mf->varlen;
if(metadata[mf->varlen - 1] == MIDI_EOX)
{
size--;
}
/* Add SYSEX event and indicate that its dynamically allocated and should be freed with event */
fluid_midi_event_set_sysex(evt, metadata, size, TRUE);
fluid_track_add_event(track, evt);
mf->dtime = 0;
}
return FLUID_OK;
}
else if(status == MIDI_META_EVENT) /* meta events */
{
int result = FLUID_OK;
/* get the type of the meta message */
type = fluid_midi_file_getc(mf);
if(type < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
/* get the length of the data part */
if(fluid_midi_file_read_varlen(mf) != FLUID_OK)
{
return FLUID_FAILED;
}
if(mf->varlen < 255)
{
metadata = &static_buf[0];
}
else
{
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
__LINE__, mf->varlen);
dyn_buf = FLUID_MALLOC(mf->varlen + 1);
if(dyn_buf == NULL)
{
FLUID_LOG(FLUID_PANIC, "Out of memory");
return FLUID_FAILED;
}
metadata = dyn_buf;
}
/* read the data */
if(mf->varlen)
{
if(fluid_midi_file_read(mf, metadata, mf->varlen) != FLUID_OK)
{
if(dyn_buf)
{
FLUID_FREE(dyn_buf);
}
return FLUID_FAILED;
}
}
/* handle meta data */
switch(type)
{
case MIDI_COPYRIGHT:
metadata[mf->varlen] = 0;
break;
case MIDI_TRACK_NAME:
metadata[mf->varlen] = 0;
fluid_track_set_name(track, (char *) metadata);
break;
case MIDI_INST_NAME:
metadata[mf->varlen] = 0;
break;
case MIDI_LYRIC:
case MIDI_TEXT:
{
void *tmp;
int size = mf->varlen + 1;
/* NULL terminate strings for safety */
metadata[size - 1] = '\0';
evt = new_fluid_midi_event();
if(evt == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
result = FLUID_FAILED;
break;
}
evt->dtime = mf->dtime;
tmp = FLUID_MALLOC(size);
if(tmp == NULL)
{
FLUID_LOG(FLUID_PANIC, "Out of memory");
delete_fluid_midi_event(evt);
evt = NULL;
result = FLUID_FAILED;
break;
}
FLUID_MEMCPY(tmp, metadata, size);
fluid_midi_event_set_sysex_LOCAL(evt, type, tmp, size, TRUE);
fluid_track_add_event(track, evt);
mf->dtime = 0;
}
break;
case MIDI_MARKER:
break;
case MIDI_CUE_POINT:
break; /* don't care much for text events */
case MIDI_EOT:
if(mf->varlen != 0)
{
FLUID_LOG(FLUID_ERR, "Invalid length for EndOfTrack event");
result = FLUID_FAILED;
break;
}
mf->eot = 1;
evt = new_fluid_midi_event();
if(evt == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
result = FLUID_FAILED;
break;
}
evt->dtime = mf->dtime;
evt->type = MIDI_EOT;
fluid_track_add_event(track, evt);
mf->dtime = 0;
break;
case MIDI_SET_TEMPO:
if(mf->varlen != 3)
{
FLUID_LOG(FLUID_ERR,
"Invalid length for SetTempo meta event");
result = FLUID_FAILED;
break;
}
tempo = (metadata[0] << 16) + (metadata[1] << 8) + metadata[2];
evt = new_fluid_midi_event();
if(evt == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
result = FLUID_FAILED;
break;
}
evt->dtime = mf->dtime;
evt->type = MIDI_SET_TEMPO;
evt->channel = 0;
evt->param1 = tempo;
evt->param2 = 0;
fluid_track_add_event(track, evt);
mf->dtime = 0;
break;
case MIDI_SMPTE_OFFSET:
if(mf->varlen != 5)
{
FLUID_LOG(FLUID_ERR,
"Invalid length for SMPTE Offset meta event");
result = FLUID_FAILED;
break;
}
break; /* we don't use smtp */
case MIDI_TIME_SIGNATURE:
if(mf->varlen != 4)
{
FLUID_LOG(FLUID_ERR,
"Invalid length for TimeSignature meta event");
result = FLUID_FAILED;
break;
}
nominator = metadata[0];
denominator = pow(2.0, (double) metadata[1]);
clocks = metadata[2];
notes = metadata[3];
FLUID_LOG(FLUID_DBG,
"signature=%d/%d, metronome=%d, 32nd-notes=%d",
nominator, denominator, clocks, notes);
break;
case MIDI_KEY_SIGNATURE:
if(mf->varlen != 2)
{
FLUID_LOG(FLUID_ERR,
"Invalid length for KeySignature meta event");
result = FLUID_FAILED;
break;
}
/* We don't care about key signatures anyway */
/* sf = metadata[0];
mi = metadata[1]; */
break;
case MIDI_SEQUENCER_EVENT:
break;
default:
break;
}
if(dyn_buf)
{
FLUID_LOG(FLUID_DBG, "%s: %d: free metadata", __FILE__, __LINE__);
FLUID_FREE(dyn_buf);
}
return result;
}
else /* channel messages */
{
type = status & 0xf0;
channel = status & 0x0f;
/* all channel message have at least 1 byte of associated data */
if((param1 = fluid_midi_file_getc(mf)) < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
switch(type)
{
case NOTE_ON:
if((param2 = fluid_midi_file_getc(mf)) < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
break;
case NOTE_OFF:
if((param2 = fluid_midi_file_getc(mf)) < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
break;
case KEY_PRESSURE:
if((param2 = fluid_midi_file_getc(mf)) < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
break;
case CONTROL_CHANGE:
if((param2 = fluid_midi_file_getc(mf)) < 0)
{
FLUID_LOG(FLUID_ERR, "Unexpected end of file");
return FLUID_FAILED;
}
break;
case PROGRAM_CHANGE:
break;
case CHANNEL_PRESSURE:
break;
case PITCH_BEND: