4632N/A/*
4632N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#define USE_ERROR
4632N/A#define USE_TRACE
4632N/A
4632N/A#if USE_PLATFORM_MIDI_OUT == TRUE
4632N/A
4632N/A#include <alsa/asoundlib.h>
4632N/A#include "PlatformMidi.h"
4632N/A#include "PLATFORM_API_BsdOS_ALSA_MidiUtils.h"
4632N/A
4632N/A
4632N/A
4632N/Astatic int CHANNEL_MESSAGE_LENGTH[] = {
4632N/A -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 3, 3, 2, 2, 3 };
4632N/A/* 8x 9x Ax Bx Cx Dx Ex */
4632N/A
4632N/Astatic int SYSTEM_MESSAGE_LENGTH[] = {
4632N/A -1, 2, 3, 2, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1 };
4632N/A/* F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF */
4632N/A
4632N/A
4632N/A// the returned length includes the status byte.
4632N/A// for illegal messages, -1 is returned.
4632N/Astatic int getShortMessageLength(int status) {
4632N/A int dataLength = 0;
4632N/A if (status < 0xF0) { // channel voice message
4632N/A dataLength = CHANNEL_MESSAGE_LENGTH[(status >> 4) & 0xF];
4632N/A } else {
4632N/A dataLength = SYSTEM_MESSAGE_LENGTH[status & 0xF];
4632N/A }
4632N/A return dataLength;
4632N/A}
4632N/A
4632N/A
4632N/A/*
4632N/A * implementation of the platform-dependent
4632N/A * MIDI out functions declared in PlatformMidi.h
4632N/A */
4632N/Achar* MIDI_OUT_GetErrorStr(INT32 err) {
4632N/A return (char*) getErrorStr(err);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_GetNumDevices() {
4632N/A TRACE0("MIDI_OUT_GetNumDevices()\n");
4632N/A return getMidiDeviceCount(SND_RAWMIDI_STREAM_OUTPUT);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_GetDeviceName(INT32 deviceIndex, char *name, UINT32 nameLength) {
4632N/A TRACE0("MIDI_OUT_GetDeviceName()\n");
4632N/A return getMidiDeviceName(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex,
4632N/A name, nameLength);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_GetDeviceVendor(INT32 deviceIndex, char *name, UINT32 nameLength) {
4632N/A TRACE0("MIDI_OUT_GetDeviceVendor()\n");
4632N/A return getMidiDeviceVendor(deviceIndex, name, nameLength);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_GetDeviceDescription(INT32 deviceIndex, char *name, UINT32 nameLength) {
4632N/A TRACE0("MIDI_OUT_GetDeviceDescription()\n");
4632N/A return getMidiDeviceDescription(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex,
4632N/A name, nameLength);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_GetDeviceVersion(INT32 deviceIndex, char *name, UINT32 nameLength) {
4632N/A TRACE0("MIDI_OUT_GetDeviceVersion()\n");
4632N/A return getMidiDeviceVersion(deviceIndex, name, nameLength);
4632N/A}
4632N/A
4632N/A
4632N/A/* *************************** MidiOutDevice implementation *************** */
4632N/A
4632N/AINT32 MIDI_OUT_OpenDevice(INT32 deviceIndex, MidiDeviceHandle** handle) {
4632N/A TRACE1("MIDI_OUT_OpenDevice(): deviceIndex: %d\n", (int) deviceIndex);
4632N/A return openMidiDevice(SND_RAWMIDI_STREAM_OUTPUT, deviceIndex, handle);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_CloseDevice(MidiDeviceHandle* handle) {
4632N/A TRACE0("MIDI_OUT_CloseDevice()\n");
4632N/A return closeMidiDevice(handle);
4632N/A}
4632N/A
4632N/A
4632N/AINT64 MIDI_OUT_GetTimeStamp(MidiDeviceHandle* handle) {
4632N/A return getMidiTimestamp(handle);
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_SendShortMessage(MidiDeviceHandle* handle, UINT32 packedMsg,
4632N/A UINT32 timestamp) {
4632N/A int err;
4632N/A int status;
4632N/A int data1;
4632N/A int data2;
4632N/A char buffer[3];
4632N/A
4632N/A TRACE2("> MIDI_OUT_SendShortMessage() %x, time: %u\n", packedMsg, (unsigned int) timestamp);
4632N/A if (!handle) {
4632N/A ERROR0("< ERROR: MIDI_OUT_SendShortMessage(): handle is NULL\n");
4632N/A return MIDI_INVALID_HANDLE;
4632N/A }
4632N/A if (!handle->deviceHandle) {
4632N/A ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): native handle is NULL\n");
4632N/A return MIDI_INVALID_HANDLE;
4632N/A }
4632N/A status = (packedMsg & 0xFF);
4632N/A buffer[0] = (char) status;
4632N/A buffer[1] = (char) ((packedMsg >> 8) & 0xFF);
4632N/A buffer[2] = (char) ((packedMsg >> 16) & 0xFF);
4632N/A TRACE4("status: %d, data1: %d, data2: %d, length: %d\n", (int) buffer[0], (int) buffer[1], (int) buffer[2], getShortMessageLength(status));
4632N/A err = snd_rawmidi_write((snd_rawmidi_t*) handle->deviceHandle, buffer, getShortMessageLength(status));
4632N/A if (err < 0) {
4632N/A ERROR1(" ERROR: MIDI_OUT_SendShortMessage(): snd_rawmidi_write() returned %d\n", err);
4632N/A }
4632N/A
4632N/A TRACE0("< MIDI_OUT_SendShortMessage()\n");
4632N/A return err;
4632N/A}
4632N/A
4632N/A
4632N/AINT32 MIDI_OUT_SendLongMessage(MidiDeviceHandle* handle, UBYTE* data,
4632N/A UINT32 size, UINT32 timestamp) {
4632N/A int err;
4632N/A
4632N/A TRACE2("> MIDI_OUT_SendLongMessage() size %u, time: %u\n", (unsigned int) size, (unsigned int) timestamp);
4632N/A if (!handle) {
4632N/A ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): handle is NULL\n");
4632N/A return MIDI_INVALID_HANDLE;
4632N/A }
4632N/A if (!handle->deviceHandle) {
4632N/A ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): native handle is NULL\n");
4632N/A return MIDI_INVALID_HANDLE;
4632N/A }
4632N/A if (!data) {
4632N/A ERROR0("< ERROR: MIDI_OUT_SendLongMessage(): data is NULL\n");
4632N/A return MIDI_INVALID_HANDLE;
4632N/A }
4632N/A err = snd_rawmidi_write((snd_rawmidi_t*) handle->deviceHandle,
4632N/A data, size);
4632N/A if (err < 0) {
4632N/A ERROR1(" ERROR: MIDI_OUT_SendLongMessage(): snd_rawmidi_write() returned %d\n", err);
4632N/A }
4632N/A
4632N/A TRACE0("< MIDI_OUT_SendLongMessage()\n");
4632N/A return err;
4632N/A}
4632N/A
4632N/A
4632N/A#endif /* USE_PLATFORM_MIDI_OUT */