first tries - midiio api
authorhackbard <hackbard>
Tue, 11 Feb 2003 04:31:01 +0000 (04:31 +0000)
committerhackbard <hackbard>
Tue, 11 Feb 2003 04:31:01 +0000 (04:31 +0000)
midiio.c [new file with mode: 0644]
midiio.h [new file with mode: 0644]

diff --git a/midiio.c b/midiio.c
new file mode 100644 (file)
index 0000000..06f547c
--- /dev/null
+++ b/midiio.c
@@ -0,0 +1,96 @@
+/* midiio.c
+ *
+ * author: hackbard
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/soundcard.h>
+
+#include "midiio.h"
+
+/* soem defines */
+#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
+
+
+/* global, do we need sockets global? */
+int midi_fd;
+
+int all_stop(int fd) {
+ int i,j;
+ for(j=0;j<16;j++)
+  for(i=0;i<128;i++)
+   note_off(fd,j,i,(int)0x7F);
+ return 0;
+}
+
+int midi_write_msg3(int fd,unsigned char status,unsigned char data1,unsigned char data2) {
+ unsigned char buf[3];
+ buf[0]=status;
+ buf[1]=data1;
+ buf[2]=data2;
+ write(fd,buf,3);
+ return 0;
+}
+
+int midi_write_msg2(int fd,unsigned char status,unsigned char data1) {
+ unsigned char buf[2];
+ buf[0]=status;
+ buf[1]=data1;
+ write(fd,buf,2);
+ return 0;
+}
+
+int note_on(int fd,int chan,int note,int vel) {
+ midi_write_msg3(fd,NOTE_ON|(CH_MASK&chan),note&NOTE_DATA,vel&VEL_MASK);
+ return 0;
+}
+
+int note_off(int fd,int chan,int note,int vel) {
+ midi_write_msg3(fd,NOTE_OFF|(CH_MASK&chan),note&NOTE_DATA,vel&VEL_MASK);
+ return 0;
+}
+
+#ifdef TEST_API
+/* test the io api ... */
+
+int main(int argc,char **argv) {
+
+ int note,channel,i;
+
+ if(argc>1) {
+  note=atoi(argv[2]);
+  channel=atoi(argv[1]);
+ }
+
+ midi_fd=open("/dev/sound/midi",O_RDWR);
+
+ all_stop(midi_fd);
+ sleep(2);
+
+ for(i=0;i<4;i++) {
+  note_off(midi_fd,0,38,127);
+  note_on(midi_fd,0,38,127);
+  sleep(1);
+  note_off(midi_fd,0,42,127);
+  note_on(midi_fd,0,42,127);
+  sleep(1);
+ }
+
+ close(midi_fd);
+}
+#endif TEST_API
+
diff --git a/midiio.h b/midiio.h
new file mode 100644 (file)
index 0000000..1925eca
--- /dev/null
+++ b/midiio.h
@@ -0,0 +1,9 @@
+/* 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_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);
+
+