[PD-cvs] pd/src s_midi_alsa.c,1.1.2.2.2.6.2.2,1.1.2.2.2.6.2.3

Mathieu Bouchard matju at users.sourceforge.net
Tue Jul 24 22:58:27 CEST 2007


Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25886

Modified Files:
      Tag: desiredata
	s_midi_alsa.c 
Log Message:
cleanup


Index: s_midi_alsa.c
===================================================================
RCS file: /cvsroot/pure-data/pd/src/s_midi_alsa.c,v
retrieving revision 1.1.2.2.2.6.2.2
retrieving revision 1.1.2.2.2.6.2.3
diff -C2 -d -r1.1.2.2.2.6.2.2 -r1.1.2.2.2.6.2.3
*** s_midi_alsa.c	9 Jul 2007 18:01:30 -0000	1.1.2.2.2.6.2.2
--- s_midi_alsa.c	24 Jul 2007 20:58:25 -0000	1.1.2.2.2.6.2.3
***************
*** 16,21 ****
  #include <errno.h>
  #include <alsa/asoundlib.h>
! #include "m_pd.h"
! #include "s_stuff.h"
  
  static int alsa_nmidiin;
--- 16,21 ----
  #include <errno.h>
  #include <alsa/asoundlib.h>
! #include "desire.h"
! //#include "s_stuff.h"
  
  static int alsa_nmidiin;
***************
*** 27,35 ****
  
  static unsigned short CombineBytes(unsigned char First, unsigned char Second) {
!     unsigned short _14bit;
!     _14bit = (unsigned short)Second;
!     _14bit <<= 7;
!     _14bit |= (unsigned short)First;
!     return(_14bit);
  }
  
--- 27,31 ----
  
  static unsigned short CombineBytes(unsigned char First, unsigned char Second) {
!     return ((unsigned short)Second << 7) | (unsigned short)First;
  }
  
***************
*** 37,60 ****
      char portname[50];
      int err = 0;
-     int client;
-     int i;
      snd_seq_client_info_t *alsainfo;
      /* do we want to connect pd automatically with other devices ?; see below! */
!     /* LATER: think about a flag to enable/disable automatic connection
!      *        (sometimes it could be a pain)
!      */
      int autoconnect = 1;
      alsa_nmidiin = 0;
      alsa_nmidiout = 0;
- 
      if (nmidiout == 0 && nmidiin == 0) return;
!     if(nmidiin>MAXMIDIINDEV ) {
!         post("midi input ports reduced to maximum %d", MAXMIDIINDEV);
!         nmidiin=MAXMIDIINDEV;
!     }
!     if(nmidiout>MAXMIDIOUTDEV) {
!         post("midi output ports reduced to maximum %d", MAXMIDIOUTDEV);
!         nmidiout=MAXMIDIOUTDEV;
!     }
      if (nmidiin>0 && nmidiout>0) err = snd_seq_open(&midi_handle,"default",SND_SEQ_OPEN_DUPLEX,0);
      else if (nmidiin > 0)        err = snd_seq_open(&midi_handle,"default",SND_SEQ_OPEN_INPUT,0);
--- 33,45 ----
      char portname[50];
      int err = 0;
      snd_seq_client_info_t *alsainfo;
      /* do we want to connect pd automatically with other devices ?; see below! */
!     /* LATER: think about a flag to enable/disable automatic connection (sometimes it could be a pain) */
      int autoconnect = 1;
      alsa_nmidiin = 0;
      alsa_nmidiout = 0;
      if (nmidiout == 0 && nmidiin == 0) return;
!     if (nmidiin>MAXMIDIINDEV)   {post( "midi input ports reduced to maximum %d", MAXMIDIINDEV);  nmidiin =MAXMIDIINDEV;}
!     if (nmidiout>MAXMIDIOUTDEV) {post("midi output ports reduced to maximum %d", MAXMIDIOUTDEV); nmidiout=MAXMIDIOUTDEV;}
      if (nmidiin>0 && nmidiout>0) err = snd_seq_open(&midi_handle,"default",SND_SEQ_OPEN_DUPLEX,0);
      else if (nmidiin > 0)        err = snd_seq_open(&midi_handle,"default",SND_SEQ_OPEN_INPUT,0);
***************
*** 65,83 ****
  	    return;
      }
