-
[sound-tools/ossmidi.git] / midiio.c
index 06f547c..b4c1c48 100644 (file)
--- a/midiio.c
+++ b/midiio.c
 #include "midiio.h"
 
 /* soem defines */
 #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 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;
 
 /* global, do we need sockets global? */
 int midi_fd;
@@ -64,25 +67,71 @@ int note_off(int fd,int chan,int note,int vel) {
  return 0;
 }
 
  return 0;
 }
 
+int midi_read(int fd,char *buf) {
+ return(read(fd,buf,1));
+}
+
+int midi_read_msg(int fd,char *buf) {
+ char tmp_buf;
+ int i;
+ midi_read(fd,tmp_buf);
+ printf("debug: %x\n",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) {
 
 #ifdef TEST_API
 /* test the io api ... */
 
 int main(int argc,char **argv) {
 
- int note,channel,i;
+ int note,channel,i,j,k;
+ char my_buf[3];
 
  if(argc>1) {
   note=atoi(argv[2]);
   channel=atoi(argv[1]);
  }
 
 
  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);
+ 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);
 
  for(i=0;i<4;i++) {
   note_off(midi_fd,0,38,127);
 
  all_stop(midi_fd);
  sleep(2);
 
  for(i=0;i<4;i++) {
   note_off(midi_fd,0,38,127);
+  note_off(midi_fd,0,42,127);
+  note_on(midi_fd,0,42,127);
   note_on(midi_fd,0,38,127);
   sleep(1);
   note_off(midi_fd,0,42,127);
   note_on(midi_fd,0,38,127);
   sleep(1);
   note_off(midi_fd,0,42,127);