-
[sound-tools/ossmidi.git] / midiio.c
index dc4cd78..8414a97 100644 (file)
--- a/midiio.c
+++ b/midiio.c
 #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<i;j++) printf("%x ",my_buf[j]);
+ i=midi_read_msg(midi_fd,&my_buf[1]);
+ printf("debug: i = %d\n",i);
+ for(k=0;k<i;k++) {
+  for(j=7;j>=0;j--) printf("%s%d%s",(j==7?"|":""),
+                                (((int)my_buf[k] & (1<<j))>0?1:0),
+                                (j==0?"|\t":"|"));
+ }
  printf("\n");
+ printf("sleep for 3 secs ...\n");
+ sleep(3);
 
  all_stop(midi_fd);
  sleep(2);