]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/midi.c
Run `make grand-replace'.
[lilypond.git] / python / midi.c
index 00262016512a90933939e2344572c6a61a5a4f06..0c698ded7054104224927abe6fbe8b2e0968e858 100644 (file)
@@ -3,42 +3,39 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 2001--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
             Jan Nieuwenhuizen <janneke@gnu.org>
 
 */
 
 /*
 
-python2
+python
 import midi
 s = open ("s.midi").read ()
 midi.parse_track (s)
 midi.parse (s)
 
+
+returns a MIDI file as the tuple
+
+ (format, division, TRACKLIST) 
+
+each track is an EVENTLIST, where EVENT is
+
+  (time, (type, ARG1, [ARG2]))
+
 */
 
-#include "config.h"
-
-/* urg */
-#if HAVE_PYTHON2_PYTHON_H
-#include <python2/Python.h>
-#elif HAVE_PYTHON2_2_PYTHON_H
-#include <python2.2/Python.h>
-#elif HAVE_PYTHON2_1_PYTHON_H
-#include <python2.1/Python.h>
-#elif HAVE_PYTHON2_0_PYTHON_H
-#include <python2.0/Python.h>
-#elif HAVE_PYTHON1_5_PYTHON_H
-#include <python1.5/Python.h>
-#elif HAVE_PYTHON_PYTHON_H
-#define assert(x)
-#include <python/Python.h>
-#elif HAVE_PYTHON_H
-#define assert(x)
 #include <Python.h>
-#else
-#error Need Python.h
+
+/* PyMIDINIT_FUNC isn't defined in Python < 2.3 */
+#ifndef PyMODINIT_FUNC
+#       if defined(__cplusplus)
+#               define PyMODINIT_FUNC extern "C" void
+#       else /* __cplusplus */
+#               define PyMODINIT_FUNC void
+#       endif /* __cplusplus */
 #endif
 
 #if 0
@@ -53,14 +50,19 @@ static PyObject *Midi_error;
 static PyObject *Midi_warning;
 
 static PyObject *
-midi_error (char *s)
+midi_error (char const *func, char *s)
 {
-  PyErr_SetString (Midi_error, s);
+  char *dest = (char*) malloc (sizeof (char) * (strlen (func) + strlen (s) + 1));
+  strcpy (dest, func);
+  strcat (dest, s);
+  PyErr_SetString (Midi_error, dest);
+  free (dest);
+  
   return 0;
 }
 
 static PyObject *
-midi_warning (char *s)
+midi_warning (char const *s)
 {
   PyErr_SetString (Midi_warning, s);
   return 0;
@@ -73,47 +75,47 @@ typedef struct message {
 } message_t;
 
 message_t channelVoiceMessages[] = {
-  0x80, "NOTE_OFF",
-  0x90, "NOTE_ON",
-  0xA0, "POLYPHONIC_KEY_PRESSURE",
-  0xB0, "CONTROLLER_CHANGE",
-  0xC0, "PROGRAM_CHANGE",
-  0xD0, "CHANNEL_KEY_PRESSURE",
-  0xE0, "PITCH_BEND",
-  0,0
+  {0x80, "NOTE_OFF"},
+  {0x90, "NOTE_ON"},
+  {0xA0, "POLYPHONIC_KEY_PRESSURE"},
+  {0xB0, "CONTROLLER_CHANGE"},
+  {0xC0, "PROGRAM_CHANGE"},
+  {0xD0, "CHANNEL_KEY_PRESSURE"},
+  {0xE0, "PITCH_BEND"},
+  {0,0}
 };
 
 message_t channelModeMessages[] = {
-  0x78, "ALL_SOUND_OFF",
-  0x79, "RESET_ALL_CONTROLLERS",
-  0x7A, "LOCAL_CONTROL",
-  0x7B, "ALL_NOTES_OFF",
-  0x7C, "OMNI_MODE_OFF",
-  0x7D, "OMNI_MODE_ON",
-  0x7E, "MONO_MODE_ON",
-  0x7F, "POLY_MODE_ON",
-  0,0
+  {0x78, "ALL_SOUND_OFF"},
+  {0x79, "RESET_ALL_CONTROLLERS"},
+  {0x7A, "LOCAL_CONTROL"},
+  {0x7B, "ALL_NOTES_OFF"},
+  {0x7C, "OMNI_MODE_OFF"},
+  {0x7D, "OMNI_MODE_ON"},
+  {0x7E, "MONO_MODE_ON"},
+  {0x7F, "POLY_MODE_ON"},
+  {0,0}
 };
 
 message_t metaEvents[] = {
-  0x00, "SEQUENCE_NUMBER",
-  0x01, "TEXT_EVENT",
-  0x02, "COPYRIGHT_NOTICE",
-  0x03, "SEQUENCE_TRACK_NAME",
-  0x04, "INSTRUMENT_NAME",
-  0x05, "LYRIC",
-  0x06, "MARKER",
-  0x07, "CUE_POINT",
-  0x20, "MIDI_CHANNEL_PREFIX",
-  0x21, "MIDI_PORT",
-  0x2F, "END_OF_TRACK",
-  0x51, "SET_TEMPO",
-  0x54, "SMTPE_OFFSET",
-  0x58, "TIME_SIGNATURE",
-  0x59, "KEY_SIGNATURE",
-  0x7F, "SEQUENCER_SPECIFIC_META_EVENT",
-  0xFF, "META_EVENT",
-  0,0
+  {0x00, "SEQUENCE_NUMBER"},
+  {0x01, "TEXT_EVENT"},
+  {0x02, "COPYRIGHT_NOTICE"},
+  {0x03, "SEQUENCE_TRACK_NAME"},
+  {0x04, "INSTRUMENT_NAME"},
+  {0x05, "LYRIC"},
+  {0x06, "MARKER"},
+  {0x07, "CUE_POINT"},
+  {0x20, "MIDI_CHANNEL_PREFIX"},
+  {0x21, "MIDI_PORT"},
+  {0x2F, "END_OF_TRACK"},
+  {0x51, "SET_TEMPO"},
+  {0x54, "SMTPE_OFFSET"},
+  {0x58, "TIME_SIGNATURE"},
+  {0x59, "KEY_SIGNATURE"},
+  {0x7F, "SEQUENCER_SPECIFIC_META_EVENT"},
+  {0xFF, "META_EVENT"},
+  {0,0}
 };
 
 void
@@ -133,7 +135,8 @@ get_number (unsigned char ** str, unsigned char * end_str, int length)
   long sum = 0;
   int i = 0;
   
-  for (; i < length; i++)
+  for (; i < length &&
+        ((*str) + i < end_str); i++)
     sum = (sum << 8) + (unsigned char) (*str)[i];
 
   *str += length;
@@ -145,7 +148,7 @@ unsigned long int
 get_variable_length_number (unsigned char **str, unsigned char * end_str)
 {
   long sum = 0;
-  int i = 0;
+
   while (*str < end_str)
     {
       unsigned char x = **str;
@@ -202,8 +205,8 @@ read_string (unsigned char **track, unsigned char *end)
 }
 
 typedef PyObject* (*Read_midi_event)
-     (unsigned char **track, unsigned char *end, 
-      unsigned char x);
+  (unsigned char **track, unsigned char *end, 
+   unsigned char x);
 
 
 static PyObject *
@@ -270,7 +273,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;
 
@@ -279,8 +281,8 @@ midi_parse_track (unsigned char **track, unsigned char *track_end)
   track_size = track_end - *track;
 
   debug_print ("%s", "\n");
-  if (strcmp (*track, "MTrk"))
-    return midi_error (__FUNCTION__ ": MTrk expected");
+  if (memcmp (*track, "MTrk", 4))
+    return midi_error (__FUNCTION__ ": MTrk expected");
   
   *track += 4;
 
@@ -293,14 +295,17 @@ midi_parse_track (unsigned char **track, unsigned char *track_end)
   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 size corrupt");
 
   pytrack = PyList_New (0);
 
-  track_end = *track + track_len;
+  if (*track + track_len < track_end)
+    track_end = *track + track_len;
 
   {  
     PyObject *pytime = PyInt_FromLong (0L);
+    unsigned char running_status = 0;
+       
     while (*track < track_end)
       {
        long dt = get_variable_length_number(track, track_end);
@@ -326,16 +331,14 @@ static PyObject *
 pymidi_parse_track (PyObject *self, PyObject *args)
 {
   unsigned char *track, *track_end;
-  unsigned long track_size, track_len;
-
-  PyObject * sobj = PyTuple_GetItem (args, 0);
+  unsigned long track_size;
 
   debug_print ("%s", "\n");
   if (!PyArg_ParseTuple (args, "s#", &track, &track_size))
     return 0;
 
   if (track_size < 0)
-    return midi_error (__FUNCTION__  ": negative track size");
+    return midi_error (__FUNCTION__  ": negative track size");
 
   track_end = track + track_size;
   
@@ -355,22 +358,21 @@ midi_parse (unsigned char **midi,unsigned  char *midi_end)
 
   /* Header */
   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");
     
   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");
   
   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;
 
@@ -389,16 +391,14 @@ static PyObject *
 pymidi_parse (PyObject *self, PyObject *args)
 {
   unsigned char *midi, *midi_end;
-  unsigned long midi_size, midi_len;
+  unsigned long midi_size;
   
-  PyObject *sobj = PyTuple_GetItem (args, 0);
-
   debug_print ("%s", "\n");
   if (!PyArg_ParseTuple (args, "s#", &midi, &midi_size))
     return 0;
 
-  if (strcmp (midi, "MThd"))
-      return midi_error (__FUNCTION__ ": MThd expected");
+  if (memcmp (midi, "MThd", 4))
+    return midi_error (__FUNCTION__,  ": MThd expected");
   
   midi += 4;
 
@@ -415,7 +415,8 @@ static PyMethodDef MidiMethods[] =
   {0, 0}        /* Sentinel */
 };
 
-initmidi ()
+PyMODINIT_FUNC
+initmidi (void)
 {
   PyObject *m, *d;
   m = Py_InitModule ("midi", MidiMethods);
@@ -426,4 +427,10 @@ initmidi ()
   add_constants (d);
   Midi_warning = PyString_FromString ("midi.warning");
   PyDict_SetItemString (d, "warning", Midi_warning);
+
+  /*
+    FIXME.
+   */
+  (void) midi_warning;
 }