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