jdk/src/solaris/native/com/sun/media/sound/PLATFORM_API_LinuxOS_ALSA_MidiIn.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#define USE_ERROR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#define USE_TRACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#if USE_PLATFORM_MIDI_IN == TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <alsa/asoundlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "PlatformMidi.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "PLATFORM_API_LinuxOS_ALSA_MidiUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Helper methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static inline UINT32 packMessage(int status, int data1, int data2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    return ((status & 0xFF) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static void setShortMessage(MidiMessage* message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                            int status, int data1, int data2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    message->type = SHORT_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    message->data.s.packedMsg = packMessage(status, data1, data2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static void setRealtimeMessage(MidiMessage* message, int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    setShortMessage(message, status, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static void set14bitMessage(MidiMessage* message, int status, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    TRACE3("14bit value: %d, lsb: %d, msb: %d\n", value, value & 0x7F, (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    value &= 0x3FFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    TRACE3("14bit value (2): %d, lsb: %d, msb: %d\n", value, value & 0x7F, (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    setShortMessage(message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    value & 0x7F,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    (value >> 7) & 0x7F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * implementation of the platform-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * MIDI in functions declared in PlatformMidi.h
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
char* MIDI_IN_GetErrorStr(INT32 err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return (char*) getErrorStr(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
INT32 MIDI_IN_GetNumDevices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    TRACE0("MIDI_IN_GetNumDevices()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    return getMidiDeviceCount(SND_RAWMIDI_STREAM_INPUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
INT32 MIDI_IN_GetDeviceName(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int ret = getMidiDeviceName(SND_RAWMIDI_STREAM_INPUT, deviceIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
INT32 MIDI_IN_GetDeviceVendor(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int ret = getMidiDeviceVendor(deviceIndex, name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
INT32 MIDI_IN_GetDeviceDescription(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int ret = getMidiDeviceDescription(SND_RAWMIDI_STREAM_INPUT, deviceIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                       name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
INT32 MIDI_IN_GetDeviceVersion(INT32 deviceIndex, char *name, UINT32 nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int ret = getMidiDeviceVersion(deviceIndex, name, nameLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/*************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
INT32 MIDI_IN_OpenDevice(INT32 deviceIndex, MidiDeviceHandle** handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    INT32 ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    TRACE0("> MIDI_IN_OpenDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    ret = openMidiDevice(SND_RAWMIDI_STREAM_INPUT, deviceIndex, handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    TRACE1("< MIDI_IN_OpenDevice: returning %d\n", (int) ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
INT32 MIDI_IN_CloseDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    INT32 ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    TRACE0("> MIDI_IN_CloseDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    ret = closeMidiDevice(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    TRACE1("< MIDI_IN_CloseDevice: returning %d\n", (int) ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
INT32 MIDI_IN_StartDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    TRACE0("MIDI_IN_StartDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
INT32 MIDI_IN_StopDevice(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    TRACE0("MIDI_IN_StopDevice\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return MIDI_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
INT64 MIDI_IN_GetTimeStamp(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return getMidiTimestamp(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
/* read the next message from the queue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
MidiMessage* MIDI_IN_GetMessage(MidiDeviceHandle* handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    snd_seq_event_t alsa_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    MidiMessage* jdk_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    char buffer[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    TRACE0("> MIDI_IN_GetMessage\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (!handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ERROR0("< ERROR: MIDI_IN_GetMessage(): handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (!handle->deviceHandle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ERROR0("< ERROR: MIDI_IN_GetMessage(): native handle is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (!handle->platformData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        ERROR0("< ERROR: MIDI_IN_GetMessage(): platformData is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /* For MIDI In, the device is left in non blocking mode. So if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
       no data from the device, snd_rawmidi_read() returns with -11 (EAGAIN).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
       This results in jumping back to the Java layer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    while (TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        TRACE0("before snd_rawmidi_read()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        err = snd_rawmidi_read((snd_rawmidi_t*) handle->deviceHandle, buffer, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        TRACE0("after snd_rawmidi_read()\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (err != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            ERROR2("< ERROR: MIDI_IN_GetMessage(): snd_rawmidi_read() returned %d : %s\n", err, snd_strerror(err));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // printf("received byte: %d\n", buffer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        err = snd_midi_event_encode_byte((snd_midi_event_t*) handle->platformData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                         (int) buffer[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                         &alsa_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (err == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ERROR1("< ERROR: MIDI_IN_GetMessage(): snd_midi_event_encode_byte() returned %d\n", err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    jdk_message = (MidiMessage*) calloc(sizeof(MidiMessage), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (!jdk_message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ERROR0("< ERROR: MIDI_IN_GetMessage(): out of memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // TODO: tra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    switch (alsa_message.type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    case SND_SEQ_EVENT_NOTEON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    case SND_SEQ_EVENT_NOTEOFF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    case SND_SEQ_EVENT_KEYPRESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        status = (alsa_message.type == SND_SEQ_EVENT_KEYPRESS) ? 0xA0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            (alsa_message.type == SND_SEQ_EVENT_NOTEON) ? 0x90 : 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        status |= alsa_message.data.note.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        alsa_message.data.note.note,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        alsa_message.data.note.velocity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    case SND_SEQ_EVENT_CONTROLLER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        status = 0xB0 | alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        alsa_message.data.control.param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    case SND_SEQ_EVENT_PGMCHANGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    case SND_SEQ_EVENT_CHANPRESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        status = (alsa_message.type == SND_SEQ_EVENT_PGMCHANGE) ? 0xC0 : 0xD0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        status |= alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        setShortMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        alsa_message.data.control.value, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    case SND_SEQ_EVENT_PITCHBEND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        status = 0xE0 | alsa_message.data.control.channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // $$mp 2003-09-23:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // possible hack to work around a bug in ALSA. Necessary for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // ALSA 0.9.2. May be fixed in newer versions of ALSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // alsa_message.data.control.value ^= 0x2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // TRACE1("pitchbend value: %d\n", alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        set14bitMessage(jdk_message, status,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        /* System exclusive messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    case SND_SEQ_EVENT_SYSEX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        jdk_message->type = LONG_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        jdk_message->data.l.size = alsa_message.data.ext.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        jdk_message->data.l.data = malloc(alsa_message.data.ext.len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (jdk_message->data.l.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ERROR0("< ERROR: MIDI_IN_GetMessage(): out of memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            free(jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            jdk_message = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            memcpy(jdk_message->data.l.data, alsa_message.data.ext.ptr, alsa_message.data.ext.len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /* System common messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    case SND_SEQ_EVENT_QFRAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        setShortMessage(jdk_message, 0xF1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        alsa_message.data.control.value & 0x7F, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    case SND_SEQ_EVENT_SONGPOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        set14bitMessage(jdk_message, 0xF2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        alsa_message.data.control.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    case SND_SEQ_EVENT_SONGSEL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        setShortMessage(jdk_message, 0xF3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        alsa_message.data.control.value & 0x7F, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    case SND_SEQ_EVENT_TUNE_REQUEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        setRealtimeMessage(jdk_message, 0xF6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /* System realtime messages */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    case SND_SEQ_EVENT_CLOCK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        setRealtimeMessage(jdk_message, 0xF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    case SND_SEQ_EVENT_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        setRealtimeMessage(jdk_message, 0xFA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    case SND_SEQ_EVENT_CONTINUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        setRealtimeMessage(jdk_message, 0xFB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    case SND_SEQ_EVENT_STOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        setRealtimeMessage(jdk_message, 0xFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    case SND_SEQ_EVENT_SENSING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        setRealtimeMessage(jdk_message, 0xFE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    case SND_SEQ_EVENT_RESET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        setRealtimeMessage(jdk_message, 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        ERROR0("< ERROR: MIDI_IN_GetMessage(): unhandled ALSA MIDI message type\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        free(jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        jdk_message = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // set timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    if (jdk_message != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        jdk_message->timestamp = getMidiTimestamp(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    TRACE1("< MIDI_IN_GetMessage: returning %p\n", jdk_message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    return jdk_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
void MIDI_IN_ReleaseMessage(MidiDeviceHandle* handle, MidiMessage* msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ERROR0("< ERROR: MIDI_IN_ReleaseMessage(): message is NULL\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    if (msg->type == LONG_MESSAGE && msg->data.l.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        free(msg->data.l.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    free(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
#endif /* USE_PLATFORM_MIDI_IN */