!     for (i=0;i<nmidiin;i++) {
!         int port;
          sprintf(portname,"Pure Data Midi-In %d",i+1);
!         port = snd_seq_create_simple_port(midi_handle,portname,
! 			SND_SEQ_PORT_CAP_WRITE |SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION);
! 	alsa_midiinfd[i] = port;
          if (port < 0) goto error;
      }
!     for (i=0;i<nmidiout;i++) {
!         int port;
          sprintf(portname,"Pure Data Midi-Out %d",i+1);
!         port = snd_seq_create_simple_port(midi_handle,portname,
! 			SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION);
! 	alsa_midioutfd[i] = port;
          if (port < 0) goto error;
      }
      snd_seq_client_info_malloc(&alsainfo);
--- 50,67 ----
  	    return;
      }
!     int client;
!     for (int i=0; i<nmidiin; i++) {
          sprintf(portname,"Pure Data Midi-In %d",i+1);
!         int port = snd_seq_create_simple_port(midi_handle,portname,
! 		SND_SEQ_PORT_CAP_WRITE |SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION);
          if (port < 0) goto error;
+ 	alsa_midiinfd[i] = port;
      }
!     for (int i=0; i<nmidiout; i++) {
          sprintf(portname,"Pure Data Midi-Out %d",i+1);
!         int port = snd_seq_create_simple_port(midi_handle,portname,
! 		SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION);
          if (port < 0) goto error;
+ 	alsa_midioutfd[i] = port;
      }
      snd_seq_client_info_malloc(&alsainfo);
***************
*** 92,154 ****
      alsa_nmidiout = nmidiout;
      alsa_nmidiin = nmidiin;
- 
      /* JMZ: connect all available devices to pd */
!     if (autoconnect)
!       {
! 
          snd_seq_client_info_t *cinfo;
          snd_seq_port_info_t *pinfo;
- 
          snd_seq_port_subscribe_t *subs;
          snd_seq_addr_t other, topd, frompd;
!         /* since i don't know how to connect multiple ports
!          * (connect everything to each port, modulo,...),
!          * i only fully connect where we have only one single port
!          */
!         if(alsa_nmidiin)
!           {
!             topd.client  =client;
!             topd.port    =alsa_midiinfd[0];
!           }
!         if(alsa_nmidiout)
!           {
!             frompd.client  =client;
!             frompd.port    =alsa_midioutfd[0];
!           }
! 
          snd_seq_port_subscribe_alloca(&subs);
-         
          snd_seq_client_info_alloca(&cinfo);
          snd_seq_port_info_alloca(&pinfo);
          snd_seq_client_info_set_client(cinfo, -1);
!         while (snd_seq_query_next_client(midi_handle, cinfo) >= 0)
!           {
              /* reset query info */
              int client_id=snd_seq_client_info_get_client(cinfo);
!             
!             if((SND_SEQ_CLIENT_SYSTEM != client_id)&&(client != client_id))
!               { /* skipping port 0 and ourself */
                  snd_seq_port_info_set_client(pinfo, client_id);
                  snd_seq_port_info_set_port(pinfo, -1);
!                 while (snd_seq_query_next_port(midi_handle, pinfo) >= 0) 
!                   {
                      other.client=client_id;
                      other.port  =snd_seq_port_info_get_port(pinfo);
!                     if(1==alsa_nmidiin) /* only autoconnect 1st port */
!                       {
                          snd_seq_port_subscribe_set_sender(subs, &other);
                          snd_seq_port_subscribe_set_dest(subs, &topd);
                          snd_seq_subscribe_port(midi_handle, subs);
!                       }
!                     if(1==alsa_nmidiout) /* only autoconnect 1st port */
!                       {
                          snd_seq_port_subscribe_set_sender(subs, &frompd);
                          snd_seq_port_subscribe_set_dest(subs, &other);
                          snd_seq_subscribe_port(midi_handle, subs);
!                       }
!                   }
!               }
!           }
!       }
      return;
   error:
--- 76,116 ----
      alsa_nmidiout = nmidiout;
      alsa_nmidiin = nmidiin;
      /* JMZ: connect all available devices to pd */
