06f547c6d70d2c7594ff10d599e438b38c7101f1
[sound-tools/ossmidi.git] / midiio.c
1 /* midiio.c
2  *
3  * author: hackbard
4  *
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15
16 #include <sys/soundcard.h>
17
18 #include "midiio.h"
19
20 /* soem defines */
21 #define MIDI_DEVICE /dev/midi
22 #define NOTE_ON 0x90
23 #define NOTE_OFF 0x80
24 #define NOTE_DATA 0x7F
25 #define CH_MASK 0x0F
26 #define VEL_MASK 0x7F
27
28
29 /* global, do we need sockets global? */
30 int midi_fd;
31
32 int all_stop(int fd) {
33  int i,j;
34  for(j=0;j<16;j++)
35   for(i=0;i<128;i++)
36    note_off(fd,j,i,(int)0x7F);
37  return 0;
38 }
39
40 int midi_write_msg3(int fd,unsigned char status,unsigned char data1,unsigned char data2) {
41  unsigned char buf[3];
42  buf[0]=status;
43  buf[1]=data1;
44  buf[2]=data2;
45  write(fd,buf,3);
46  return 0;
47 }
48
49 int midi_write_msg2(int fd,unsigned char status,unsigned char data1) {
50  unsigned char buf[2];
51  buf[0]=status;
52  buf[1]=data1;
53  write(fd,buf,2);
54  return 0;
55 }
56
57 int note_on(int fd,int chan,int note,int vel) {
58  midi_write_msg3(fd,NOTE_ON|(CH_MASK&chan),note&NOTE_DATA,vel&VEL_MASK);
59  return 0;
60 }
61
62 int note_off(int fd,int chan,int note,int vel) {
63  midi_write_msg3(fd,NOTE_OFF|(CH_MASK&chan),note&NOTE_DATA,vel&VEL_MASK);
64  return 0;
65 }
66
67 #ifdef TEST_API
68 /* test the io api ... */
69
70 int main(int argc,char **argv) {
71
72  int note,channel,i;
73
74  if(argc>1) {
75   note=atoi(argv[2]);
76   channel=atoi(argv[1]);
77  }
78
79  midi_fd=open("/dev/sound/midi",O_RDWR);
80
81  all_stop(midi_fd);
82  sleep(2);
83
84  for(i=0;i<4;i++) {
85   note_off(midi_fd,0,38,127);
86   note_on(midi_fd,0,38,127);
87   sleep(1);
88   note_off(midi_fd,0,42,127);
89   note_on(midi_fd,0,42,127);
90   sleep(1);
91  }
92
93  close(midi_fd);
94 }
95 #endif TEST_API
96