Fix some greedy sed changes in imported code. Also provide a sys/types.h for compatib...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / s_midi_sgi.c
blob105a812b497b405c8224a17a72dbfdfcfde6ed9c
1 /* Copyright (c) 1997-1999 Miller Puckette.
2 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
3 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
5 #include "s_stuff.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #ifdef HAVE_BSTRING_H
11 #include <bstring.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/time.h>
16 #include <dmedia/audio.h>
17 #include <sys/fpu.h>
18 #include <dmedia/midi.h>
19 int mdInit(void); /* prototype was messed up in midi.h */
20 /* #include "sys/select.h" */
24 set the special "flush zero" but (FS, bit 24) in the
25 Control Status Register of the FPU of R4k and beyond
26 so that the result of any underflowing operation will
27 be clamped to zero, and no exception of any kind will
28 be generated on the CPU.
30 thanks to cpirazzi@cp.esd.sgi.com (Chris Pirazzi).
33 static void sgi_flush_all_underflows_to_zero(void)
35 union fpc_csr f;
36 f.fc_word = get_fpc_csr();
37 f.fc_struct.flush = 1;
38 set_fpc_csr(f.fc_word);
41 #define NPORT 2
43 static MDport sgi_inport[NPORT];
44 static MDport sgi_outport[NPORT];
46 void sgi_open_midi(int midiin, int midiout)
48 int i;
49 int sgi_nports = mdInit();
50 if (sgi_nports < 0) sgi_nports = 0;
51 else if (sgi_nports > NPORT) sgi_nports = NPORT;
52 if (sys_verbose)
54 if (!sgi_nports)
56 post("no serial ports are configured for MIDI;");
57 post("if you want to use MIDI, try exiting Pd, typing");
58 post("'startmidi -d /dev/ttyd2' to a shell, and restarting Pd.");
60 else if (sgi_nports == 1)
61 post("Found one MIDI port on %s", mdGetName(0));
62 else if (sgi_nports == 2)
63 post("Found MIDI ports on %s and %s",
64 mdGetName(0), mdGetName(1));
66 if (midiin)
68 for (i = 0; i < sgi_nports; i++)
70 if (!(sgi_inport[i] = mdOpenInPort(mdGetName(i))))
71 error("MIDI input port %d: open failed", i+1);;
74 if (midiout)
76 for (i = 0; i < sgi_nports; i++)
78 if (!(sgi_outport[i] = mdOpenOutPort(mdGetName(i))))
79 error("MIDI output port %d: open failed", i+1);;
82 return;
85 void sys_putmidimess(int portno, int a, int b, int c)
87 MDevent mdv;
88 if (portno >= NPORT || portno < 0 || !sgi_outport[portno]) return;
89 mdv.msg[0] = a;
90 mdv.msg[1] = b;
91 mdv.msg[2] = c;
92 mdv.msg[3] = 0;
93 mdv.sysexmsg = 0;
94 mdv.stamp = 0;
95 mdv.msglen = 0;
96 if (mdSend(sgi_outport[portno], &mdv, 1) < 0)
97 error("MIDI output error\n");
98 post("msg out %d %d %d", a, b, c);
101 void sys_putmidibyte(int portno, int foo)
103 error("MIDI raw byte output not available on SGI");
106 void inmidi_noteon(int portno, int channel, int pitch, int velo);
107 void inmidi_controlchange(int portno, int channel, int ctlnumber, int value);
108 void inmidi_programchange(int portno, int channel, int value);
109 void inmidi_pitchbend(int portno, int channel, int value);
110 void inmidi_aftertouch(int portno, int channel, int value);
111 void inmidi_polyaftertouch(int portno, int channel, int pitch, int value);
113 void sys_poll_midi(void)
115 int i;
116 MDport *mp;
117 for (i = 0, mp = sgi_inport; i < NPORT; i++, mp++)
119 int ret, status, b1, b2, nfds;
120 MDevent mdv;
121 fd_set inports;
122 struct timeval timeout;
123 timeout.tv_sec = 0;
124 timeout.tv_usec = 0;
125 if (!*mp) continue;
126 FD_ZERO(&inports);
127 FD_SET(mdGetFd(*mp), &inports);
129 if (select(mdGetFd(*mp)+1 , &inports, 0, 0, &timeout) < 0)
130 perror("midi select");
131 if (FD_ISSET(mdGetFd(*mp),&inports))
133 if (mdReceive(*mp, &mdv, 1) < 0)
134 error("failure receiving message\n");
135 else if (mdv.msg[0] == MD_SYSEX) mdFree(mdv.sysexmsg);
137 else
139 int status = mdv.msg[0];
140 int channel = (status & 0xf) + 1;
141 int b1 = mdv.msg[1];
142 int b2 = mdv.msg[2];
143 switch(status & 0xf0)
145 case MD_NOTEOFF:
146 inmidi_noteon(i, channel, b1, 0);
147 break;
148 case MD_NOTEON:
149 inmidi_noteon(i, channel, b1, b2);
150 break;
151 case MD_POLYKEYPRESSURE:
152 inmidi_polyaftertouch(i, channel, b1, b2);
153 break;
154 case MD_CONTROLCHANGE:
155 inmidi_controlchange(i, channel, b1, b2);
156 break;
157 case MD_PITCHBENDCHANGE:
158 inmidi_pitchbend(i, channel, ((b2 << 7) + b1));
159 break;
160 case MD_PROGRAMCHANGE:
161 inmidi_programchange(i, channel, b1);
162 break;
163 case MD_CHANNELPRESSURE:
164 inmidi_aftertouch(i, channel, b1);
165 break;
172 void sys_open_midi(int nmidiin, int *midiinvec,
173 int nmidiout, int *midioutvec)
175 sgi_open_midi(nmidiin!=0, nmidiout!=0);
179 void sys_close_midi( void)
181 /* ??? */
184 void sys_set_priority(int foo)
186 fprintf(stderr,
187 "warning: priority boosting in IRIX not implemented yet\n");