!     if (autoconnect) {
          snd_seq_client_info_t *cinfo;
          snd_seq_port_info_t *pinfo;
          snd_seq_port_subscribe_t *subs;
          snd_seq_addr_t other, topd, frompd;
!         /* since i don't know how to connect multiple ports (connect everything to each port, modulo,...),
!          * i only fully connect where we have only one single port */
!         if(alsa_nmidiin ) {  topd.client = client;   topd.port   = alsa_midiinfd [0];}
!         if(alsa_nmidiout) {frompd.client = client; frompd.port   = alsa_midioutfd[0];}
          snd_seq_port_subscribe_alloca(&subs);
          snd_seq_client_info_alloca(&cinfo);
          snd_seq_port_info_alloca(&pinfo);
          snd_seq_client_info_set_client(cinfo, -1);
!         while (snd_seq_query_next_client(midi_handle, cinfo) >= 0) {
              /* reset query info */
              int client_id=snd_seq_client_info_get_client(cinfo);
!             if((SND_SEQ_CLIENT_SYSTEM != client_id)&&(client != client_id)) { /* skipping port 0 and ourself */
                  snd_seq_port_info_set_client(pinfo, client_id);
                  snd_seq_port_info_set_port(pinfo, -1);
!                 while (snd_seq_query_next_port(midi_handle, pinfo) >= 0) {
                      other.client=client_id;
                      other.port  =snd_seq_port_info_get_port(pinfo);
!                     if(1==alsa_nmidiin) /* only autoconnect 1st port */ {
                          snd_seq_port_subscribe_set_sender(subs, &other);
                          snd_seq_port_subscribe_set_dest(subs, &topd);
                          snd_seq_subscribe_port(midi_handle, subs);
!                     }
!                     if(1==alsa_nmidiout) /* only autoconnect 1st port */ {
                          snd_seq_port_subscribe_set_sender(subs, &frompd);
                          snd_seq_port_subscribe_set_dest(subs, &other);
                          snd_seq_subscribe_port(midi_handle, subs);
!                     }
!                 }
!             }
!         }
!     }
      return;
   error:
***************
*** 161,185 ****
      ((x)==0xF2)?2:((x)<0xF4)?1:0)
  
