]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/midi.c
Midi2ly: grok midi files with up to 256 tracks, was 32 -- midi.c-part. Fixes #1479.
[lilypond.git] / python / midi.c
index f80a469380e87748ffcbfe0a4b92c4947dbe5816..4e6368a172062b53d2d28c803c773129aedfb7b7 100644 (file)
@@ -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 <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/>.
 */
 
 /*
@@ -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 <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)
@@ -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);
   
@@ -273,7 +294,6 @@ static PyObject *
 midi_parse_track (unsigned char **track, unsigned char *track_end)
 {
   unsigned int time = 0;
-  unsigned char running_status;
   unsigned long track_len, track_size;
   PyObject *pytrack = 0;
 
@@ -283,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);
 
@@ -305,6 +327,8 @@ midi_parse_track (unsigned char **track, unsigned char *track_end)
 
   {  
     PyObject *pytime = PyInt_FromLong (0L);
+    unsigned char running_status = 0;
+       
     while (*track < track_end)
       {
        long dt = get_variable_length_number(track, track_end);
@@ -337,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;
   
@@ -359,19 +383,19 @@ 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");
+  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;
 
@@ -397,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;