X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=python%2Fmidi.c;h=01c9925ece5445a65d947109f6efc7bc3c80945a;hb=1d4914c023a672e0e80b9b9eafc123605f4c0f00;hp=69ac12848cea0a9f2b9eb060e722fe79dd762ee1;hpb=bb1c909545600953c385954222c78be0f2a05d26;p=lilypond.git diff --git a/python/midi.c b/python/midi.c index 69ac12848c..01c9925ece 100644 --- a/python/midi.c +++ b/python/midi.c @@ -1,11 +1,22 @@ /* - midi.c -- implement Python midi parser module + This file is part of LilyPond, the GNU music typesetter. - source file of the GNU LilyPond music typesetter - - (c) 2001--2006 Han-Wen Nienhuys + Copyright (C) 2001--2010 Han-Wen Nienhuys Jan Nieuwenhuizen + + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . */ /* @@ -19,7 +30,7 @@ midi.parse (s) returns a MIDI file as the tuple - (format, division, TRACKLIST) + ((format, division), TRACKLIST) each track is an EVENTLIST, where EVENT is @@ -29,6 +40,14 @@ each track is an EVENTLIST, where EVENT is #include +char * +compat_itoa (int i) +{ + static char buffer[9]; + snprintf (buffer, 8, "%d", i); + return buffer; +} + /* PyMIDINIT_FUNC isn't defined in Python < 2.3 */ #ifndef PyMODINIT_FUNC # if defined(__cplusplus) @@ -50,11 +69,13 @@ static PyObject *Midi_error; static PyObject *Midi_warning; static PyObject * -midi_error (char const *func, char *s) +midi_error (char const *func, char *s, char *t) { - char *dest = (char*) malloc (sizeof (char) * (strlen (func) + strlen (s) + 1)); + char *dest = (char*) malloc (sizeof (char) + * (strlen (func) + strlen (s) + strlen (t) + 1)); strcpy (dest, func); strcat (dest, s); + strcat (dest, t); PyErr_SetString (Midi_error, dest); free (dest); @@ -282,20 +303,22 @@ midi_parse_track (unsigned char **track, unsigned char *track_end) debug_print ("%s", "\n"); if (memcmp (*track, "MTrk", 4)) - return midi_error (__FUNCTION__, ": MTrk expected"); + { + *track[4] = 0; + return midi_error (__FUNCTION__, ": MTrk expected, got: ", *(char**)track); + } *track += 4; track_len = get_number (track, *track + 4, 4); - debug_print ("track_len: %u\n", track_len); debug_print ("track_size: %u\n", track_size); debug_print ("track begin: %p\n", track); debug_print ("track end: %p\n", track + track_len); if (track_len > track_size) - return midi_error (__FUNCTION__, ": track size corrupt"); + return midi_error (__FUNCTION__, ": track length corrupt: ", compat_itoa (track_len)); pytrack = PyList_New (0); @@ -338,7 +361,7 @@ pymidi_parse_track (PyObject *self, PyObject *args) return 0; if (track_size < 0) - return midi_error (__FUNCTION__, ": negative track size"); + return midi_error (__FUNCTION__, ": negative track size: ", compat_itoa (track_size)); track_end = track + track_size; @@ -360,13 +383,13 @@ midi_parse (unsigned char **midi,unsigned char *midi_end) header_len = get_number (midi, *midi + 4, 4); if (header_len < 6) - return midi_error (__FUNCTION__, ": header too short"); + return midi_error (__FUNCTION__, ": header too short: ", compat_itoa (header_len)); format = get_number (midi, *midi + 2, 2); tracks = get_number (midi, *midi + 2, 2); if (tracks > 32) - return midi_error (__FUNCTION__, ": too many tracks"); + return midi_error (__FUNCTION__, ": too many tracks: ", compat_itoa (tracks)); division = get_number (midi, *midi + 2, 2) * 4; @@ -398,8 +421,11 @@ pymidi_parse (PyObject *self, PyObject *args) return 0; if (memcmp (midi, "MThd", 4)) - return midi_error (__FUNCTION__, ": MThd expected"); - + { + midi[4] = 0; + return midi_error (__FUNCTION__, ": MThd expected, got: ", (char*)midi); + } + midi += 4; midi_end = midi + midi_size;