4639N/A/*
4639N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
4639N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4639N/A *
4639N/A * This code is free software; you can redistribute it and/or modify it
4639N/A * under the terms of the GNU General Public License version 2 only, as
4639N/A * published by the Free Software Foundation. Oracle designates this
4639N/A * particular file as subject to the "Classpath" exception as provided
4639N/A * by Oracle in the LICENSE file that accompanied this code.
4639N/A *
4639N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4639N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4639N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4639N/A * version 2 for more details (a copy is included in the LICENSE file that
4639N/A * accompanied this code).
4639N/A *
4639N/A * You should have received a copy of the GNU General Public License version
4639N/A * 2 along with this work; if not, write to the Free Software Foundation,
4639N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4639N/A *
4639N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4639N/A * or visit www.oracle.com if you need additional information or have any
4639N/A * questions.
4639N/A */
4639N/A
4639N/A//#define USE_ERROR
4639N/A//#define USE_TRACE
4639N/A
4639N/A#if USE_PLATFORM_MIDI_OUT == TRUE
4639N/A
4639N/A#include "PLATFORM_API_MacOSX_MidiUtils.h"
4639N/A
4639N/Achar* MIDI_OUT_GetErrorStr(INT32 err) {
4639N/A return (char *) MIDI_Utils_GetErrorMsg((int) err);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_GetNumDevices() {
4639N/A return MIDI_Utils_GetNumDevices(MIDI_OUT);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
4639N/A return MIDI_Utils_GetDeviceName(MIDI_OUT, deviceID, name, nameLength);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_GetDeviceVendor(INT32 deviceID, char *name, UINT32 nameLength) {
4639N/A return MIDI_Utils_GetDeviceVendor(MIDI_OUT, deviceID, name, nameLength);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLength) {
4639N/A return MIDI_Utils_GetDeviceDescription(MIDI_OUT, deviceID, name, nameLength);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
4639N/A return MIDI_Utils_GetDeviceVersion(MIDI_OUT, deviceID, name, nameLength);
4639N/A}
4639N/A
4639N/A
4639N/A/* *************************** MidiOutDevice implementation ***************************************** */
4639N/A
4639N/AINT32 MIDI_OUT_OpenDevice(INT32 deviceID, MidiDeviceHandle** handle) {
4639N/A TRACE1("MIDI_OUT_OpenDevice: deviceID: %d\n", (int) deviceID);
4639N/A /* queue sizes are ignored for MIDI_OUT only (uses STREAMS) */
4639N/A return MIDI_Utils_OpenDevice(MIDI_OUT, deviceID, (MacMidiDeviceHandle**) handle, 0, 0, 0);
4639N/A}
4639N/A
4639N/AINT32 MIDI_OUT_CloseDevice(MidiDeviceHandle* handle) {
4639N/A TRACE0("MIDI_OUT_CloseDevice\n");
4639N/A
4639N/A // issue a "SUSTAIN OFF" message to each MIDI channel, 0 to 15.
4639N/A // "CONTROL CHANGE" is 176, "SUSTAIN CONTROLLER" is 64, and the value is 0.
4639N/A // $$fb 2002-04-04: It is responsability of the application developer to
4639N/A // leave the device in a consistent state. So I put this in comments
4639N/A /*
4639N/A for (channel = 0; channel < 16; channel++)
4639N/A MIDI_OUT_SendShortMessage(deviceHandle, (unsigned char)(176 + channel),
4639N/A (unsigned char)64, (unsigned char)0, (UINT32)-1);
4639N/A */
4639N/A return MIDI_Utils_CloseDevice((MacMidiDeviceHandle*) handle);
4639N/A}
4639N/A
4639N/A
4639N/AINT64 MIDI_OUT_GetTimeStamp(MidiDeviceHandle* handle) {
4639N/A return MIDI_Utils_GetTimeStamp((MacMidiDeviceHandle*) handle);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_SendShortMessage(MidiDeviceHandle* handle, UINT32 packedMsg, UINT32 timestamp) {
4639N/A OSStatus err = noErr;
4639N/A
4639N/A TRACE2("> MIDI_OUT_SendShortMessage %x, time: %d\n", (uint) packedMsg, (int) timestamp);
4639N/A if (!handle) {
4639N/A ERROR0("< ERROR: MIDI_OUT_SendShortMessage: handle is NULL\n");
4639N/A return MIDI_INVALID_HANDLE;
4639N/A }
4639N/A
4639N/A MacMidiDeviceHandle* macHandle = (MacMidiDeviceHandle*) handle;
4639N/A UInt8 mBuffers[100];
4639N/A MIDIPacketList* packetList = (MIDIPacketList*) mBuffers;
4639N/A MIDIPacket* packet;
4639N/A UINT32 nData;
4639N/A Byte data[3] = {packedMsg & 0xFF, (packedMsg >> 8) & 0xFF, (packedMsg >> 16) & 0xFF};
4639N/A bool byteIsInvalid = FALSE;
4639N/A
4639N/A packet = MIDIPacketListInit(packetList);
4639N/A switch (data[0] & 0xF0) {
4639N/A case 0x80: // Note off
4639N/A case 0x90: // Note on
4639N/A case 0xA0: // Aftertouch
4639N/A case 0xB0: // Controller
4639N/A case 0xE0: // Pitch wheel
4639N/A nData = 3;
4639N/A break;
4639N/A
4639N/A case 0xC0: // Program change
4639N/A case 0xD0: // Channel pressure
4639N/A nData = 2;
4639N/A break;
4639N/A
4639N/A case 0xF0: {
4639N/A // System common message
4639N/A switch (data[0]) {
4639N/A case 0xF0:
4639N/A case 0xF7:
4639N/A // System exclusive
4639N/A fprintf(stderr, "%s: %d->internal error: sysex message status=0x%X while sending short message\n",
4639N/A __FILE__, __LINE__, data[0]);
4639N/A byteIsInvalid = TRUE;
4639N/A break;
4639N/A
4639N/A case 0xF1: // MTC quarter frame message
4639N/A //fprintf(stderr, ">>>MIDI_OUT_SendShortMessage: MTC quarter frame message....\n");
4639N/A nData = 2;
4639N/A break;
4639N/A case 0xF3: // Song select
4639N/A //fprintf(stderr, ">>>MIDI_OUT_SendShortMessage: Song select....\n");
4639N/A nData = 2;
4639N/A break;
4639N/A
4639N/A case 0xF2: // Song position pointer
4639N/A //fprintf(stderr, ">>>MIDI_OUT_SendShortMessage: Song position pointer....\n");
4639N/A nData = 3;
4639N/A break;
4639N/A
4639N/A case 0xF6: // Tune request
4639N/A //fprintf(stderr, ">>>MIDI_OUT_SendShortMessage: Tune request....\n");
4639N/A nData = 1;
4639N/A break;
4639N/A
4639N/A default:
4639N/A // Invalid message
4639N/A fprintf(stderr, "%s: %d->Invalid message: message status=0x%X while sending short message\n",
4639N/A __FILE__, __LINE__, data[0]);
4639N/A byteIsInvalid = TRUE;
4639N/A break;
4639N/A }
4639N/A break;
4639N/A }
4639N/A
4639N/A default:
4639N/A // This can't happen, but handle it anyway.
4639N/A fprintf(stderr, "%s: %d->Invalid message: message status=0x%X while sending short message\n",
4639N/A __FILE__, __LINE__, data[0]);
4639N/A byteIsInvalid = TRUE;
4639N/A break;
4639N/A }
4639N/A
4639N/A if (byteIsInvalid) return -1;
4639N/A
4639N/A MIDIPacketListAdd(packetList, sizeof(mBuffers), packet, 0, nData, data);
4639N/A err = MIDISend(macHandle->port, (MIDIEndpointRef) (intptr_t) handle->deviceHandle, packetList);
4639N/A
4639N/A MIDI_CHECK_ERROR;
4639N/A TRACE0("< MIDI_OUT_SendShortMessage\n");
4639N/A return (err == noErr ? MIDI_SUCCESS : -1);
4639N/A}
4639N/A
4639N/A
4639N/AINT32 MIDI_OUT_SendLongMessage(MidiDeviceHandle* handle, UBYTE* data, UINT32 size, UINT32 timestamp) {
4639N/A OSStatus err = noErr;
4639N/A
4639N/A TRACE2("> MIDI_OUT_SendLongMessage size %d, time: %d\n", (int) size, (int) timestamp);
4639N/A if (!handle || !data) {
4639N/A ERROR0("< ERROR: MIDI_OUT_SendLongMessage: handle, or data is NULL\n");
4639N/A return MIDI_INVALID_HANDLE;
4639N/A }
4639N/A if (size == 0) {
4639N/A return MIDI_SUCCESS;
4639N/A }
4639N/A
4639N/A MacMidiDeviceHandle* macHandle = (MacMidiDeviceHandle*) handle;
4639N/A UInt8 mBuffers[8196];
4639N/A MIDIPacketList* packetList = (MIDIPacketList*) mBuffers;
4639N/A MIDIPacket* packet = NULL;
4639N/A UINT32 remaining = size;
4639N/A UINT32 increment = 512;
4639N/A UINT32 nData;
4639N/A
4639N/A handle->isWaiting = TRUE;
4639N/A
4639N/A while (remaining > 0) {
4639N/A
4639N/A if (packet == NULL) {
4639N/A packet = MIDIPacketListInit(packetList);
4639N/A }
4639N/A
4639N/A if (remaining > increment) {
4639N/A nData = increment;
4639N/A } else {
4639N/A nData = remaining;
4639N/A }
4639N/A
4639N/A // Copies the bytes to our current packet.
4639N/A if ((packet = MIDIPacketListAdd(packetList, sizeof(mBuffers), packet, 0, nData, (const Byte*) data)) == NULL) {
4639N/A // Packet list is full, send it.
4639N/A err = MIDISend(macHandle->port, (MIDIEndpointRef) (intptr_t) handle->deviceHandle, packetList);
4639N/A if (err != noErr) {
4639N/A break;
4639N/A }
4639N/A } else {
4639N/A // Moves the data pointer to the next segment.
4639N/A data += nData;
4639N/A remaining -= nData;
4639N/A packet = MIDIPacketNext(packet);
4639N/A }
4639N/A }
4639N/A
4639N/A MIDI_CHECK_ERROR;
4639N/A handle->isWaiting = FALSE;
4639N/A TRACE0("< MIDI_OUT_SendLongMessage\n");
4639N/A return (err == noErr ? MIDI_SUCCESS : -1);
4639N/A}
4639N/A
4639N/A#endif /* USE_PLATFORM_MIDI_OUT */