/*
- 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 <hanwen@xs4all.nl>
+ Copyright (C) 2001--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
Jan Nieuwenhuizen <janneke@gnu.org>
+
+ 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 <http://www.gnu.org/licenses/>.
*/
/*
returns a MIDI file as the tuple
- (format, division, TRACKLIST)
+ ((format, division), TRACKLIST)
each track is an EVENTLIST, where EVENT is
#include <Python.h>
+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)
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);
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);
{
PyObject *pytime = PyInt_FromLong (0L);
+ unsigned char running_status = 0;
+
while (*track < track_end)
{
- unsigned char running_status = 0;
-
long dt = get_variable_length_number(track, track_end);
PyObject *pyev = 0;
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;
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");
+ if (tracks > 256)
+ return midi_error (__FUNCTION__, ": too many tracks: ", compat_itoa (tracks));
division = get_number (midi, *midi + 2, 2) * 4;
if (division < 0)
- /* return midi_error ("can't handle non-metrical time"); */
+ /* return midi_error (cannot handle non-metrical time"); */
;
*midi += header_len - 6;
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;