OSDN Git Service

ucm: implemented card list feature
[android-x86/external-alsa-lib.git] / test / rawmidi.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include "../include/asoundlib.h"
5 #include <signal.h>
6
7 static void usage(void)
8 {
9         fprintf(stderr, "usage: rawmidi [options]\n");
10         fprintf(stderr, "  options:\n");
11         fprintf(stderr, "    -v: verbose mode\n");
12         fprintf(stderr, "    -i device-id : test ALSA input device\n");
13         fprintf(stderr, "    -o device-id : test ALSA output device\n");
14         fprintf(stderr, "    -I node      : test input node\n");
15         fprintf(stderr, "    -O node      : test output node\n");
16         fprintf(stderr, "    -t: test midi thru\n");
17         fprintf(stderr, "  example:\n");
18         fprintf(stderr, "    rawmidi -i hw:0,0 -O /dev/midi1\n");
19         fprintf(stderr, "    tests input for card 0, device 0, using snd_rawmidi API\n");
20         fprintf(stderr, "    and /dev/midi1 using file descriptors\n");
21 }
22
23 int stop=0;
24
25 void sighandler(int dum)
26 {
27         stop=1;
28 }
29
30 int main(int argc,char** argv)
31 {
32         int i;
33         int err;
34         int thru=0;
35         int verbose = 0;
36         char *device_in = NULL;
37         char *device_out = NULL;
38         char *node_in = NULL;
39         char *node_out = NULL;
40         
41         int fd_in = -1,fd_out = -1;
42         snd_rawmidi_t *handle_in = 0,*handle_out = 0;
43         
44         if (argc==1) {
45                 usage();
46                 exit(0);
47         }
48         
49         for (i = 1 ; i<argc ; i++) {
50                 if (argv[i][0]=='-') {
51                         switch (argv[i][1]) {
52                                 case 'h':
53                                         usage();
54                                         break;
55                                 case 'v':
56                                         verbose = 1;
57                                         break;
58                                 case 't':
59                                         thru = 1;
60                                         break;
61                                 case 'i':
62                                         if (i + 1 < argc)
63                                                 device_in = argv[++i];
64                                         break;
65                                 case 'I':
66                                         if (i + 1 < argc)
67                                                 node_in = argv[++i];
68                                         break;
69                                 case 'o':
70                                         if (i + 1 < argc)
71                                                 device_out = argv[++i];
72                                         break;
73                                 case 'O':
74                                         if (i + 1 < argc)
75                                                 node_out = argv[++i];
76                                         break;
77                         }                       
78                 }
79         }
80
81         if (verbose) {
82                 fprintf(stderr,"Using: \n");
83                 fprintf(stderr,"Input: ");
84                 if (device_in) {
85                         fprintf(stderr,"device %s\n",device_in);
86                 }else if (node_in){
87                         fprintf(stderr,"%s\n",node_in); 
88                 }else{
89                         fprintf(stderr,"NONE\n");
90                 }
91                 fprintf(stderr,"Output: ");
92                 if (device_out) {
93                         fprintf(stderr,"device %s\n",device_out);
94                 }else if (node_out){
95                         fprintf(stderr,"%s\n",node_out);                
96                 }else{
97                         fprintf(stderr,"NONE\n");
98                 }
99         }
100         
101         if (device_in) {
102                 err = snd_rawmidi_open(&handle_in,NULL,device_in,0);    
103                 if (err) {
104                         fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err);
105                 }
106         }
107         if (node_in && (!node_out || strcmp(node_out,node_in))) {
108                 fd_in = open(node_in,O_RDONLY);
109                 if (fd_in<0) {
110                         fprintf(stderr,"open %s for input failed\n",node_in);
111                 }       
112         }
113
114         signal(SIGINT,sighandler);
115
116         if (device_out) {
117                 err = snd_rawmidi_open(NULL,&handle_out,device_out,0);
118                 if (err) {
119                         fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err);
120                 }
121         }
122         if (node_out && (!node_in || strcmp(node_out,node_in))) {
123                 fd_out = open(node_out,O_WRONLY);               
124                 if (fd_out<0) {
125                         fprintf(stderr,"open %s for output failed\n",node_out);
126                 }       
127         }
128
129         if (node_in && node_out && strcmp(node_out,node_in)==0) {
130                 fd_in = fd_out = open(node_out,O_RDWR);         
131                 if (fd_out<0) {
132                         fprintf(stderr,"open %s for input and output failed\n",node_out);
133                 }               
134         }
135
136
137
138         if (!thru) {
139                 if (handle_in || fd_in!=-1) {
140                         fprintf(stderr,"Read midi in\n");
141                         fprintf(stderr,"Press ctrl-c to stop\n");
142                 }
143
144                 if (handle_in) {
145                         unsigned char ch;
146                         while (!stop) {
147                                 snd_rawmidi_read(handle_in,&ch,1);
148                                 if (verbose) {
149                                         fprintf(stderr,"read %02x\n",ch);
150                                 }
151                         }
152                 }
153                 if (fd_in!=-1) {
154                         unsigned char ch;
155                         while (!stop) {
156                                 read(fd_in,&ch,1);
157                                 if (verbose) {
158                                         fprintf(stderr,"read %02x\n",ch);
159                                 }
160                         }       
161                 }
162
163                 if (handle_out || fd_out!=-1) {
164                         fprintf(stderr,"Writing note on / note off\n");
165                 }
166
167                 if (handle_out) {
168                         unsigned char ch;
169                         ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
170                         ch=60;   snd_rawmidi_write(handle_out,&ch,1);
171                         ch=100;  snd_rawmidi_write(handle_out,&ch,1);
172                         snd_rawmidi_drain(handle_out);
173                         sleep(1);
174                         ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
175                         ch=60;   snd_rawmidi_write(handle_out,&ch,1);
176                         ch=0;    snd_rawmidi_write(handle_out,&ch,1);
177                         snd_rawmidi_drain(handle_out); 
178                 }
179                 if (fd_out!=-1) {
180                         unsigned char ch;
181                         ch=0x90; write(fd_out,&ch,1);
182                         ch=60;   write(fd_out,&ch,1);
183                         ch=100;  write(fd_out,&ch,1);
184                         sleep(1);
185                         ch=0x90; write(fd_out,&ch,1);
186                         ch=60;   write(fd_out,&ch,1);
187                         ch=0;    write(fd_out,&ch,1);
188                 }
189         } else {
190                 if ((handle_in || fd_in!=-1) && (handle_out || fd_out!=-1)) {
191                         if (verbose) {
192                                 fprintf(stderr,"Testing midi thru in\n");
193                         }
194                         while (!stop) {
195                                 unsigned char ch;
196                         
197                                 if (handle_in) {
198                                         snd_rawmidi_read(handle_in,&ch,1);
199                                 }
200                                 if (fd_in!=-1) {
201                                         read(fd_in,&ch,1);
202                                 }       
203                                 if (verbose) {
204                                         fprintf(stderr,"thru: %02x\n",ch);
205                                 }
206
207                                 if (handle_out) {
208                                         snd_rawmidi_write(handle_out,&ch,1);
209                                         snd_rawmidi_drain(handle_out); 
210                                 }
211                                 if (fd_out!=-1) {
212                                         write(fd_out,&ch,1);
213                                 }
214                         }
215                 }else{
216                                 fprintf(stderr,"Testing midi thru needs both input and output\n");              
217                                 exit(-1);
218                 }
219         }
220
221         if (verbose) {
222                 fprintf(stderr,"Closing\n");
223         }
224         
225         if (handle_in) {
226                 snd_rawmidi_drain(handle_in); 
227                 snd_rawmidi_close(handle_in);   
228         }
229         if (handle_out) {
230                 snd_rawmidi_drain(handle_out); 
231                 snd_rawmidi_close(handle_out);  
232         }
233         if (fd_in!=-1) {
234                 close(fd_in);
235         }
236         if (fd_out!=-1) {
237                 close(fd_out);
238         }
239
240         return 0;
241 }