-
Notifications
You must be signed in to change notification settings - Fork 9
/
3rd_mid.h
464 lines (373 loc) Β· 14.6 KB
/
3rd_mid.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
mid.h - v0.1 - Midi playback library using the TinySoundFont library.
Do this:
#define MID_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef mid_h
#define mid_h
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
typedef struct mid_t mid_t;
typedef struct tsf tsf;
mid_t* mid_create( void const* midi_data, size_t midi_size, void* memctx );
void mid_destroy( mid_t* mid );
int mid_render_short( mid_t* mid, short* sample_pairs, int sample_pairs_count, tsf* sound_font );
int mid_render_float( mid_t* mid, float* sample_pairs, int sample_pairs_count, tsf* sound_font );
void mid_skip_leading_silence( mid_t* mid, tsf* sound_font );
#endif /* mid_h */
#ifdef MID_ENABLE_RAW
#ifndef mid_raw_h
#define mid_raw_h
#ifndef MID_U8
#define MID_U8 unsigned char
#endif
#ifndef MID_U16
#define MID_U16 unsigned short
#endif
#ifndef MID_U32
#define MID_U32 unsigned int
#endif
#ifndef MID_U64
#define MID_U64 unsigned long long
#endif
typedef struct mid_event_t
{
MID_U32 delay_us;
MID_U8 channel;
MID_U8 type;
union
{
struct { MID_U8 program; } program_change;
struct { MID_U8 note; MID_U8 velocity; } note_on;
struct { MID_U8 note; } note_off;
struct { MID_U8 key; MID_U8 key_pressure; } key_pressure;
struct { MID_U16 value; } pitch_bend;
struct { MID_U8 control, control_value; } control_change;
struct { MID_U8 channel_pressure; } channel_pressure;
} data;
} mid_event_t;
typedef struct mid_song_t
{
int event_count;
mid_event_t* events;
} mid_song_t;
struct mid_t
{
void* memctx;
mid_song_t song;
int percussion_preset;
MID_U64 playback_accumulated_time_us;
int playback_sample_pos;
int playback_event_pos;
};
int mid_init_raw( mid_t* mid, void const* raw_data, size_t raw_size );
size_t mid_save_raw( mid_t* mid, void* data, size_t capacity );
#endif /* MID_ENABLE_RAW */
#endif /* mid_raw_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef MID_IMPLEMENTATION
#undef MID_IMPLEMENTATION
#ifndef MID_U8
#define MID_U8 unsigned char
#endif
#ifndef MID_U16
#define MID_U16 unsigned short
#endif
#ifndef MID_U32
#define MID_U32 unsigned int
#endif
#ifndef MID_U64
#define MID_U64 unsigned long long
#endif
#ifndef MID_MALLOC
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#if defined(_cplusplus)
#define MID_MALLOC( ctx, size ) ( ::malloc( size ) )
#define MID_FREE( ctx, ptr ) ( ::free( ptr ) )
#else
#define MID_MALLOC( ctx, size ) ( malloc( size ) )
#define MID_FREE( ctx, ptr ) ( free( ptr ) )
#endif
#endif
#include <assert.h>
#define MID_LOG(...) (void) __VA_ARGS__
#include <string.h>
#pragma warning( push )
#pragma warning( disable: 4242 )
#pragma warning( disable: 4244 )
#pragma warning( disable: 4365 )
#pragma warning( disable: 4668 )
#pragma warning( disable: 4701 )
#pragma warning( disable: 4703 )
#ifndef MID_NO_TSF_IMPLEMENTATION
#define TSF_NO_STDIO
#define TSF_IMPLEMENTATION
#endif
#include "3rd_tsf.h"
#pragma warning( disable: 4201 )
#ifndef MID_NO_TML_IMPLEMENTATION
#define TML_NO_STDIO
#define TML_IMPLEMENTATION
#endif
#include "3rd_tml.h"
#pragma warning( pop )
#ifndef MID_ENABLE_RAW
typedef struct mid_event_t
{
MID_U32 delay_us;
MID_U8 channel;
MID_U8 type;
union
{
struct { MID_U8 program; } program_change;
struct { MID_U8 note; MID_U8 velocity; } note_on;
struct { MID_U8 note; } note_off;
struct { MID_U8 key; MID_U8 key_pressure; } key_pressure;
struct { MID_U16 value; } pitch_bend;
struct { MID_U8 control, control_value; } control_change;
struct { MID_U8 channel_pressure; } channel_pressure;
} data;
} mid_event_t;
typedef struct mid_song_t
{
int event_count;
mid_event_t* events;
} mid_song_t;
struct mid_t
{
void* memctx;
mid_song_t song;
int percussion_preset;
MID_U64 playback_accumulated_time_us;
int playback_sample_pos;
int playback_event_pos;
};
#endif /* MID_ENABLE_RAW */
mid_t* mid_create( void const* midi_data, size_t midi_size, void* memctx )
{
tml_message* mid_file = tml_load_memory( midi_data, (int) midi_size );
if( !mid_file ) return NULL;
int count = 0;
tml_message* iter = mid_file;
while( iter )
{
if( iter->type == TML_PROGRAM_CHANGE || iter->type == TML_NOTE_ON || iter->type == TML_NOTE_OFF ||
iter->type == TML_PITCH_BEND || iter->type == TML_CONTROL_CHANGE )
{
++count;
}
iter = iter->next;
}
mid_event_t* events = (mid_event_t*) malloc( sizeof( mid_event_t ) * count );
int events_count = 0;
unsigned int time = 0;
tml_message* msg = mid_file;
while( msg )
{
if( msg->type == TML_PROGRAM_CHANGE || msg->type == TML_NOTE_ON || msg->type == TML_NOTE_OFF ||
msg->type == TML_PITCH_BEND || msg->type == TML_CONTROL_CHANGE )
{
mid_event_t* event = &events[ events_count++ ];
event->delay_us = ( msg->time - time ) * 1000;
time = msg->time;
event->channel = msg->channel;
event->type = msg->type;
switch( msg->type )
{
case TML_PROGRAM_CHANGE:
event->data.program_change.program = (MID_U8) msg->program;
break;
case TML_NOTE_ON: //play a note
event->data.note_on.note = (MID_U8) msg->key;
event->data.note_on.velocity = (MID_U8) msg->velocity;
break;
case TML_NOTE_OFF: //stop a note
event->data.note_off.note = (MID_U8) msg->key;
break;
case TML_PITCH_BEND: //pitch wheel modification
event->data.pitch_bend.value = (MID_U16) msg->pitch_bend;
break;
case TML_CONTROL_CHANGE: //MIDI controller messages
event->data.control_change.control = (MID_U8) msg->control;
event->data.control_change.control_value = (MID_U8) msg->control_value;
break;
}
}
msg = msg->next;
}
tml_free( mid_file );
mid_t* mid = (mid_t*) MID_MALLOC( memctx, sizeof( mid_t ) );
mid->memctx = memctx;
mid->song.event_count = events_count;
mid->song.events = events;
mid->playback_accumulated_time_us = 0ull;
mid->playback_sample_pos = 0;
mid->playback_event_pos = 0;
return mid;
}
void mid_destroy( mid_t* mid )
{
if( mid->song.events ) MID_FREE( mid->memctx, mid->song.events );
MID_FREE( mid->memctx, mid );
}
int mid_init_raw( mid_t* mid, void const* raw_data, size_t raw_size )
{
int events_count = *(int*)raw_data;
if( sizeof( mid_event_t ) * events_count != raw_size - sizeof( int ) ) return 0;
mid->memctx = NULL;
mid->song.event_count = events_count;
mid->song.events = (mid_event_t*)( ( (int*)raw_data ) + 1 );
mid->playback_accumulated_time_us = 0ull;
mid->playback_sample_pos = 0;
mid->playback_event_pos = 0;
return 1;
}
size_t mid_save_raw( mid_t* mid, void* data, size_t capacity )
{
size_t size = sizeof( mid_event_t ) * mid->song.event_count + sizeof( int );
if( data && capacity >= size )
{
*(int*)data = mid->song.event_count;
memcpy( ( (int*)data ) + 1, mid->song.events, sizeof( mid_event_t ) * mid->song.event_count );
}
return size;
}
void mid_skip_leading_silence( mid_t* mid, tsf* sound_font )
{
(void) sound_font;
for( ; ; )
{
MID_U64 next_event_delay_us = mid->song.events[ mid->playback_event_pos ].delay_us;
MID_U64 playback_time_us = ( mid->playback_sample_pos * 1000000ull ) / 44100ull;
MID_U64 next_event_time_us = mid->playback_accumulated_time_us + next_event_delay_us;
assert( next_event_time_us >= playback_time_us );
MID_U64 time_until_next_event = next_event_time_us - playback_time_us;
int samples_until_next_event = (int)( ( time_until_next_event * 44100ull ) / 1000000ull );
mid_event_t* event = &mid->song.events[ mid->playback_event_pos ];
switch( event->type )
{
case TML_PROGRAM_CHANGE:
tsf_channel_set_presetnumber( sound_font, event->channel, event->data.program_change.program, ( event->channel == 9 ) );
break;
case TML_NOTE_ON:
return;
case TML_NOTE_OFF: //stop a note
tsf_channel_note_off( sound_font, event->channel, event->data.note_off.note );
break;
case TML_PITCH_BEND: //pitch wheel modification
tsf_channel_set_pitchwheel( sound_font, event->channel, event->data.pitch_bend.value );
break;
case TML_CONTROL_CHANGE: //MIDI controller messages
tsf_channel_midi_control( sound_font, event->channel, event->data.control_change.control, event->data.control_change.control_value );
break;
}
mid->playback_sample_pos += samples_until_next_event;
mid->playback_accumulated_time_us += next_event_delay_us;
mid->playback_event_pos++;
}
}
int mid_render_short( mid_t* mid, short* sample_pairs, int sample_pairs_count, tsf* sound_font )
{
int samples_rendered = 0;
memset( sample_pairs, 0, sample_pairs_count * sizeof( short ) * 2 );
while( samples_rendered < sample_pairs_count )
{
MID_U64 next_event_delay_us = mid->song.events[ mid->playback_event_pos ].delay_us;
MID_U64 playback_time_us = ( mid->playback_sample_pos * 1000000ull ) / 44100ull;
MID_U64 next_event_time_us = mid->playback_accumulated_time_us + next_event_delay_us;
assert( next_event_time_us >= playback_time_us );
MID_U64 time_until_next_event = next_event_time_us - playback_time_us;
int samples_until_next_event = (int)( ( time_until_next_event * 44100ull ) / 1000000ull );
int samples_to_render = samples_until_next_event;
if( samples_to_render > sample_pairs_count - samples_rendered )
{
samples_to_render = sample_pairs_count - samples_rendered;
tsf_render_short( sound_font, sample_pairs + samples_rendered * 2,
samples_to_render, 1 );
samples_rendered += samples_to_render;
mid->playback_sample_pos += samples_to_render;
return samples_rendered;
}
else
{
tsf_render_short( sound_font, sample_pairs + samples_rendered * 2,
samples_to_render, 1 );
samples_rendered += samples_to_render;
mid->playback_sample_pos += samples_to_render;
}
mid->playback_accumulated_time_us += next_event_delay_us;
mid_event_t* event = &mid->song.events[ mid->playback_event_pos++ ];
switch( event->type )
{
case TML_PROGRAM_CHANGE:
tsf_channel_set_presetnumber( sound_font, event->channel, event->data.program_change.program, ( event->channel == 9 ) );
break;
case TML_NOTE_ON:
tsf_channel_note_on( sound_font, event->channel, event->data.note_on.note, event->data.note_on.velocity / 127.0f );
break;
case TML_NOTE_OFF: //stop a note
tsf_channel_note_off( sound_font, event->channel, event->data.note_off.note );
break;
case TML_PITCH_BEND: //pitch wheel modification
tsf_channel_set_pitchwheel( sound_font, event->channel, event->data.pitch_bend.value );
break;
case TML_CONTROL_CHANGE: //MIDI controller messages
tsf_channel_midi_control( sound_font, event->channel, event->data.control_change.control, event->data.control_change.control_value );
break;
}
}
return samples_rendered;
}
#endif /* MID_IMPLEMENTATION */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2016 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/