Lines Matching defs:*
0 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include "../include/asoundlib.h"
5 #include <signal.h>
7 static void usage(void)
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");
23 int stop=0;
25 void sighandler(int dum)
27 stop=1;
30 int main(int argc,char** argv)
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;
41 int fd_in = -1,fd_out = -1;
42 snd_rawmidi_t *handle_in = 0,*handle_out = 0;
44 if (argc==1) {
45 usage();
46 exit(0);
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;
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");
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");
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);
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);
114 signal(SIGINT,sighandler);
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);
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);
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);
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");
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);
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);
163 if (handle_out || fd_out!=-1) {
164 fprintf(stderr,"Writing note on / note off\n");
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);
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);
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");
194 while (!stop) {
195 unsigned char ch;
197 if (handle_in) {
198 snd_rawmidi_read(handle_in,&ch,1);
200 if (fd_in!=-1) {
201 read(fd_in,&ch,1);
203 if (verbose) {
204 fprintf(stderr,"thru: %02x\n",ch);
207 if (handle_out) {
208 snd_rawmidi_write(handle_out,&ch,1);
209 snd_rawmidi_drain(handle_out);
211 if (fd_out!=-1) {
212 write(fd_out,&ch,1);
215 }else{
216 fprintf(stderr,"Testing midi thru needs both input and output\n");
217 exit(-1);
221 if (verbose) {
222 fprintf(stderr,"Closing\n");
225 if (handle_in) {
226 snd_rawmidi_drain(handle_in);
227 snd_rawmidi_close(handle_in);
229 if (handle_out) {
230 snd_rawmidi_drain(handle_out);
231 snd_rawmidi_close(handle_out);
233 if (fd_in!=-1) {
234 close(fd_in);
236 if (fd_out!=-1) {
237 close(fd_out);
240 return 0;