All of lore.kernel.org
 help / color / mirror / Atom feed
* [Fwd: ALSA programming example fix]
@ 2002-07-10 17:37 Patrick Shirkey
  0 siblings, 0 replies; only message in thread
From: Patrick Shirkey @ 2002-07-10 17:37 UTC (permalink / raw)
  To: alsa-devel

[-- Attachment #1: Type: text/plain, Size: 63 bytes --]

This has been sent to me as a fix for one of the doxygen docs.

[-- Attachment #2: ALSA programming example fix --]
[-- Type: message/rfc822, Size: 10180 bytes --]

[-- Attachment #2.1.1: Type: text/plain, Size: 1369 bytes --]

Patrick --

Here is a fix for the test/rawmidi.c file that comes with the 0.9 RawMidi API 
docs.

On line 18 the usage example erroneously suggests that the '-o' flag will 
work with a file descriptor. There's three days of my life I'll never get 
back.

On line 140 it suggests a ctrl-c to get contol back from read() la-la land. I 
would fix this, but perhaps the original author could be contacted as to his 
intentions when he wrote it.

Regards,

Matt Walker

--------------------------------------------------------------------------------------

Message: 1
Date: Mon, 10 Jun 2002 15:16:50 +0900
From: Patrick Shirkey <pshirkey@boosthardware.com>
Organization: Boost Hardware
To: Paul Davis <pbd@op.net>
CC: tomasz motylewski <T.Motylewski@bfad.de>, Tim Goetze <tim@quitte.de>,
        Philipp Vollmer <vollmer.philipp@t-online.de>,
        alsa-devel@alsa-project.org
Subject: Re: [Alsa-devel] Alsa Programming Examples

Paul Davis wrote:

> 
> agreed. perhaps you can fix them, or produce a version of the document
> with them removed so that a better link can be used. If you would like
> to pay me to rearrange my time to get the code removed and/or fixed,
> thats fine. Otherwise, please accept the limited offering thats linked
> to right now.
> 
> 

If people send in a fix to that file I'll add it in and put up the 
revision on ALSA if Paul doesn't mind.

[-- Attachment #2.1.2: rawmidi.c --]
[-- Type: text/x-c, Size: 5567 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <alsa/asoundlib.h>
#include <signal.h>

static void usage(void)
{
    fprintf(stderr, "usage: rawmidi [options]\n");
    fprintf(stderr, "  options:\n");
    fprintf(stderr, "    -v: verbose mode\n");
    fprintf(stderr, "    -i device-id : test ALSA input device\n");
    fprintf(stderr, "    -o device-id : test ALSA output device\n");
    fprintf(stderr, "    -I node      : test input node\n");
    fprintf(stderr, "    -O node      : test output node\n");
    fprintf(stderr, "    -t: test midi thru\n");
    fprintf(stderr, "  example:\n");
    fprintf(stderr, "    rawmidi -i hw:0,0 -O /dev/snd/midiC0D0\n");
    fprintf(stderr, "    tests input for card 0, device 0, using snd_rawmidi API\n");
    fprintf(stderr, "    and /dev/midi1 using file descriptors\n");
}

int stop=0;

void sighandler(int dum)
{
    stop=1;
}

int main(int argc,char** argv)
{
    int i;
    int err;
    int thru=0;
    int verbose = 0;
    char *device_in = NULL;
    char *device_out = NULL;
    char *node_in = NULL;
    char *node_out = NULL;
        
    int fd_in = -1,fd_out = -1;
    snd_rawmidi_t *handle_in = 0,*handle_out = 0;
        
    if (argc==1) {
	usage();
	exit(0);
    }
        
    for (i = 1 ; i<argc ; i++) {
	if (argv[i][0]=='-') {
	    switch (argv[i][1]) {
		case 'h':
		    usage();
		    break;
		case 'v':
		    verbose = 1;
		    break;
		case 't':
		    thru = 1;
		    break;
		case 'i':
		    if (i + 1 < argc)
			device_in = argv[++i];
		    break;
		case 'I':
		    if (i + 1 < argc)
			node_in = argv[++i];
		    break;
		case 'o':
		    if (i + 1 < argc)
			device_out = argv[++i];
		    break;
		case 'O':
		    if (i + 1 < argc)
			node_out = argv[++i];
		    break;
	    }                       
	}
    }

    if (verbose) {
	fprintf(stderr,"Using: \n");
	fprintf(stderr,"Input: ");
	if (device_in) {
	    fprintf(stderr,"device %s\n",device_in);
	}else if (node_in){
	    fprintf(stderr,"%s\n",node_in); 
	}else{
	    fprintf(stderr,"NONE\n");
	}
	fprintf(stderr,"Output: ");
	if (device_out) {
	    fprintf(stderr,"device %s\n",device_out);
	}else if (node_out){
	    fprintf(stderr,"%s\n",node_out);                
	}else{
	    fprintf(stderr,"NONE\n");
	}
    }
        
    if (device_in) {
	err = snd_rawmidi_open(&handle_in,NULL,device_in,0);    
	if (err) {
	    fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err);
	}
    }
    if (node_in && (!node_out || strcmp(node_out,node_in))) {
	fd_in = open(node_in,O_RDONLY);
	if (fd_in<0) {
	    fprintf(stderr,"open %s for input failed\n",node_in);
	}       
    }

    signal(SIGINT,sighandler);

    if (device_out) {
	err = snd_rawmidi_open(NULL,&handle_out,device_out,0);
	if (err) {
	    fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err);
	}
    }
    if (node_out && (!node_in || strcmp(node_out,node_in))) {
	fd_out = open(node_out,O_WRONLY);               
	if (fd_out<0) {
	    fprintf(stderr,"open %s for output failed\n",node_out);
	}       
    }

    if (node_in && node_out && strcmp(node_out,node_in)==0) {
	fd_in = fd_out = open(node_out,O_RDWR);         
	if (fd_out<0) {
	    fprintf(stderr,"open %s for input and output failed\n",node_out);
	}               
    }



    if (!thru) {
	if (handle_in || fd_in!=-1) {
	    fprintf(stderr,"Read midi in\n");
	    fprintf(stderr,"Press ctrl-c to stop\n");
	}

	if (handle_in) {
	    unsigned char ch;
	    while (!stop) {
		snd_rawmidi_read(handle_in,&ch,1);
		if (verbose) {
		    fprintf(stderr,"read %02x\n",ch);
		}
	    }
	}
	if (fd_in!=-1) {
	    unsigned char ch;
	    while (!stop) {
		read(fd_in,&ch,1);
		if (verbose) {
		    fprintf(stderr,"read %02x\n",ch);
		}
	    }       
	}

	if (handle_out || fd_out!=-1) {
	    fprintf(stderr,"Writing note on / note off\n");
	}

	if (handle_out) {
	    unsigned char ch;
	    ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
	    ch=60;   snd_rawmidi_write(handle_out,&ch,1);
	    ch=100;  snd_rawmidi_write(handle_out,&ch,1);
	    snd_rawmidi_drain(handle_out);
	    sleep(1);
	    ch=0x90; snd_rawmidi_write(handle_out,&ch,1);
	    ch=60;   snd_rawmidi_write(handle_out,&ch,1);
	    ch=0;    snd_rawmidi_write(handle_out,&ch,1);
	    snd_rawmidi_drain(handle_out); 
	}
	if (fd_out!=-1) {
	    unsigned char ch;
	    ch=0x90; write(fd_out,&ch,1);
	    ch=60;   write(fd_out,&ch,1);
	    ch=100;  write(fd_out,&ch,1);
	    sleep(1);
	    ch=0x90; write(fd_out,&ch,1);
	    ch=60;   write(fd_out,&ch,1);
	    ch=0;    write(fd_out,&ch,1);
	}
    } else {
	if ((handle_in || fd_in!=-1) && (handle_out || fd_out!=-1)) {
	    if (verbose) {
		fprintf(stderr,"Testing midi thru in\n");
	    }
	    while (!stop) {
		unsigned char ch;
                        
		if (handle_in) {
		    snd_rawmidi_read(handle_in,&ch,1);
		}
		if (fd_in!=-1) {
		    read(fd_in,&ch,1);
		}       
		if (verbose) {
		    fprintf(stderr,"thru: %02x\n",ch);
		}

		if (handle_out) {
		    snd_rawmidi_write(handle_out,&ch,1);
		    snd_rawmidi_drain(handle_out); 
		}
		if (fd_out!=-1) {
		    write(fd_out,&ch,1);
		}
	    }
	}else{
	    fprintf(stderr,"Testing midi thru needs both input and output\n");              
	    exit(-1);
	}
    }

    if (verbose) {
	fprintf(stderr,"Closing\n");
    }
        
    if (handle_in) {
	snd_rawmidi_drain(handle_in); 
	snd_rawmidi_close(handle_in);   
    }
    if (handle_out) {
	snd_rawmidi_drain(handle_out); 
	snd_rawmidi_close(handle_out);  
    }
    if (fd_in!=-1) {
	close(fd_in);
    }
    if (fd_out!=-1) {
	close(fd_out);
    }

    return 0;
}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-07-10 17:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-10 17:37 [Fwd: ALSA programming example fix] Patrick Shirkey

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.