| 1 | /* ScummVM - Graphic Adventure Engine |
| 2 | * |
| 3 | * ScummVM is the legal property of its developers, whose names |
| 4 | * are too numerous to list here. Please refer to the COPYRIGHT |
| 5 | * file distributed with this source distribution. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * $URL: |
| 22 | * $Id: dmedia.cpp |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * IRIX dmedia support by Rainer Canavan <scumm@canavan.de> |
| 27 | * some code liberated from seq.cpp and coremidi.cpp |
| 28 | */ |
| 29 | |
| 30 | #if defined(IRIX) |
| 31 | |
| 32 | #include "common/stdafx.h" |
| 33 | #include "sound/mpu401.h" |
| 34 | #include "common/util.h" |
| 35 | |
| 36 | #include <dmedia/midi.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <bstring.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | //////////////////////////////////////// |
| 42 | // |
| 43 | // IRIX dmedia midi driver |
| 44 | // |
| 45 | //////////////////////////////////////// |
| 46 | |
| 47 | #define SEQ_MIDIPUTC 5 |
| 48 | |
| 49 | class MidiDriver_DMEDIA : public MidiDriver_MPU401 { |
| 50 | public: |
| 51 | MidiDriver_DMEDIA(); |
| 52 | int open(); |
| 53 | void close(); |
| 54 | void send(uint32 b); |
| 55 | void sysEx(const byte *msg, uint16 length); |
| 56 | |
| 57 | private: |
| 58 | bool _isOpen; |
| 59 | int _deviceNum; |
| 60 | char *_midiportName; |
| 61 | MDport _midiPort; |
| 62 | int _fd; |
| 63 | }; |
| 64 | |
| 65 | MidiDriver_DMEDIA::MidiDriver_DMEDIA() { |
| 66 | _isOpen = false; |
| 67 | _deviceNum = 0; |
| 68 | _midiportName = NULL; |
| 69 | } |
| 70 | |
| 71 | int MidiDriver_DMEDIA::open() { |
| 72 | int numinterfaces; |
| 73 | |
| 74 | if (_isOpen) |
| 75 | return MERR_ALREADY_OPEN; |
| 76 | _isOpen = true; |
| 77 | |
| 78 | warning("dmedia init"); |
| 79 | numinterfaces = mdInit(); |
| 80 | if (numinterfaces <= 0) { |
| 81 | fprintf(stderr,"No MIDI interfaces configured.\n"); |
| 82 | perror("Cannot initialize libmd for sound output"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | if (getenv("SCUMMVM_MIDIPORT")) { |
| 87 | _deviceNum = atoi(getenv("SCUMMVM_MIDIPORT")); |
| 88 | _midiportName = mdGetName(_deviceNum); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | _midiportName = mdGetName(0); |
| 93 | warning("SCUMMVM_MIDIPORT environment variable not set, using Port %s", _midiportName); |
| 94 | _deviceNum = 0; |
| 95 | } |
| 96 | |
| 97 | _midiPort = mdOpenOutPort(_midiportName); |
| 98 | if (!_midiPort) { |
| 99 | warning("Failed to open MIDI interface %s", _midiportName); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | _fd = mdGetFd(_midiPort); |
| 104 | if (!_fd) { |
| 105 | warning("Failed to aquire filehandle for MIDI port %s", _midiportName); |
| 106 | mdClosePort(_midiPort); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | mdSetStampMode(_midiPort, MD_NOSTAMP); /* don't use Timestamps */ |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | void MidiDriver_DMEDIA::close() { |
| 116 | mdClosePort(_midiPort); |
| 117 | _isOpen = false; |
| 118 | _deviceNum = 0; |
| 119 | _midiportName = NULL; |
| 120 | } |
| 121 | |
| 122 | void MidiDriver_DMEDIA::send(uint32 b) { |
| 123 | MDevent event; |
| 124 | byte status_byte = (b & 0x000000FF); |
| 125 | byte first_byte = (b & 0x0000FF00) >> 8; |
| 126 | byte second_byte = (b & 0x00FF0000) >> 16; |
| 127 | |
| 128 | |
| 129 | event.sysexmsg = NULL; |
| 130 | event.msg[0] = status_byte; |
| 131 | event.msg[1] = first_byte; |
| 132 | event.msg[2] = second_byte; |
| 133 | |
| 134 | switch (status_byte & 0xF0) { |
| 135 | case 0x80: // Note Off |
| 136 | case 0x90: // Note On |
| 137 | case 0xA0: // Polyphonic Aftertouch |
| 138 | case 0xB0: // Controller Change |
| 139 | case 0xE0: // Pitch Bending |
| 140 | event.msglen = 3; |
| 141 | break; |
| 142 | case 0xC0: // Programm Change |
| 143 | case 0xD0: // Monophonic Aftertouch |
| 144 | event.msglen = 2; |
| 145 | break; |
| 146 | default: |
| 147 | warning("DMediaMIDI driver encountered unsupported status byte: 0x%02x", status_byte); |
| 148 | event.msglen = 3; |
| 149 | break; |
| 150 | } |
| 151 | if (mdSend(_midiPort, &event, 1) != 1) { |
| 152 | warning("failed sending MIDI event (dump follows...)"); |
| 153 | warning("MIDI Event (len=%u):", event.msglen); |
| 154 | for (int i=0; i<event.msglen; i++) warning("%02x ",(int)event.msg[i]); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void MidiDriver_DMEDIA::sysEx (const byte *msg, uint16 length) { |
| 159 | MDevent event; |
| 160 | char buf [1024]; |
| 161 | |
| 162 | assert(length + 2 <= 256); |
| 163 | |
| 164 | memcpy(buf, msg, length); |
| 165 | buf[length] = MD_EOX; |
| 166 | event.sysexmsg = buf; |
| 167 | event.msglen = length; |
| 168 | event.msg[0] = MD_SYSEX; |
| 169 | event.msg[1] = 0; |
| 170 | event.msg[2] = 0; |
| 171 | |
| 172 | if (mdSend(_midiPort, &event, 1) != 1) { |
| 173 | fprintf(stderr,"failed sending MIDI SYSEX event (dump follows...)\n"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | MidiDriver *MidiDriver_DMEDIA_create() { |
| 178 | return new MidiDriver_DMEDIA(); |
| 179 | } |
| 180 | |
| 181 | #endif |