diff options
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/s_midi_pm.c')
-rw-r--r-- | apps/plugins/pdbox/PDa/src/s_midi_pm.c | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/s_midi_pm.c b/apps/plugins/pdbox/PDa/src/s_midi_pm.c new file mode 100644 index 0000000000..b167175396 --- /dev/null +++ b/apps/plugins/pdbox/PDa/src/s_midi_pm.c | |||
@@ -0,0 +1,332 @@ | |||
1 | /* Copyright (c) 1997-2003 Guenter Geiger, Miller Puckette, Larry Troxler, | ||
2 | * Winfried Ritsch, Karl MacMillan, and others. | ||
3 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
4 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. | ||
5 | |||
6 | this file calls portmidi to do MIDI I/O for MSW and Mac OSX. | ||
7 | |||
8 | */ | ||
9 | |||
10 | #include "m_pd.h" | ||
11 | #include "s_stuff.h" | ||
12 | #include <stdio.h> | ||
13 | #ifdef UNIX | ||
14 | #include <unistd.h> | ||
15 | #include <sys/time.h> | ||
16 | #include <sys/resource.h> | ||
17 | #endif | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | #include <errno.h> | ||
21 | #include "portaudio.h" | ||
22 | #include "portmidi.h" | ||
23 | #include "porttime.h" | ||
24 | #include "pminternal.h" | ||
25 | |||
26 | static PmStream *mac_midiindevlist[MAXMIDIINDEV]; | ||
27 | static PmStream *mac_midioutdevlist[MAXMIDIOUTDEV]; | ||
28 | static int mac_nmidiindev; | ||
29 | static int mac_nmidioutdev; | ||
30 | |||
31 | void sys_open_midi(int nmidiin, int *midiinvec, | ||
32 | int nmidiout, int *midioutvec) | ||
33 | { | ||
34 | int i = 0; | ||
35 | int n = 0; | ||
36 | PmError err; | ||
37 | |||
38 | Pt_Start(1, 0, 0); /* start a timer with millisecond accuracy */ | ||
39 | mac_nmidiindev = 0; | ||
40 | |||
41 | /* protect the unwary from having MIDI inputs open; they're | ||
42 | bad news if you close Pd's terminal window. see sys_nmidiin | ||
43 | in s_main.c too. */ | ||
44 | #ifdef MSW | ||
45 | if (nmidiin) | ||
46 | { | ||
47 | post( | ||
48 | "midi input enabled; warning, don't close the DOS window directly!"); | ||
49 | } | ||
50 | else post("not using MIDI input (use 'pd -midiindev 1' to override)"); | ||
51 | #endif | ||
52 | |||
53 | for (i = 0; i < nmidiin; i++) | ||
54 | { | ||
55 | if (midiinvec[i] == DEFMIDIDEV) | ||
56 | midiinvec[i] = Pm_GetDefaultInputDeviceID(); | ||
57 | err = Pm_OpenInput(&mac_midiindevlist[mac_nmidiindev], midiinvec[i], | ||
58 | NULL, 100, NULL, NULL, NULL); | ||
59 | if (err) | ||
60 | post("could not open midi input device number %d: %s", | ||
61 | midiinvec[i], Pm_GetErrorText(err)); | ||
62 | else | ||
63 | { | ||
64 | if (sys_verbose) | ||
65 | post("Midi Input opened.\n"); | ||
66 | mac_nmidiindev++; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | mac_nmidioutdev = 0; | ||
71 | for (i = 0; i < nmidiout; i++) | ||
72 | { | ||
73 | if (midioutvec[i] == DEFMIDIDEV) | ||
74 | midioutvec[i] = Pm_GetDefaultOutputDeviceID(); | ||
75 | err = Pm_OpenOutput(&mac_midioutdevlist[mac_nmidioutdev], midioutvec[i], | ||
76 | NULL, 0, NULL, NULL, 0); | ||
77 | if (err) | ||
78 | post("could not open midi output device number %d: %s", | ||
79 | midioutvec[i], Pm_GetErrorText(err)); | ||
80 | else | ||
81 | { | ||
82 | if (sys_verbose) | ||
83 | post("Midi Output opened.\n"); | ||
84 | mac_nmidioutdev++; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | void sys_close_midi( void) | ||
90 | { | ||
91 | int i; | ||
92 | for (i = 0; i < mac_nmidiindev; i++) | ||
93 | Pm_Close(mac_midiindevlist[mac_nmidiindev]); | ||
94 | mac_nmidiindev = 0; | ||
95 | for (i = 0; i < mac_nmidioutdev; i++) | ||
96 | Pm_Close(mac_midioutdevlist[mac_nmidioutdev]); | ||
97 | mac_nmidioutdev = 0; | ||
98 | } | ||
99 | |||
100 | void sys_putmidimess(int portno, int a, int b, int c) | ||
101 | { | ||
102 | PmEvent buffer; | ||
103 | fprintf(stderr, "put 1 msg %d %d\n", portno, mac_nmidioutdev); | ||
104 | if (portno >= 0 && portno < mac_nmidioutdev) | ||
105 | { | ||
106 | buffer.message = Pm_Message(a, b, c); | ||
107 | buffer.timestamp = 0; | ||
108 | fprintf(stderr, "put msg\n"); | ||
109 | Pm_Write(mac_midioutdevlist[portno], &buffer, 1); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | void sys_putmidibyte(int portno, int byte) | ||
114 | { | ||
115 | post("sorry, no byte-by-byte MIDI output implemented in MAC OSX"); | ||
116 | } | ||
117 | |||
118 | void sys_poll_midi(void) | ||
119 | { | ||
120 | int i, nmess; | ||
121 | PmEvent buffer; | ||
122 | for (i = 0; i < mac_nmidiindev; i++) | ||
123 | { | ||
124 | int nmess = Pm_Read(mac_midiindevlist[i], &buffer, 1); | ||
125 | if (nmess > 0) | ||
126 | { | ||
127 | int status = Pm_MessageStatus(buffer.message); | ||
128 | int data1 = Pm_MessageData1(buffer.message); | ||
129 | int data2 = Pm_MessageData2(buffer.message); | ||
130 | int msgtype = (status >> 4) - 8; | ||
131 | switch (msgtype) | ||
132 | { | ||
133 | case 0: | ||
134 | case 1: | ||
135 | case 2: | ||
136 | case 3: | ||
137 | case 6: | ||
138 | sys_midibytein(i, status); | ||
139 | sys_midibytein(i, data1); | ||
140 | sys_midibytein(i, data2); | ||
141 | break; | ||
142 | case 4: | ||
143 | case 5: | ||
144 | sys_midibytein(i, status); | ||
145 | sys_midibytein(i, data1); | ||
146 | break; | ||
147 | case 7: | ||
148 | sys_midibytein(i, status); | ||
149 | break; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
155 | void sys_listmididevs(void) /* lifted from pa_devs.c in portaudio */ | ||
156 | { | ||
157 | int i,j; | ||
158 | for (i = 0; i < Pm_CountDevices(); i++) | ||
159 | { | ||
160 | const PmDeviceInfo *info = Pm_GetDeviceInfo(i); | ||
161 | printf("%d: %s, %s", i, info->interf, info->name); | ||
162 | if (info->input) printf(" (input)"); | ||
163 | if (info->output) printf(" (output)"); | ||
164 | printf("\n"); | ||
165 | } | ||
166 | } | ||
167 | /* Copyright (c) 1997-2003 Guenter Geiger, Miller Puckette, Larry Troxler, | ||
168 | * Winfried Ritsch, Karl MacMillan, and others. | ||
169 | * For information on usage and redistribution, and for a DISCLAIMER OF ALL | ||
170 | * WARRANTIES, see the file, "LICENSE.txt," in this distribution. | ||
171 | |||
172 | this file calls portmidi to do MIDI I/O for MSW and Mac OSX. | ||
173 | |||
174 | */ | ||
175 | |||
176 | #include "m_pd.h" | ||
177 | #include "s_stuff.h" | ||
178 | #include <stdio.h> | ||
179 | #ifdef UNIX | ||
180 | #include <unistd.h> | ||
181 | #include <sys/time.h> | ||
182 | #include <sys/resource.h> | ||
183 | #endif | ||
184 | #include <stdlib.h> | ||
185 | #include <string.h> | ||
186 | #include <errno.h> | ||
187 | #include "portaudio.h" | ||
188 | #include "portmidi.h" | ||
189 | #include "porttime.h" | ||
190 | #include "pminternal.h" | ||
191 | |||
192 | static PmStream *mac_midiindevlist[MAXMIDIINDEV]; | ||
193 | static PmStream *mac_midioutdevlist[MAXMIDIOUTDEV]; | ||
194 | static int mac_nmidiindev; | ||
195 | static int mac_nmidioutdev; | ||
196 | |||
197 | void sys_open_midi(int nmidiin, int *midiinvec, | ||
198 | int nmidiout, int *midioutvec) | ||
199 | { | ||
200 | int i = 0; | ||
201 | int n = 0; | ||
202 | PmError err; | ||
203 | |||
204 | Pt_Start(1, 0, 0); /* start a timer with millisecond accuracy */ | ||
205 | mac_nmidiindev = 0; | ||
206 | |||
207 | /* protect the unwary from having MIDI inputs open; they're | ||
208 | bad news if you close Pd's terminal window. see sys_nmidiin | ||
209 | in s_main.c too. */ | ||
210 | #ifdef MSW | ||
211 | if (nmidiin) | ||
212 | { | ||
213 | post( | ||
214 | "midi input enabled; warning, don't close the DOS window directly!"); | ||
215 | } | ||
216 | else post("not using MIDI input (use 'pd -midiindev 1' to override)"); | ||
217 | #endif | ||
218 | |||
219 | for (i = 0; i < nmidiin; i++) | ||
220 | { | ||
221 | if (midiinvec[i] == DEFMIDIDEV) | ||
222 | midiinvec[i] = Pm_GetDefaultInputDeviceID(); | ||
223 | err = Pm_OpenInput(&mac_midiindevlist[mac_nmidiindev], midiinvec[i], | ||
224 | NULL, 100, NULL, NULL, NULL); | ||
225 | if (err) | ||
226 | post("could not open midi input device number %d: %s", | ||
227 | midiinvec[i], Pm_GetErrorText(err)); | ||
228 | else | ||
229 | { | ||
230 | if (sys_verbose) | ||
231 | post("Midi Input opened.\n"); | ||
232 | mac_nmidiindev++; | ||
233 | } | ||
234 | } | ||
235 | |||
236 | mac_nmidioutdev = 0; | ||
237 | for (i = 0; i < nmidiout; i++) | ||
238 | { | ||
239 | if (midioutvec[i] == DEFMIDIDEV) | ||
240 | midioutvec[i] = Pm_GetDefaultOutputDeviceID(); | ||
241 | err = Pm_OpenOutput(&mac_midioutdevlist[mac_nmidioutdev], midioutvec[i], | ||
242 | NULL, 0, NULL, NULL, 0); | ||
243 | if (err) | ||
244 | post("could not open midi output device number %d: %s", | ||
245 | midioutvec[i], Pm_GetErrorText(err)); | ||
246 | else | ||
247 | { | ||
248 | if (sys_verbose) | ||
249 | post("Midi Output opened.\n"); | ||
250 | mac_nmidioutdev++; | ||
251 | } | ||
252 | } | ||
253 | } | ||
254 | |||
255 | void sys_close_midi( void) | ||
256 | { | ||
257 | int i; | ||
258 | for (i = 0; i < mac_nmidiindev; i++) | ||
259 | Pm_Close(mac_midiindevlist[mac_nmidiindev]); | ||
260 | mac_nmidiindev = 0; | ||
261 | for (i = 0; i < mac_nmidioutdev; i++) | ||
262 | Pm_Close(mac_midioutdevlist[mac_nmidioutdev]); | ||
263 | mac_nmidioutdev = 0; | ||
264 | } | ||
265 | |||
266 | void sys_putmidimess(int portno, int a, int b, int c) | ||
267 | { | ||
268 | PmEvent buffer; | ||
269 | fprintf(stderr, "put 1 msg %d %d\n", portno, mac_nmidioutdev); | ||
270 | if (portno >= 0 && portno < mac_nmidioutdev) | ||
271 | { | ||
272 | buffer.message = Pm_Message(a, b, c); | ||
273 | buffer.timestamp = 0; | ||
274 | fprintf(stderr, "put msg\n"); | ||
275 | Pm_Write(mac_midioutdevlist[portno], &buffer, 1); | ||
276 | } | ||
277 | } | ||
278 | |||
279 | void sys_putmidibyte(int portno, int byte) | ||
280 | { | ||
281 | post("sorry, no byte-by-byte MIDI output implemented in MAC OSX"); | ||
282 | } | ||
283 | |||
284 | void sys_poll_midi(void) | ||
285 | { | ||
286 | int i, nmess; | ||
287 | PmEvent buffer; | ||
288 | for (i = 0; i < mac_nmidiindev; i++) | ||
289 | { | ||
290 | int nmess = Pm_Read(mac_midiindevlist[i], &buffer, 1); | ||
291 | if (nmess > 0) | ||
292 | { | ||
293 | int status = Pm_MessageStatus(buffer.message); | ||
294 | int data1 = Pm_MessageData1(buffer.message); | ||
295 | int data2 = Pm_MessageData2(buffer.message); | ||
296 | int msgtype = (status >> 4) - 8; | ||
297 | switch (msgtype) | ||
298 | { | ||
299 | case 0: | ||
300 | case 1: | ||
301 | case 2: | ||
302 | case 3: | ||
303 | case 6: | ||
304 | sys_midibytein(i, status); | ||
305 | sys_midibytein(i, data1); | ||
306 | sys_midibytein(i, data2); | ||
307 | break; | ||
308 | case 4: | ||
309 | case 5: | ||
310 | sys_midibytein(i, status); | ||
311 | sys_midibytein(i, data1); | ||
312 | break; | ||
313 | case 7: | ||
314 | sys_midibytein(i, status); | ||
315 | break; | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | |||
321 | void sys_listmididevs(void) /* lifted from pa_devs.c in portaudio */ | ||
322 | { | ||
323 | int i,j; | ||
324 | for (i = 0; i < Pm_CountDevices(); i++) | ||
325 | { | ||
326 | const PmDeviceInfo *info = Pm_GetDeviceInfo(i); | ||
327 | printf("%d: %s, %s", i, info->interf, info->name); | ||
328 | if (info->input) printf(" (input)"); | ||
329 | if (info->output) printf(" (output)"); | ||
330 | printf("\n"); | ||
331 | } | ||
332 | } | ||