1 /* 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #define USE_ERROR 27 #define USE_TRACE 28 29 #if USE_PLATFORM_MIDI_OUT == TRUE 30 31 #include <alsa/asoundlib.h> 32 #include "PlatformMidi.h" 33 #include "PLATFORM_API_BsdOS_ALSA_MidiUtils.h" 34 35 36 37 static int CHANNEL_MESSAGE_LENGTH[] = { 38 -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 3, 3, 2, 2, 3 }; 39 /* 8x 9x Ax Bx Cx Dx Ex */ 40 41 static int SYSTEM_MESSAGE_LENGTH[] = { 42 -1, 2, 3, 2, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1 }; 43 /* F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF */ 44 45 46 // the returned length includes the status byte. 47 // for illegal messages, -1 is returned. 48 static int getShortMessageLength(int status) { 49 int dataLength = 0; 50 if (status < 0xF0) { // channel voice message 51 dataLength = CHANNEL_MESSAGE_LENGTH[(status >> 4) & 0xF]; 52 } else { 53 dataLength = SYSTEM_MESSAGE_LENGTH[status & 0xF]; 54 } 55 return dataLength; 56 } 57 58 59 /* 60 * implementation of the platform-dependent 61 * MIDI out functions declared in PlatformMidi.h 62 */ 63 char* MIDI_OUT_GetErrorStr(INT32 err) { 64 return (char*) getErrorStr(err); 65 } 66 67 68 INT32 MIDI_OUT_GetNumDevices() { 69 TRACE0("MIDI_OUT_GetNumDevices()\n"); 70 return getMidiDeviceCount(SND_RAWMIDI_STREAM_OUTPUT); 71 } 72 73 74 INT32 MIDI_OUT_GetDeviceName(INT32 deviceIndex, char *name, UINT32 nameLength) { 75 TRACE0("MIDI_OUT_GetDeviceName()\n"); 76 return getMidiDeviceName(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex, 77 name, nameLength); 78 } 79 80 81 INT32 MIDI_OUT_GetDeviceVendor(INT32 deviceIndex, char *name, UINT32 nameLength) { 82 TRACE0("MIDI_OUT_GetDeviceVendor()\n"); 83 return getMidiDeviceVendor(deviceIndex, name, nameLength); 84 } 85 86 87 INT32 MIDI_OUT_GetDeviceDescription(INT32 deviceIndex, char *name, UINT32 nameLength) { 88 TRACE0("MIDI_OUT_GetDeviceDescription()\n"); 89 return getMidiDeviceDescription(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex, 90 name, nameLength); 91 } 92 93 94 INT32 MIDI_OUT_GetDeviceVersion(INT32 deviceIndex, char *name, UINT32 nameLength) { 95 TRACE0("MIDI_OUT_GetDeviceVersion()\n"); 96 return getMidiDeviceVersion(deviceIndex, name, nameLength); 97 } 98 99 100 /* *************************** MidiOutDevice implementation *************** */ 101 102 INT32 MIDI_OUT_OpenDevice(INT32 deviceIndex, MidiDeviceHandle** handle) { 103 TRACE1("MIDI_OUT_OpenDevice(): deviceIndex: %d\n", (int) deviceIndex); 104 return openMidiDevice(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex, handle); 105 } 106 107 108 INT32 MIDI_OUT_CloseDevice(MidiDeviceHandle* handle) { 109 TRACE0("MIDI_OUT_CloseDevice()\n"); 110 return closeMidiDevice(handle); 111 } 112 113 114 INT64 MIDI_OUT_GetTimeStamp(MidiDeviceHandle* handle) { 115 return getMidiTimestamp(handle); 116 } 117 118 119 INT32 MIDI_OUT_SendShortMessage(MidiDeviceHandle* handle, UINT32 packedMsg, 120 UINT32 timestamp) { 121 int err; 122 int status; 123 int data1; 124 int data2; 125 char buffer[3]; 126 127 TRACE2("> MIDI_OUT_SendShortMessage() %x, time: %u\n", packedMsg, (unsigned int) timestamp); 128 if (!handle) { 129 ERROR0("< ERROR: MIDI_OUT_SendShortMessage(): handle is NULL\n"); 130 return MIDI_INVALID_HANDLE; 131 } 132 if (!handle->deviceHandle) { 133 ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): native handle is NULL\n"); 134 return MIDI_INVALID_HANDLE; 135 } 136 status = (packedMsg & 0xFF); 137 buffer[0] = (char) status; 138 buffer[1] = (char) ((packedMsg >> 8) & 0xFF); 139 buffer[2] = (char) ((packedMsg >> 16) & 0xFF); 140 TRACE4("status: %d, data1: %d, data2: %d, length: %d\n", (int) buffer[0], (int) buffer[1], (int) buffer[2], getShortMessageLength(status)); 141 err = snd_rawmidi_write((snd_rawmidi_t*) handle->deviceHandle, buffer, getShortMessageLength(status)); 142 if (err < 0) { 143 ERROR1(" ERROR: MIDI_OUT_SendShortMessage(): snd_rawmidi_write() returned %d\n", err); 144 } 145 146 TRACE0("< MIDI_OUT_SendShortMessage()\n"); 147 return err; 148 } 149 150 151 INT32 MIDI_OUT_SendLongMessage(MidiDeviceHandle* handle, UBYTE* data, 152 UINT32 size, UINT32 timestamp) { 153 int err; 154 155 TRACE2("> MIDI_OUT_SendLongMessage() size %u, time: %u\n", (unsigned int) size, (unsigned int) timestamp); 156 if (!handle) { 157 ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): handle is NULL\n"); 158 return MIDI_INVALID_HANDLE; 159 } 160 if (!handle->deviceHandle) { 161 ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): native handle is NULL\n"); 162 return MIDI_INVALID_HANDLE; 163 } 164 if (!data) { 165 ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): data is NULL\n"); 166 return MIDI_INVALID_HANDLE; 167 } 168 err = snd_rawmidi_write((snd_rawmidi_t*) handle->deviceHandle, 169 data, size); 170 if (err < 0) { 171 ERROR1(" ERROR: MIDI_OUT_SendLongMessage(): snd_rawmidi_write() returned %d\n", err); 172 } 173 174 TRACE0("< MIDI_OUT_SendLongMessage()\n"); 175 return err; 176 } 177 178 179 #endif /* USE_PLATFORM_MIDI_OUT */