-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmididec.c
1320 lines (1057 loc) · 30 KB
/
kmididec.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
/****************************************************************************
**
** K MIDI DECoder - MIDI to PCM decoder using fluidsynth
**
** Copyright (C) 2018 by KO Myung-Hun <komh@chollian.net>
**
** This file is part of K MIDI DECoder.
**
** $BEGIN_LICENSE$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $END_LICENSE$
**
****************************************************************************/
/** @file kmididec.c */
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <sys/param.h>
#include <io.h>
#include <fcntl.h>
#include <errno.h>
#include <fluidsynth.h>
/* missed API declaration in 1.0.9 */
FLUIDSYNTH_API
int fluid_synth_channel_pressure( fluid_synth_t *synth, int chan, int val );
#include "kmididec.h"
/* OS/2 real-time midi format */
#define OS2MIDI 0xFFFF
/**
* Header information
*/
typedef struct kmthd
{
uint16_t format; /**< format */
uint16_t tracks; /**< a number of tracks */
uint16_t division; /**< time format */
} KMTHD, *PKMTHD;
/* marker of end of track for next tick */
#define END_OF_TRACK (( uint32_t )-1 )
/**
* Track information
*/
typedef struct kmtrk
{
struct kmdec *dec; /**< MIDI decoder */
uint64_t start; /**< start position of a track */
uint32_t length; /**< length of a track */
uint32_t offset; /**< offset relative to start position */
uint32_t nextTick; /**< next tick */
uint8_t status; /**< status byte */
} KMTRK, *PKMTRK;
/**
* Memory FD
*/
typedef struct kmemfd
{
uint8_t *buffer; /**< buffer for a file */
uint32_t size; /**< size of a buffer @a buffer in bytes */
uint32_t length; /**< bytes filled in a buffer @a buffer */
uint32_t offset; /**< current position */
} KMEMFD, *PKMEMFD;
/* defaul values */
#define DEFAULT_TEMPO 500000 /* us/qn */
#define DEFAULT_NUMERATOR 4
#define DEFAULT_DENOMINATOR 4
/* clocks per sec */
#define CLOCK_BASE INT64_C( 1000000 ) /* us */
/**
* MIDI decoder
*/
typedef struct kmdec
{
int fd; /**< fd for MIDI file */
bool closeFd; /**< flag to indicate to close fd */
PKMDECIOFUNCS io; /**< IO functions */
PKMEMFD mfd; /**< fd for memory IO */
KMTHD header; /**< header */
PKMTRK tracks; /**< array of a track */
fluid_settings_t *settings; /**< setting of fluidsynth */
fluid_synth_t *synth; /**< synthesizer of fluidsynth */
int sf; /**< sound font file */
/** synthesize in float or in s16 */
FLUIDSYNTH_API int ( *synth_write )( fluid_synth_t *, int,
void *, int, int, void *, int, int );
int clockUnit; /**< us/MIDI clock */
int sampleRate; /**< sample rate */
int sampleSize; /**< bytes per sample */
uint32_t tempo; /**< tempo in us/qn */
uint8_t numerator; /**< numerator */
uint8_t denominator; /**< denominator */
uint32_t tick; /**< current tick */
uint64_t clock; /**< current clock in us */
uint64_t duration; /**< duration of MIDI file in us */
char *buffer; /**< buffer for samples */
int bufLen; /**< legnth of buffer */
int bufPos; /**< position in buffer */
} KMDEC, *PKMDEC;
/* a mode for decode() */
#define DECODE_SEEK 0 /* seek mode */
#define DECODE_PLAY 1 /* play mode */
static PKMEMFD memOpen( int fd, PKMDECIOFUNCS io );
static int memClose( PKMEMFD mfd );
static int memRead( PKMEMFD mfd, void *buf, size_t n );
static int memSeek( PKMEMFD mfd, long offset, int origin );
static int memTell( PKMEMFD mfd );
static int initMidiInfo( PKMDEC dec );
static int reset( PKMDEC dec );
static int readVarQ( PKMTRK track, int *val );
static int decodeDelta( PKMTRK track);
static int decodeMetaEvent( PKMTRK track);
static int decodeEvent( PKMTRK track);
static int decodeOS2SysExEvent( PKMTRK track );
static int decodeOS2Event( PKMTRK track );
static int decode( PKMDEC dec, int mode );
#define MEMFD_BUF_DELTA ( 64 * 1024 )
static PKMEMFD memOpen( int fd, PKMDECIOFUNCS io )
{
PKMEMFD mfd;
uint8_t *buffer;
mfd = calloc( 1, sizeof( *mfd ));
if( !mfd )
return NULL;
while( 1 )
{
if( mfd->length == mfd->size )
{
mfd->size += MEMFD_BUF_DELTA;
buffer = realloc( mfd->buffer, mfd->size );
if( !buffer )
{
fail:
free( mfd->buffer );
free( mfd );
return NULL;
}
mfd->buffer = buffer;
}
int size = MEMFD_BUF_DELTA - ( mfd->length % MEMFD_BUF_DELTA );
int len = io->read( fd, mfd->buffer + mfd->length, size );
if( len == -1 )
goto fail;
if( len == 0 )
break;
mfd->length += len;
}
/* shrink to fit */
buffer = realloc( mfd->buffer, mfd->length );
if( !buffer )
goto fail;
mfd->buffer = buffer;
return mfd;
}
static int memClose( PKMEMFD mfd )
{
if( !mfd )
return -1;
free( mfd->buffer );
free( mfd );
return 0;
}
static int memRead( PKMEMFD mfd, void *buf, size_t n )
{
if( !mfd )
return -1;
if( n == 0 || mfd->length == mfd->offset )
return 0;
int len = MIN( n, mfd->length - mfd->offset );
memcpy( buf, mfd->buffer + mfd->offset, len );
mfd->offset += len;
return len;
}
static int memSeek( PKMEMFD mfd, long offset, int origin )
{
long pos;
if( !mfd )
return -1;
switch( origin )
{
case SEEK_SET:
pos = offset;
break;
case SEEK_CUR:
pos = mfd->offset + offset;
break;
case SEEK_END:
pos = mfd->length + offset;
break;
}
if( pos < 0 || pos > mfd->length )
return -1;
mfd->offset = pos;
return pos;
}
static int memTell( PKMEMFD mfd )
{
if( !mfd )
return -1;
return mfd->offset;
}
/**
* Initialize header and track information
*
* @param[in] dec Pointer to decoder
* @return 0 on success, -1 on error
*/
static int initMidiInfo( PKMDEC dec )
{
PKMEMFD mfd = dec->mfd;
PKMTHD header = &dec->header;
PKMTRK *tracks = &dec->tracks;
uint8_t data[ 14 ];
if( memRead( mfd, data, 10 ) == -1 )
return -1;
/* Timing Generation Control of OS/2 real-time midi data ? */
if( memcmp( data, "\xF0\x00\x00\x3A\x03\x01\x18", 7 ) == 0
&& data[ 9 ] == 0xF7 )
{
header->format = OS2MIDI;
header->tracks = 1;
uint8_t pp = data[ 7 ] & 0x7F;
if( pp & 0x40 )
header->division = 24 / ((( pp & 0x3F ) + 1 ) * 3 );
else
header->division = 24 * ( pp + 1 );
/*
* PPQN below 1 is not supported. If there are real cases, consider
* then.
*/
if( header->division == 0 )
{
fprintf( stderr, "Not supported time format\n");
return -1;
}
PKMTRK track = calloc( 1, sizeof( *track ));
if( !track )
return -1;
track->dec = dec;
track->start = memTell( mfd );
if( memSeek( mfd, 0, SEEK_END ) == -1
|| ( track->length = memTell( mfd ) - track->start,
memSeek( mfd, track->start, SEEK_SET ) == -1 ))
{
free( track );
return -1;
}
*tracks = track;
return 0;
}
if( memRead( mfd, data + 10, 4 ) == -1 )
return -1;
if( memcmp( data, "MThd\x00\x00\x00\x06", 8 ) != 0 )
{
fprintf( stderr, "Not supported MIDI file\n");
return -1;
}
header->format = ntohs( *( uint16_t * )( data + 8 ));
header->tracks = ntohs( *( uint16_t * )( data + 10 ));
header->division = ntohs( *( uint16_t * )( data + 12 ));
if( header->format >= 2 )
{
fprintf( stderr, "Not supported MIDI format\n");
return -1;
}
if(( header->division >> 15 ) & 1)
{
fprintf( stderr, "Not supported time format\n");
return -1;
}
*tracks = calloc( header->tracks, sizeof( **tracks ));
if( !*tracks )
return -1;
for( int i = 0; i < header->tracks; i++ )
{
PKMTRK track = ( *tracks ) + i;
if( memRead( mfd, data, 8 ) == -1
|| memcmp( data, "MTrk", 4 ) != 0 )
{
fail:
free( *tracks );
*tracks = NULL;
return -1;
}
track->dec = dec;
track->start = memTell( mfd );
track->length = ntohl( *( long * )( data + 4 ));
if( decodeDelta( track ) == -1 )
goto fail;
if( memSeek( mfd, track->start + track->length, SEEK_SET ) == -1 )
goto fail;
}
return 0;
}
/**
* Reset decoder
*
* @param[in] dec Pointer to a decoder
* @return 0 on success, -1 on error
*/
static int reset( PKMDEC dec )
{
/* reset tracks */
for( int i = 0; i < dec->header.tracks; i++ )
{
PKMTRK track = dec->tracks + i;
track->offset = 0;
track->nextTick = 0;
if( memSeek( dec->mfd, track->start, SEEK_SET ) == -1 )
return -1;
if( dec->header.format != OS2MIDI && decodeDelta( track ) == -1 )
return -1;
track->status = 0;
}
/* reset fluidsynth */
fluid_synth_system_reset( dec->synth );
/* reset to default values */
dec->tempo = DEFAULT_TEMPO;
dec->numerator = DEFAULT_NUMERATOR;
dec->denominator = DEFAULT_DENOMINATOR;
dec->tick = 0;
dec->clock = 0;
dec->bufLen = 0;
dec->bufPos = 0;
return 0;
}
/**
* Read varialbe qunatity
*
* @param[in] track Pointer to a track
* @param[out] val Decoded integer
* @return 0 on success, -1 on error
*/
static int readVarQ( PKMTRK track, int *val )
{
PKMEMFD mfd = track->dec->mfd;
uint8_t b;
int count = 0;
int vq = 0;
do
{
if( count >= 4 || memRead( mfd, &b, 1 ) == -1 )
return -1;
count++;
track->offset++;
vq = ( vq << 7 ) | ( b & 0x7F );
} while( b & 0x80 );
*val = vq;
return 0;
}
/**
* Decode delta time
*
* @param[in] track Pointer to a track
* @return 0 on success, -1 on error
*/
static int decodeDelta( PKMTRK track )
{
if( track->offset >= track->length )
{
track->nextTick = END_OF_TRACK;
return 0;
}
int delta;
if( readVarQ( track, &delta ) == -1 )
return -1;
/* accumulate delta time */
track->nextTick += delta;
return 0;
}
/**
* Decode meta event
*
* @param[in] track Pointer to a track
* @return 0 on success, -1 on error
*/
static int decodeMetaEvent( PKMTRK track )
{
PKMEMFD mfd = track->dec->mfd;
uint8_t type;
int len;
if( track->offset >= track->length )
return 0;
/* type */
if( memRead( mfd, &type, 1 ) == -1 )
return -1;
track->offset++;
/* length */
if( readVarQ( track, &len ) == -1 )
return -1;
uint8_t data[ len + 1 ];
data[ len ] = '\0';
if( memRead( mfd, data, len ) == -1 )
return -1;
track->offset += len;
switch( type )
{
case 0x00: /* sequence number */
if( len != 2 )
return -1;
break;
case 0x01: /* text event */
case 0x02: /* copyright notice */
case 0x03: /* sequence/track name */
case 0x04: /* instrument name */
case 0x05: /* lyric */
case 0x06: /* marker */
case 0x07: /* cue point */
/* text */
break;
case 0x20: /* MIDI channel prefix */
if( len != 1 )
return -1;
break;
case 0x2F: /* end of track */
if( len != 0 || track->offset != track->length )
return -1;
break;
case 0x51: /* set tempo */
{
if( len != 3 )
return -1;
track->dec->tempo = data[ 0 ] << 16 | data[ 1 ] << 8 | data[ 2 ];
break;
}
case 0x54: /* SMPTE offset */
if( len != 5 )
return -1;
break;
case 0x58: /* time signature */
{
if( len != 4 )
return -1;
PKMDEC dec = track->dec;
dec->numerator = data[ 0 ];
dec->denominator = 1 << data[ 1 ]; /* power of 2 */
break;
}
case 0x59: /* key signature */
if( len != 2 )
return -1;
break;
case 0x7F: /* sequencer-specific meta-event */
/*
* Some MIDI files such as JSBLUES.MID in \MMOS2\SOUND, does not
* respect the format of type 0x7F. Disable the length check
*/
#if 0
if( len >= 5 );
return -1;
#endif
break;
}
return 0;
}
/**
* Decode event
*
* @param[in] track Pointer to a track
* @return 0 on success, -1 on error
*/
static int decodeEvent( PKMTRK track )
{
PKMEMFD mfd = track->dec->mfd;
fluid_synth_t *synth = track->dec->synth;
uint8_t status;
uint8_t event;
uint8_t channel;
int len;
if( track->offset >= track->length )
return 0;
if( memSeek( mfd, track->start + track->offset, SEEK_SET ) == -1 )
return -1;
if( memRead( mfd, &status, 1 ) == -1 )
return -1;
track->offset++;
/* implicit status ? */
if( status < 0x80 )
{
status = track->status;
if( memSeek( mfd, -1, KMDEC_SEEK_CUR ) == -1 )
return -1;
track->offset--;
}
if( status < 0x80 )
return -1;
if( status < 0xF0 )
track->status = status;
event = status & 0xF0;
channel = status & 0x0F;
/* calculate length of event data */
if( status == 0xF0 || status == 0xF7 )
{
if( readVarQ( track, &len ) == -1 )
return -1;
}
else if( status == 0xFF )
{
if( decodeMetaEvent( track ) == -1 )
return -1;
len = 0;
}
else
{
len = 2;
/*
* status 0xF2, event 0x80, 0x90, 0xA0, 0xB0, 0xE0: len = 2
* status 0xF3, event 0xC0, 0xD0: len = 1
* status 0xF1, 0xF4, 0xF5, 0xF6, 0xF8 - 0xFE: len = 0
*/
if( status == 0xF3 || event == 0xC0 || event == 0xD0 )
len = 1;
else if( status == 0xF1 || ( status >= 0xF4 && status <= 0xF6 )
|| ( status >= 0xF8 && status <= 0xFE ))
len = 0;
}
uint8_t data[ len ];
if( memRead( mfd, data, len ) == -1 )
return -1;
track->offset += len;
/* check F0 SysEx syntax which should end with F7 EOX */
if( status == 0xF0 && data[ len - 1 ] != 0xF7 )
return -1;
data[ 0 ] &= 0x7F;
data[ 1 ] &= 0x7F;
/* pass MIDI event to fluidsynth */
switch( event )
{
case 0x80: /* note off */
fluid_synth_noteoff( synth, channel, data[ 0 ]);
break;
case 0x90: /* note on */
fluid_synth_noteon( synth, channel, data[ 0 ], data[ 1 ]);
break;
case 0xA0: /* polyphonic aftertouch */
/* not supported */
break;
case 0xB0: /* control mode hnage */
fluid_synth_cc( synth, channel, data[ 0 ], data[ 1 ]);
break;
case 0xC0: /* program change */
fluid_synth_program_change( synth, channel, data[ 0 ]);
break;
case 0xD0: /* channel key pressure */
fluid_synth_channel_pressure( synth, channel, data[ 0 ]);
break;
case 0xE0: /* pitch bend */
fluid_synth_pitch_bend( synth, channel,
( data[ 1 ] << 7) | data[ 0 ]);
break;
case 0xF0:
/* ignore SysEx events */
break;
}
return decodeDelta( track );
}
/**
* Decode OS/2 SysEx event
*
* @param[in] track Pointer to a track
* @return 0 on success, -1 on error
*/
static int decodeOS2SysExEvent( PKMTRK track )
{
PKMEMFD mfd = track->dec->mfd;
uint8_t sysex[ 10 - 1 ]; /* F0 was consumed already */
int i;
/* SysEx event ends with 0xF7 */
for( i = 0; i < sizeof( sysex ); i++ )
{
if( memRead( mfd, sysex + i, 1 ) == -1)
return -1;
track->offset++;
if( sysex[ i ] == 0xF7 )
break;
}
/* Ignore not supported SysEx event */
if( i == sizeof( sysex ))
{
do
{
if( memRead( mfd, sysex, 1 ) == -1 )
return -1;
track->offset++;
} while( sysex[ 0 ] != 0xF7 );
}
else if( memcmp( sysex, "\x00\x00\x3A", 3 ) == 0 )
{
uint8_t type = sysex[ 3 ] & 0x7F;
if( type == 1 ) /* Timing Compression(Long) */
{
uint8_t ll = sysex[ 4 ] & 0x7F;
uint8_t mm = sysex[ 5 ] & 0x7F;
track->nextTick += mm << 7 | ll;
}
else if( type >= 7 ) /* Timing Compression(Short) */
track->nextTick += type;
else if( type == 3 ) /* Device Driver Control */
{
uint8_t cmd = sysex[ 4 ];
if( cmd == 2 ) /* Tempo Control */
{
uint8_t tl = sysex[ 5 ] & 0x7F;
uint8_t tm = sysex[ 6 ] & 0x7F;
track->dec->tempo = 60 * 1000000 / (( tm << 7 | tl ) / 10 );
}
}
}
return 0;
}
/**
* Decode OS/2 event
*
* @param[in] track Pointer to a track
* @return 0 on success, -1 on error
*/
static int decodeOS2Event( PKMTRK track )
{
PKMEMFD mfd = track->dec->mfd;
fluid_synth_t *synth = track->dec->synth;
uint8_t status;
uint8_t event;
uint8_t channel;
if( track->offset >= track->length )
{
track->nextTick = END_OF_TRACK;
return 0;
}
if( memRead( mfd, &status, 1 ) == -1 )
return -1;
track->offset++;
/* implicit status ? */
if( status < 0x80 )
{
status = track->status;
if( memSeek( mfd, -1, KMDEC_SEEK_CUR ) == -1 )
return -1;
track->offset--;
}
if( status < 0x80 )
return -1;
if( status < 0xF0 )
track->status = status;
event = status & 0xF0;
channel = status & 0x0F;
/* calculate length of event data */
int len = 0;
/*
* status event 0x80, 0x90, 0xA0, 0xB0, 0xE0: len = 2
* status event 0xC0, 0xD0: len = 1
*/
switch( event )
{
case 0x80: case 0x90: case 0xA0: case 0xB0: case 0xE0:
len = 2;
break;
case 0xC0: case 0xD0:
len = 1;
break;
}
uint8_t data[ len ];
if( memRead( mfd, data, len ) == -1 )
return -1;
track->offset += len;
data[ 0 ] &= 0x7F;
data[ 1 ] &= 0x7F;
/* pass MIDI event to fluidsynth */
switch( event )
{
case 0x80: /* note off */
fluid_synth_noteoff( synth, channel, data[ 0 ]);
break;
case 0x90: /* note on */
fluid_synth_noteon( synth, channel, data[ 0 ], data[ 1 ]);
break;
case 0xA0: /* polyphonic aftertouch */
/* not supported */
break;
case 0xB0: /* control mode hnage */
fluid_synth_cc( synth, channel, data[ 0 ], data[ 1 ]);
break;
case 0xC0: /* program change */
fluid_synth_program_change( synth, channel, data[ 0 ]);
break;
case 0xD0: /* channel key pressure */
fluid_synth_channel_pressure( synth, channel, data[ 0 ]);
break;
case 0xE0: /* pitch bend */
fluid_synth_pitch_bend( synth, channel,
( data[ 1 ] << 7) | data[ 0 ]);
break;
case 0xF0:
if( status == 0xF8 )
track->nextTick++;
else
return decodeOS2SysExEvent( track );
break;
}
return 0;
}
/**
* Decode MIDI messages
*
* @param[in] track Pointer to a track
* @param[in] mode Decode mode
* @return 0 on success, -1 on error or on finished
*/
static int decode( PKMDEC dec, int mode )
{
uint32_t nextTick = END_OF_TRACK;
/* update next tick of a track if already decoded */
for( int i = 0; i < dec->header.tracks; i++ )
{
PKMTRK track = dec->tracks + i;
if( track->nextTick <= dec->tick )
{
if( track->dec->header.format == OS2MIDI )
{
if( decodeOS2Event( track ) == -1 )
return -1;
}
else if( decodeEvent( track ) == -1 )
return -1;
}
if( nextTick > track->nextTick )
nextTick = track->nextTick;
}
/* finished ? */
if( nextTick == END_OF_TRACK )
return -1;
/* generate samples if new message was decoded every MIDI clock */
if( nextTick > dec->tick )
{
int ticksPerSec = dec->header.division * CLOCK_BASE / dec->tempo;
int delta = ticksPerSec * dec->clockUnit / CLOCK_BASE;
/*
* delta should be 1 at least. Otherwise tick does not progress
* any more until tempo is changed to set delta to a value bigger
* than 0.
*/
if( delta == 0 )
delta = 1;
if( dec->tick + delta > nextTick )
delta = nextTick - dec->tick;
if( mode == DECODE_PLAY )
{
int samples = delta * dec->sampleRate / ticksPerSec;
int len = samples * dec->sampleSize;
free( dec->buffer );
dec->buffer = malloc( len );
if( !dec->buffer )
return -1;
dec->synth_write( dec->synth, samples,
dec->buffer, 0, 2, dec->buffer, 1, 2 );
dec->bufLen = len;
dec->bufPos = 0;
}
/* accumulate ticks */
dec->tick += delta;
/* accumulate clocks */
dec->clock += CLOCK_BASE * delta / ticksPerSec;
}
return 0;
}
static int defaultOpen( const char *name )
{
return open( name, O_RDONLY | O_BINARY );
}
static int defaultRead( int fd, void *buf, size_t n )
{
return read( fd, buf, n );
}
static int defaultSeek( int fd, long offset, int origin )
{
static int origins[] = { SEEK_SET, SEEK_CUR, SEEK_END };
if( origin >= sizeof( origins ) / sizeof( origins[ 0 ]))
{
errno = EINVAL;
return -1;
}
return lseek( fd, offset, origins[ origin ]);
}
static int defaultTell( int fd )
{
return tell( fd );
}
static int defaultClose( int fd )
{
return close( fd );
}