/test/rawmidi.c

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <ctype.h>
00004 #include "../include/asoundlib.h"
00005 #include <signal.h>
00006 
00007 static void usage(void)
00008 {
00009         fprintf(stderr, "usage: rawmidi [options]\n");
00010         fprintf(stderr, "  options:\n");
00011         fprintf(stderr, "    -v: verbose mode\n");
00012         fprintf(stderr, "    -i device-id : test ALSA input device\n");
00013         fprintf(stderr, "    -o device-id : test ALSA output device\n");
00014         fprintf(stderr, "    -I node      : test input node\n");
00015         fprintf(stderr, "    -O node      : test output node\n");
00016         fprintf(stderr, "    -t: test midi thru\n");
00017         fprintf(stderr, "  example:\n");
00018         fprintf(stderr, "    rawmidi -i hw:0,0 -O /dev/midi1\n");
00019         fprintf(stderr, "    tests input for card 0, device 0, using snd_rawmidi API\n");
00020         fprintf(stderr, "    and /dev/midi1 using file descriptors\n");
00021 }
00022 
00023 int stop=0;
00024 
00025 void sighandler(int dum)
00026 {
00027         stop=1;
00028 }
00029 
00030 int main(int argc,char** argv)
00031 {
00032         int i;
00033         int err;
00034         int thru=0;
00035         int verbose = 0;
00036         char *device_in = NULL;
00037         char *device_out = NULL;
00038         char *node_in = NULL;
00039         char *node_out = NULL;
00040         
00041         int fd_in = -1,fd_out = -1;
00042         snd_rawmidi_t *handle_in = 0,*handle_out = 0;
00043         
00044         if (argc==1) {
00045                 usage();
00046                 exit(0);
00047         }
00048         
00049         for (i = 1 ; i<argc ; i++) {
00050                 if (argv[i][0]=='-') {
00051                         switch (argv[i][1]) {
00052                                 case 'h':
00053                                         usage();
00054                                         break;
00055                                 case 'v':
00056                                         verbose = 1;
00057                                         break;
00058                                 case 't':
00059                                         thru = 1;
00060                                         break;
00061                                 case 'i':
00062                                         if (i + 1 < argc)
00063                                                 device_in = argv[++i];
00064                                         break;
00065                                 case 'I':
00066                                         if (i + 1 < argc)
00067                                                 node_in = argv[++i];
00068                                         break;
00069                                 case 'o':
00070                                         if (i + 1 < argc)
00071                                                 device_out = argv[++i];
00072                                         break;
00073                                 case 'O':
00074                                         if (i + 1 < argc)
00075                                                 node_out = argv[++i];
00076                                         break;
00077                         }                       
00078                 }
00079         }
00080 
00081         if (verbose) {
00082                 fprintf(stderr,"Using: \n");
00083                 fprintf(stderr,"Input: ");
00084                 if (device_in) {
00085                         fprintf(stderr,"device %s\n",device_in);
00086                 }else if (node_in){
00087                         fprintf(stderr,"%s\n",node_in); 
00088                 }else{
00089                         fprintf(stderr,"NONE\n");
00090                 }
00091                 fprintf(stderr,"Output: ");
00092                 if (device_out) {
00093                         fprintf(stderr,"device %s\n",device_out);
00094                 }else if (node_out){
00095                         fprintf(stderr,"%s\n",node_out);                
00096                 }else{
00097                         fprintf(stderr,"NONE\n");
00098                 }
00099         }
00100         
00101         if (device_in) {
00102                 err = snd_rawmidi_open(&handle_in,NULL,device_in,0);    
00103                 if (err) {
00104                         fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err);
00105                 }
00106         }
00107         if (node_in && (!node_out || strcmp(node_out,node_in))) {
00108                 fd_in = open(node_in,O_RDONLY);
00109                 if (fd_in<0) {
00110                         fprintf(stderr,"open %s for input failed\n",node_in);
00111                 }       
00112         }
00113 
00114         signal(SIGINT,sighandler);
00115 
00116         if (device_out) {
00117                 err = snd_rawmidi_open(NULL,&handle_out,device_out,0);
00118                 if (err) {
00119                         fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err);
00120                 }
00121         }
00122         if (node_out && (!node_in || strcmp(node_out,node_in))) {
00123                 fd_out = open(node_out,O_WRONLY);               
00124                 if (fd_out<0) {
00125                         fprintf(stderr,"open %s for output failed\n",node_out);
00126                 }       
00127         }
00128 
00129         if (node_in && node_out && strcmp(node_out,node_in)==0) {
00130                 fd_in = fd_out = open(node_out,O_RDWR);         
00131                 if (fd_out<0) {
00132                         fprintf(stderr,"open %s for input and output failed\n",node_out);
00133                 }               
00134         }
00135 
00136 
00137 
00138         if (!thru) {
00139                 if (handle_in || fd_in!=-1) {
00140                         fprintf(stderr,"Read midi in\n");
00141                         fprintf(stderr,"Press ctrl-c to stop\n");
00142                 }
00143 
00144                 if (handle_in) {
00145                         unsigned char ch;
00146                         while (!stop) {
00147                                 snd_rawmidi_read(handle_in,&ch,1);
00148                                 if (verbose) {
00149                                         fprintf(stderr,"read %02x\n",ch);
00150                                 }
00151                         }
00152                 }
00153                 if (fd_in!=-1) {
00154                         unsigned char ch;
00155                         while (!stop) {
00156                                 read(fd_in,&ch,1);
00157                                 if (verbose) {
00158                                         fprintf(stderr,"read %02x\n",ch);
00159                                 }
00160                         }       
00161                 }
00162 
00163                 if (handle_out || fd_out!=-1) {
00164                         fprintf(stderr,"Writing note on / note off\n");
00165                 }
00166 
00167                 if (handle_out) {
00168                         unsigned char ch;
00169                         ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
00170                         ch=60;   snd_rawmidi_write(handle_out,&ch,1);
00171                         ch=100;  snd_rawmidi_write(handle_out,&ch,1);
00172                         snd_rawmidi_drain(handle_out);
00173                         sleep(1);
00174                         ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
00175                         ch=60;   snd_rawmidi_write(handle_out,&ch,1);
00176                         ch=0;    snd_rawmidi_write(handle_out,&ch,1);
00177                         snd_rawmidi_drain(handle_out); 
00178                 }
00179                 if (fd_out!=-1) {
00180                         unsigned char ch;
00181                         ch=0x90; write(fd_out,&ch,1);
00182                         ch=60;   write(fd_out,&ch,1);
00183                         ch=100;  write(fd_out,&ch,1);
00184                         sleep(1);
00185                         ch=0x90; write(fd_out,&ch,1);
00186                         ch=60;   write(fd_out,&ch,1);
00187                         ch=0;    write(fd_out,&ch,1);
00188                 }
00189         } else {
00190                 if ((handle_in || fd_in!=-1) && (handle_out || fd_out!=-1)) {
00191                         if (verbose) {
00192                                 fprintf(stderr,"Testing midi thru in\n");
00193                         }
00194                         while (!stop) {
00195                                 unsigned char ch;
00196                         
00197                                 if (handle_in) {
00198                                         snd_rawmidi_read(handle_in,&ch,1);
00199                                 }
00200                                 if (fd_in!=-1) {
00201                                         read(fd_in,&ch,1);
00202                                 }       
00203                                 if (verbose) {
00204                                         fprintf(stderr,"thru: %02x\n",ch);
00205                                 }
00206 
00207                                 if (handle_out) {
00208                                         snd_rawmidi_write(handle_out,&ch,1);
00209                                         snd_rawmidi_drain(handle_out); 
00210                                 }
00211                                 if (fd_out!=-1) {
00212                                         write(fd_out,&ch,1);
00213                                 }
00214                         }
00215                 }else{
00216                                 fprintf(stderr,"Testing midi thru needs both input and output\n");              
00217                                 exit(-1);
00218                 }
00219         }
00220 
00221         if (verbose) {
00222                 fprintf(stderr,"Closing\n");
00223         }
00224         
00225         if (handle_in) {
00226                 snd_rawmidi_drain(handle_in); 
00227                 snd_rawmidi_close(handle_in);   
00228         }
00229         if (handle_out) {
00230                 snd_rawmidi_drain(handle_out); 
00231                 snd_rawmidi_close(handle_out);  
00232         }
00233         if (fd_in!=-1) {
00234                 close(fd_in);
00235         }
00236         if (fd_out!=-1) {
00237                 close(fd_out);
00238         }
00239 
00240         return 0;
00241 }

Generated on Tue Apr 10 08:32:00 2007 for ALSA project - the C library reference by  doxygen 1.5.1