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_2_PYTHON_H
27 #include <python2.2/Python.h>
28 #elif HAVE_PYTHON2_1_PYTHON_H
29 #include <python2.1/Python.h>
30 #elif HAVE_PYTHON2_0_PYTHON_H
31 #include <python2.0/Python.h>
32 #elif HAVE_PYTHON1_5_PYTHON_H
33 #include <python1.5/Python.h>
34 #elif HAVE_PYTHON_PYTHON_H
36 #include <python/Python.h>
47 #define debug_print(f, args...) fprintf (stderr, "%s:%d: track: %p :" f, __FUNCTION__, __LINE__, *track, ##args)
49 #define debug_print(f, args...)
52 static PyObject
*Midi_error
;
53 static PyObject
*Midi_warning
;
56 midi_error (char * func
, char *s
)
58 char*dest
= (char*) malloc (sizeof (char) * (strlen (func
) + strlen (s
) + 1));
61 PyErr_SetString (Midi_error
, dest
);
68 midi_warning (char *s
)
70 PyErr_SetString (Midi_warning
, s
);
75 typedef struct message
{
80 message_t channelVoiceMessages
[] = {
83 0xA0, "POLYPHONIC_KEY_PRESSURE",
84 0xB0, "CONTROLLER_CHANGE",
85 0xC0, "PROGRAM_CHANGE",
86 0xD0, "CHANNEL_KEY_PRESSURE",
91 message_t channelModeMessages
[] = {
92 0x78, "ALL_SOUND_OFF",
93 0x79, "RESET_ALL_CONTROLLERS",
94 0x7A, "LOCAL_CONTROL",
95 0x7B, "ALL_NOTES_OFF",
96 0x7C, "OMNI_MODE_OFF",
103 message_t metaEvents
[] = {
104 0x00, "SEQUENCE_NUMBER",
106 0x02, "COPYRIGHT_NOTICE",
107 0x03, "SEQUENCE_TRACK_NAME",
108 0x04, "INSTRUMENT_NAME",
112 0x20, "MIDI_CHANNEL_PREFIX",
114 0x2F, "END_OF_TRACK",
116 0x54, "SMTPE_OFFSET",
117 0x58, "TIME_SIGNATURE",
118 0x59, "KEY_SIGNATURE",
119 0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
125 add_constants (PyObject
*dict
)
127 message_t
* p
[] = {metaEvents
, channelModeMessages
, channelVoiceMessages
,0};
129 for ( j
=0; p
[j
]; j
++)
130 for ( i
= 0; p
[j
][i
].description
; i
++)
131 PyDict_SetItemString (dict
, p
[j
][i
].description
, Py_BuildValue ("i", p
[j
][i
].msg
));
135 get_number (unsigned char ** str
, unsigned char * end_str
, int length
)
137 /* # MIDI uses big-endian for everything */
141 for (; i
< length
; i
++)
142 sum
= (sum
<< 8) + (unsigned char) (*str
)[i
];
145 debug_print ("%d:\n", sum
);
150 get_variable_length_number (unsigned char **str
, unsigned char * end_str
)
154 while (*str
< end_str
)
156 unsigned char x
= **str
;
158 sum
= (sum
<< 7) + (x
& 0x7F);
162 debug_print ("%d:\n", sum
);
167 read_one_byte (unsigned char **track
, unsigned char *end
,
170 PyObject
*pyev
= Py_BuildValue ("(i)", x
);
171 debug_print ("%x:%s", x
, "event\n");
177 read_two_bytes (unsigned char **track
, unsigned char *end
,
180 PyObject
*pyev
= Py_BuildValue ("(ii)", x
, (*track
)[0]);
182 debug_print ("%x:%s", x
, "event\n");
187 read_three_bytes (unsigned char **track
, unsigned char *end
,
190 PyObject
*pyev
= Py_BuildValue ("(iii)", x
, (*track
)[0],
194 debug_print ("%x:%s", x
, "event\n");
199 read_string (unsigned char **track
, unsigned char *end
)
201 unsigned long length
= get_variable_length_number (track
, end
);
202 if (length
> end
- *track
)
203 length
= end
- *track
;
206 return Py_BuildValue ("s#", ((*track
) -length
), length
);
209 typedef PyObject
* (*Read_midi_event
)
210 (unsigned char **track
, unsigned char *end
,
215 read_f0_byte (unsigned char **track
, unsigned char *end
,
219 debug_print ("%x:%s", x
, "event\n");
222 unsigned char z
= (*track
)[0 ];
224 debug_print ("%x:%s", z
, "f0-event\n");
226 return Py_BuildValue ("(iiO)", x
, z
, read_string (track
, end
));
229 return Py_BuildValue ("(iO)", x
, read_string (track
, end
));
232 Read_midi_event read_midi_event
[16] =
240 read_one_byte
, // 60 data entry.
241 read_two_bytes
, // 70 all notes off
242 read_three_bytes
, // 80 note off
243 read_three_bytes
, // 90 note on
244 read_three_bytes
, // a0 poly aftertouch
245 read_three_bytes
, // b0 control
246 read_two_bytes
, // c0 prog change
247 read_two_bytes
, // d0 ch aftertouch
248 read_three_bytes
, // e0 pitchwheel range
254 read_event (unsigned char **track
, unsigned char *end
, PyObject
*time
,
255 unsigned char *running_status
)
257 int rsb_skip
= ((**track
& 0x80)) ? 1 :0;
259 unsigned char x
= (rsb_skip
) ? (*track
)[0]: *running_status
;
261 PyObject
* bare_event
= 0;
262 debug_print ("%x:%s", x
, "event\n");
266 // printf ("%x %x %d next %x\n", x, (*track)[0], rsb_skip, (*track)[1]);
267 bare_event
= (*read_midi_event
[x
>> 4]) (track
, end
, x
);
269 return Py_BuildValue ("(OO)", time
, bare_event
);
275 midi_parse_track (unsigned char **track
, unsigned char *track_end
)
277 unsigned int time
= 0;
278 unsigned char running_status
;
279 unsigned long track_len
, track_size
;
280 PyObject
*pytrack
= 0;
282 debug_print ("%s", "\n");
284 track_size
= track_end
- *track
;
286 debug_print ("%s", "\n");
287 if (strcmp (*track
, "MTrk"))
288 return midi_error (__FUNCTION__
, ": MTrk expected");
292 track_len
= get_number (track
, *track
+ 4, 4);
295 debug_print ("track_len: %u\n", track_len
);
296 debug_print ("track_size: %u\n", track_size
);
297 debug_print ("track begin: %p\n", track
);
298 debug_print ("track end: %p\n", track
+ track_len
);
300 if (track_len
> track_size
)
301 return midi_error (__FUNCTION__
, ": track size corrupt");
303 pytrack
= PyList_New (0);
305 track_end
= *track
+ track_len
;
308 PyObject
*pytime
= PyInt_FromLong (0L);
309 while (*track
< track_end
)
311 long dt
= get_variable_length_number(track
, track_end
);
316 pytime
= PyInt_FromLong (time
);
318 pyev
= read_event (track
, track_end
, pytime
,
321 PyList_Append (pytrack
, pyev
);
331 pymidi_parse_track (PyObject
*self
, PyObject
*args
)
333 unsigned char *track
, *track_end
;
334 unsigned long track_size
, track_len
;
336 PyObject
* sobj
= PyTuple_GetItem (args
, 0);
338 debug_print ("%s", "\n");
339 if (!PyArg_ParseTuple (args
, "s#", &track
, &track_size
))
343 return midi_error (__FUNCTION__
, ": negative track size");
345 track_end
= track
+ track_size
;
347 return midi_parse_track (&track
, track_end
);
351 midi_parse (unsigned char **midi
,unsigned char *midi_end
)
353 PyObject
*pymidi
= 0;
354 unsigned long header_len
;
355 unsigned format
, tracks
;
359 debug_print ("%s", "\n");
362 header_len
= get_number (midi
, *midi
+ 4, 4);
366 return midi_error (__FUNCTION__
, ": header too short");
368 format
= get_number (midi
, *midi
+ 2, 2);
369 tracks
= get_number (midi
, *midi
+ 2, 2);
372 return midi_error (__FUNCTION__
, ": too many tracks");
374 division
= get_number (midi
, *midi
+ 2, 2) * 4;
378 /* return midi_error ("can't handle non-metrical time"); */
380 *midi
+= header_len
- 6;
382 pymidi
= PyList_New (0);
385 for (i
= 0; i
< tracks
; i
++)
386 PyList_Append (pymidi
, midi_parse_track (midi
, midi_end
));
388 pymidi
= Py_BuildValue ("(OO)", Py_BuildValue ("(ii)", format
, division
),
394 pymidi_parse (PyObject
*self
, PyObject
*args
)
396 unsigned char *midi
, *midi_end
;
397 unsigned long midi_size
, midi_len
;
399 PyObject
*sobj
= PyTuple_GetItem (args
, 0);
401 debug_print ("%s", "\n");
402 if (!PyArg_ParseTuple (args
, "s#", &midi
, &midi_size
))
405 if (strcmp (midi
, "MThd"))
406 return midi_error (__FUNCTION__
, ": MThd expected");
410 midi_end
= midi
+ midi_size
;
412 return midi_parse (&midi
, midi_end
);
416 static PyMethodDef MidiMethods
[] =
418 {"parse", pymidi_parse
, 1},
419 {"parse_track", pymidi_parse_track
, 1},
420 {0, 0} /* Sentinel */
426 m
= Py_InitModule ("midi", MidiMethods
);
427 d
= PyModule_GetDict (m
);
429 Midi_error
= PyString_FromString ("midi.error");
430 PyDict_SetItemString (d
, "error", Midi_error
);
432 Midi_warning
= PyString_FromString ("midi.warning");
433 PyDict_SetItemString (d
, "warning", Midi_warning
);