2 midi.c -- implement Python midi parser module
4 source file of the GNU LilyPond music typesetter
6 (c) 2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
15 s = open ("s.midi").read ()
24 #if HAVE_PYTHON2_PYTHON_H
25 #include <python2/Python.h>
26 #elif HAVE_PYTHON2_1_PYTHON_H
27 #include <python2.1/Python.h>
28 #elif HAVE_PYTHON2_0_PYTHON_H
29 #include <python2.0/Python.h>
30 #elif HAVE_PYTHON1_5_PYTHON_H
31 #include <python1.5/Python.h>
32 #elif HAVE_PYTHON_PYTHON_H
34 #include <python/Python.h>
45 #define debug_print(f, args...) fprintf (stderr, "%s:%d: track: %p :" f, __FUNCTION__, __LINE__, *track, ##args)
47 #define debug_print(f, args...)
50 static PyObject *Midi_error;
51 static PyObject *Midi_warning;
56 PyErr_SetString (Midi_error, s);
61 midi_warning (char *s)
63 PyErr_SetString (Midi_warning, s);
68 typedef struct message {
73 message_t channelVoiceMessages[] = {
76 0xA0, "POLYPHONIC_KEY_PRESSURE",
77 0xB0, "CONTROLLER_CHANGE",
78 0xC0, "PROGRAM_CHANGE",
79 0xD0, "CHANNEL_KEY_PRESSURE",
84 message_t channelModeMessages[] = {
85 0x78, "ALL_SOUND_OFF",
86 0x79, "RESET_ALL_CONTROLLERS",
87 0x7A, "LOCAL_CONTROL",
88 0x7B, "ALL_NOTES_OFF",
89 0x7C, "OMNI_MODE_OFF",
96 message_t metaEvents[] = {
97 0x00, "SEQUENCE_NUMBER",
99 0x02, "COPYRIGHT_NOTICE",
100 0x03, "SEQUENCE_TRACK_NAME",
101 0x04, "INSTRUMENT_NAME",
105 0x20, "MIDI_CHANNEL_PREFIX",
107 0x2F, "END_OF_TRACK",
109 0x54, "SMTPE_OFFSET",
110 0x58, "TIME_SIGNATURE",
111 0x59, "KEY_SIGNATURE",
112 0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
118 add_constants (PyObject *dict)
120 message_t * p[] = {metaEvents, channelModeMessages, channelVoiceMessages ,0};
122 for ( j =0; p[j]; j++)
123 for ( i = 0; p[j][i].description; i++)
124 PyDict_SetItemString (dict, p[j][i].description, Py_BuildValue ("i", p[j][i].msg));
128 get_number (unsigned char ** str, unsigned char * end_str, int length)
130 /* # MIDI uses big-endian for everything */
134 for (; i < length; i++)
135 sum = (sum << 8) + (unsigned char) (*str)[i];
138 debug_print ("%d:\n", sum);
143 get_variable_length_number (unsigned char **str, unsigned char * end_str)
147 while (*str < end_str)
149 unsigned char x = **str;
151 sum = (sum << 7) + (x & 0x7F);
155 debug_print ("%d:\n", sum);
160 read_one_byte (unsigned char **track, unsigned char *end,
163 PyObject *pyev = Py_BuildValue ("(i)", x);
164 debug_print ("%x:%s", x, "event\n");
170 read_two_bytes (unsigned char **track, unsigned char *end,
173 PyObject *pyev = Py_BuildValue ("(ii)", x, (*track)[0]);
175 debug_print ("%x:%s", x, "event\n");
180 read_three_bytes (unsigned char **track, unsigned char *end,
183 PyObject *pyev = Py_BuildValue ("(iii)", x, (*track)[0],
187 debug_print ("%x:%s", x, "event\n");
192 read_string (unsigned char **track, unsigned char *end)
194 unsigned long length = get_variable_length_number (track, end);
195 if (length > end - *track)
196 length = end - *track;
199 return Py_BuildValue ("s#", ((*track) -length), length);
202 typedef PyObject* (*Read_midi_event)
203 (unsigned char **track, unsigned char *end,
208 read_f0_byte (unsigned char **track, unsigned char *end,
212 debug_print ("%x:%s", x, "event\n");
215 unsigned char z = (*track)[0 ];
217 debug_print ("%x:%s", z, "f0-event\n");
219 return Py_BuildValue ("(iiO)", x, z, read_string (track, end));
222 return Py_BuildValue ("(iO)", x, read_string (track, end));
225 Read_midi_event read_midi_event [16] =
233 read_one_byte, // 60 data entry.
234 read_two_bytes, // 70 all notes off
235 read_three_bytes, // 80 note off
236 read_three_bytes, // 90 note on
237 read_three_bytes, // a0 poly aftertouch
238 read_three_bytes, // b0 control
239 read_two_bytes, // c0 prog change
240 read_two_bytes, // d0 ch aftertouch
241 read_three_bytes, // e0 pitchwheel range
247 read_event (unsigned char **track, unsigned char *end, PyObject *time,
248 unsigned char *running_status)
250 int rsb_skip = ((**track & 0x80)) ? 1 :0;
252 unsigned char x = (rsb_skip) ? (*track)[0]: *running_status;
254 PyObject * bare_event = 0;
255 debug_print ("%x:%s", x, "event\n");
259 // printf ("%x %x %d next %x\n", x, (*track)[0], rsb_skip, (*track)[1]);
260 bare_event = (*read_midi_event[x >> 4]) (track, end, x);
262 return Py_BuildValue ("(OO)", time, bare_event);
268 midi_parse_track (unsigned char **track, unsigned char *track_end)
270 unsigned int time = 0;
271 unsigned char running_status;
272 unsigned long track_len, track_size;
273 PyObject *pytrack = 0;
275 debug_print ("%s", "\n");
277 track_size = track_end - *track;
279 debug_print ("%s", "\n");
280 if (strcmp (*track, "MTrk"))
281 return midi_error (__FUNCTION__ ": MTrk expected");
285 track_len = get_number (track, *track + 4, 4);
288 debug_print ("track_len: %u\n", track_len);
289 debug_print ("track_size: %u\n", track_size);
290 debug_print ("track begin: %p\n", track);
291 debug_print ("track end: %p\n", track + track_len);
293 if (track_len > track_size)
294 return midi_error (__FUNCTION__ ": track size corrupt");
296 pytrack = PyList_New (0);
298 track_end = *track + track_len;
301 PyObject *pytime = PyInt_FromLong (0L);
302 while (*track < track_end)
304 long dt = get_variable_length_number(track, track_end);
309 pytime = PyInt_FromLong (time);
311 pyev = read_event (track, track_end, pytime,
314 PyList_Append (pytrack, pyev);
324 pymidi_parse_track (PyObject *self, PyObject *args)
326 unsigned char *track, *track_end;
327 unsigned long track_size, track_len;
329 PyObject * sobj = PyTuple_GetItem (args, 0);
331 debug_print ("%s", "\n");
332 if (!PyArg_ParseTuple (args, "s#", &track, &track_size))
336 return midi_error (__FUNCTION__ ": negative track size");
338 track_end = track + track_size;
340 return midi_parse_track (&track, track_end);
344 midi_parse (unsigned char **midi,unsigned char *midi_end)
346 PyObject *pymidi = 0;
347 unsigned long header_len;
348 unsigned format, tracks;
352 debug_print ("%s", "\n");
355 header_len = get_number (midi, *midi + 4, 4);
359 return midi_error (__FUNCTION__ ": header too short");
361 format = get_number (midi, *midi + 2, 2);
362 tracks = get_number (midi, *midi + 2, 2);
365 return midi_error (__FUNCTION__ ": too many tracks");
367 division = get_number (midi, *midi + 2, 2) * 4;
371 /* return midi_error ("can't handle non-metrical time"); */
373 *midi += header_len - 6;
375 pymidi = PyList_New (0);
378 for (i = 0; i < tracks; i++)
379 PyList_Append (pymidi, midi_parse_track (midi, midi_end));
381 pymidi = Py_BuildValue ("(OO)", Py_BuildValue ("(ii)", format, division),
387 pymidi_parse (PyObject *self, PyObject *args)
389 unsigned char *midi, *midi_end;
390 unsigned long midi_size, midi_len;
392 PyObject *sobj = PyTuple_GetItem (args, 0);
394 debug_print ("%s", "\n");
395 if (!PyArg_ParseTuple (args, "s#", &midi, &midi_size))
398 if (strcmp (midi, "MThd"))
399 return midi_error (__FUNCTION__ ": MThd expected");
403 midi_end = midi + midi_size;
405 return midi_parse (&midi, midi_end);
409 static PyMethodDef MidiMethods[] =
411 {"parse", pymidi_parse, 1},
412 {"parse_track", pymidi_parse_track, 1},
413 {0, 0} /* Sentinel */
419 m = Py_InitModule ("midi", MidiMethods);
420 d = PyModule_GetDict (m);
422 Midi_error = PyString_FromString ("midi.error");
423 PyDict_SetItemString (d, "error", Midi_error);
425 Midi_warning = PyString_FromString ("midi.warning");
426 PyDict_SetItemString (d, "warning", Midi_warning);