[AD] Win32 MIDI input driver

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


This is the Win32 MIDI input driver that was posted a while back as a patch 
on Allegro's tracker. I fixed a bug AJ pointed out, and changed some 
malloc/memcpy calls to strdups, as Bob rightly suggested.
One thing I am unsure of is wether or not the driver names returned by 
Windows are plain 7 bit ISO strings, or UTF8 and if they should be 
converted to a different encoding before being stored in Allegro's MIDI 
driver struct.

Documentation patch to follow.

Evert
Index: src/win/wmidi.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/win/wmidi.c,v
retrieving revision 1.17
diff -u -r1.17 wmidi.c
--- src/win/wmidi.c	2 Jul 2004 16:25:43 -0000	1.17
+++ src/win/wmidi.c	20 Aug 2004 07:45:21 -0000
@@ -12,6 +12,8 @@
  *
  *      By Stefan Schimanski.
  *
+ *      Midi input added by Daniel Verkamp.
+ *
  *      See readme.txt for copyright information.
  */
 
@@ -43,15 +45,31 @@
 int midi_win32_mixer_volume(int volume);
 void midi_win32_raw_midi(int data);
 
+int midi_win32_in_detect(int input);
+int midi_win32_in_init(int input, int voices);
+void midi_win32_in_exit(int input);
+void CALLBACK midi_in_proc(HMIDIIN, UINT, DWORD, DWORD, DWORD);
+
 
 /* driver globals */
 static HMIDIOUT midi_device = NULL;
+static HMIDIIN  midi_in_device = NULL;
 
 
 /* dynamically generated driver list */
 static _DRIVER_INFO *driver_list = NULL;
 
 
+/* MIDI recording callback */
+void CALLBACK midi_in_proc(HMIDIIN hMidiIn, UINT wMsg, DWORD dwInstance, 
+			   DWORD dwParam1, DWORD dwParam2)
+{
+   if ((midi_in_device == NULL) || (midi_recorder == NULL)) return;
+   midi_recorder((unsigned char)(dwParam1 & 0xff));         /* status byte */
+   midi_recorder((unsigned char)((dwParam1 >> 8) & 0xff));  /* data byte 1 */
+   midi_recorder((unsigned char)((dwParam1 >> 16) & 0xff)); /* data byte 2 */
+}
+
 
 /* _get_win_midi_driver_list:
  *  System driver hook for listing the available MIDI drivers. This generates
@@ -62,6 +80,7 @@
 {
    MIDI_DRIVER *driver;
    MIDIOUTCAPS caps;
+   MIDIINCAPS  caps_in;
    int num_drivers, i;
 
    if (!driver_list) {
@@ -85,8 +104,7 @@
 
          midiOutGetDevCaps(i-1, &caps, sizeof(caps));
 
-	 driver->ascii_name = malloc(strlen(caps.szPname)+1);
-	 _al_sane_strncpy((char *)driver->ascii_name, caps.szPname, strlen(caps.szPname)+1);
+	 driver->ascii_name = strdup(caps.szPname);
 
 	 driver->detect = midi_win32_detect;
 	 driver->init = midi_win32_init;
@@ -97,6 +115,25 @@
          _driver_list_append_driver(&driver_list, driver->id, driver, TRUE);
       }
 
+      /* MidiIn drivers */
+      num_drivers = midiInGetNumDevs();
+      for (i=0; i<num_drivers; i++) {
+	 driver = malloc(sizeof(MIDI_DRIVER));
+	 memcpy(driver, &_midi_none, sizeof(MIDI_DRIVER));
+
+	 driver->id = MIDI_WIN32_IN(i); /* added MIDI_WIN32_IN to alwin.h */
+
+	 midiInGetDevCaps(i, &caps_in, sizeof(caps_in));
+
+	 driver->ascii_name = strdup(caps_in.szPname);
+
+	 driver->detect = midi_win32_in_detect;
+	 driver->init = midi_win32_in_init;
+	 driver->exit = midi_win32_in_exit;
+
+	 _driver_list_append_driver(&driver_list, driver->id, driver, TRUE);
+      }
+
       /* cross-platform DIGital MIDi driver */
       _driver_list_append_driver(&driver_list, MIDI_DIGMID, &midi_digmid, TRUE);
    }
@@ -131,7 +168,7 @@
  */
 int midi_win32_detect(int input)
 {
-   /* the current driver doesn't support input */
+   /* the input drivers are separate from the output drivers */
    if (input)
       return FALSE;
 
@@ -140,6 +177,18 @@
 
 
 
+/* midi_win32_in_detect:
+ */
+int midi_win32_in_detect(int input)
+{
+   if (input)
+      return TRUE;
+
+   return FALSE;
+}
+
+
+
 /* midi_win32_init:
  */
 int midi_win32_init(int input, int voices)
@@ -174,6 +223,34 @@
 
 
 
+/* midi_win32_in_init:
+ */
+int midi_win32_in_init(int input, int voices)
+{
+   MMRESULT hr;
+   int id;
+
+   /* deduce our device number from the driver ID code */
+   id = (midi_input_driver->id & 0xFF) - 'A';
+
+   /* open midi input device */
+   hr = midiInOpen(&midi_in_device, id, (DWORD)midi_in_proc,
+		   (DWORD)NULL, CALLBACK_FUNCTION);
+   if (hr != MMSYSERR_NOERROR) {
+      _TRACE("midiInOpen failed (%x)\n", hr);
+      midi_win32_in_exit(input);
+      return -1;
+   }
+
+
+   midiInReset(midi_in_device);
+   midiInStart(midi_in_device);
+
+   return 0;
+}
+
+
+
 /* midi_win32_exit:
  */
 void midi_win32_exit(int input)
@@ -189,6 +266,20 @@
 
 
 
+/* midi_win32_in_exit:
+ */
+void midi_win32_in_exit(int input)
+{
+   if (midi_in_device != NULL) {
+      midiInStop(midi_in_device);
+      midiInReset(midi_in_device);
+      midiInClose(midi_in_device);
+      midi_in_device = NULL;
+   }
+}
+
+
+
 /* mixer_volume:
  */
 int midi_win32_mixer_volume(int volume)


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/