diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
commit | 513389b4c1bc8afe4b2dc9947c534bfeb105e3da (patch) | |
tree | 10e673b35651ac567fed2eda0c679c7ade64cbc6 /apps/plugins/pdbox/PDa/src/s_midi.c | |
parent | 95fa7f6a2ef466444fbe3fe87efc6d5db6b77b36 (diff) | |
download | rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.tar.gz rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.zip |
Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin project of Wincent Balin. Stripped some non-sourcefiles and added a rockbox readme that needs a bit more info from Wincent. Is added to CATEGORIES and viewers, but not yet to SUBDIRS (ie doesn't build yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21044 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_midi.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/src/s_midi.c | 1282 |
1 files changed, 1282 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_midi.c b/apps/plugins/pdbox/PDa/src/s_midi.c new file mode 100644 index 0000000000..4338b49432 --- /dev/null +++ b/apps/plugins/pdbox/PDa/src/s_midi.c | |||
@@ -0,0 +1,1282 @@ | |||
1 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
2 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
3 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
4 | |||
5 | /* Clock functions (which should move, but where?) and MIDI queueing */ | ||
6 | |||
7 | #include "m_pd.h" | ||
8 | #include "s_stuff.h" | ||
9 | #include "m_imp.h" | ||
10 | #ifdef UNIX | ||
11 | #include <unistd.h> | ||
12 | #include <sys/time.h> | ||
13 | #ifdef HAVE_BSTRING_H | ||
14 | #include <bstring.h> | ||
15 | #endif | ||
16 | #endif | ||
17 | #ifdef MSW | ||
18 | #include <winsock.h> | ||
19 | #include <sys/types.h> | ||
20 | #include <sys/timeb.h> | ||
21 | #include <wtypes.h> | ||
22 | #endif | ||
23 | #include <string.h> | ||
24 | #include <stdio.h> | ||
25 | #include <signal.h> | ||
26 | |||
27 | typedef struct _midiqelem | ||
28 | { | ||
29 | double q_time; | ||
30 | int q_portno; | ||
31 | unsigned char q_onebyte; | ||
32 | unsigned char q_byte1; | ||
33 | unsigned char q_byte2; | ||
34 | unsigned char q_byte3; | ||
35 | } t_midiqelem; | ||
36 | |||
37 | #define MIDIQSIZE 1024 | ||
38 | |||
39 | t_midiqelem midi_outqueue[MIDIQSIZE]; | ||
40 | int midi_outhead, midi_outtail; | ||
41 | t_midiqelem midi_inqueue[MIDIQSIZE]; | ||
42 | int midi_inhead, midi_intail; | ||
43 | static double sys_midiinittime; | ||
44 | |||
45 | /* this is our current estimate for at what "system" real time the | ||
46 | current logical time's output should occur. */ | ||
47 | static double sys_dactimeminusrealtime; | ||
48 | /* same for input, should be schduler advance earlier. */ | ||
49 | static double sys_adctimeminusrealtime; | ||
50 | |||
51 | static double sys_newdactimeminusrealtime = -1e20; | ||
52 | static double sys_newadctimeminusrealtime = -1e20; | ||
53 | static double sys_whenupdate; | ||
54 | |||
55 | void sys_initmidiqueue( void) | ||
56 | { | ||
57 | sys_midiinittime = clock_getlogicaltime(); | ||
58 | sys_dactimeminusrealtime = sys_adctimeminusrealtime = 0; | ||
59 | } | ||
60 | |||
61 | /* this is called from the OS dependent code from time to time when we | ||
62 | think we know the delay (outbuftime) in seconds, at which the last-output | ||
63 | audio sample will go out the door. */ | ||
64 | void sys_setmiditimediff(double inbuftime, double outbuftime) | ||
65 | { | ||
66 | double dactimeminusrealtime = | ||
67 | .001 * clock_gettimesince(sys_midiinittime) | ||
68 | - outbuftime - sys_getrealtime(); | ||
69 | double adctimeminusrealtime = | ||
70 | .001 * clock_gettimesince(sys_midiinittime) | ||
71 | + inbuftime - sys_getrealtime(); | ||
72 | if (dactimeminusrealtime > sys_newdactimeminusrealtime) | ||
73 | sys_newdactimeminusrealtime = dactimeminusrealtime; | ||
74 | if (adctimeminusrealtime > sys_newadctimeminusrealtime) | ||
75 | sys_newadctimeminusrealtime = adctimeminusrealtime; | ||
76 | if (sys_getrealtime() > sys_whenupdate) | ||
77 | { | ||
78 | sys_dactimeminusrealtime = sys_newdactimeminusrealtime; | ||
79 | sys_adctimeminusrealtime = sys_newadctimeminusrealtime; | ||
80 | sys_newdactimeminusrealtime = -1e20; | ||
81 | sys_newadctimeminusrealtime = -1e20; | ||
82 | sys_whenupdate = sys_getrealtime() + 1; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /* return the logical time of the DAC sample we believe is currently | ||
87 | going out, based on how much "system time" has elapsed since the | ||
88 | last time sys_setmiditimediff got called. */ | ||
89 | static double sys_getmidioutrealtime( void) | ||
90 | { | ||
91 | return (sys_getrealtime() + sys_dactimeminusrealtime); | ||
92 | } | ||
93 | |||
94 | static double sys_getmidiinrealtime( void) | ||
95 | { | ||
96 | return (sys_getrealtime() + sys_adctimeminusrealtime); | ||
97 | } | ||
98 | |||
99 | static void sys_putnext( void) | ||
100 | { | ||
101 | int portno = midi_outqueue[midi_outtail].q_portno; | ||
102 | if (midi_outqueue[midi_outtail].q_onebyte) | ||
103 | sys_putmidibyte(portno, midi_outqueue[midi_outtail].q_byte1); | ||
104 | else sys_putmidimess(portno, midi_outqueue[midi_outtail].q_byte1, | ||
105 | midi_outqueue[midi_outtail].q_byte2, | ||
106 | midi_outqueue[midi_outtail].q_byte3); | ||
107 | midi_outtail = (midi_outtail + 1 == MIDIQSIZE ? 0 : midi_outtail + 1); | ||
108 | } | ||
109 | |||
110 | /* #define TEST_DEJITTER */ | ||
111 | |||
112 | void sys_pollmidioutqueue( void) | ||
113 | { | ||
114 | #ifdef TEST_DEJITTER | ||
115 | static int db = 0; | ||
116 | #endif | ||
117 | double midirealtime = sys_getmidioutrealtime(); | ||
118 | #ifdef TEST_DEJITTER | ||
119 | if (midi_outhead == midi_outtail) | ||
120 | db = 0; | ||
121 | #endif | ||
122 | while (midi_outhead != midi_outtail) | ||
123 | { | ||
124 | #ifdef TEST_DEJITTER | ||
125 | if (!db) | ||
126 | { | ||
127 | post("out: del %f, midiRT %f logicaltime %f, RT %f dacminusRT %f", | ||
128 | (midi_outqueue[midi_outtail].q_time - midirealtime), | ||
129 | midirealtime, .001 * clock_gettimesince(sys_midiinittime), | ||
130 | sys_getrealtime(), sys_dactimeminusrealtime); | ||
131 | db = 1; | ||
132 | } | ||
133 | #endif | ||
134 | if (midi_outqueue[midi_outtail].q_time <= midirealtime) | ||
135 | sys_putnext(); | ||
136 | else break; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | static void sys_queuemidimess(int portno, int onebyte, int a, int b, int c) | ||
141 | { | ||
142 | t_midiqelem *midiqelem; | ||
143 | int newhead = midi_outhead +1; | ||
144 | if (newhead == MIDIQSIZE) | ||
145 | newhead = 0; | ||
146 | /* if FIFO is full flush an element to make room */ | ||
147 | if (newhead == midi_outtail) | ||
148 | sys_putnext(); | ||
149 | midi_outqueue[midi_outhead].q_portno = portno; | ||
150 | midi_outqueue[midi_outhead].q_onebyte = onebyte; | ||
151 | midi_outqueue[midi_outhead].q_byte1 = a; | ||
152 | midi_outqueue[midi_outhead].q_byte2 = b; | ||
153 | midi_outqueue[midi_outhead].q_byte3 = c; | ||
154 | midi_outqueue[midi_outhead].q_time = | ||
155 | .001 * clock_gettimesince(sys_midiinittime); | ||
156 | midi_outhead = newhead; | ||
157 | sys_pollmidioutqueue(); | ||
158 | } | ||
159 | |||
160 | #define MIDI_NOTEON 144 | ||
161 | #define MIDI_POLYAFTERTOUCH 160 | ||
162 | #define MIDI_CONTROLCHANGE 176 | ||
163 | #define MIDI_PROGRAMCHANGE 192 | ||
164 | #define MIDI_AFTERTOUCH 208 | ||
165 | #define MIDI_PITCHBEND 224 | ||
166 | |||
167 | void outmidi_noteon(int portno, int channel, int pitch, int velo) | ||
168 | { | ||
169 | if (pitch < 0) pitch = 0; | ||
170 | else if (pitch > 127) pitch = 127; | ||
171 | if (velo < 0) velo = 0; | ||
172 | else if (velo > 127) velo = 127; | ||
173 | sys_queuemidimess(portno, 0, MIDI_NOTEON + (channel & 0xf), pitch, velo); | ||
174 | } | ||
175 | |||
176 | void outmidi_controlchange(int portno, int channel, int ctl, int value) | ||
177 | { | ||
178 | if (ctl < 0) ctl = 0; | ||
179 | else if (ctl > 127) ctl = 127; | ||
180 | if (value < 0) value = 0; | ||
181 | else if (value > 127) value = 127; | ||
182 | sys_queuemidimess(portno, 0, MIDI_CONTROLCHANGE + (channel & 0xf), | ||
183 | ctl, value); | ||
184 | } | ||
185 | |||
186 | void outmidi_programchange(int portno, int channel, int value) | ||
187 | { | ||
188 | if (value < 0) value = 0; | ||
189 | else if (value > 127) value = 127; | ||
190 | sys_queuemidimess(portno, 0, | ||
191 | MIDI_PROGRAMCHANGE + (channel & 0xf), value, 0); | ||
192 | } | ||
193 | |||
194 | void outmidi_pitchbend(int portno, int channel, int value) | ||
195 | { | ||
196 | if (value < 0) value = 0; | ||
197 | else if (value > 16383) value = 16383; | ||
198 | sys_queuemidimess(portno, 0, MIDI_PITCHBEND + (channel & 0xf), | ||
199 | (value & 127), ((value>>7) & 127)); | ||
200 | } | ||
201 | |||
202 | void outmidi_aftertouch(int portno, int channel, int value) | ||
203 | { | ||
204 | if (value < 0) value = 0; | ||
205 | else if (value > 127) value = 127; | ||
206 | sys_queuemidimess(portno, 0, MIDI_AFTERTOUCH + (channel & 0xf), value, 0); | ||
207 | } | ||
208 | |||
209 | void outmidi_polyaftertouch(int portno, int channel, int pitch, int value) | ||
210 | { | ||
211 | if (pitch < 0) pitch = 0; | ||
212 | else if (pitch > 127) pitch = 127; | ||
213 | if (value < 0) value = 0; | ||
214 | else if (value > 127) value = 127; | ||
215 | sys_queuemidimess(portno, 0, MIDI_POLYAFTERTOUCH + (channel & 0xf), | ||
216 | pitch, value); | ||
217 | } | ||
218 | |||
219 | void outmidi_mclk(int portno) | ||
220 | { | ||
221 | sys_queuemidimess(portno, 1, 0xf8, 0,0); | ||
222 | } | ||
223 | |||
224 | /* ------------------------- MIDI input queue handling ------------------ */ | ||
225 | typedef struct midiparser | ||
226 | { | ||
227 | int mp_status; | ||
228 | int mp_gotbyte1; | ||
229 | int mp_byte1; | ||
230 | } t_midiparser; | ||
231 | |||
232 | #define MIDINOTEOFF 0x80 /* 2 following 'data bytes' */ | ||
233 | #define MIDINOTEON 0x90 /* 2 */ | ||
234 | #define MIDIPOLYTOUCH 0xa0 /* 2 */ | ||
235 | #define MIDICONTROLCHANGE 0xb0 /* 2 */ | ||
236 | #define MIDIPROGRAMCHANGE 0xc0 /* 1 */ | ||
237 | #define MIDICHANNELTOUCH 0xd0 /* 1 */ | ||
238 | #define MIDIPITCHBEND 0xe0 /* 2 */ | ||
239 | #define MIDISTARTSYSEX 0xf0 /* (until F7) */ | ||
240 | #define MIDITIMECODE 0xf1 /* 1 */ | ||
241 | #define MIDISONGPOS 0xf2 /* 2 */ | ||
242 | #define MIDISONGSELECT 0xf3 /* 1 */ | ||
243 | #define MIDIRESERVED1 0xf4 /* ? */ | ||
244 | #define MIDIRESERVED2 0xf5 /* ? */ | ||
245 | #define MIDITUNEREQUEST 0xf6 /* 0 */ | ||
246 | #define MIDIENDSYSEX 0xf7 /* 0 */ | ||
247 | #define MIDICLOCK 0xf8 /* 0 */ | ||
248 | #define MIDITICK 0xf9 /* 0 */ | ||
249 | #define MIDISTART 0xfa /* 0 */ | ||
250 | #define MIDICONT 0xfb /* 0 */ | ||
251 | #define MIDISTOP 0xfc /* 0 */ | ||
252 | #define MIDIACTIVESENSE 0xfe /* 0 */ | ||
253 | #define MIDIRESET 0xff /* 0 */ | ||
254 | |||
255 | /* functions in x_midi.c */ | ||
256 | void inmidi_realtimein(int portno, int cmd); | ||
257 | void inmidi_byte(int portno, int byte); | ||
258 | void inmidi_sysex(int portno, int byte); | ||
259 | void inmidi_noteon(int portno, int channel, int pitch, int velo); | ||
260 | void inmidi_controlchange(int portno, int channel, int ctlnumber, int value); | ||
261 | void inmidi_programchange(int portno, int channel, int value); | ||
262 | void inmidi_pitchbend(int portno, int channel, int value); | ||
263 | void inmidi_aftertouch(int portno, int channel, int value); | ||
264 | void inmidi_polyaftertouch(int portno, int channel, int pitch, int value); | ||
265 | |||
266 | static void sys_dispatchnextmidiin( void) | ||
267 | { | ||
268 | static t_midiparser parser[MAXMIDIINDEV], *parserp; | ||
269 | int portno = midi_inqueue[midi_intail].q_portno, | ||
270 | byte = midi_inqueue[midi_intail].q_byte1; | ||
271 | if (!midi_inqueue[midi_intail].q_onebyte) | ||
272 | bug("sys_dispatchnextmidiin"); | ||
273 | if (portno < 0 || portno >= MAXMIDIINDEV) | ||
274 | bug("sys_dispatchnextmidiin 2"); | ||
275 | parserp = parser + portno; | ||
276 | outlet_setstacklim(); | ||
277 | |||
278 | if (byte >= 0xf8) | ||
279 | inmidi_realtimein(portno, byte); | ||
280 | else | ||
281 | { | ||
282 | inmidi_byte(portno, byte); | ||
283 | if (byte & 0x80) | ||
284 | { | ||
285 | if (byte == MIDITUNEREQUEST || byte == MIDIRESERVED1 || | ||
286 | byte == MIDIRESERVED2) | ||
287 | parserp->mp_status = 0; | ||
288 | else if (byte == MIDISTARTSYSEX) | ||
289 | { | ||
290 | inmidi_sysex(portno, byte); | ||
291 | parserp->mp_status = byte; | ||
292 | } | ||
293 | else if (byte == MIDIENDSYSEX) | ||
294 | { | ||
295 | inmidi_sysex(portno, byte); | ||
296 | parserp->mp_status = 0; | ||
297 | } | ||
298 | else | ||
299 | { | ||
300 | parserp->mp_status = byte; | ||
301 | } | ||
302 | parserp->mp_gotbyte1 = 0; | ||
303 | } | ||
304 | else | ||
305 | { | ||
306 | int cmd = (parserp->mp_status >= 0xf0 ? parserp->mp_status : | ||
307 | (parserp->mp_status & 0xf0)); | ||
308 | int chan = (parserp->mp_status & 0xf); | ||
309 | int byte1 = parserp->mp_byte1, gotbyte1 = parserp->mp_gotbyte1; | ||
310 | switch (cmd) | ||
311 | { | ||
312 | case MIDINOTEOFF: | ||
313 | if (gotbyte1) | ||
314 | inmidi_noteon(portno, chan, byte1, 0), | ||
315 | parserp->mp_gotbyte1 = 0; | ||
316 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
317 | break; | ||
318 | case MIDINOTEON: | ||
319 | if (gotbyte1) | ||
320 | inmidi_noteon(portno, chan, byte1, byte), | ||
321 | parserp->mp_gotbyte1 = 0; | ||
322 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
323 | break; | ||
324 | case MIDIPOLYTOUCH: | ||
325 | if (gotbyte1) | ||
326 | inmidi_polyaftertouch(portno, chan, byte1, byte), | ||
327 | parserp->mp_gotbyte1 = 0; | ||
328 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
329 | break; | ||
330 | case MIDICONTROLCHANGE: | ||
331 | if (gotbyte1) | ||
332 | inmidi_controlchange(portno, chan, byte1, byte), | ||
333 | parserp->mp_gotbyte1 = 0; | ||
334 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
335 | break; | ||
336 | case MIDIPROGRAMCHANGE: | ||
337 | inmidi_programchange(portno, chan, byte); | ||
338 | break; | ||
339 | case MIDICHANNELTOUCH: | ||
340 | inmidi_aftertouch(portno, chan, byte); | ||
341 | break; | ||
342 | case MIDIPITCHBEND: | ||
343 | if (gotbyte1) | ||
344 | inmidi_pitchbend(portno, chan, ((byte << 7) + byte1)), | ||
345 | parserp->mp_gotbyte1 = 0; | ||
346 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
347 | break; | ||
348 | case MIDISTARTSYSEX: | ||
349 | inmidi_sysex(portno, byte); | ||
350 | break; | ||
351 | |||
352 | /* other kinds of messages are just dropped here. We'll | ||
353 | need another status byte before we start letting MIDI in | ||
354 | again (no running status across "system" messages). */ | ||
355 | case MIDITIMECODE: /* 1 data byte*/ | ||
356 | break; | ||
357 | case MIDISONGPOS: /* 2 */ | ||
358 | break; | ||
359 | case MIDISONGSELECT: /* 1 */ | ||
360 | break; | ||
361 | } | ||
362 | } | ||
363 | } | ||
364 | midi_intail = (midi_intail + 1 == MIDIQSIZE ? 0 : midi_intail + 1); | ||
365 | } | ||
366 | |||
367 | void sys_pollmidiinqueue( void) | ||
368 | { | ||
369 | #ifdef TEST_DEJITTER | ||
370 | static int db = 0; | ||
371 | #endif | ||
372 | double logicaltime = .001 * clock_gettimesince(sys_midiinittime); | ||
373 | #ifdef TEST_DEJITTER | ||
374 | if (midi_inhead == midi_intail) | ||
375 | db = 0; | ||
376 | #endif | ||
377 | while (midi_inhead != midi_intail) | ||
378 | { | ||
379 | #ifdef TEST_DEJITTER | ||
380 | if (!db) | ||
381 | { | ||
382 | post("in del %f, logicaltime %f, RT %f adcminusRT %f", | ||
383 | (midi_inqueue[midi_intail].q_time - logicaltime), | ||
384 | logicaltime, sys_getrealtime(), sys_adctimeminusrealtime); | ||
385 | db = 1; | ||
386 | } | ||
387 | #endif | ||
388 | #if 0 | ||
389 | if (midi_inqueue[midi_intail].q_time <= logicaltime - 0.007) | ||
390 | post("late %f", | ||
391 | 1000 * (logicaltime - midi_inqueue[midi_intail].q_time)); | ||
392 | #endif | ||
393 | if (midi_inqueue[midi_intail].q_time <= logicaltime) | ||
394 | { | ||
395 | #if 0 | ||
396 | post("diff %f", | ||
397 | 1000* (logicaltime - midi_inqueue[midi_intail].q_time)); | ||
398 | #endif | ||
399 | sys_dispatchnextmidiin(); | ||
400 | } | ||
401 | else break; | ||
402 | } | ||
403 | } | ||
404 | |||
405 | /* this should be called from the system dependent MIDI code when a byte | ||
406 | comes in, as a result of our calling sys_poll_midi. We stick it on a | ||
407 | timetag queue and dispatch it at the appropriate logical time. */ | ||
408 | |||
409 | |||
410 | void sys_midibytein(int portno, int byte) | ||
411 | { | ||
412 | static int warned = 0; | ||
413 | t_midiqelem *midiqelem; | ||
414 | int newhead = midi_inhead +1; | ||
415 | if (newhead == MIDIQSIZE) | ||
416 | newhead = 0; | ||
417 | /* if FIFO is full flush an element to make room */ | ||
418 | if (newhead == midi_intail) | ||
419 | { | ||
420 | if (!warned) | ||
421 | { | ||
422 | post("warning: MIDI timing FIFO overflowed"); | ||
423 | warned = 1; | ||
424 | } | ||
425 | sys_dispatchnextmidiin(); | ||
426 | } | ||
427 | midi_inqueue[midi_inhead].q_portno = portno; | ||
428 | midi_inqueue[midi_inhead].q_onebyte = 1; | ||
429 | midi_inqueue[midi_inhead].q_byte1 = byte; | ||
430 | midi_inqueue[midi_inhead].q_time = sys_getmidiinrealtime(); | ||
431 | midi_inhead = newhead; | ||
432 | sys_pollmidiinqueue(); | ||
433 | } | ||
434 | |||
435 | void sys_pollmidiqueue( void) | ||
436 | { | ||
437 | #if 0 | ||
438 | static double lasttime; | ||
439 | double newtime = sys_getrealtime(); | ||
440 | if (newtime - lasttime > 0.007) | ||
441 | post("delay %d", (int)(1000 * (newtime - lasttime))); | ||
442 | lasttime = newtime; | ||
443 | #endif | ||
444 | sys_poll_midi(); /* OS dependent poll for MIDI input */ | ||
445 | sys_pollmidioutqueue(); | ||
446 | sys_pollmidiinqueue(); | ||
447 | } | ||
448 | |||
449 | /******************** dialog window and device listing ********************/ | ||
450 | |||
451 | #ifdef USEAPI_OSS | ||
452 | void midi_oss_init( void); | ||
453 | #endif | ||
454 | |||
455 | /* last requested parameters */ | ||
456 | static int midi_nmidiindev; | ||
457 | static int midi_midiindev[MAXMIDIINDEV]; | ||
458 | static int midi_nmidioutdev; | ||
459 | static int midi_midioutdev[MAXMIDIOUTDEV]; | ||
460 | |||
461 | static void sys_get_midi_params(int *pnmidiindev, int *pmidiindev, | ||
462 | int *pnmidioutdev, int *pmidioutdev) | ||
463 | { | ||
464 | int i; | ||
465 | *pnmidiindev = midi_nmidiindev; | ||
466 | for (i = 0; i < MAXMIDIINDEV; i++) | ||
467 | pmidiindev[i] = midi_midiindev[i]; | ||
468 | *pnmidioutdev = midi_nmidioutdev; | ||
469 | for (i = 0; i < MAXMIDIOUTDEV; i++) | ||
470 | pmidioutdev[i] = midi_midioutdev[i]; | ||
471 | } | ||
472 | |||
473 | static void sys_save_midi_params( | ||
474 | int nmidiindev, int *midiindev, | ||
475 | int nmidioutdev, int *midioutdev) | ||
476 | { | ||
477 | int i; | ||
478 | midi_nmidiindev = nmidiindev; | ||
479 | for (i = 0; i < MAXMIDIINDEV; i++) | ||
480 | midi_midiindev[i] = midiindev[i]; | ||
481 | midi_nmidioutdev = nmidioutdev; | ||
482 | for (i = 0; i < MAXMIDIOUTDEV; i++) | ||
483 | midi_midioutdev[i] = midioutdev[i]; | ||
484 | } | ||
485 | |||
486 | void sys_open_midi(int nmidiindev, int *midiindev, | ||
487 | int nmidioutdev, int *midioutdev) | ||
488 | { | ||
489 | #ifdef USEAPI_OSS | ||
490 | midi_oss_init(); | ||
491 | #endif | ||
492 | sys_do_open_midi(nmidiindev, midiindev, nmidioutdev, midioutdev); | ||
493 | sys_save_midi_params(nmidiindev, midiindev, | ||
494 | nmidioutdev, midioutdev); | ||
495 | } | ||
496 | |||
497 | /* open midi using whatever parameters were last used */ | ||
498 | void sys_reopen_midi( void) | ||
499 | { | ||
500 | int nmidiindev, midiindev[MAXMIDIINDEV]; | ||
501 | int nmidioutdev, midioutdev[MAXMIDIOUTDEV]; | ||
502 | sys_get_midi_params(&nmidiindev, midiindev, &nmidioutdev, midioutdev); | ||
503 | sys_open_midi(nmidiindev, midiindev, nmidioutdev, midioutdev); | ||
504 | } | ||
505 | |||
506 | #define MAXNDEV 20 | ||
507 | #define DEVDESCSIZE 80 | ||
508 | |||
509 | #ifdef MSW | ||
510 | #define DEVONSET 0 /* microsoft device list starts at 0 (the "mapper"). */ | ||
511 | #else /* (see also MSW ifdef in sys_parsedevlist(), s_main.c) */ | ||
512 | #define DEVONSET 1 /* To agree with command line flags, normally start at 1 */ | ||
513 | #endif | ||
514 | |||
515 | void sys_listmididevs(void ) | ||
516 | { | ||
517 | char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE]; | ||
518 | int nindevs = 0, noutdevs = 0, i; | ||
519 | |||
520 | midi_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, | ||
521 | MAXNDEV, DEVDESCSIZE); | ||
522 | |||
523 | if (!nindevs) | ||
524 | post("no midi input devices found"); | ||
525 | else | ||
526 | { | ||
527 | post("input devices:"); | ||
528 | for (i = 0; i < nindevs; i++) | ||
529 | post("%d. %s", i+1, indevlist + i * DEVDESCSIZE); | ||
530 | } | ||
531 | if (!noutdevs) | ||
532 | post("no midi output devices found"); | ||
533 | else | ||
534 | { | ||
535 | post("output devices:"); | ||
536 | for (i = 0; i < noutdevs; i++) | ||
537 | post("%d. %s", i+DEVONSET, outdevlist + i * DEVDESCSIZE); | ||
538 | } | ||
539 | } | ||
540 | |||
541 | extern t_class *glob_pdobject; | ||
542 | |||
543 | /* start an midi settings dialog window */ | ||
544 | void glob_midi_properties(t_pd *dummy, t_floatarg flongform) | ||
545 | { | ||
546 | char buf[1024 + 2 * MAXNDEV*(DEVDESCSIZE+4)]; | ||
547 | /* these are the devices you're using: */ | ||
548 | int nindev, midiindev[MAXMIDIINDEV]; | ||
549 | int noutdev, midioutdev[MAXMIDIOUTDEV]; | ||
550 | int midiindev1, midiindev2, midiindev3, midiindev4, | ||
551 | midioutdev1, midioutdev2, midioutdev3, midioutdev4; | ||
552 | |||
553 | /* these are all the devices on your system: */ | ||
554 | char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE]; | ||
555 | int nindevs = 0, noutdevs = 0, i; | ||
556 | |||
557 | char indevliststring[MAXNDEV*(DEVDESCSIZE+4)+80], | ||
558 | outdevliststring[MAXNDEV*(DEVDESCSIZE+4)+80]; | ||
559 | |||
560 | midi_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, | ||
561 | MAXNDEV, DEVDESCSIZE); | ||
562 | |||
563 | strcpy(indevliststring, "{ {none} "); | ||
564 | for (i = 0; i < nindevs; i++) | ||
565 | { | ||
566 | strcat(indevliststring, "\""); | ||
567 | strcat(indevliststring, indevlist + i * DEVDESCSIZE); | ||
568 | strcat(indevliststring, "\" "); | ||
569 | } | ||
570 | strcat(indevliststring, "}"); | ||
571 | |||
572 | strcpy(outdevliststring, "{ {none} "); | ||
573 | for (i = 0; i < noutdevs; i++) | ||
574 | { | ||
575 | strcat(outdevliststring, "\""); | ||
576 | strcat(outdevliststring, outdevlist + i * DEVDESCSIZE); | ||
577 | strcat(outdevliststring, "\" "); | ||
578 | } | ||
579 | strcat(outdevliststring, "}"); | ||
580 | |||
581 | sys_get_midi_params(&nindev, midiindev, &noutdev, midioutdev); | ||
582 | |||
583 | if (nindev > 1 || noutdev > 1) | ||
584 | flongform = 1; | ||
585 | |||
586 | midiindev1 = (nindev > 0 && midiindev[0]>= 0 ? midiindev[0]+1 : 0); | ||
587 | midiindev2 = (nindev > 1 && midiindev[1]>= 0 ? midiindev[1]+1 : 0); | ||
588 | midiindev3 = (nindev > 2 && midiindev[2]>= 0 ? midiindev[2]+1 : 0); | ||
589 | midiindev4 = (nindev > 3 && midiindev[3]>= 0 ? midiindev[3]+1 : 0); | ||
590 | midioutdev1 = (noutdev > 0 && midioutdev[0]>=0 ? midioutdev[0]+1 : 0); | ||
591 | midioutdev2 = (noutdev > 1 && midioutdev[1]>=0 ? midioutdev[1]+1 : 0); | ||
592 | midioutdev3 = (noutdev > 2 && midioutdev[2]>=0 ? midioutdev[2]+1 : 0); | ||
593 | midioutdev4 = (noutdev > 3 && midioutdev[3]>=0 ? midioutdev[3]+1 : 0); | ||
594 | |||
595 | sprintf(buf, | ||
596 | "pdtk_midi_dialog %%s \ | ||
597 | %s %d %d %d %d %s %d %d %d %d \ | ||
598 | %d\n", | ||
599 | indevliststring, | ||
600 | midiindev1, midiindev2, midiindev3, midiindev4, | ||
601 | outdevliststring, | ||
602 | midioutdev1, midioutdev2, midioutdev3, midioutdev4, | ||
603 | (flongform != 0)); | ||
604 | gfxstub_deleteforkey(0); | ||
605 | gfxstub_new(&glob_pdobject, glob_midi_properties, buf); | ||
606 | } | ||
607 | |||
608 | /* new values from dialog window */ | ||
609 | void glob_midi_dialog(t_pd *dummy, t_symbol *s, int argc, t_atom *argv) | ||
610 | { | ||
611 | int nmidiindev, midiindev[MAXMIDIINDEV]; | ||
612 | int nmidioutdev, midioutdev[MAXMIDIOUTDEV]; | ||
613 | int i, nindev, noutdev; | ||
614 | int newmidiindev[4], newmidioutdev[4]; | ||
615 | |||
616 | for (i = 0; i < 4; i++) | ||
617 | { | ||
618 | newmidiindev[i] = atom_getintarg(i, argc, argv); | ||
619 | newmidioutdev[i] = atom_getintarg(i+4, argc, argv); | ||
620 | } | ||
621 | |||
622 | for (i = 0, nindev = 0; i < 4; i++) | ||
623 | { | ||
624 | if (newmidiindev[i] > 0) | ||
625 | { | ||
626 | newmidiindev[nindev] = newmidiindev[i]-1; | ||
627 | nindev++; | ||
628 | } | ||
629 | } | ||
630 | for (i = 0, noutdev = 0; i < 4; i++) | ||
631 | { | ||
632 | if (newmidioutdev[i] > 0) | ||
633 | { | ||
634 | newmidioutdev[noutdev] = newmidioutdev[i]-1; | ||
635 | noutdev++; | ||
636 | } | ||
637 | } | ||
638 | |||
639 | sys_close_midi(); | ||
640 | sys_open_midi(nindev, newmidiindev, noutdev, newmidioutdev); | ||
641 | } | ||
642 | /* Copyright (c) 1997-1999 Miller Puckette and others. | ||
643 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
644 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ | ||
645 | |||
646 | /* Clock functions (which should move, but where?) and MIDI queueing */ | ||
647 | |||
648 | #include "m_pd.h" | ||
649 | #include "s_stuff.h" | ||
650 | #include "m_imp.h" | ||
651 | #ifdef UNIX | ||
652 | #include <unistd.h> | ||
653 | #include <sys/time.h> | ||
654 | #ifdef HAVE_BSTRING_H | ||
655 | #include <bstring.h> | ||
656 | #endif | ||
657 | #endif | ||
658 | #ifdef MSW | ||
659 | #include <winsock.h> | ||
660 | #include <sys/types.h> | ||
661 | #include <sys/timeb.h> | ||
662 | #include <wtypes.h> | ||
663 | #endif | ||
664 | #include <string.h> | ||
665 | #include <stdio.h> | ||
666 | #include <signal.h> | ||
667 | |||
668 | typedef struct _midiqelem | ||
669 | { | ||
670 | double q_time; | ||
671 | int q_portno; | ||
672 | unsigned char q_onebyte; | ||
673 | unsigned char q_byte1; | ||
674 | unsigned char q_byte2; | ||
675 | unsigned char q_byte3; | ||
676 | } t_midiqelem; | ||
677 | |||
678 | #define MIDIQSIZE 1024 | ||
679 | |||
680 | t_midiqelem midi_outqueue[MIDIQSIZE]; | ||
681 | int midi_outhead, midi_outtail; | ||
682 | t_midiqelem midi_inqueue[MIDIQSIZE]; | ||
683 | int midi_inhead, midi_intail; | ||
684 | static double sys_midiinittime; | ||
685 | |||
686 | /* this is our current estimate for at what "system" real time the | ||
687 | current logical time's output should occur. */ | ||
688 | static double sys_dactimeminusrealtime; | ||
689 | /* same for input, should be schduler advance earlier. */ | ||
690 | static double sys_adctimeminusrealtime; | ||
691 | |||
692 | static double sys_newdactimeminusrealtime = -1e20; | ||
693 | static double sys_newadctimeminusrealtime = -1e20; | ||
694 | static double sys_whenupdate; | ||
695 | |||
696 | void sys_initmidiqueue( void) | ||
697 | { | ||
698 | sys_midiinittime = clock_getlogicaltime(); | ||
699 | sys_dactimeminusrealtime = sys_adctimeminusrealtime = 0; | ||
700 | } | ||
701 | |||
702 | /* this is called from the OS dependent code from time to time when we | ||
703 | think we know the delay (outbuftime) in seconds, at which the last-output | ||
704 | audio sample will go out the door. */ | ||
705 | void sys_setmiditimediff(double inbuftime, double outbuftime) | ||
706 | { | ||
707 | double dactimeminusrealtime = | ||
708 | .001 * clock_gettimesince(sys_midiinittime) | ||
709 | - outbuftime - sys_getrealtime(); | ||
710 | double adctimeminusrealtime = | ||
711 | .001 * clock_gettimesince(sys_midiinittime) | ||
712 | + inbuftime - sys_getrealtime(); | ||
713 | if (dactimeminusrealtime > sys_newdactimeminusrealtime) | ||
714 | sys_newdactimeminusrealtime = dactimeminusrealtime; | ||
715 | if (adctimeminusrealtime > sys_newadctimeminusrealtime) | ||
716 | sys_newadctimeminusrealtime = adctimeminusrealtime; | ||
717 | if (sys_getrealtime() > sys_whenupdate) | ||
718 | { | ||
719 | sys_dactimeminusrealtime = sys_newdactimeminusrealtime; | ||
720 | sys_adctimeminusrealtime = sys_newadctimeminusrealtime; | ||
721 | sys_newdactimeminusrealtime = -1e20; | ||
722 | sys_newadctimeminusrealtime = -1e20; | ||
723 | sys_whenupdate = sys_getrealtime() + 1; | ||
724 | } | ||
725 | } | ||
726 | |||
727 | /* return the logical time of the DAC sample we believe is currently | ||
728 | going out, based on how much "system time" has elapsed since the | ||
729 | last time sys_setmiditimediff got called. */ | ||
730 | static double sys_getmidioutrealtime( void) | ||
731 | { | ||
732 | return (sys_getrealtime() + sys_dactimeminusrealtime); | ||
733 | } | ||
734 | |||
735 | static double sys_getmidiinrealtime( void) | ||
736 | { | ||
737 | return (sys_getrealtime() + sys_adctimeminusrealtime); | ||
738 | } | ||
739 | |||
740 | static void sys_putnext( void) | ||
741 | { | ||
742 | int portno = midi_outqueue[midi_outtail].q_portno; | ||
743 | if (midi_outqueue[midi_outtail].q_onebyte) | ||
744 | sys_putmidibyte(portno, midi_outqueue[midi_outtail].q_byte1); | ||
745 | else sys_putmidimess(portno, midi_outqueue[midi_outtail].q_byte1, | ||
746 | midi_outqueue[midi_outtail].q_byte2, | ||
747 | midi_outqueue[midi_outtail].q_byte3); | ||
748 | midi_outtail = (midi_outtail + 1 == MIDIQSIZE ? 0 : midi_outtail + 1); | ||
749 | } | ||
750 | |||
751 | /* #define TEST_DEJITTER */ | ||
752 | |||
753 | void sys_pollmidioutqueue( void) | ||
754 | { | ||
755 | #ifdef TEST_DEJITTER | ||
756 | static int db = 0; | ||
757 | #endif | ||
758 | double midirealtime = sys_getmidioutrealtime(); | ||
759 | #ifdef TEST_DEJITTER | ||
760 | if (midi_outhead == midi_outtail) | ||
761 | db = 0; | ||
762 | #endif | ||
763 | while (midi_outhead != midi_outtail) | ||
764 | { | ||
765 | #ifdef TEST_DEJITTER | ||
766 | if (!db) | ||
767 | { | ||
768 | post("out: del %f, midiRT %f logicaltime %f, RT %f dacminusRT %f", | ||
769 | (midi_outqueue[midi_outtail].q_time - midirealtime), | ||
770 | midirealtime, .001 * clock_gettimesince(sys_midiinittime), | ||
771 | sys_getrealtime(), sys_dactimeminusrealtime); | ||
772 | db = 1; | ||
773 | } | ||
774 | #endif | ||
775 | if (midi_outqueue[midi_outtail].q_time <= midirealtime) | ||
776 | sys_putnext(); | ||
777 | else break; | ||
778 | } | ||
779 | } | ||
780 | |||
781 | static void sys_queuemidimess(int portno, int onebyte, int a, int b, int c) | ||
782 | { | ||
783 | t_midiqelem *midiqelem; | ||
784 | int newhead = midi_outhead +1; | ||
785 | if (newhead == MIDIQSIZE) | ||
786 | newhead = 0; | ||
787 | /* if FIFO is full flush an element to make room */ | ||
788 | if (newhead == midi_outtail) | ||
789 | sys_putnext(); | ||
790 | midi_outqueue[midi_outhead].q_portno = portno; | ||
791 | midi_outqueue[midi_outhead].q_onebyte = onebyte; | ||
792 | midi_outqueue[midi_outhead].q_byte1 = a; | ||
793 | midi_outqueue[midi_outhead].q_byte2 = b; | ||
794 | midi_outqueue[midi_outhead].q_byte3 = c; | ||
795 | midi_outqueue[midi_outhead].q_time = | ||
796 | .001 * clock_gettimesince(sys_midiinittime); | ||
797 | midi_outhead = newhead; | ||
798 | sys_pollmidioutqueue(); | ||
799 | } | ||
800 | |||
801 | #define MIDI_NOTEON 144 | ||
802 | #define MIDI_POLYAFTERTOUCH 160 | ||
803 | #define MIDI_CONTROLCHANGE 176 | ||
804 | #define MIDI_PROGRAMCHANGE 192 | ||
805 | #define MIDI_AFTERTOUCH 208 | ||
806 | #define MIDI_PITCHBEND 224 | ||
807 | |||
808 | void outmidi_noteon(int portno, int channel, int pitch, int velo) | ||
809 | { | ||
810 | if (pitch < 0) pitch = 0; | ||
811 | else if (pitch > 127) pitch = 127; | ||
812 | if (velo < 0) velo = 0; | ||
813 | else if (velo > 127) velo = 127; | ||
814 | sys_queuemidimess(portno, 0, MIDI_NOTEON + (channel & 0xf), pitch, velo); | ||
815 | } | ||
816 | |||
817 | void outmidi_controlchange(int portno, int channel, int ctl, int value) | ||
818 | { | ||
819 | if (ctl < 0) ctl = 0; | ||
820 | else if (ctl > 127) ctl = 127; | ||
821 | if (value < 0) value = 0; | ||
822 | else if (value > 127) value = 127; | ||
823 | sys_queuemidimess(portno, 0, MIDI_CONTROLCHANGE + (channel & 0xf), | ||
824 | ctl, value); | ||
825 | } | ||
826 | |||
827 | void outmidi_programchange(int portno, int channel, int value) | ||
828 | { | ||
829 | if (value < 0) value = 0; | ||
830 | else if (value > 127) value = 127; | ||
831 | sys_queuemidimess(portno, 0, | ||
832 | MIDI_PROGRAMCHANGE + (channel & 0xf), value, 0); | ||
833 | } | ||
834 | |||
835 | void outmidi_pitchbend(int portno, int channel, int value) | ||
836 | { | ||
837 | if (value < 0) value = 0; | ||
838 | else if (value > 16383) value = 16383; | ||
839 | sys_queuemidimess(portno, 0, MIDI_PITCHBEND + (channel & 0xf), | ||
840 | (value & 127), ((value>>7) & 127)); | ||
841 | } | ||
842 | |||
843 | void outmidi_aftertouch(int portno, int channel, int value) | ||
844 | { | ||
845 | if (value < 0) value = 0; | ||
846 | else if (value > 127) value = 127; | ||
847 | sys_queuemidimess(portno, 0, MIDI_AFTERTOUCH + (channel & 0xf), value, 0); | ||
848 | } | ||
849 | |||
850 | void outmidi_polyaftertouch(int portno, int channel, int pitch, int value) | ||
851 | { | ||
852 | if (pitch < 0) pitch = 0; | ||
853 | else if (pitch > 127) pitch = 127; | ||
854 | if (value < 0) value = 0; | ||
855 | else if (value > 127) value = 127; | ||
856 | sys_queuemidimess(portno, 0, MIDI_POLYAFTERTOUCH + (channel & 0xf), | ||
857 | pitch, value); | ||
858 | } | ||
859 | |||
860 | void outmidi_mclk(int portno) | ||
861 | { | ||
862 | sys_queuemidimess(portno, 1, 0xf8, 0,0); | ||
863 | } | ||
864 | |||
865 | /* ------------------------- MIDI input queue handling ------------------ */ | ||
866 | typedef struct midiparser | ||
867 | { | ||
868 | int mp_status; | ||
869 | int mp_gotbyte1; | ||
870 | int mp_byte1; | ||
871 | } t_midiparser; | ||
872 | |||
873 | #define MIDINOTEOFF 0x80 /* 2 following 'data bytes' */ | ||
874 | #define MIDINOTEON 0x90 /* 2 */ | ||
875 | #define MIDIPOLYTOUCH 0xa0 /* 2 */ | ||
876 | #define MIDICONTROLCHANGE 0xb0 /* 2 */ | ||
877 | #define MIDIPROGRAMCHANGE 0xc0 /* 1 */ | ||
878 | #define MIDICHANNELTOUCH 0xd0 /* 1 */ | ||
879 | #define MIDIPITCHBEND 0xe0 /* 2 */ | ||
880 | #define MIDISTARTSYSEX 0xf0 /* (until F7) */ | ||
881 | #define MIDITIMECODE 0xf1 /* 1 */ | ||
882 | #define MIDISONGPOS 0xf2 /* 2 */ | ||
883 | #define MIDISONGSELECT 0xf3 /* 1 */ | ||
884 | #define MIDIRESERVED1 0xf4 /* ? */ | ||
885 | #define MIDIRESERVED2 0xf5 /* ? */ | ||
886 | #define MIDITUNEREQUEST 0xf6 /* 0 */ | ||
887 | #define MIDIENDSYSEX 0xf7 /* 0 */ | ||
888 | #define MIDICLOCK 0xf8 /* 0 */ | ||
889 | #define MIDITICK 0xf9 /* 0 */ | ||
890 | #define MIDISTART 0xfa /* 0 */ | ||
891 | #define MIDICONT 0xfb /* 0 */ | ||
892 | #define MIDISTOP 0xfc /* 0 */ | ||
893 | #define MIDIACTIVESENSE 0xfe /* 0 */ | ||
894 | #define MIDIRESET 0xff /* 0 */ | ||
895 | |||
896 | /* functions in x_midi.c */ | ||
897 | void inmidi_realtimein(int portno, int cmd); | ||
898 | void inmidi_byte(int portno, int byte); | ||
899 | void inmidi_sysex(int portno, int byte); | ||
900 | void inmidi_noteon(int portno, int channel, int pitch, int velo); | ||
901 | void inmidi_controlchange(int portno, int channel, int ctlnumber, int value); | ||
902 | void inmidi_programchange(int portno, int channel, int value); | ||
903 | void inmidi_pitchbend(int portno, int channel, int value); | ||
904 | void inmidi_aftertouch(int portno, int channel, int value); | ||
905 | void inmidi_polyaftertouch(int portno, int channel, int pitch, int value); | ||
906 | |||
907 | static void sys_dispatchnextmidiin( void) | ||
908 | { | ||
909 | static t_midiparser parser[MAXMIDIINDEV], *parserp; | ||
910 | int portno = midi_inqueue[midi_intail].q_portno, | ||
911 | byte = midi_inqueue[midi_intail].q_byte1; | ||
912 | if (!midi_inqueue[midi_intail].q_onebyte) | ||
913 | bug("sys_dispatchnextmidiin"); | ||
914 | if (portno < 0 || portno >= MAXMIDIINDEV) | ||
915 | bug("sys_dispatchnextmidiin 2"); | ||
916 | parserp = parser + portno; | ||
917 | outlet_setstacklim(); | ||
918 | |||
919 | if (byte >= 0xf8) | ||
920 | inmidi_realtimein(portno, byte); | ||
921 | else | ||
922 | { | ||
923 | inmidi_byte(portno, byte); | ||
924 | if (byte & 0x80) | ||
925 | { | ||
926 | if (byte == MIDITUNEREQUEST || byte == MIDIRESERVED1 || | ||
927 | byte == MIDIRESERVED2) | ||
928 | parserp->mp_status = 0; | ||
929 | else if (byte == MIDISTARTSYSEX) | ||
930 | { | ||
931 | inmidi_sysex(portno, byte); | ||
932 | parserp->mp_status = byte; | ||
933 | } | ||
934 | else if (byte == MIDIENDSYSEX) | ||
935 | { | ||
936 | inmidi_sysex(portno, byte); | ||
937 | parserp->mp_status = 0; | ||
938 | } | ||
939 | else | ||
940 | { | ||
941 | parserp->mp_status = byte; | ||
942 | } | ||
943 | parserp->mp_gotbyte1 = 0; | ||
944 | } | ||
945 | else | ||
946 | { | ||
947 | int cmd = (parserp->mp_status >= 0xf0 ? parserp->mp_status : | ||
948 | (parserp->mp_status & 0xf0)); | ||
949 | int chan = (parserp->mp_status & 0xf); | ||
950 | int byte1 = parserp->mp_byte1, gotbyte1 = parserp->mp_gotbyte1; | ||
951 | switch (cmd) | ||
952 | { | ||
953 | case MIDINOTEOFF: | ||
954 | if (gotbyte1) | ||
955 | inmidi_noteon(portno, chan, byte1, 0), | ||
956 | parserp->mp_gotbyte1 = 0; | ||
957 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
958 | break; | ||
959 | case MIDINOTEON: | ||
960 | if (gotbyte1) | ||
961 | inmidi_noteon(portno, chan, byte1, byte), | ||
962 | parserp->mp_gotbyte1 = 0; | ||
963 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
964 | break; | ||
965 | case MIDIPOLYTOUCH: | ||
966 | if (gotbyte1) | ||
967 | inmidi_polyaftertouch(portno, chan, byte1, byte), | ||
968 | parserp->mp_gotbyte1 = 0; | ||
969 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
970 | break; | ||
971 | case MIDICONTROLCHANGE: | ||
972 | if (gotbyte1) | ||
973 | inmidi_controlchange(portno, chan, byte1, byte), | ||
974 | parserp->mp_gotbyte1 = 0; | ||
975 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
976 | break; | ||
977 | case MIDIPROGRAMCHANGE: | ||
978 | inmidi_programchange(portno, chan, byte); | ||
979 | break; | ||
980 | case MIDICHANNELTOUCH: | ||
981 | inmidi_aftertouch(portno, chan, byte); | ||
982 | break; | ||
983 | case MIDIPITCHBEND: | ||
984 | if (gotbyte1) | ||
985 | inmidi_pitchbend(portno, chan, ((byte << 7) + byte1)), | ||
986 | parserp->mp_gotbyte1 = 0; | ||
987 | else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1; | ||
988 | break; | ||
989 | case MIDISTARTSYSEX: | ||
990 | inmidi_sysex(portno, byte); | ||
991 | break; | ||
992 | |||
993 | /* other kinds of messages are just dropped here. We'll | ||
994 | need another status byte before we start letting MIDI in | ||
995 | again (no running status across "system" messages). */ | ||
996 | case MIDITIMECODE: /* 1 data byte*/ | ||
997 | break; | ||
998 | case MIDISONGPOS: /* 2 */ | ||
999 | break; | ||
1000 | case MIDISONGSELECT: /* 1 */ | ||
1001 | break; | ||
1002 | } | ||
1003 | } | ||
1004 | } | ||
1005 | midi_intail = (midi_intail + 1 == MIDIQSIZE ? 0 : midi_intail + 1); | ||
1006 | } | ||
1007 | |||
1008 | void sys_pollmidiinqueue( void) | ||
1009 | { | ||
1010 | #ifdef TEST_DEJITTER | ||
1011 | static int db = 0; | ||
1012 | #endif | ||
1013 | double logicaltime = .001 * clock_gettimesince(sys_midiinittime); | ||
1014 | #ifdef TEST_DEJITTER | ||
1015 | if (midi_inhead == midi_intail) | ||
1016 | db = 0; | ||
1017 | #endif | ||
1018 | while (midi_inhead != midi_intail) | ||
1019 | { | ||
1020 | #ifdef TEST_DEJITTER | ||
1021 | if (!db) | ||
1022 | { | ||
1023 | post("in del %f, logicaltime %f, RT %f adcminusRT %f", | ||
1024 | (midi_inqueue[midi_intail].q_time - logicaltime), | ||
1025 | logicaltime, sys_getrealtime(), sys_adctimeminusrealtime); | ||
1026 | db = 1; | ||
1027 | } | ||
1028 | #endif | ||
1029 | #if 0 | ||
1030 | if (midi_inqueue[midi_intail].q_time <= logicaltime - 0.007) | ||
1031 | post("late %f", | ||
1032 | 1000 * (logicaltime - midi_inqueue[midi_intail].q_time)); | ||
1033 | #endif | ||
1034 | if (midi_inqueue[midi_intail].q_time <= logicaltime) | ||
1035 | { | ||
1036 | #if 0 | ||
1037 | post("diff %f", | ||
1038 | 1000* (logicaltime - midi_inqueue[midi_intail].q_time)); | ||
1039 | #endif | ||
1040 | sys_dispatchnextmidiin(); | ||
1041 | } | ||
1042 | else break; | ||
1043 | } | ||
1044 | } | ||
1045 | |||
1046 | /* this should be called from the system dependent MIDI code when a byte | ||
1047 | comes in, as a result of our calling sys_poll_midi. We stick it on a | ||
1048 | timetag queue and dispatch it at the appropriate logical time. */ | ||
1049 | |||
1050 | |||
1051 | void sys_midibytein(int portno, int byte) | ||
1052 | { | ||
1053 | static int warned = 0; | ||
1054 | t_midiqelem *midiqelem; | ||
1055 | int newhead = midi_inhead +1; | ||
1056 | if (newhead == MIDIQSIZE) | ||
1057 | newhead = 0; | ||
1058 | /* if FIFO is full flush an element to make room */ | ||
1059 | if (newhead == midi_intail) | ||
1060 | { | ||
1061 | if (!warned) | ||
1062 | { | ||
1063 | post("warning: MIDI timing FIFO overflowed"); | ||
1064 | warned = 1; | ||
1065 | } | ||
1066 | sys_dispatchnextmidiin(); | ||
1067 | } | ||
1068 | midi_inqueue[midi_inhead].q_portno = portno; | ||
1069 | midi_inqueue[midi_inhead].q_onebyte = 1; | ||
1070 | midi_inqueue[midi_inhead].q_byte1 = byte; | ||
1071 | midi_inqueue[midi_inhead].q_time = sys_getmidiinrealtime(); | ||
1072 | midi_inhead = newhead; | ||
1073 | sys_pollmidiinqueue(); | ||
1074 | } | ||
1075 | |||
1076 | void sys_pollmidiqueue( void) | ||
1077 | { | ||
1078 | #if 0 | ||
1079 | static double lasttime; | ||
1080 | double newtime = sys_getrealtime(); | ||
1081 | if (newtime - lasttime > 0.007) | ||
1082 | post("delay %d", (int)(1000 * (newtime - lasttime))); | ||
1083 | lasttime = newtime; | ||
1084 | #endif | ||
1085 | sys_poll_midi(); /* OS dependent poll for MIDI input */ | ||
1086 | sys_pollmidioutqueue(); | ||
1087 | sys_pollmidiinqueue(); | ||
1088 | } | ||
1089 | |||
1090 | /******************** dialog window and device listing ********************/ | ||
1091 | |||
1092 | #ifdef USEAPI_OSS | ||
1093 | void midi_oss_init( void); | ||
1094 | #endif | ||
1095 | |||
1096 | /* last requested parameters */ | ||
1097 | static int midi_nmidiindev; | ||
1098 | static int midi_midiindev[MAXMIDIINDEV]; | ||
1099 | static int midi_nmidioutdev; | ||
1100 | static int midi_midioutdev[MAXMIDIOUTDEV]; | ||
1101 | |||
1102 | static void sys_get_midi_params(int *pnmidiindev, int *pmidiindev, | ||
1103 | int *pnmidioutdev, int *pmidioutdev) | ||
1104 | { | ||
1105 | int i; | ||
1106 | *pnmidiindev = midi_nmidiindev; | ||
1107 | for (i = 0; i < MAXMIDIINDEV; i++) | ||
1108 | pmidiindev[i] = midi_midiindev[i]; | ||
1109 | *pnmidioutdev = midi_nmidioutdev; | ||
1110 | for (i = 0; i < MAXMIDIOUTDEV; i++) | ||
1111 | pmidioutdev[i] = midi_midioutdev[i]; | ||
1112 | } | ||
1113 | |||
1114 | static void sys_save_midi_params( | ||
1115 | int nmidiindev, int *midiindev, | ||
1116 | int nmidioutdev, int *midioutdev) | ||
1117 | { | ||
1118 | int i; | ||
1119 | midi_nmidiindev = nmidiindev; | ||
1120 | for (i = 0; i < MAXMIDIINDEV; i++) | ||
1121 | midi_midiindev[i] = midiindev[i]; | ||
1122 | midi_nmidioutdev = nmidioutdev; | ||
1123 | for (i = 0; i < MAXMIDIOUTDEV; i++) | ||
1124 | midi_midioutdev[i] = midioutdev[i]; | ||
1125 | } | ||
1126 | |||
1127 | void sys_open_midi(int nmidiindev, int *midiindev, | ||
1128 | int nmidioutdev, int *midioutdev) | ||
1129 | { | ||
1130 | #ifdef USEAPI_OSS | ||
1131 | midi_oss_init(); | ||
1132 | #endif | ||
1133 | sys_do_open_midi(nmidiindev, midiindev, nmidioutdev, midioutdev); | ||
1134 | sys_save_midi_params(nmidiindev, midiindev, | ||
1135 | nmidioutdev, midioutdev); | ||
1136 | } | ||
1137 | |||
1138 | /* open midi using whatever parameters were last used */ | ||
1139 | void sys_reopen_midi( void) | ||
1140 | { | ||
1141 | int nmidiindev, midiindev[MAXMIDIINDEV]; | ||
1142 | int nmidioutdev, midioutdev[MAXMIDIOUTDEV]; | ||
1143 | sys_get_midi_params(&nmidiindev, midiindev, &nmidioutdev, midioutdev); | ||
1144 | sys_open_midi(nmidiindev, midiindev, nmidioutdev, midioutdev); | ||
1145 | } | ||
1146 | |||
1147 | #define MAXNDEV 20 | ||
1148 | #define DEVDESCSIZE 80 | ||
1149 | |||
1150 | #ifdef MSW | ||
1151 | #define DEVONSET 0 /* microsoft device list starts at 0 (the "mapper"). */ | ||
1152 | #else /* (see also MSW ifdef in sys_parsedevlist(), s_main.c) */ | ||
1153 | #define DEVONSET 1 /* To agree with command line flags, normally start at 1 */ | ||
1154 | #endif | ||
1155 | |||
1156 | void sys_listmididevs(void ) | ||
1157 | { | ||
1158 | char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE]; | ||
1159 | int nindevs = 0, noutdevs = 0, i; | ||
1160 | |||
1161 | midi_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, | ||
1162 | MAXNDEV, DEVDESCSIZE); | ||
1163 | |||
1164 | if (!nindevs) | ||
1165 | post("no midi input devices found"); | ||
1166 | else | ||
1167 | { | ||
1168 | post("input devices:"); | ||
1169 | for (i = 0; i < nindevs; i++) | ||
1170 | post("%d. %s", i+1, indevlist + i * DEVDESCSIZE); | ||
1171 | } | ||
1172 | if (!noutdevs) | ||
1173 | post("no midi output devices found"); | ||
1174 | else | ||
1175 | { | ||
1176 | post("output devices:"); | ||
1177 | for (i = 0; i < noutdevs; i++) | ||
1178 | post("%d. %s", i+DEVONSET, outdevlist + i * DEVDESCSIZE); | ||
1179 | } | ||
1180 | } | ||
1181 | |||
1182 | extern t_class *glob_pdobject; | ||
1183 | |||
1184 | /* start an midi settings dialog window */ | ||
1185 | void glob_midi_properties(t_pd *dummy, t_floatarg flongform) | ||
1186 | { | ||
1187 | char buf[1024 + 2 * MAXNDEV*(DEVDESCSIZE+4)]; | ||
1188 | /* these are the devices you're using: */ | ||
1189 | int nindev, midiindev[MAXMIDIINDEV]; | ||
1190 | int noutdev, midioutdev[MAXMIDIOUTDEV]; | ||
1191 | int midiindev1, midiindev2, midiindev3, midiindev4, | ||
1192 | midioutdev1, midioutdev2, midioutdev3, midioutdev4; | ||
1193 | |||
1194 | /* these are all the devices on your system: */ | ||
1195 | char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE]; | ||
1196 | int nindevs = 0, noutdevs = 0, i; | ||
1197 | |||
1198 | char indevliststring[MAXNDEV*(DEVDESCSIZE+4)+80], | ||
1199 | outdevliststring[MAXNDEV*(DEVDESCSIZE+4)+80]; | ||
1200 | |||
1201 | midi_getdevs(indevlist, &nindevs, outdevlist, &noutdevs, | ||
1202 | MAXNDEV, DEVDESCSIZE); | ||
1203 | |||
1204 | strcpy(indevliststring, "{ {none} "); | ||
1205 | for (i = 0; i < nindevs; i++) | ||
1206 | { | ||
1207 | strcat(indevliststring, "\""); | ||
1208 | strcat(indevliststring, indevlist + i * DEVDESCSIZE); | ||
1209 | strcat(indevliststring, "\" "); | ||
1210 | } | ||
1211 | strcat(indevliststring, "}"); | ||
1212 | |||
1213 | strcpy(outdevliststring, "{ {none} "); | ||
1214 | for (i = 0; i < noutdevs; i++) | ||
1215 | { | ||
1216 | strcat(outdevliststring, "\""); | ||
1217 | strcat(outdevliststring, outdevlist + i * DEVDESCSIZE); | ||
1218 | strcat(outdevliststring, "\" "); | ||
1219 | } | ||
1220 | strcat(outdevliststring, "}"); | ||
1221 | |||
1222 | sys_get_midi_params(&nindev, midiindev, &noutdev, midioutdev); | ||
1223 | |||
1224 | if (nindev > 1 || noutdev > 1) | ||
1225 | flongform = 1; | ||
1226 | |||
1227 | midiindev1 = (nindev > 0 && midiindev[0]>= 0 ? midiindev[0]+1 : 0); | ||
1228 | midiindev2 = (nindev > 1 && midiindev[1]>= 0 ? midiindev[1]+1 : 0); | ||
1229 | midiindev3 = (nindev > 2 && midiindev[2]>= 0 ? midiindev[2]+1 : 0); | ||
1230 | midiindev4 = (nindev > 3 && midiindev[3]>= 0 ? midiindev[3]+1 : 0); | ||
1231 | midioutdev1 = (noutdev > 0 && midioutdev[0]>=0 ? midioutdev[0]+1 : 0); | ||
1232 | midioutdev2 = (noutdev > 1 && midioutdev[1]>=0 ? midioutdev[1]+1 : 0); | ||
1233 | midioutdev3 = (noutdev > 2 && midioutdev[2]>=0 ? midioutdev[2]+1 : 0); | ||
1234 | midioutdev4 = (noutdev > 3 && midioutdev[3]>=0 ? midioutdev[3]+1 : 0); | ||
1235 | |||
1236 | sprintf(buf, | ||
1237 | "pdtk_midi_dialog %%s \ | ||
1238 | %s %d %d %d %d %s %d %d %d %d \ | ||
1239 | %d\n", | ||
1240 | indevliststring, | ||
1241 | midiindev1, midiindev2, midiindev3, midiindev4, | ||
1242 | outdevliststring, | ||
1243 | midioutdev1, midioutdev2, midioutdev3, midioutdev4, | ||
1244 | (flongform != 0)); | ||
1245 | gfxstub_deleteforkey(0); | ||
1246 | gfxstub_new(&glob_pdobject, glob_midi_properties, buf); | ||
1247 | } | ||
1248 | |||
1249 | /* new values from dialog window */ | ||
1250 | void glob_midi_dialog(t_pd *dummy, t_symbol *s, int argc, t_atom *argv) | ||
1251 | { | ||
1252 | int nmidiindev, midiindev[MAXMIDIINDEV]; | ||
1253 | int nmidioutdev, midioutdev[MAXMIDIOUTDEV]; | ||
1254 | int i, nindev, noutdev; | ||
1255 | int newmidiindev[4], newmidioutdev[4]; | ||
1256 | |||
1257 | for (i = 0; i < 4; i++) | ||
1258 | { | ||
1259 | newmidiindev[i] = atom_getintarg(i, argc, argv); | ||
1260 | newmidioutdev[i] = atom_getintarg(i+4, argc, argv); | ||
1261 | } | ||
1262 | |||
1263 | for (i = 0, nindev = 0; i < 4; i++) | ||
1264 | { | ||
1265 | if (newmidiindev[i] > 0) | ||
1266 | { | ||
1267 | newmidiindev[nindev] = newmidiindev[i]-1; | ||
1268 | nindev++; | ||
1269 | } | ||
1270 | } | ||
1271 | for (i = 0, noutdev = 0; i < 4; i++) | ||
1272 | { | ||
1273 | if (newmidioutdev[i] > 0) | ||
1274 | { | ||
1275 | newmidioutdev[noutdev] = newmidioutdev[i]-1; | ||
1276 | noutdev++; | ||
1277 | } | ||
1278 | } | ||
1279 | |||
1280 | sys_close_midi(); | ||
1281 | sys_open_midi(nindev, newmidiindev, noutdev, newmidioutdev); | ||
1282 | } | ||