! void sys_alsa_putmidimess(int portno, int a, int b, int c)
! {
!     int channel;
      snd_seq_event_t ev;
      snd_seq_ev_clear(&ev);
!     if (portno >= 0 && portno < alsa_nmidiout)
!     {
          if (a >= 224) { // pitchbend
-             channel = a-224;
      	    snd_seq_ev_set_pitchbend(&ev,channel,CombineBytes(b,c));
          } else if (a >= 208) { // touch
- 	    channel = a-208;
  	    snd_seq_ev_set_chanpress(&ev,channel,b);
          } else if (a >= 192) { // program
- 	    channel = a-192;
  	    snd_seq_ev_set_pgmchange(&ev,channel,b);
          } else if (a >= 176) { // controller
- 	    channel = a-176;
      	    snd_seq_ev_set_controller(&ev,channel,b,c);
          } else if (a >= 160) { // polytouch
- 	    channel = a-160;
  	    snd_seq_ev_set_keypress(&ev,channel,b,c);
          } else if (a >= 144) { // note
--- 123,140 ----
      ((x)==0xF2)?2:((x)<0xF4)?1:0)
  
! void sys_alsa_putmidimess(int portno, int a, int b, int c) {
!     int channel = a&15;
      snd_seq_event_t ev;
      snd_seq_ev_clear(&ev);
!     if (portno >= 0 && portno < alsa_nmidiout) {
          if (a >= 224) { // pitchbend
      	    snd_seq_ev_set_pitchbend(&ev,channel,CombineBytes(b,c));
          } else if (a >= 208) { // touch
  	    snd_seq_ev_set_chanpress(&ev,channel,b);
          } else if (a >= 192) { // program
  	    snd_seq_ev_set_pgmchange(&ev,channel,b);
          } else if (a >= 176) { // controller
      	    snd_seq_ev_set_controller(&ev,channel,b,c);
          } else if (a >= 160) { // polytouch
  	    snd_seq_ev_set_keypress(&ev,channel,b,c);
          } else if (a >= 144) { // note
***************
*** 193,209 ****
          snd_seq_event_output_direct(midi_handle,&ev);
      }
-     //post("%d %d %d\n",a,b,c);
  }
  
! void sys_alsa_putmidibyte(int portno, int byte)
! {
      snd_seq_event_t ev;
      snd_seq_ev_clear(&ev);
!     if (portno >= 0 && portno < alsa_nmidiout)
!     {
          // repack into 1 byte char and put somewhere to point at
          unsigned char *dataptr = (unsigned char *)malloc(1);
          memcpy(dataptr,&byte,1);
- 
          snd_seq_ev_set_sysex(&ev,1,dataptr); //...set_variable *should* have worked but didn't
          snd_seq_ev_set_direct(&ev);
--- 148,160 ----
          snd_seq_event_output_direct(midi_handle,&ev);
      }
  }
  
! void sys_alsa_putmidibyte(int portno, int byte) {
      snd_seq_event_t ev;
      snd_seq_ev_clear(&ev);
!     if (portno >= 0 && portno < alsa_nmidiout) {
          // repack into 1 byte char and put somewhere to point at
          unsigned char *dataptr = (unsigned char *)malloc(1);
          memcpy(dataptr,&byte,1);
          snd_seq_ev_set_sysex(&ev,1,dataptr); //...set_variable *should* have worked but didn't
          snd_seq_ev_set_direct(&ev);
***************
*** 215,224 ****
  
  
!     /* this version uses the asynchronous "read()" ... */
! void sys_alsa_poll_midi(void)
! {
     unsigned char buf[20];
     int count, alsa_source;
-    int i;
     snd_seq_event_t *midievent = NULL;
     if (alsa_nmidiout == 0 && alsa_nmidiin == 0) return;
--- 166,173 ----
  
  
! /* this version uses the asynchronous "read()" ... */
! void sys_alsa_poll_midi(void) {
     unsigned char buf[20];
     int count, alsa_source;
     snd_seq_event_t *midievent = NULL;
     if (alsa_nmidiout == 0 && alsa_nmidiin == 0) return;
***************
*** 227,236 ****
     count = snd_seq_event_input_pending(midi_handle,1);
     if (count != 0) count = snd_seq_event_input(midi_handle,&midievent);
!    if (midievent != NULL)
!    {
         count = snd_midi_event_decode(midiev,buf,20,midievent);
         alsa_source = midievent->dest.port;
!        for(i=0;i<count;i++)
!            sys_midibytein(alsa_source, (buf[i] & 0xff));
         //post("received %d midi bytes\n",count);
     }
--- 176,183 ----
     count = snd_seq_event_input_pending(midi_handle,1);
     if (count != 0) count = snd_seq_event_input(midi_handle,&midievent);
!    if (midievent != NULL) {
         count = snd_midi_event_decode(midiev,buf,20,midievent);
         alsa_source = midievent->dest.port;
!        for(int i=0; i<count; i++) sys_midibytein(alsa_source, (buf[i] & 0xff));
         //post("received %d midi bytes\n",count);
     }
***************
*** 253,265 ****
  }
  
! void midi_alsa_getdevs(char *indevlist, int *nindevs,
!     char *outdevlist, int *noutdevs, int maxndev, int devdescsize)
! {
!     int i, ndev;
!     if ((ndev = alsa_nmidiindevs) > maxndev) ndev = maxndev;
!     for (i = 0; i < ndev; i++) sprintf(indevlist + i * devdescsize, "ALSA MIDI device #%d", i+1);
      *nindevs = ndev;
!     if ((ndev = alsa_nmidioutdevs) > maxndev) ndev = maxndev;
!     for (i = 0; i < ndev; i++) sprintf(outdevlist + i * devdescsize, "ALSA MIDI device #%d", i+1);
      *noutdevs = ndev;
  }
--- 200,209 ----
  }
  
! void midi_alsa_getdevs(char *indevlist, int *nindevs, char *outdevlist, int *noutdevs, int maxndev, int devdescsize) {
!     int ndev = min(maxndev,alsa_nmidiindevs);
!     for (int i=0; i<ndev; i++) sprintf(indevlist + i * devdescsize, "ALSA MIDI device #%d", i+1);
      *nindevs = ndev;
!     ndev    = min(maxndev,alsa_nmidioutdevs);
!     for (int i=0; i<ndev; i++) sprintf(outdevlist + i * devdescsize, "ALSA MIDI device #%d", i+1);
      *noutdevs = ndev;
  }





More information about the Pd-cvs mailing list