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