From: hackbard Date: Wed, 12 Feb 2003 03:03:37 +0000 (+0000) Subject: - X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=bc1a171461ae70604afcd46eaa969f23597aa1a4;p=sound-tools%2Fossmidi.git - --- diff --git a/midiio.c b/midiio.c index dc4cd78..8414a97 100644 --- a/midiio.c +++ b/midiio.c @@ -18,13 +18,16 @@ #include "midiio.h" /* soem defines */ -#define MIDI_DEVICE /dev/midi +#define MIDI_DEVICE "/dev/midi" #define NOTE_ON 0x90 #define NOTE_OFF 0x80 #define NOTE_DATA 0x7F #define CH_MASK 0x0F #define VEL_MASK 0x7F +#define MSB_MASK 0xF0 +#define LSB_MASK 0x0F + /* global, do we need sockets global? */ int midi_fd; @@ -64,31 +67,64 @@ int note_off(int fd,int chan,int note,int vel) { return 0; } -int midi_read_msg(int fd,char *buf) { +int midi_read(int fd,char *buf) { int bytes_read; - bytes_read=read(fd,buf,10); + bytes_read=read(fd,buf,1); return bytes_read; } +int midi_read_msg(int fd,char *buf) { + char tmp_buf; + int i; + midi_read(fd,&tmp_buf); + buf[0]=tmp_buf; + + /* decide how much to read */ + if(((buf[0]&MSB_MASK)==PROGRAM_CHANGE) || ((buf[0]&MSB_MASK)==CHANNEL_PRESSURE)) { + printf("debug: program change or channel pressure event detected\n"); + midi_read(fd,&tmp_buf); + buf[1]=tmp_buf; + return 2; + } else { + printf("debug: none program change or channel pressure event detected\n"); + for(i=0;i<2;i++) { + midi_read(fd,&tmp_buf); + buf[i+1]=tmp_buf; + } + return 3; + } +} + + + + #ifdef TEST_API /* test the io api ... */ int main(int argc,char **argv) { - int note,channel,i,j; - char my_buf[10]="MIDIMIDI"; + int note,channel,i,j,k; + char my_buf[3]; if(argc>1) { note=atoi(argv[2]); channel=atoi(argv[1]); } - midi_fd=open("/dev/sound/midi",O_RDWR); + midi_fd=open(MIDI_DEVICE,O_RDWR); + printf("debug: midi_fd = %d\n",midi_fd); printf("reading ...\n"); - i=midi_read_msg(midi_fd,my_buf); - for(j=0;j=0;j--) printf("%s%d%s",(j==7?"|":""), + (((int)my_buf[k] & (1<0?1:0), + (j==0?"|\t":"|")); + } printf("\n"); + printf("sleep for 3 secs ...\n"); + sleep(3); all_stop(midi_fd); sleep(2); diff --git a/midiio.h b/midiio.h index 1925eca..5e2ff62 100644 --- a/midiio.h +++ b/midiio.h @@ -1,9 +1,17 @@ +/* defines */ + +#define PROGRAM_CHANGE 0xC0 +#define CHANNEL_PRESSURE 0xD0 + /* function prototypes */ int all_stop(int fd); -int midi_write_msg3(int fd,unsigned char status,unsigned char data1,unsigned char data2); +int midi_write_msg3(int fd,unsigned char status,unsigned char data1, + unsigned char data2); int midi_write_msg2(int fd,unsigned char status,unsigned char data1); int note_on(int fd,int chan,int note,int vel); int note_off(int fd,int chan,int note,int vel); +int midi_read_msg(int fd,char *buf); +int midi_read(int fd,char *buf);