2 midi.c -- implement Python midi parser module
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
15 s = open ("s.midi").read ()
26 #define debug_print(f, args...) fprintf (stderr, "%s:%d: track: %p :" f, __FUNCTION__, __LINE__, *track, ##args)
28 #define debug_print(f, args...)
31 static PyObject *Midi_error;
32 static PyObject *Midi_warning;
35 midi_error (char const *func, char *s)
37 char*dest = (char*) malloc (sizeof (char) * (strlen (func) + strlen (s) + 1));
40 PyErr_SetString (Midi_error, dest);
47 midi_warning (char const *s)
49 PyErr_SetString (Midi_warning, s);
54 typedef struct message {
59 message_t channelVoiceMessages[] = {
62 0xA0, "POLYPHONIC_KEY_PRESSURE",
63 0xB0, "CONTROLLER_CHANGE",
64 0xC0, "PROGRAM_CHANGE",
65 0xD0, "CHANNEL_KEY_PRESSURE",
70 message_t channelModeMessages[] = {
71 0x78, "ALL_SOUND_OFF",
72 0x79, "RESET_ALL_CONTROLLERS",
73 0x7A, "LOCAL_CONTROL",
74 0x7B, "ALL_NOTES_OFF",
75 0x7C, "OMNI_MODE_OFF",
82 message_t metaEvents[] = {
83 0x00, "SEQUENCE_NUMBER",
85 0x02, "COPYRIGHT_NOTICE",
86 0x03, "SEQUENCE_TRACK_NAME",
87 0x04, "INSTRUMENT_NAME",
91 0x20, "MIDI_CHANNEL_PREFIX",
96 0x58, "TIME_SIGNATURE",
97 0x59, "KEY_SIGNATURE",
98 0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
104 add_constants (PyObject *dict)
106 message_t * p[] = {metaEvents, channelModeMessages, channelVoiceMessages ,0};
108 for ( j =0; p[j]; j++)
109 for ( i = 0; p[j][i].description; i++)
110 PyDict_SetItemString (dict, p[j][i].description, Py_BuildValue ("i", p[j][i].msg));
114 get_number (unsigned char ** str, unsigned char * end_str, int length)
116 /* # MIDI uses big-endian for everything */
120 for (; i < length; i++)
121 sum = (sum << 8) + (unsigned char) (*str)[i];
124 debug_print ("%d:\n", sum);
129 get_variable_length_number (unsigned char **str, unsigned char * end_str)
133 while (*str < end_str)
135 unsigned char x = **str;
137 sum = (sum << 7) + (x & 0x7F);
141 debug_print ("%d:\n", sum);
146 read_one_byte (unsigned char **track, unsigned char *end,
149 PyObject *pyev = Py_BuildValue ("(i)", x);
150 debug_print ("%x:%s", x, "event\n");
156 read_two_bytes (unsigned char **track, unsigned char *end,
159 PyObject *pyev = Py_BuildValue ("(ii)", x, (*track)[0]);
161 debug_print ("%x:%s", x, "event\n");
166 read_three_bytes (unsigned char **track, unsigned char *end,
169 PyObject *pyev = Py_BuildValue ("(iii)", x, (*track)[0],
173 debug_print ("%x:%s", x, "event\n");
178 read_string (unsigned char **track, unsigned char *end)
180 unsigned long length = get_variable_length_number (track, end);
181 if (length > end - *track)
182 length = end - *track;
185 return Py_BuildValue ("s#", ((*track) -length), length);
188 typedef PyObject* (*Read_midi_event)
189 (unsigned char **track, unsigned char *end,
194 read_f0_byte (unsigned char **track, unsigned char *end,
198 debug_print ("%x:%s", x, "event\n");
201 unsigned char z = (*track)[0 ];
203 debug_print ("%x:%s", z, "f0-event\n");
205 return Py_BuildValue ("(iiO)", x, z, read_string (track, end));
208 return Py_BuildValue ("(iO)", x, read_string (track, end));
211 Read_midi_event read_midi_event [16] =
219 read_one_byte, // 60 data entry.
220 read_two_bytes, // 70 all notes off
221 read_three_bytes, // 80 note off
222 read_three_bytes, // 90 note on
223 read_three_bytes, // a0 poly aftertouch
224 read_three_bytes, // b0 control
225 read_two_bytes, // c0 prog change
226 read_two_bytes, // d0 ch aftertouch
227 read_three_bytes, // e0 pitchwheel range
233 read_event (unsigned char **track, unsigned char *end, PyObject *time,
234 unsigned char *running_status)
236 int rsb_skip = ((**track & 0x80)) ? 1 :0;
238 unsigned char x = (rsb_skip) ? (*track)[0]: *running_status;
240 PyObject * bare_event = 0;
241 debug_print ("%x:%s", x, "event\n");
245 // printf ("%x %x %d next %x\n", x, (*track)[0], rsb_skip, (*track)[1]);
246 bare_event = (*read_midi_event[x >> 4]) (track, end, x);
248 return Py_BuildValue ("(OO)", time, bare_event);
254 midi_parse_track (unsigned char **track, unsigned char *track_end)
256 unsigned int time = 0;
257 unsigned char running_status;
258 unsigned long track_len, track_size;
259 PyObject *pytrack = 0;
261 debug_print ("%s", "\n");
263 track_size = track_end - *track;
265 debug_print ("%s", "\n");
266 if (strcmp (*track, "MTrk"))
267 return midi_error (__FUNCTION__, ": MTrk expected");
271 track_len = get_number (track, *track + 4, 4);
274 debug_print ("track_len: %u\n", track_len);
275 debug_print ("track_size: %u\n", track_size);
276 debug_print ("track begin: %p\n", track);
277 debug_print ("track end: %p\n", track + track_len);
279 if (track_len > track_size)
280 return midi_error (__FUNCTION__, ": track size corrupt");
282 pytrack = PyList_New (0);
284 track_end = *track + track_len;
287 PyObject *pytime = PyInt_FromLong (0L);
288 while (*track < track_end)
290 long dt = get_variable_length_number(track, track_end);
295 pytime = PyInt_FromLong (time);
297 pyev = read_event (track, track_end, pytime,
300 PyList_Append (pytrack, pyev);
310 pymidi_parse_track (PyObject *self, PyObject *args)
312 unsigned char *track, *track_end;
313 unsigned long track_size, track_len;
315 PyObject * sobj = PyTuple_GetItem (args, 0);
317 debug_print ("%s", "\n");
318 if (!PyArg_ParseTuple (args, "s#", &track, &track_size))
322 return midi_error (__FUNCTION__, ": negative track size");
324 track_end = track + track_size;
326 return midi_parse_track (&track, track_end);
330 midi_parse (unsigned char **midi,unsigned char *midi_end)
332 PyObject *pymidi = 0;
333 unsigned long header_len;
334 unsigned format, tracks;
338 debug_print ("%s", "\n");
341 header_len = get_number (midi, *midi + 4, 4);
345 return midi_error (__FUNCTION__, ": header too short");
347 format = get_number (midi, *midi + 2, 2);
348 tracks = get_number (midi, *midi + 2, 2);
351 return midi_error (__FUNCTION__, ": too many tracks");
353 division = get_number (midi, *midi + 2, 2) * 4;
357 /* return midi_error ("can't handle non-metrical time"); */
359 *midi += header_len - 6;
361 pymidi = PyList_New (0);
364 for (i = 0; i < tracks; i++)
365 PyList_Append (pymidi, midi_parse_track (midi, midi_end));
367 pymidi = Py_BuildValue ("(OO)", Py_BuildValue ("(ii)", format, division),
373 pymidi_parse (PyObject *self, PyObject *args)
375 unsigned char *midi, *midi_end;
376 unsigned long midi_size, midi_len;
378 PyObject *sobj = PyTuple_GetItem (args, 0);
380 debug_print ("%s", "\n");
381 if (!PyArg_ParseTuple (args, "s#", &midi, &midi_size))
384 if (strcmp (midi, "MThd"))
385 return midi_error (__FUNCTION__, ": MThd expected");
389 midi_end = midi + midi_size;
391 return midi_parse (&midi, midi_end);
395 static PyMethodDef MidiMethods[] =
397 {"parse", pymidi_parse, 1},
398 {"parse_track", pymidi_parse_track, 1},
399 {0, 0} /* Sentinel */
406 m = Py_InitModule ("midi", MidiMethods);
407 d = PyModule_GetDict (m);
409 Midi_error = PyString_FromString ("midi.error");
410 PyDict_SetItemString (d, "error", Midi_error);
412 Midi_warning = PyString_FromString ("midi.warning");
413 PyDict_SetItemString (d, "warning", Midi_warning);