2 midi.c -- implement Python midi parser module
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
15 s = open ("s.midi").read ()
27 #define debug_print(f, args...) fprintf (stderr, "%s:%d: track: %p :" f, __FUNCTION__, __LINE__, *track, ##args)
29 #define debug_print(f, args...)
32 static PyObject *Midi_error;
33 static PyObject *Midi_warning;
36 midi_error (char const *func, char *s)
38 char*dest = (char*) malloc (sizeof (char) * (strlen (func) + strlen (s) + 1));
41 PyErr_SetString (Midi_error, dest);
48 midi_warning (char const *s)
50 PyErr_SetString (Midi_warning, s);
55 typedef struct message {
60 message_t channelVoiceMessages[] = {
63 0xA0, "POLYPHONIC_KEY_PRESSURE",
64 0xB0, "CONTROLLER_CHANGE",
65 0xC0, "PROGRAM_CHANGE",
66 0xD0, "CHANNEL_KEY_PRESSURE",
71 message_t channelModeMessages[] = {
72 0x78, "ALL_SOUND_OFF",
73 0x79, "RESET_ALL_CONTROLLERS",
74 0x7A, "LOCAL_CONTROL",
75 0x7B, "ALL_NOTES_OFF",
76 0x7C, "OMNI_MODE_OFF",
83 message_t metaEvents[] = {
84 0x00, "SEQUENCE_NUMBER",
86 0x02, "COPYRIGHT_NOTICE",
87 0x03, "SEQUENCE_TRACK_NAME",
88 0x04, "INSTRUMENT_NAME",
92 0x20, "MIDI_CHANNEL_PREFIX",
97 0x58, "TIME_SIGNATURE",
98 0x59, "KEY_SIGNATURE",
99 0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
105 add_constants (PyObject *dict)
107 message_t * p[] = {metaEvents, channelModeMessages, channelVoiceMessages ,0};
109 for ( j =0; p[j]; j++)
110 for ( i = 0; p[j][i].description; i++)
111 PyDict_SetItemString (dict, p[j][i].description, Py_BuildValue ("i", p[j][i].msg));
115 get_number (unsigned char ** str, unsigned char * end_str, int length)
117 /* # MIDI uses big-endian for everything */
121 for (; i < length; i++)
122 sum = (sum << 8) + (unsigned char) (*str)[i];
125 debug_print ("%d:\n", sum);
130 get_variable_length_number (unsigned char **str, unsigned char * end_str)
134 while (*str < end_str)
136 unsigned char x = **str;
138 sum = (sum << 7) + (x & 0x7F);
142 debug_print ("%d:\n", sum);
147 read_one_byte (unsigned char **track, unsigned char *end,
150 PyObject *pyev = Py_BuildValue ("(i)", x);
151 debug_print ("%x:%s", x, "event\n");
157 read_two_bytes (unsigned char **track, unsigned char *end,
160 PyObject *pyev = Py_BuildValue ("(ii)", x, (*track)[0]);
162 debug_print ("%x:%s", x, "event\n");
167 read_three_bytes (unsigned char **track, unsigned char *end,
170 PyObject *pyev = Py_BuildValue ("(iii)", x, (*track)[0],
174 debug_print ("%x:%s", x, "event\n");
179 read_string (unsigned char **track, unsigned char *end)
181 unsigned long length = get_variable_length_number (track, end);
182 if (length > end - *track)
183 length = end - *track;
186 return Py_BuildValue ("s#", ((*track) -length), length);
189 typedef PyObject* (*Read_midi_event)
190 (unsigned char **track, unsigned char *end,
195 read_f0_byte (unsigned char **track, unsigned char *end,
199 debug_print ("%x:%s", x, "event\n");
202 unsigned char z = (*track)[0 ];
204 debug_print ("%x:%s", z, "f0-event\n");
206 return Py_BuildValue ("(iiO)", x, z, read_string (track, end));
209 return Py_BuildValue ("(iO)", x, read_string (track, end));
212 Read_midi_event read_midi_event [16] =
220 read_one_byte, // 60 data entry.
221 read_two_bytes, // 70 all notes off
222 read_three_bytes, // 80 note off
223 read_three_bytes, // 90 note on
224 read_three_bytes, // a0 poly aftertouch
225 read_three_bytes, // b0 control
226 read_two_bytes, // c0 prog change
227 read_two_bytes, // d0 ch aftertouch
228 read_three_bytes, // e0 pitchwheel range
234 read_event (unsigned char **track, unsigned char *end, PyObject *time,
235 unsigned char *running_status)
237 int rsb_skip = ((**track & 0x80)) ? 1 :0;
239 unsigned char x = (rsb_skip) ? (*track)[0]: *running_status;
241 PyObject * bare_event = 0;
242 debug_print ("%x:%s", x, "event\n");
246 // printf ("%x %x %d next %x\n", x, (*track)[0], rsb_skip, (*track)[1]);
247 bare_event = (*read_midi_event[x >> 4]) (track, end, x);
249 return Py_BuildValue ("(OO)", time, bare_event);
255 midi_parse_track (unsigned char **track, unsigned char *track_end)
257 unsigned int time = 0;
258 unsigned char running_status;
259 unsigned long track_len, track_size;
260 PyObject *pytrack = 0;
262 debug_print ("%s", "\n");
264 track_size = track_end - *track;
266 debug_print ("%s", "\n");
267 if (strcmp (*track, "MTrk"))
268 return midi_error (__FUNCTION__, ": MTrk expected");
272 track_len = get_number (track, *track + 4, 4);
275 debug_print ("track_len: %u\n", track_len);
276 debug_print ("track_size: %u\n", track_size);
277 debug_print ("track begin: %p\n", track);
278 debug_print ("track end: %p\n", track + track_len);
280 if (track_len > track_size)
281 return midi_error (__FUNCTION__, ": track size corrupt");
283 pytrack = PyList_New (0);
285 track_end = *track + track_len;
288 PyObject *pytime = PyInt_FromLong (0L);
289 while (*track < track_end)
291 long dt = get_variable_length_number(track, track_end);
296 pytime = PyInt_FromLong (time);
298 pyev = read_event (track, track_end, pytime,
301 PyList_Append (pytrack, pyev);
311 pymidi_parse_track (PyObject *self, PyObject *args)
313 unsigned char *track, *track_end;
314 unsigned long track_size, track_len;
316 PyObject * sobj = PyTuple_GetItem (args, 0);
318 debug_print ("%s", "\n");
319 if (!PyArg_ParseTuple (args, "s#", &track, &track_size))
323 return midi_error (__FUNCTION__, ": negative track size");
325 track_end = track + track_size;
327 return midi_parse_track (&track, track_end);
331 midi_parse (unsigned char **midi,unsigned char *midi_end)
333 PyObject *pymidi = 0;
334 unsigned long header_len;
335 unsigned format, tracks;
339 debug_print ("%s", "\n");
342 header_len = get_number (midi, *midi + 4, 4);
346 return midi_error (__FUNCTION__, ": header too short");
348 format = get_number (midi, *midi + 2, 2);
349 tracks = get_number (midi, *midi + 2, 2);
352 return midi_error (__FUNCTION__, ": too many tracks");
354 division = get_number (midi, *midi + 2, 2) * 4;
358 /* return midi_error ("can't handle non-metrical time"); */
360 *midi += header_len - 6;
362 pymidi = PyList_New (0);
365 for (i = 0; i < tracks; i++)
366 PyList_Append (pymidi, midi_parse_track (midi, midi_end));
368 pymidi = Py_BuildValue ("(OO)", Py_BuildValue ("(ii)", format, division),
374 pymidi_parse (PyObject *self, PyObject *args)
376 unsigned char *midi, *midi_end;
377 unsigned long midi_size, midi_len;
379 PyObject *sobj = PyTuple_GetItem (args, 0);
381 debug_print ("%s", "\n");
382 if (!PyArg_ParseTuple (args, "s#", &midi, &midi_size))
385 if (strcmp (midi, "MThd"))
386 return midi_error (__FUNCTION__, ": MThd expected");
390 midi_end = midi + midi_size;
392 return midi_parse (&midi, midi_end);
396 static PyMethodDef MidiMethods[] =
398 {"parse", pymidi_parse, 1},
399 {"parse_track", pymidi_parse_track, 1},
400 {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);