PLATFORM_API_WinOS_MidiIn.cpp revision 2362
829N/A/*
829N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
829N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
829N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
829N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
829N/A * or visit www.oracle.com if you need additional information or have any
829N/A * questions.
829N/A */
829N/A
829N/A#define USE_ERROR
829N/A#define USE_TRACE
829N/A
829N/A/* include Java Sound specific headers as C code */
829N/Aextern "C" {
829N/A#include "PLATFORM_API_WinOS_Util.h"
829N/A}
829N/A
829N/A#if USE_PLATFORM_MIDI_IN == TRUE
829N/A
829N/A#ifdef USE_ERROR
829N/A#include <stdio.h>
829N/A#define MIDIIN_CHECK_ERROR { \
829N/A if (err != MMSYSERR_NOERROR) \
829N/A ERROR3("MIDI IN Error in %s:%d : %s\n", __FILE__, __LINE__, MIDI_IN_GetErrorStr((INT32) err)); \
829N/A }
829N/A#else
829N/A#define MIDIIN_CHECK_ERROR
829N/A#endif
829N/A
829N/A/*
829N/A * Callback from the MIDI device for all messages.
829N/A */
829N/A//$$fb dwParam1 holds a pointer for long messages. How can that be a DWORD then ???
829N/Avoid CALLBACK MIDI_IN_PutMessage( HMIDIIN hMidiIn, UINT wMsg, UINT_PTR dwInstance, UINT_PTR dwParam1, UINT_PTR dwParam2 ) {
829N/A
829N/A MidiDeviceHandle* handle = (MidiDeviceHandle*) dwInstance;
829N/A
829N/A TRACE3("> MIDI_IN_PutMessage, hMidiIn: %x, wMsg: %x, dwInstance: %x\n", hMidiIn, wMsg, dwInstance);
829N/A TRACE2(" dwParam1: %x, dwParam2: %x\n", dwParam1, dwParam2);
829N/A
829N/A switch(wMsg) {
829N/A
829N/A case MIM_OPEN:
829N/A TRACE0("< MIDI_IN_PutMessage: MIM_OPEN\n");
829N/A break;
829N/A
829N/A case MIM_CLOSE:
829N/A TRACE0("< MIDI_IN_PutMessage: MIM_CLOSE\n");
829N/A break;
829N/A
829N/A case MIM_MOREDATA:
829N/A case MIM_DATA:
829N/A TRACE3(" MIDI_IN_PutMessage: MIM_MOREDATA or MIM_DATA. status=%x data1=%x data2=%x\n",
829N/A dwParam1 & 0xFF, (dwParam1 & 0xFF00)>>8, (dwParam1 & 0xFF0000)>>16);
829N/A if (handle!=NULL && handle->queue!=NULL && handle->platformData) {
829N/A MIDI_QueueAddShort(handle->queue,
829N/A // queue stores packedMsg in big endian
829N/A //(dwParam1 << 24) | ((dwParam1 << 8) & 0xFF0000) | ((dwParam1 >> 8) & 0xFF00),
829N/A (UINT32) dwParam1,
829N/A // queue uses microseconds
829N/A ((INT64) dwParam2)*1000,
829N/A // overwrite if queue is full
829N/A TRUE);
829N/A SetEvent((HANDLE) handle->platformData);
829N/A }
829N/A TRACE0("< MIDI_IN_PutMessage\n");
829N/A break;
829N/A
829N/A case MIM_LONGDATA:
829N/A TRACE1(" MIDI_IN_PutMessage: MIM_LONGDATA (%d bytes recorded)\n", (int) (((MIDIHDR*) dwParam1)->dwBytesRecorded));
829N/A if (handle!=NULL && handle->queue!=NULL && handle->platformData) {
829N/A MIDIHDR* hdr = (MIDIHDR*) dwParam1;
829N/A TRACE2(" MIDI_IN_PutMessage: Adding to queue: index %d, %d bytes\n", (INT32) hdr->dwUser, hdr->dwBytesRecorded);
829N/A MIDI_QueueAddLong(handle->queue,
829N/A (UBYTE*) hdr->lpData,
829N/A (UINT32) hdr->dwBytesRecorded,
829N/A // sysex buffer index
829N/A (INT32) hdr->dwUser,
829N/A // queue uses microseconds
829N/A ((INT64) dwParam2)*1000,
829N/A // overwrite if queue is full
829N/A TRUE);
829N/A SetEvent((HANDLE) handle->platformData);
829N/A }
829N/A TRACE0("< MIDI_IN_PutMessage\n");
829N/A break;
829N/A
829N/A case MIM_ERROR:
829N/A ERROR0("< MIDI_IN_PutMessage: MIM_ERROR!\n");
829N/A break;
829N/A
829N/A case MIM_LONGERROR:
829N/A if (dwParam1 != 0) {
829N/A MIDIHDR* hdr = (MIDIHDR*) dwParam1;
829N/A#ifdef USE_TRACE
829N/A if (hdr->dwBytesRecorded > 0) {
829N/A TRACE2(" MIDI_IN_PutMessage: MIM_LONGERROR! recorded: %d bytes with status 0x%2x\n",
829N/A hdr->dwBytesRecorded, (int) (*((UBYTE*) hdr->lpData)));
829N/A }
829N/A#endif
829N/A // re-add hdr to device query
829N/A hdr->dwBytesRecorded = 0;
829N/A midiInAddBuffer((HMIDIIN)handle->deviceHandle, hdr, sizeof(MIDIHDR));
829N/A }
829N/A ERROR0("< MIDI_IN_PutMessage: MIM_LONGERROR!\n");
829N/A break;
829N/A
829N/A default:
829N/A ERROR1("< MIDI_IN_PutMessage: ERROR unknown message %d!\n", wMsg);
829N/A break;
829N/A
829N/A } // switch (wMsg)
829N/A}
829N/A
829N/A
829N/A/*
829N/A** data/routines for opening MIDI input (MidiIn) device by separate thread
829N/A** (joint into MidiIn_OpenHelper class)
829N/A** see 6415669 - MidiIn device stops work and crushes JVM after exiting
829N/A** from thread that has open the device (it looks like WinMM bug).
829N/A*/
829N/Aclass MidiIn_OpenHelper {
829N/Apublic:
829N/A /* opens MidiIn device */
829N/A static MMRESULT midiInOpen(INT32 deviceID, MidiDeviceHandle* handle);
829N/A /* checks for initialization success */
829N/A static inline BOOL isInitialized() { return data.threadHandle != NULL; }
829N/Aprotected:
829N/A MidiIn_OpenHelper() {} // no need to create an instance
829N/A
829N/A /* data class */
829N/A class Data {
829N/A public:
829N/A Data();
829N/A ~Data();
829N/A // public data to access from parent class
829N/A CRITICAL_SECTION crit_sect;
829N/A volatile HANDLE threadHandle;
829N/A volatile HANDLE doEvent; // event to resume thread
829N/A volatile HANDLE doneEvent; // processing has been completed
829N/A volatile MMRESULT err; // processing result
829N/A // data to process; (handle == null) is command to thread terminating
829N/A volatile INT32 deviceID;
829N/A volatile MidiDeviceHandle* handle;
829N/A } static data;
829N/A
829N/A /* StartThread function */
829N/A static DWORD WINAPI __stdcall ThreadProc(void *param);
829N/A};
829N/A
829N/A/* MidiIn_OpenHelper class implementation
829N/A*/
829N/AMidiIn_OpenHelper::Data MidiIn_OpenHelper::data;
829N/A
829N/AMidiIn_OpenHelper::Data::Data() {
829N/A threadHandle = NULL;
829N/A ::InitializeCriticalSection(&crit_sect);
829N/A doEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
829N/A doneEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
829N/A if (doEvent != NULL && doneEvent != NULL)
829N/A threadHandle = ::CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
829N/A}
829N/A
829N/AMidiIn_OpenHelper::Data::~Data() {
829N/A ::EnterCriticalSection(&crit_sect);
829N/A if (threadHandle != NULL) {
829N/A // terminate thread
829N/A handle = NULL;
829N/A ::SetEvent(doEvent);
829N/A ::CloseHandle(threadHandle);
829N/A threadHandle = NULL;
829N/A }
829N/A ::LeaveCriticalSection(&crit_sect);
829N/A // won't delete doEvent/doneEvent/crit_sect
829N/A // - Windows will do during process shutdown
829N/A}
829N/A
829N/ADWORD WINAPI __stdcall MidiIn_OpenHelper::ThreadProc(void *param) {
829N/A while (1) {
829N/A // wait for something to do
829N/A ::WaitForSingleObject(data.doEvent, INFINITE);
829N/A if (data.handle == NULL) {
829N/A // (data.handle == NULL) is a signal to terminate thread
829N/A break;
829N/A }
829N/A
829N/A data.err = ::midiInOpen((HMIDIIN*)&(data.handle->deviceHandle),
829N/A data.deviceID, (UINT_PTR)&(MIDI_IN_PutMessage),
829N/A (UINT_PTR)data.handle,
829N/A CALLBACK_FUNCTION|MIDI_IO_STATUS);
829N/A
829N/A ::SetEvent(data.doneEvent);
829N/A }
829N/A return 0;
829N/A}
829N/A
829N/AMMRESULT MidiIn_OpenHelper::midiInOpen(INT32 deviceID, MidiDeviceHandle* handle) {
829N/A MMRESULT err;
829N/A ::EnterCriticalSection(&data.crit_sect);
829N/A if (!isInitialized()) {
829N/A ::LeaveCriticalSection(&data.crit_sect);
829N/A return MMSYSERR_ERROR;
829N/A }
829N/A data.deviceID = deviceID;
1991N/A data.handle = handle;
1991N/A ::SetEvent(data.doEvent);
1991N/A ::WaitForSingleObject(data.doneEvent, INFINITE);
1991N/A err = data.err;
1991N/A ::LeaveCriticalSection(&data.crit_sect);
1991N/A return err;
1991N/A}
1991N/A
1991N/A
829N/A// PLATFORM_MIDI_IN method implementations
829N/A
829N/A/* not thread safe */
829N/Astatic char winMidiInErrMsg[WIN_MAX_ERROR_LEN];
829N/A
829N/Achar* MIDI_IN_GetErrorStr(INT32 err) {
829N/A winMidiInErrMsg[0] = 0;
829N/A midiInGetErrorText((MMRESULT) err, winMidiInErrMsg, WIN_MAX_ERROR_LEN);
829N/A return winMidiInErrMsg;
829N/A}
829N/A
829N/AINT32 MIDI_IN_GetNumDevices() {
829N/A return (INT32) midiInGetNumDevs();
829N/A}
829N/A
829N/AINT32 getMidiInCaps(INT32 deviceID, MIDIINCAPS* caps, INT32* err) {
829N/A (*err) = midiInGetDevCaps(deviceID, caps, sizeof(MIDIINCAPS));
829N/A return ((*err) == MMSYSERR_NOERROR);
829N/A}
829N/A
829N/AINT32 MIDI_IN_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
829N/A MIDIINCAPS midiInCaps;
829N/A INT32 err;
829N/A
829N/A if (getMidiInCaps(deviceID, &midiInCaps, &err)) {
829N/A strncpy(name, midiInCaps.szPname, nameLength-1);
829N/A name[nameLength-1] = 0;
829N/A return MIDI_SUCCESS;
829N/A }
829N/A MIDIIN_CHECK_ERROR;
829N/A return err;
829N/A}
829N/A
829N/A
829N/AINT32 MIDI_IN_GetDeviceVendor(INT32 deviceID, char *name, UINT32 nameLength) {
829N/A return MIDI_NOT_SUPPORTED;
829N/A}
829N/A
829N/A
829N/AINT32 MIDI_IN_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLength) {
829N/A return MIDI_NOT_SUPPORTED;
829N/A}
829N/A
829N/A
829N/A
829N/AINT32 MIDI_IN_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
829N/A MIDIINCAPS midiInCaps;
829N/A INT32 err = MIDI_NOT_SUPPORTED;
829N/A
829N/A if (getMidiInCaps(deviceID, &midiInCaps, &err) && (nameLength>7)) {
829N/A sprintf(name, "%d.%d", (midiInCaps.vDriverVersion & 0xFF00) >> 8, midiInCaps.vDriverVersion & 0xFF);
829N/A return MIDI_SUCCESS;
829N/A }
829N/A MIDIIN_CHECK_ERROR;
829N/A return err;
829N/A}
829N/A
829N/A
829N/AINT32 prepareBuffers(MidiDeviceHandle* handle) {
829N/A SysExQueue* sysex;
829N/A MMRESULT err = MMSYSERR_NOERROR;
829N/A int i;
829N/A
829N/A if (!handle || !handle->longBuffers || !handle->deviceHandle) {
829N/A ERROR0("MIDI_IN_prepareBuffers: handle, or longBuffers, or deviceHandle==NULL\n");
829N/A return MIDI_INVALID_HANDLE;
829N/A }
829N/A sysex = (SysExQueue*) handle->longBuffers;
829N/A for (i = 0; i<sysex->count; i++) {
829N/A MIDIHDR* hdr = &(sysex->header[i]);
829N/A midiInPrepareHeader((HMIDIIN) handle->deviceHandle, hdr, sizeof(MIDIHDR));
829N/A err = midiInAddBuffer((HMIDIIN) handle->deviceHandle, hdr, sizeof(MIDIHDR));
829N/A }
829N/A MIDIIN_CHECK_ERROR;
829N/A return (INT32) err;
829N/A}
829N/A
829N/AINT32 unprepareBuffers(MidiDeviceHandle* handle) {
829N/A SysExQueue* sysex;
829N/A MMRESULT err = MMSYSERR_NOERROR;
829N/A int i;
829N/A
829N/A if (!handle || !handle->longBuffers || !handle->deviceHandle) {
829N/A ERROR0("MIDI_IN_unprepareBuffers: handle, or longBuffers, or deviceHandle==NULL\n");
829N/A return MIDI_INVALID_HANDLE;
829N/A }
829N/A sysex = (SysExQueue*) handle->longBuffers;
829N/A for (i = 0; i<sysex->count; i++) {
829N/A err = midiInUnprepareHeader((HMIDIIN) handle->deviceHandle, &(sysex->header[i]), sizeof(MIDIHDR));
829N/A }
829N/A MIDIIN_CHECK_ERROR;
829N/A return (INT32) err;
829N/A}
829N/A
829N/AINT32 MIDI_IN_OpenDevice(INT32 deviceID, MidiDeviceHandle** handle) {
829N/A MMRESULT err;
829N/A
829N/A TRACE0("> MIDI_IN_OpenDevice\n");
829N/A#ifdef USE_ERROR
829N/A setvbuf(stdout, NULL, (int)_IONBF, 0);
829N/A setvbuf(stderr, NULL, (int)_IONBF, 0);
829N/A#endif
829N/A
829N/A (*handle) = (MidiDeviceHandle*) malloc(sizeof(MidiDeviceHandle));
829N/A if (!(*handle)) {
829N/A ERROR0("< ERROR: MIDI_IN_OpenDevice: out of memory\n");
829N/A return MIDI_OUT_OF_MEMORY;
829N/A }
829N/A memset(*handle, 0, sizeof(MidiDeviceHandle));
829N/A
1990N/A // create queue
829N/A (*handle)->queue = MIDI_CreateQueue(MIDI_IN_MESSAGE_QUEUE_SIZE);
829N/A if (!(*handle)->queue) {
829N/A ERROR0("< ERROR: MIDI_IN_OpenDevice: could not create queue\n");
829N/A free(*handle);
829N/A (*handle) = NULL;
829N/A return MIDI_OUT_OF_MEMORY;
829N/A }
829N/A
829N/A // create long buffer queue
829N/A if (!MIDI_WinCreateLongBufferQueue(*handle, MIDI_IN_LONG_QUEUE_SIZE, MIDI_IN_LONG_MESSAGE_SIZE, NULL)) {
829N/A ERROR0("< ERROR: MIDI_IN_OpenDevice: could not create long Buffers\n");
829N/A MIDI_DestroyQueue((*handle)->queue);
829N/A free(*handle);
829N/A (*handle) = NULL;
829N/A return MIDI_OUT_OF_MEMORY;
829N/A }
829N/A
829N/A // finally open the device
829N/A err = MidiIn_OpenHelper::midiInOpen(deviceID, *handle);
829N/A
829N/A if ((err != MMSYSERR_NOERROR) || (!(*handle)->deviceHandle)) {
829N/A MIDIIN_CHECK_ERROR;
829N/A MIDI_WinDestroyLongBufferQueue(*handle);
829N/A MIDI_DestroyQueue((*handle)->queue);
829N/A free(*handle);
829N/A (*handle) = NULL;
829N/A return (INT32) err;
829N/A }
829N/A
829N/A prepareBuffers(*handle);
829N/A MIDI_SetStartTime(*handle);
829N/A TRACE0("< MIDI_IN_OpenDevice: midiInOpen succeeded\n");
829N/A return MIDI_SUCCESS;
829N/A}
1990N/A
829N/A
829N/AINT32 MIDI_IN_CloseDevice(MidiDeviceHandle* handle) {
829N/A MMRESULT err;
829N/A
1171N/A TRACE0("> MIDI_IN_CloseDevice: midiInClose\n");
829N/A if (!handle) {
1171N/A ERROR0("ERROR: MIDI_IN_CloseDevice: handle is NULL\n");
829N/A return MIDI_INVALID_HANDLE;
829N/A }
1171N/A midiInReset((HMIDIIN) handle->deviceHandle);
829N/A unprepareBuffers(handle);
829N/A err = midiInClose((HMIDIIN) handle->deviceHandle);
829N/A handle->deviceHandle=NULL;
829N/A MIDIIN_CHECK_ERROR;
829N/A MIDI_WinDestroyLongBufferQueue(handle);
829N/A
829N/A if (handle->queue!=NULL) {
1171N/A MidiMessageQueue* queue = handle->queue;
829N/A handle->queue = NULL;
829N/A MIDI_DestroyQueue(queue);
829N/A }
829N/A free(handle);
829N/A
829N/A TRACE0("< MIDI_IN_CloseDevice: midiInClose succeeded\n");
829N/A return (INT32) err;
829N/A}
829N/A
829N/A
829N/AINT32 MIDI_IN_StartDevice(MidiDeviceHandle* handle) {
829N/A MMRESULT err;
829N/A
829N/A if (!handle || !handle->deviceHandle || !handle->queue) {
829N/A ERROR0("ERROR: MIDI_IN_StartDevice: handle or queue is NULL\n");
829N/A return MIDI_INVALID_HANDLE;
829N/A }
1990N/A
1990N/A // clear all the events from the queue
1990N/A MIDI_QueueClear(handle->queue);
1990N/A
1990N/A handle->platformData = (void*) CreateEvent(NULL, FALSE /*manual reset*/, FALSE /*signaled*/, NULL);
1990N/A if (!handle->platformData) {
1990N/A ERROR0("ERROR: MIDI_IN_StartDevice: could not create event\n");
829N/A return MIDI_OUT_OF_MEMORY;
829N/A }
1990N/A
829N/A err = midiInStart((HMIDIIN) handle->deviceHandle);
829N/A /* $$mp 200308-11: This method is already called in ...open(). It is
829N/A unclear why is is called again. The specification says that
829N/A MidiDevice.getMicrosecondPosition() returns the time since the
1990N/A device was opened (the spec doesn't know about start/stop).
829N/A So I guess this call is obsolete. */
829N/A MIDI_SetStartTime(handle);
829N/A
829N/A MIDIIN_CHECK_ERROR;
829N/A TRACE0("MIDI_IN_StartDevice: midiInStart finished\n");
829N/A return (INT32) err;
829N/A}
829N/A
829N/A
829N/AINT32 MIDI_IN_StopDevice(MidiDeviceHandle* handle) {
829N/A MMRESULT err;
829N/A HANDLE event;
829N/A
829N/A TRACE0("> MIDI_IN_StopDevice: midiInStop \n");
829N/A if (!handle || !handle->platformData) {
829N/A ERROR0("ERROR: MIDI_IN_StopDevice: handle or event is NULL\n");
829N/A return MIDI_INVALID_HANDLE;
829N/A }
829N/A // encourage MIDI_IN_GetMessage to return soon
829N/A event = handle->platformData;
829N/A handle->platformData = NULL;
829N/A SetEvent(event);
829N/A
829N/A err = midiInStop((HMIDIIN) handle->deviceHandle);
829N/A
829N/A // wait until the Java thread has exited
829N/A while (handle->isWaiting) Sleep(0);
829N/A CloseHandle(event);
829N/A
829N/A MIDIIN_CHECK_ERROR;
829N/A TRACE0("< MIDI_IN_StopDevice: midiInStop finished\n");
829N/A return (INT32) err;
829N/A}
829N/A
829N/A
829N/A/* return time stamp in microseconds */
829N/AINT64 MIDI_IN_GetTimeStamp(MidiDeviceHandle* handle) {
829N/A return MIDI_GetTimeStamp(handle);
829N/A}
829N/A
829N/A
1171N/A// read the next message from the queue
829N/AMidiMessage* MIDI_IN_GetMessage(MidiDeviceHandle* handle) {
829N/A if (handle == NULL) {
829N/A return NULL;
829N/A }
1171N/A while (handle->queue!=NULL && handle->platformData!=NULL) {
829N/A MidiMessage* msg = MIDI_QueueRead(handle->queue);
829N/A DWORD res;
829N/A if (msg != NULL) {
829N/A //fprintf(stdout, "GetMessage returns index %d\n", msg->data.l.index); fflush(stdout);
829N/A return msg;
829N/A }
1171N/A TRACE0("MIDI_IN_GetMessage: before waiting\n");
829N/A handle->isWaiting = TRUE;
829N/A res = WaitForSingleObject((HANDLE) handle->platformData, 2000);
829N/A handle->isWaiting = FALSE;
829N/A if (res == WAIT_TIMEOUT) {
829N/A // break out back to Java from time to time - just to be sure
829N/A TRACE0("MIDI_IN_GetMessage: waiting finished with timeout\n");
829N/A break;
829N/A }
829N/A TRACE0("MIDI_IN_GetMessage: waiting finished\n");
829N/A }
829N/A return NULL;
829N/A}
829N/A
829N/Avoid MIDI_IN_ReleaseMessage(MidiDeviceHandle* handle, MidiMessage* msg) {
829N/A SysExQueue* sysex;
829N/A if (handle == NULL || handle->queue == NULL) {
829N/A return;
829N/A }
829N/A sysex = (SysExQueue*) handle->longBuffers;
829N/A if (msg->type == LONG_MESSAGE && sysex) {
829N/A MIDIHDR* hdr = &(sysex->header[msg->data.l.index]);
829N/A //fprintf(stdout, "ReleaseMessage index %d\n", msg->data.l.index); fflush(stdout);
829N/A hdr->dwBytesRecorded = 0;
829N/A midiInAddBuffer((HMIDIIN) handle->deviceHandle, hdr, sizeof(MIDIHDR));
829N/A }
829N/A MIDI_QueueRemove(handle->queue, TRUE /*onlyLocked*/);
829N/A}
829N/A
829N/A#endif // USE_PLATFORM_MIDI_IN
1990N/A