trunk/src/osd/portmedia/pmmidi.c
| r28730 | r28731 | |
| 1 | | //============================================================ |
| 2 | | // |
| 3 | | // pmmidi.c - OSD interface for PortMidi |
| 4 | | // |
| 5 | | // Copyright (c) 1996-2013, Nicola Salmoria and the MAME Team. |
| 6 | | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | | // |
| 8 | | //============================================================ |
| 9 | | |
| 10 | | #include "portmidi/portmidi.h" |
| 11 | | #include "osdcore.h" |
| 12 | | |
| 13 | | static const int RX_EVENT_BUF_SIZE = 512; |
| 14 | | |
| 15 | | #define MIDI_SYSEX 0xf0 |
| 16 | | #define MIDI_EOX 0xf7 |
| 17 | | |
| 18 | | struct osd_midi_device |
| 19 | | { |
| 20 | | #ifndef DISABLE_MIDI |
| 21 | | PortMidiStream *pmStream; |
| 22 | | PmEvent rx_evBuf[RX_EVENT_BUF_SIZE]; |
| 23 | | #endif |
| 24 | | UINT8 xmit_in[4]; // Pm_Messages mean we can at most have 3 residue bytes |
| 25 | | int xmit_cnt; |
| 26 | | UINT8 last_status; |
| 27 | | bool rx_sysex; |
| 28 | | }; |
| 29 | | |
| 30 | | void osd_list_midi_devices(void) |
| 31 | | { |
| 32 | | #ifndef DISABLE_MIDI |
| 33 | | int num_devs = Pm_CountDevices(); |
| 34 | | const PmDeviceInfo *pmInfo; |
| 35 | | |
| 36 | | printf("\n"); |
| 37 | | |
| 38 | | if (num_devs == 0) |
| 39 | | { |
| 40 | | printf("No MIDI ports were found\n"); |
| 41 | | return; |
| 42 | | } |
| 43 | | |
| 44 | | printf("MIDI input ports:\n"); |
| 45 | | for (int i = 0; i < num_devs; i++) |
| 46 | | { |
| 47 | | pmInfo = Pm_GetDeviceInfo(i); |
| 48 | | |
| 49 | | if (pmInfo->input) |
| 50 | | { |
| 51 | | printf("%s %s\n", pmInfo->name, (i == Pm_GetDefaultInputDeviceID()) ? "(default)" : ""); |
| 52 | | } |
| 53 | | } |
| 54 | | |
| 55 | | printf("\nMIDI output ports:\n"); |
| 56 | | for (int i = 0; i < num_devs; i++) |
| 57 | | { |
| 58 | | pmInfo = Pm_GetDeviceInfo(i); |
| 59 | | |
| 60 | | if (pmInfo->output) |
| 61 | | { |
| 62 | | printf("%s %s\n", pmInfo->name, (i == Pm_GetDefaultOutputDeviceID()) ? "(default)" : ""); |
| 63 | | } |
| 64 | | } |
| 65 | | #else |
| 66 | | printf("\nMIDI is not supported in this build\n"); |
| 67 | | #endif |
| 68 | | } |
| 69 | | |
| 70 | | osd_midi_device *osd_open_midi_input(const char *devname) |
| 71 | | { |
| 72 | | #ifndef DISABLE_MIDI |
| 73 | | int num_devs = Pm_CountDevices(); |
| 74 | | int found_dev = -1; |
| 75 | | const PmDeviceInfo *pmInfo; |
| 76 | | PortMidiStream *stm; |
| 77 | | osd_midi_device *ret; |
| 78 | | |
| 79 | | for (int i = 0; i < num_devs; i++) |
| 80 | | { |
| 81 | | pmInfo = Pm_GetDeviceInfo(i); |
| 82 | | |
| 83 | | if (pmInfo->input) |
| 84 | | { |
| 85 | | if (!strcmp(devname, pmInfo->name)) |
| 86 | | { |
| 87 | | found_dev = i; |
| 88 | | break; |
| 89 | | } |
| 90 | | } |
| 91 | | } |
| 92 | | |
| 93 | | if (found_dev >= 0) |
| 94 | | { |
| 95 | | if (Pm_OpenInput(&stm, found_dev, NULL, RX_EVENT_BUF_SIZE, NULL, NULL) == pmNoError) |
| 96 | | { |
| 97 | | ret = (osd_midi_device *)osd_malloc(sizeof(osd_midi_device)); |
| 98 | | memset(ret, 0, sizeof(osd_midi_device)); |
| 99 | | ret->pmStream = stm; |
| 100 | | return ret; |
| 101 | | } |
| 102 | | else |
| 103 | | { |
| 104 | | printf("Couldn't open PM device\n"); |
| 105 | | return NULL; |
| 106 | | } |
| 107 | | } |
| 108 | | else |
| 109 | | { |
| 110 | | return NULL; |
| 111 | | } |
| 112 | | #else |
| 113 | | return NULL; |
| 114 | | #endif |
| 115 | | } |
| 116 | | |
| 117 | | osd_midi_device *osd_open_midi_output(const char *devname) |
| 118 | | { |
| 119 | | #ifndef DISABLE_MIDI |
| 120 | | int num_devs = Pm_CountDevices(); |
| 121 | | int found_dev = -1; |
| 122 | | const PmDeviceInfo *pmInfo; |
| 123 | | PortMidiStream *stm; |
| 124 | | osd_midi_device *ret; |
| 125 | | |
| 126 | | for (int i = 0; i < num_devs; i++) |
| 127 | | { |
| 128 | | pmInfo = Pm_GetDeviceInfo(i); |
| 129 | | |
| 130 | | if (pmInfo->output) |
| 131 | | { |
| 132 | | if (!strcmp(devname, pmInfo->name)) |
| 133 | | { |
| 134 | | found_dev = i; |
| 135 | | break; |
| 136 | | } |
| 137 | | } |
| 138 | | } |
| 139 | | |
| 140 | | if (found_dev >= 0) |
| 141 | | { |
| 142 | | if (Pm_OpenOutput(&stm, found_dev, NULL, 100, NULL, NULL, 0) == pmNoError) |
| 143 | | { |
| 144 | | ret = (osd_midi_device *)osd_malloc(sizeof(osd_midi_device)); |
| 145 | | memset(ret, 0, sizeof(osd_midi_device)); |
| 146 | | ret->pmStream = stm; |
| 147 | | return ret; |
| 148 | | } |
| 149 | | else |
| 150 | | { |
| 151 | | printf("Couldn't open PM device\n"); |
| 152 | | return NULL; |
| 153 | | } |
| 154 | | } |
| 155 | | else |
| 156 | | { |
| 157 | | return NULL; |
| 158 | | } |
| 159 | | #endif |
| 160 | | return NULL; |
| 161 | | } |
| 162 | | |
| 163 | | void osd_close_midi_channel(osd_midi_device *dev) |
| 164 | | { |
| 165 | | #ifndef DISABLE_MIDI |
| 166 | | Pm_Close(dev->pmStream); |
| 167 | | osd_free(dev); |
| 168 | | #endif |
| 169 | | } |
| 170 | | |
| 171 | | bool osd_poll_midi_channel(osd_midi_device *dev) |
| 172 | | { |
| 173 | | #ifndef DISABLE_MIDI |
| 174 | | PmError chk = Pm_Poll(dev->pmStream); |
| 175 | | |
| 176 | | return (chk == pmGotData) ? true : false; |
| 177 | | #else |
| 178 | | return false; |
| 179 | | #endif |
| 180 | | } |
| 181 | | |
| 182 | | int osd_read_midi_channel(osd_midi_device *dev, UINT8 *pOut) |
| 183 | | { |
| 184 | | #ifndef DISABLE_MIDI |
| 185 | | int msgsRead = Pm_Read(dev->pmStream, dev->rx_evBuf, RX_EVENT_BUF_SIZE); |
| 186 | | int bytesOut = 0; |
| 187 | | |
| 188 | | if (msgsRead <= 0) |
| 189 | | { |
| 190 | | return 0; |
| 191 | | } |
| 192 | | |
| 193 | | for (int msg = 0; msg < msgsRead; msg++) |
| 194 | | { |
| 195 | | UINT8 status = Pm_MessageStatus(dev->rx_evBuf[msg].message); |
| 196 | | |
| 197 | | if (dev->rx_sysex) |
| 198 | | { |
| 199 | | if (status & 0x80) // sys real-time imposing on us? |
| 200 | | { |
| 201 | | if ((status == 0xf2) || (status == 0xf3)) |
| 202 | | { |
| 203 | | *pOut++ = status; |
| 204 | | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 205 | | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 206 | | bytesOut += 3; |
| 207 | | } |
| 208 | | else |
| 209 | | { |
| 210 | | *pOut++ = status; |
| 211 | | bytesOut++; |
| 212 | | if (status == MIDI_EOX) |
| 213 | | { |
| 214 | | dev->rx_sysex = false; |
| 215 | | } |
| 216 | | } |
| 217 | | } |
| 218 | | else // shift out the sysex bytes |
| 219 | | { |
| 220 | | for (int i = 0; i < 4; i++) |
| 221 | | { |
| 222 | | UINT8 byte = dev->rx_evBuf[msg].message & 0xff; |
| 223 | | *pOut++ = byte; |
| 224 | | bytesOut++; |
| 225 | | if (byte == MIDI_EOX) |
| 226 | | { |
| 227 | | dev->rx_sysex = false; |
| 228 | | break; |
| 229 | | } |
| 230 | | dev->rx_evBuf[msg].message >>= 8; |
| 231 | | } |
| 232 | | } |
| 233 | | } |
| 234 | | else |
| 235 | | { |
| 236 | | switch ((status>>4) & 0xf) |
| 237 | | { |
| 238 | | case 0xc: // 2-byte messages |
| 239 | | case 0xd: |
| 240 | | *pOut++ = status; |
| 241 | | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 242 | | bytesOut += 2; |
| 243 | | break; |
| 244 | | |
| 245 | | case 0xf: // system common |
| 246 | | switch (status & 0xf) |
| 247 | | { |
| 248 | | case 0: // System Exclusive |
| 249 | | { |
| 250 | | *pOut++ = status; // this should be OK: the shortest legal sysex is F0 tt dd F7, I believe |
| 251 | | *pOut++ = (dev->rx_evBuf[msg].message>>8) & 0xff; |
| 252 | | *pOut++ = (dev->rx_evBuf[msg].message>>16) & 0xff; |
| 253 | | UINT8 last = *pOut++ = (dev->rx_evBuf[msg].message>>24) & 0xff; |
| 254 | | bytesOut += 4; |
| 255 | | dev->rx_sysex = (last != MIDI_EOX); |
| 256 | | break; |
| 257 | | } |
| 258 | | |
| 259 | | case 7: // End of System Exclusive |
| 260 | | *pOut++ = status; |
| 261 | | bytesOut += 1; |
| 262 | | dev->rx_sysex = false; |
| 263 | | break; |
| 264 | | |
| 265 | | case 2: // song pos |
| 266 | | case 3: // song select |
| 267 | | *pOut++ = status; |
| 268 | | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 269 | | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 270 | | bytesOut += 3; |
| 271 | | break; |
| 272 | | |
| 273 | | default: // all other defined Fx messages are 1 byte |
| 274 | | break; |
| 275 | | } |
| 276 | | break; |
| 277 | | |
| 278 | | default: |
| 279 | | *pOut++ = status; |
| 280 | | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 281 | | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 282 | | bytesOut += 3; |
| 283 | | break; |
| 284 | | } |
| 285 | | } |
| 286 | | } |
| 287 | | |
| 288 | | return bytesOut; |
| 289 | | #else |
| 290 | | return 0; |
| 291 | | #endif |
| 292 | | } |
| 293 | | |
| 294 | | void osd_write_midi_channel(osd_midi_device *dev, UINT8 data) |
| 295 | | { |
| 296 | | #ifndef DISABLE_MIDI |
| 297 | | int bytes_needed = 0; |
| 298 | | PmEvent ev; |
| 299 | | ev.timestamp = 0; // use the current time |
| 300 | | |
| 301 | | // printf("write: %02x (%d)\n", data, dev->xmit_cnt); |
| 302 | | |
| 303 | | if (dev->xmit_cnt >= 4) |
| 304 | | { |
| 305 | | printf("MIDI out: packet assembly overflow, contact MAMEdev!\n"); |
| 306 | | return; |
| 307 | | } |
| 308 | | |
| 309 | | // handle sysex |
| 310 | | if (dev->last_status == MIDI_SYSEX) |
| 311 | | { |
| 312 | | // printf("sysex: %02x (%d)\n", data, dev->xmit_cnt); |
| 313 | | |
| 314 | | // if we get a status that isn't sysex, assume it's system common |
| 315 | | if ((data & 0x80) && (data != MIDI_EOX)) |
| 316 | | { |
| 317 | | // printf("common during sysex!\n"); |
| 318 | | ev.message = Pm_Message(data, 0, 0); |
| 319 | | Pm_Write(dev->pmStream, &ev, 1); |
| 320 | | return; |
| 321 | | } |
| 322 | | |
| 323 | | dev->xmit_in[dev->xmit_cnt++] = data; |
| 324 | | |
| 325 | | // if EOX or 4 bytes filled, transmit 4 bytes |
| 326 | | if ((dev->xmit_cnt == 4) || (data == MIDI_EOX)) |
| 327 | | { |
| 328 | | ev.message = dev->xmit_in[0] | (dev->xmit_in[1]<<8) | (dev->xmit_in[2]<<16) | (dev->xmit_in[3]<<24); |
| 329 | | Pm_Write(dev->pmStream, &ev, 1); |
| 330 | | dev->xmit_in[0] = dev->xmit_in[1] = dev->xmit_in[2] = dev->xmit_in[3] = 0; |
| 331 | | dev->xmit_cnt = 0; |
| 332 | | |
| 333 | | // printf("SysEx packet: %08x\n", ev.message); |
| 334 | | |
| 335 | | // if this is EOX, kill the running status |
| 336 | | if (data == MIDI_EOX) |
| 337 | | { |
| 338 | | dev->last_status = 0; |
| 339 | | } |
| 340 | | } |
| 341 | | |
| 342 | | return; |
| 343 | | } |
| 344 | | |
| 345 | | // handle running status. don't allow system real-time messages to be considered as running status. |
| 346 | | if ((dev->xmit_cnt == 0) && (data & 0x80) && (data < 0xf8)) |
| 347 | | { |
| 348 | | dev->last_status = data; |
| 349 | | } |
| 350 | | |
| 351 | | if ((dev->xmit_cnt == 0) && !(data & 0x80)) |
| 352 | | { |
| 353 | | dev->xmit_in[dev->xmit_cnt++] = dev->last_status; |
| 354 | | dev->xmit_in[dev->xmit_cnt++] = data; |
| 355 | | // printf("\trunning status: [%d] = %02x, [%d] = %02x, last_status = %02x\n", dev->xmit_cnt-2, dev->last_status, dev->xmit_cnt-1, data, dev->last_status); |
| 356 | | } |
| 357 | | else |
| 358 | | { |
| 359 | | dev->xmit_in[dev->xmit_cnt++] = data; |
| 360 | | // printf("\tNRS: [%d] = %02x\n", dev->xmit_cnt-1, data); |
| 361 | | } |
| 362 | | |
| 363 | | if ((dev->xmit_cnt == 1) && (dev->xmit_in[0] == MIDI_SYSEX)) |
| 364 | | { |
| 365 | | // printf("Start SysEx!\n"); |
| 366 | | dev->last_status = MIDI_SYSEX; |
| 367 | | return; |
| 368 | | } |
| 369 | | |
| 370 | | // are we there yet? |
| 371 | | // printf("status check: %02x\n", dev->xmit_in[0]); |
| 372 | | switch ((dev->xmit_in[0]>>4) & 0xf) |
| 373 | | { |
| 374 | | case 0xc: // 2-byte messages |
| 375 | | case 0xd: |
| 376 | | bytes_needed = 2; |
| 377 | | break; |
| 378 | | |
| 379 | | case 0xf: // system common |
| 380 | | switch (dev->xmit_in[0] & 0xf) |
| 381 | | { |
| 382 | | case 0: // System Exclusive is handled above |
| 383 | | break; |
| 384 | | |
| 385 | | case 7: // End of System Exclusive |
| 386 | | bytes_needed = 1; |
| 387 | | break; |
| 388 | | |
| 389 | | case 2: // song pos |
| 390 | | case 3: // song select |
| 391 | | bytes_needed = 3; |
| 392 | | break; |
| 393 | | |
| 394 | | default: // all other defined Fx messages are 1 byte |
| 395 | | bytes_needed = 1; |
| 396 | | break; |
| 397 | | } |
| 398 | | break; |
| 399 | | |
| 400 | | default: |
| 401 | | bytes_needed = 3; |
| 402 | | break; |
| 403 | | } |
| 404 | | |
| 405 | | if (dev->xmit_cnt == bytes_needed) |
| 406 | | { |
| 407 | | ev.message = Pm_Message(dev->xmit_in[0], dev->xmit_in[1], dev->xmit_in[2]); |
| 408 | | Pm_Write(dev->pmStream, &ev, 1); |
| 409 | | dev->xmit_cnt = 0; |
| 410 | | } |
| 411 | | |
| 412 | | #endif |
| 413 | | } |
| 414 | | |
| 415 | | void osd_init_midi(void) |
| 416 | | { |
| 417 | | #ifndef DISABLE_MIDI |
| 418 | | Pm_Initialize(); |
| 419 | | #endif |
| 420 | | } |
| 421 | | |
| 422 | | void osd_shutdown_midi(void) |
| 423 | | { |
| 424 | | #ifndef DISABLE_MIDI |
| 425 | | Pm_Terminate(); |
| 426 | | #endif |
| 427 | | } |
trunk/src/osd/portmedia/pmmidi.inc
| r0 | r28731 | |
| 1 | //============================================================ |
| 2 | // |
| 3 | // pmmidi.inc - OSD interface for PortMidi |
| 4 | // |
| 5 | // Copyright (c) 1996-2013, Nicola Salmoria and the MAME Team. |
| 6 | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | // |
| 8 | //============================================================ |
| 9 | |
| 10 | #include "portmidi/portmidi.h" |
| 11 | #include "osdcore.h" |
| 12 | |
| 13 | static const int RX_EVENT_BUF_SIZE = 512; |
| 14 | |
| 15 | #define MIDI_SYSEX 0xf0 |
| 16 | #define MIDI_EOX 0xf7 |
| 17 | |
| 18 | struct osd_midi_device |
| 19 | { |
| 20 | #ifndef DISABLE_MIDI |
| 21 | PortMidiStream *pmStream; |
| 22 | PmEvent rx_evBuf[RX_EVENT_BUF_SIZE]; |
| 23 | #endif |
| 24 | UINT8 xmit_in[4]; // Pm_Messages mean we can at most have 3 residue bytes |
| 25 | int xmit_cnt; |
| 26 | UINT8 last_status; |
| 27 | bool rx_sysex; |
| 28 | }; |
| 29 | |
| 30 | void osd_list_midi_devices(void) |
| 31 | { |
| 32 | #ifndef DISABLE_MIDI |
| 33 | int num_devs = Pm_CountDevices(); |
| 34 | const PmDeviceInfo *pmInfo; |
| 35 | |
| 36 | printf("\n"); |
| 37 | |
| 38 | if (num_devs == 0) |
| 39 | { |
| 40 | printf("No MIDI ports were found\n"); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | printf("MIDI input ports:\n"); |
| 45 | for (int i = 0; i < num_devs; i++) |
| 46 | { |
| 47 | pmInfo = Pm_GetDeviceInfo(i); |
| 48 | |
| 49 | if (pmInfo->input) |
| 50 | { |
| 51 | printf("%s %s\n", pmInfo->name, (i == Pm_GetDefaultInputDeviceID()) ? "(default)" : ""); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | printf("\nMIDI output ports:\n"); |
| 56 | for (int i = 0; i < num_devs; i++) |
| 57 | { |
| 58 | pmInfo = Pm_GetDeviceInfo(i); |
| 59 | |
| 60 | if (pmInfo->output) |
| 61 | { |
| 62 | printf("%s %s\n", pmInfo->name, (i == Pm_GetDefaultOutputDeviceID()) ? "(default)" : ""); |
| 63 | } |
| 64 | } |
| 65 | #else |
| 66 | printf("\nMIDI is not supported in this build\n"); |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | osd_midi_device *osd_open_midi_input(const char *devname) |
| 71 | { |
| 72 | #ifndef DISABLE_MIDI |
| 73 | int num_devs = Pm_CountDevices(); |
| 74 | int found_dev = -1; |
| 75 | const PmDeviceInfo *pmInfo; |
| 76 | PortMidiStream *stm; |
| 77 | osd_midi_device *ret; |
| 78 | |
| 79 | for (int i = 0; i < num_devs; i++) |
| 80 | { |
| 81 | pmInfo = Pm_GetDeviceInfo(i); |
| 82 | |
| 83 | if (pmInfo->input) |
| 84 | { |
| 85 | if (!strcmp(devname, pmInfo->name)) |
| 86 | { |
| 87 | found_dev = i; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (found_dev >= 0) |
| 94 | { |
| 95 | if (Pm_OpenInput(&stm, found_dev, NULL, RX_EVENT_BUF_SIZE, NULL, NULL) == pmNoError) |
| 96 | { |
| 97 | ret = (osd_midi_device *)osd_malloc(sizeof(osd_midi_device)); |
| 98 | memset(ret, 0, sizeof(osd_midi_device)); |
| 99 | ret->pmStream = stm; |
| 100 | return ret; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | printf("Couldn't open PM device\n"); |
| 105 | return NULL; |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | return NULL; |
| 111 | } |
| 112 | #else |
| 113 | return NULL; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | osd_midi_device *osd_open_midi_output(const char *devname) |
| 118 | { |
| 119 | #ifndef DISABLE_MIDI |
| 120 | int num_devs = Pm_CountDevices(); |
| 121 | int found_dev = -1; |
| 122 | const PmDeviceInfo *pmInfo; |
| 123 | PortMidiStream *stm; |
| 124 | osd_midi_device *ret; |
| 125 | |
| 126 | for (int i = 0; i < num_devs; i++) |
| 127 | { |
| 128 | pmInfo = Pm_GetDeviceInfo(i); |
| 129 | |
| 130 | if (pmInfo->output) |
| 131 | { |
| 132 | if (!strcmp(devname, pmInfo->name)) |
| 133 | { |
| 134 | found_dev = i; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (found_dev >= 0) |
| 141 | { |
| 142 | if (Pm_OpenOutput(&stm, found_dev, NULL, 100, NULL, NULL, 0) == pmNoError) |
| 143 | { |
| 144 | ret = (osd_midi_device *)osd_malloc(sizeof(osd_midi_device)); |
| 145 | memset(ret, 0, sizeof(osd_midi_device)); |
| 146 | ret->pmStream = stm; |
| 147 | return ret; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | printf("Couldn't open PM device\n"); |
| 152 | return NULL; |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | return NULL; |
| 158 | } |
| 159 | #endif |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | void osd_close_midi_channel(osd_midi_device *dev) |
| 164 | { |
| 165 | #ifndef DISABLE_MIDI |
| 166 | Pm_Close(dev->pmStream); |
| 167 | osd_free(dev); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | bool osd_poll_midi_channel(osd_midi_device *dev) |
| 172 | { |
| 173 | #ifndef DISABLE_MIDI |
| 174 | PmError chk = Pm_Poll(dev->pmStream); |
| 175 | |
| 176 | return (chk == pmGotData) ? true : false; |
| 177 | #else |
| 178 | return false; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | int osd_read_midi_channel(osd_midi_device *dev, UINT8 *pOut) |
| 183 | { |
| 184 | #ifndef DISABLE_MIDI |
| 185 | int msgsRead = Pm_Read(dev->pmStream, dev->rx_evBuf, RX_EVENT_BUF_SIZE); |
| 186 | int bytesOut = 0; |
| 187 | |
| 188 | if (msgsRead <= 0) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | for (int msg = 0; msg < msgsRead; msg++) |
| 194 | { |
| 195 | UINT8 status = Pm_MessageStatus(dev->rx_evBuf[msg].message); |
| 196 | |
| 197 | if (dev->rx_sysex) |
| 198 | { |
| 199 | if (status & 0x80) // sys real-time imposing on us? |
| 200 | { |
| 201 | if ((status == 0xf2) || (status == 0xf3)) |
| 202 | { |
| 203 | *pOut++ = status; |
| 204 | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 205 | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 206 | bytesOut += 3; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | *pOut++ = status; |
| 211 | bytesOut++; |
| 212 | if (status == MIDI_EOX) |
| 213 | { |
| 214 | dev->rx_sysex = false; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | else // shift out the sysex bytes |
| 219 | { |
| 220 | for (int i = 0; i < 4; i++) |
| 221 | { |
| 222 | UINT8 byte = dev->rx_evBuf[msg].message & 0xff; |
| 223 | *pOut++ = byte; |
| 224 | bytesOut++; |
| 225 | if (byte == MIDI_EOX) |
| 226 | { |
| 227 | dev->rx_sysex = false; |
| 228 | break; |
| 229 | } |
| 230 | dev->rx_evBuf[msg].message >>= 8; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | switch ((status>>4) & 0xf) |
| 237 | { |
| 238 | case 0xc: // 2-byte messages |
| 239 | case 0xd: |
| 240 | *pOut++ = status; |
| 241 | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 242 | bytesOut += 2; |
| 243 | break; |
| 244 | |
| 245 | case 0xf: // system common |
| 246 | switch (status & 0xf) |
| 247 | { |
| 248 | case 0: // System Exclusive |
| 249 | { |
| 250 | *pOut++ = status; // this should be OK: the shortest legal sysex is F0 tt dd F7, I believe |
| 251 | *pOut++ = (dev->rx_evBuf[msg].message>>8) & 0xff; |
| 252 | *pOut++ = (dev->rx_evBuf[msg].message>>16) & 0xff; |
| 253 | UINT8 last = *pOut++ = (dev->rx_evBuf[msg].message>>24) & 0xff; |
| 254 | bytesOut += 4; |
| 255 | dev->rx_sysex = (last != MIDI_EOX); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | case 7: // End of System Exclusive |
| 260 | *pOut++ = status; |
| 261 | bytesOut += 1; |
| 262 | dev->rx_sysex = false; |
| 263 | break; |
| 264 | |
| 265 | case 2: // song pos |
| 266 | case 3: // song select |
| 267 | *pOut++ = status; |
| 268 | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 269 | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 270 | bytesOut += 3; |
| 271 | break; |
| 272 | |
| 273 | default: // all other defined Fx messages are 1 byte |
| 274 | break; |
| 275 | } |
| 276 | break; |
| 277 | |
| 278 | default: |
| 279 | *pOut++ = status; |
| 280 | *pOut++ = Pm_MessageData1(dev->rx_evBuf[msg].message); |
| 281 | *pOut++ = Pm_MessageData2(dev->rx_evBuf[msg].message); |
| 282 | bytesOut += 3; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return bytesOut; |
| 289 | #else |
| 290 | return 0; |
| 291 | #endif |
| 292 | } |
| 293 | |
| 294 | void osd_write_midi_channel(osd_midi_device *dev, UINT8 data) |
| 295 | { |
| 296 | #ifndef DISABLE_MIDI |
| 297 | int bytes_needed = 0; |
| 298 | PmEvent ev; |
| 299 | ev.timestamp = 0; // use the current time |
| 300 | |
| 301 | // printf("write: %02x (%d)\n", data, dev->xmit_cnt); |
| 302 | |
| 303 | if (dev->xmit_cnt >= 4) |
| 304 | { |
| 305 | printf("MIDI out: packet assembly overflow, contact MAMEdev!\n"); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // handle sysex |
| 310 | if (dev->last_status == MIDI_SYSEX) |
| 311 | { |
| 312 | // printf("sysex: %02x (%d)\n", data, dev->xmit_cnt); |
| 313 | |
| 314 | // if we get a status that isn't sysex, assume it's system common |
| 315 | if ((data & 0x80) && (data != MIDI_EOX)) |
| 316 | { |
| 317 | // printf("common during sysex!\n"); |
| 318 | ev.message = Pm_Message(data, 0, 0); |
| 319 | Pm_Write(dev->pmStream, &ev, 1); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | dev->xmit_in[dev->xmit_cnt++] = data; |
| 324 | |
| 325 | // if EOX or 4 bytes filled, transmit 4 bytes |
| 326 | if ((dev->xmit_cnt == 4) || (data == MIDI_EOX)) |
| 327 | { |
| 328 | ev.message = dev->xmit_in[0] | (dev->xmit_in[1]<<8) | (dev->xmit_in[2]<<16) | (dev->xmit_in[3]<<24); |
| 329 | Pm_Write(dev->pmStream, &ev, 1); |
| 330 | dev->xmit_in[0] = dev->xmit_in[1] = dev->xmit_in[2] = dev->xmit_in[3] = 0; |
| 331 | dev->xmit_cnt = 0; |
| 332 | |
| 333 | // printf("SysEx packet: %08x\n", ev.message); |
| 334 | |
| 335 | // if this is EOX, kill the running status |
| 336 | if (data == MIDI_EOX) |
| 337 | { |
| 338 | dev->last_status = 0; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // handle running status. don't allow system real-time messages to be considered as running status. |
| 346 | if ((dev->xmit_cnt == 0) && (data & 0x80) && (data < 0xf8)) |
| 347 | { |
| 348 | dev->last_status = data; |
| 349 | } |
| 350 | |
| 351 | if ((dev->xmit_cnt == 0) && !(data & 0x80)) |
| 352 | { |
| 353 | dev->xmit_in[dev->xmit_cnt++] = dev->last_status; |
| 354 | dev->xmit_in[dev->xmit_cnt++] = data; |
| 355 | // printf("\trunning status: [%d] = %02x, [%d] = %02x, last_status = %02x\n", dev->xmit_cnt-2, dev->last_status, dev->xmit_cnt-1, data, dev->last_status); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | dev->xmit_in[dev->xmit_cnt++] = data; |
| 360 | // printf("\tNRS: [%d] = %02x\n", dev->xmit_cnt-1, data); |
| 361 | } |
| 362 | |
| 363 | if ((dev->xmit_cnt == 1) && (dev->xmit_in[0] == MIDI_SYSEX)) |
| 364 | { |
| 365 | // printf("Start SysEx!\n"); |
| 366 | dev->last_status = MIDI_SYSEX; |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // are we there yet? |
| 371 | // printf("status check: %02x\n", dev->xmit_in[0]); |
| 372 | switch ((dev->xmit_in[0]>>4) & 0xf) |
| 373 | { |
| 374 | case 0xc: // 2-byte messages |
| 375 | case 0xd: |
| 376 | bytes_needed = 2; |
| 377 | break; |
| 378 | |
| 379 | case 0xf: // system common |
| 380 | switch (dev->xmit_in[0] & 0xf) |
| 381 | { |
| 382 | case 0: // System Exclusive is handled above |
| 383 | break; |
| 384 | |
| 385 | case 7: // End of System Exclusive |
| 386 | bytes_needed = 1; |
| 387 | break; |
| 388 | |
| 389 | case 2: // song pos |
| 390 | case 3: // song select |
| 391 | bytes_needed = 3; |
| 392 | break; |
| 393 | |
| 394 | default: // all other defined Fx messages are 1 byte |
| 395 | bytes_needed = 1; |
| 396 | break; |
| 397 | } |
| 398 | break; |
| 399 | |
| 400 | default: |
| 401 | bytes_needed = 3; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | if (dev->xmit_cnt == bytes_needed) |
| 406 | { |
| 407 | ev.message = Pm_Message(dev->xmit_in[0], dev->xmit_in[1], dev->xmit_in[2]); |
| 408 | Pm_Write(dev->pmStream, &ev, 1); |
| 409 | dev->xmit_cnt = 0; |
| 410 | } |
| 411 | |
| 412 | #endif |
| 413 | } |
| 414 | |
| 415 | void osd_init_midi(void) |
| 416 | { |
| 417 | #ifndef DISABLE_MIDI |
| 418 | Pm_Initialize(); |
| 419 | #endif |
| 420 | } |
| 421 | |
| 422 | void osd_shutdown_midi(void) |
| 423 | { |
| 424 | #ifndef DISABLE_MIDI |
| 425 | Pm_Terminate(); |
| 426 | #endif |
| 427 | } |