Skip to content

Latest commit

 

History

History
414 lines (351 loc) · 11.2 KB

native_midi_common.c

File metadata and controls

414 lines (351 loc) · 11.2 KB
 
Dec 31, 2011
Dec 31, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
native_midi: Hardware Midi support for the SDL_mixer library
Copyright (C) 2000,2001 Florian 'Proff' Schulze <florian.proff.schulze@gmx.net>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
*/
#include "native_midi_common.h"
Apr 30, 2006
Apr 30, 2006
25
#include "../SDL_mixer.h"
26
27
28
29
30
#include <stdlib.h>
#include <string.h>
#include <limits.h>
Oct 22, 2017
Oct 22, 2017
31
32
/* The constant 'MThd' */
#define MIDI_MAGIC 0x4d546864
33
34
35
36
/* A single midi track as read from the midi file */
typedef struct
{
May 22, 2013
May 22, 2013
37
38
Uint8 *data; /* MIDI message stream */
int len; /* length of the track data */
39
40
41
42
43
} MIDITrack;
/* A midi file, stripped down to the absolute minimum - divison & track data */
typedef struct
{
May 22, 2013
May 22, 2013
44
int division; /* number of pulses per quarter note (ppqn) */
Feb 21, 2003
Feb 21, 2003
45
int nTracks; /* number of tracks */
May 22, 2013
May 22, 2013
46
MIDITrack *track; /* tracks */
47
48
49
50
51
52
53
54
} MIDIFile;
/* Some macros that help us stay endianess-independant */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define BE_SHORT(x) (x)
#define BE_LONG(x) (x)
#else
May 22, 2013
May 22, 2013
55
56
57
58
59
#define BE_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
#define BE_LONG(x) ((((x)&0x0000FF)<<24) | \
(((x)&0x00FF00)<<8) | \
(((x)&0xFF0000)>>8) | \
(((x)>>24)&0xFF))
60
61
62
63
64
65
66
#endif
/* Get Variable Length Quantity */
static int GetVLQ(MIDITrack *track, int *currentPos)
{
May 22, 2013
May 22, 2013
67
68
69
70
71
72
73
74
75
76
77
int l = 0;
Uint8 c;
while(1)
{
c = track->data[*currentPos];
(*currentPos)++;
l += (c & 0x7f);
if (!(c & 0x80))
return l;
l <<= 7;
}
78
79
80
81
82
}
/* Create a single MIDIEvent */
static MIDIEvent *CreateEvent(Uint32 time, Uint8 event, Uint8 a, Uint8 b)
{
May 22, 2013
May 22, 2013
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
MIDIEvent *newEvent;
newEvent = calloc(1, sizeof(MIDIEvent));
if (newEvent)
{
newEvent->time = time;
newEvent->status = event;
newEvent->data[0] = a;
newEvent->data[1] = b;
}
else
Mix_SetError("Out of memory");
return newEvent;
98
99
100
101
102
}
/* Convert a single midi track to a list of MIDIEvents */
static MIDIEvent *MIDITracktoStream(MIDITrack *track)
{
May 22, 2013
May 22, 2013
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
Uint32 atime = 0;
Uint32 len = 0;
Uint8 event,type,a,b;
Uint8 laststatus = 0;
Uint8 lastchan = 0;
int currentPos = 0;
int end = 0;
MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */
MIDIEvent *currentEvent = head;
while (!end)
{
if (currentPos >= track->len)
break; /* End of data stream reached */
atime += GetVLQ(track, &currentPos);
event = track->data[currentPos++];
/* Handle SysEx seperatly */
if (((event>>4) & 0x0F) == MIDI_STATUS_SYSEX)
{
if (event == 0xFF)
{
type = track->data[currentPos];
currentPos++;
switch(type)
{
case 0x2f: /* End of data marker */
end = 1;
case 0x51: /* Tempo change */
/*
a=track->data[currentPos];
b=track->data[currentPos+1];
c=track->data[currentPos+2];
AddEvent(song, atime, MEVT_TEMPO, c, b, a);
*/
break;
}
}
else
type = 0;
len = GetVLQ(track, &currentPos);
/* Create an event and attach the extra data, if any */
currentEvent->next = CreateEvent(atime, event, type, 0);
currentEvent = currentEvent->next;
if (NULL == currentEvent)
{
FreeMIDIEventList(head);
return NULL;
}
if (len)
{
currentEvent->extraLen = len;
currentEvent->extraData = malloc(len);
memcpy(currentEvent->extraData, &(track->data[currentPos]), len);
currentPos += len;
}
}
else
{
a = event;
if (a & 0x80) /* It's a status byte */
{
/* Extract channel and status information */
lastchan = a & 0x0F;
laststatus = (a>>4) & 0x0F;
/* Read the next byte which should always be a data byte */
a = track->data[currentPos++] & 0x7F;
}
switch(laststatus)
{
case MIDI_STATUS_NOTE_OFF:
case MIDI_STATUS_NOTE_ON: /* Note on */
case MIDI_STATUS_AFTERTOUCH: /* Key Pressure */
case MIDI_STATUS_CONTROLLER: /* Control change */
case MIDI_STATUS_PITCH_WHEEL: /* Pitch wheel */
b = track->data[currentPos++] & 0x7F;
currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, b);
currentEvent = currentEvent->next;
if (NULL == currentEvent)
{
FreeMIDIEventList(head);
return NULL;
}
break;
case MIDI_STATUS_PROG_CHANGE: /* Program change */
case MIDI_STATUS_PRESSURE: /* Channel pressure */
a &= 0x7f;
currentEvent->next = CreateEvent(atime, (Uint8)((laststatus<<4)+lastchan), a, 0);
currentEvent = currentEvent->next;
if (NULL == currentEvent)
{
FreeMIDIEventList(head);
return NULL;
}
break;
default: /* Sysex already handled above */
break;
}
}
}
currentEvent = head->next;
free(head); /* release the dummy head event */
return currentEvent;
213
214
215
216
217
218
219
220
221
}
/*
* Convert a midi song, consisting of up to 32 tracks, to a list of MIDIEvents.
* To do so, first convert the tracks seperatly, then interweave the resulting
* MIDIEvent-Lists to one big list.
*/
static MIDIEvent *MIDItoStream(MIDIFile *mididata)
{
May 22, 2013
May 22, 2013
222
223
224
225
MIDIEvent **track;
MIDIEvent *head = CreateEvent(0,0,0,0); /* dummy event to make handling the list easier */
MIDIEvent *currentEvent = head;
int trackID;
Sep 6, 2001
Sep 6, 2001
226
Feb 21, 2003
Feb 21, 2003
227
if (NULL == head)
May 22, 2013
May 22, 2013
228
229
return NULL;
Feb 21, 2003
Feb 21, 2003
230
track = (MIDIEvent**) calloc(1, sizeof(MIDIEvent*) * mididata->nTracks);
Sep 28, 2013
Sep 28, 2013
231
if (NULL == track)
Oct 13, 2017
Oct 13, 2017
232
233
{
free(head);
Sep 28, 2013
Sep 28, 2013
234
return NULL;
Oct 13, 2017
Oct 13, 2017
235
}
Sep 28, 2013
Sep 28, 2013
236
May 22, 2013
May 22, 2013
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
/* First, convert all tracks to MIDIEvent lists */
for (trackID = 0; trackID < mididata->nTracks; trackID++)
track[trackID] = MIDITracktoStream(&mididata->track[trackID]);
/* Now, merge the lists. */
/* TODO */
while(1)
{
Uint32 lowestTime = INT_MAX;
int currentTrackID = -1;
/* Find the next event */
for (trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (track[trackID] && (track[trackID]->time < lowestTime))
{
currentTrackID = trackID;
lowestTime = track[currentTrackID]->time;
}
}
/* Check if we processes all events */
if (currentTrackID == -1)
break;
currentEvent->next = track[currentTrackID];
track[currentTrackID] = track[currentTrackID]->next;
currentEvent = currentEvent->next;
lowestTime = 0;
}
/* Make sure the list is properly terminated */
currentEvent->next = 0;
currentEvent = head->next;
Feb 21, 2003
Feb 21, 2003
275
free(track);
May 22, 2013
May 22, 2013
276
277
free(head); /* release the dummy head event */
return currentEvent;
Jun 2, 2013
Jun 2, 2013
280
static int ReadMIDIFile(MIDIFile *mididata, SDL_RWops *src)
May 22, 2013
May 22, 2013
282
283
284
285
286
287
288
289
290
int i = 0;
Uint32 ID;
Uint32 size;
Uint16 format;
Uint16 tracks;
Uint16 division;
if (!mididata)
return 0;
Jun 2, 2013
Jun 2, 2013
291
if (!src)
May 22, 2013
May 22, 2013
292
293
294
return 0;
/* Make sure this is really a MIDI file */
Jun 2, 2013
Jun 2, 2013
295
SDL_RWread(src, &ID, 1, 4);
Oct 22, 2017
Oct 22, 2017
296
if (BE_LONG(ID) != MIDI_MAGIC)
May 22, 2013
May 22, 2013
297
298
299
return 0;
/* Header size must be 6 */
Jun 2, 2013
Jun 2, 2013
300
SDL_RWread(src, &size, 1, 4);
May 22, 2013
May 22, 2013
301
302
303
304
305
size = BE_LONG(size);
if (size != 6)
return 0;
/* We only support format 0 and 1, but not 2 */
Jun 2, 2013
Jun 2, 2013
306
SDL_RWread(src, &format, 1, 2);
May 22, 2013
May 22, 2013
307
308
309
310
format = BE_SHORT(format);
if (format != 0 && format != 1)
return 0;
Jun 2, 2013
Jun 2, 2013
311
SDL_RWread(src, &tracks, 1, 2);
May 22, 2013
May 22, 2013
312
313
314
tracks = BE_SHORT(tracks);
mididata->nTracks = tracks;
Feb 21, 2003
Feb 21, 2003
315
316
317
318
319
320
321
/* Allocate tracks */
mididata->track = (MIDITrack*) calloc(1, sizeof(MIDITrack) * mididata->nTracks);
if (NULL == mididata->track)
{
Mix_SetError("Out of memory");
goto bail;
}
May 22, 2013
May 22, 2013
322
323
/* Retrieve the PPQN value, needed for playback */
Jun 2, 2013
Jun 2, 2013
324
SDL_RWread(src, &division, 1, 2);
May 22, 2013
May 22, 2013
325
326
327
328
329
mididata->division = BE_SHORT(division);
for (i=0; i<tracks; i++)
{
Jun 2, 2013
Jun 2, 2013
330
331
SDL_RWread(src, &ID, 1, 4); /* We might want to verify this is MTrk... */
SDL_RWread(src, &size, 1, 4);
May 22, 2013
May 22, 2013
332
333
334
335
336
337
338
339
size = BE_LONG(size);
mididata->track[i].len = size;
mididata->track[i].data = malloc(size);
if (NULL == mididata->track[i].data)
{
Mix_SetError("Out of memory");
goto bail;
}
Jun 2, 2013
Jun 2, 2013
340
SDL_RWread(src, mididata->track[i].data, 1, size);
May 22, 2013
May 22, 2013
341
342
}
return 1;
May 22, 2013
May 22, 2013
345
346
347
348
349
for(;i >= 0; i--)
{
if (mididata->track[i].data)
free(mididata->track[i].data);
}
May 22, 2013
May 22, 2013
351
return 0;
Jun 2, 2013
Jun 2, 2013
354
MIDIEvent *CreateMIDIEventList(SDL_RWops *src, Uint16 *division)
May 22, 2013
May 22, 2013
356
357
358
359
360
361
362
363
364
MIDIFile *mididata = NULL;
MIDIEvent *eventList;
int trackID;
mididata = calloc(1, sizeof(MIDIFile));
if (!mididata)
return NULL;
/* Open the file */
Jun 2, 2013
Jun 2, 2013
365
if ( src != NULL )
May 22, 2013
May 22, 2013
366
367
{
/* Read in the data */
Jun 2, 2013
Jun 2, 2013
368
if ( ! ReadMIDIFile(mididata, src))
May 22, 2013
May 22, 2013
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
free(mididata);
return NULL;
}
}
else
{
free(mididata);
return NULL;
}
if (division)
*division = mididata->division;
eventList = MIDItoStream(mididata);
Oct 13, 2017
Oct 13, 2017
384
385
386
387
388
if (eventList == NULL)
{
free(mididata);
return NULL;
}
May 22, 2013
May 22, 2013
389
390
391
392
393
394
for(trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (mididata->track[trackID].data)
free(mididata->track[trackID].data);
}
free(mididata->track);
Feb 21, 2003
Feb 21, 2003
395
free(mididata);
May 22, 2013
May 22, 2013
396
397
return eventList;
398
399
400
401
}
void FreeMIDIEventList(MIDIEvent *head)
{
May 22, 2013
May 22, 2013
402
403
404
405
406
407
408
409
410
411
412
413
MIDIEvent *cur, *next;
cur = head;
while (cur)
{
next = cur->next;
if (cur->extraData)
free (cur->extraData);
free (cur);
cur = next;
}