aboutsummaryrefslogtreecommitdiff
path: root/pd/portmidi/pm_common/portmidi.c
diff options
context:
space:
mode:
Diffstat (limited to 'pd/portmidi/pm_common/portmidi.c')
-rw-r--r--pd/portmidi/pm_common/portmidi.c702
1 files changed, 453 insertions, 249 deletions
diff --git a/pd/portmidi/pm_common/portmidi.c b/pd/portmidi/pm_common/portmidi.c
index e1b962d7..71988e47 100644
--- a/pd/portmidi/pm_common/portmidi.c
+++ b/pd/portmidi/pm_common/portmidi.c
@@ -2,6 +2,9 @@
#include "string.h"
#include "portmidi.h"
#include "porttime.h"
+#ifdef NEWBUFFER
+#include "pmutil.h"
+#endif
#include "pminternal.h"
#include <assert.h>
@@ -31,7 +34,7 @@
#define is_empty(midi) ((midi)->tail == (midi)->head)
static int pm_initialized = FALSE;
-int pm_hosterror = FALSE;
+int pm_hosterror;
char pm_hosterror_text[PM_HOST_ERROR_MSG_LEN];
#ifdef PM_CHECK_ERRORS
@@ -80,24 +83,24 @@ int pm_descriptor_max = 0;
int pm_descriptor_index = 0;
descriptor_type descriptors = NULL;
-/* pm_add_device -- describe interface/device pair to library
+/* pm_add_device -- describe interface/device pair to library
*
- * This is called at intialization time, once for each
+ * This is called at intialization time, once for each
* interface (e.g. DirectSound) and device (e.g. SoundBlaster 1)
* The strings are retained but NOT COPIED, so do not destroy them!
*
* returns pmInvalidDeviceId if device memory is exceeded
* otherwise returns pmNoError
*/
-PmError pm_add_device(char *interf, char *name, int input,
+PmError pm_add_device(char *interf, char *name, int input,
void *descriptor, pm_fns_type dictionary) {
if (pm_descriptor_index >= pm_descriptor_max) {
// expand descriptors
- descriptor_type new_descriptors =
+ descriptor_type new_descriptors =
pm_alloc(sizeof(descriptor_node) * (pm_descriptor_max + 32));
if (!new_descriptors) return pmInsufficientMemory;
if (descriptors) {
- memcpy(new_descriptors, descriptors,
+ memcpy(new_descriptors, descriptors,
sizeof(descriptor_node) * pm_descriptor_max);
free(descriptors);
}
@@ -114,14 +117,14 @@ PmError pm_add_device(char *interf, char *name, int input,
/* ID number passed to win32 multimedia API open */
descriptors[pm_descriptor_index].descriptor = descriptor;
-
+
/* points to PmInternal, allows automatic device closing */
descriptors[pm_descriptor_index].internalDescriptor = NULL;
descriptors[pm_descriptor_index].dictionary = dictionary;
-
+
pm_descriptor_index++;
-
+
return pmNoError;
}
@@ -132,20 +135,15 @@ portmidi implementation
====================================================================
*/
-int Pm_CountDevices( void )
-{
- PmError err = Pm_Initialize();
- if (err)
- return pm_errmsg(err);
+int Pm_CountDevices( void ) {
+ Pm_Initialize();
+ /* no error checking -- Pm_Initialize() does not fail */
return pm_descriptor_index;
}
-const PmDeviceInfo* Pm_GetDeviceInfo( PmDeviceID id )
-{
- PmError err = Pm_Initialize();
- if (err)
- return NULL;
+const PmDeviceInfo* Pm_GetDeviceInfo( PmDeviceID id ) {
+ Pm_Initialize(); /* no error check needed */
if (id >= 0 && id < pm_descriptor_index) {
return &descriptors[id].pub;
}
@@ -158,35 +156,30 @@ PmError pm_success_fn(PmInternal *midi) {
}
/* none_write -- returns an error if called */
-PmError none_write_short(PmInternal *midi, PmEvent *buffer)
-{
+PmError none_write_short(PmInternal *midi, PmEvent *buffer) {
return pmBadPtr;
}
-/* none_sysex -- placeholder for begin_sysex and end_sysex */
-PmError none_sysex(PmInternal *midi, PmTimestamp timestamp)
-{
+/* pm_fail_timestamp_fn -- placeholder for begin_sysex and flush */
+PmError pm_fail_timestamp_fn(PmInternal *midi, PmTimestamp timestamp) {
return pmBadPtr;
}
-PmError none_write_byte(PmInternal *midi, unsigned char byte,
- PmTimestamp timestamp)
-{
+PmError none_write_byte(PmInternal *midi, unsigned char byte,
+ PmTimestamp timestamp) {
return pmBadPtr;
}
/* pm_fail_fn -- generic function, returns error if called */
-PmError pm_fail_fn(PmInternal *midi)
-{
+PmError pm_fail_fn(PmInternal *midi) {
return pmBadPtr;
}
-static PmError none_open(PmInternal *midi, void *driverInfo)
-{
+static PmError none_open(PmInternal *midi, void *driverInfo) {
return pmBadPtr;
}
static void none_get_host_error(PmInternal * midi, char * msg, unsigned int len) {
- strcpy(msg,"");
+ strcpy(msg, "");
}
static unsigned int none_has_host_error(PmInternal * midi) {
return FALSE;
@@ -207,11 +200,11 @@ pm_fns_node pm_none_dictionary = {
none_write_flush,
none_synchronize,
none_open,
- none_abort,
+ none_abort,
none_close,
none_poll,
none_has_host_error,
- none_get_host_error
+ none_get_host_error
};
@@ -220,34 +213,38 @@ const char *Pm_GetErrorText( PmError errnum ) {
switch(errnum)
{
- case pmNoError:
- msg = "";
+ case pmNoError:
+ msg = "";
break;
- case pmHostError:
- msg = "PortMidi: `Host error'";
+ case pmHostError:
+ msg = "PortMidi: `Host error'";
break;
- case pmInvalidDeviceId:
- msg = "PortMidi: `Invalid device ID'";
+ case pmInvalidDeviceId:
+ msg = "PortMidi: `Invalid device ID'";
break;
- case pmInsufficientMemory:
- msg = "PortMidi: `Insufficient memory'";
+ case pmInsufficientMemory:
+ msg = "PortMidi: `Insufficient memory'";
break;
- case pmBufferTooSmall:
- msg = "PortMidi: `Buffer too small'";
+ case pmBufferTooSmall:
+ msg = "PortMidi: `Buffer too small'";
break;
- case pmBadPtr:
- msg = "PortMidi: `Bad pointer'";
+ case pmBadPtr:
+ msg = "PortMidi: `Bad pointer'";
break;
- case pmInternalError:
- msg = "PortMidi: `Internal PortMidi Error'";
+ case pmInternalError:
+ msg = "PortMidi: `Internal PortMidi Error'";
break;
case pmBufferOverflow:
msg = "PortMidi: `Buffer overflow'";
break;
case pmBadData:
msg = "PortMidi: `Invalid MIDI message Data'";
- default:
- msg = "PortMidi: `Illegal error number'";
+ break;
+ case pmBufferMaxSize:
+ msg = "PortMidi: `Buffer cannot be made larger'";
+ break;
+ default:
+ msg = "PortMidi: `Illegal error number'";
break;
}
return msg;
@@ -260,11 +257,11 @@ const char *Pm_GetErrorText( PmError errnum ) {
void Pm_GetHostErrorText(char * msg, unsigned int len) {
assert(msg);
assert(len > 0);
- if (pm_hosterror) { /* we have the string already from open or close */
+ if (pm_hosterror) {
strncpy(msg, (char *) pm_hosterror_text, len);
pm_hosterror = FALSE;
pm_hosterror_text[0] = 0; /* clear the message; not necessary, but it
- might help with debugging */
+ might help with debugging */
msg[len - 1] = 0; /* make sure string is terminated */
} else {
msg[0] = 0; /* no string to return */
@@ -273,11 +270,12 @@ void Pm_GetHostErrorText(char * msg, unsigned int len) {
int Pm_HasHostError(PortMidiStream * stream) {
+ if (pm_hosterror) return TRUE;
if (stream) {
PmInternal * midi = (PmInternal *) stream;
pm_hosterror = (*midi->dictionary->has_host_error)(midi);
if (pm_hosterror) {
- midi->dictionary->host_error(midi, pm_hosterror_text,
+ midi->dictionary->host_error(midi, pm_hosterror_text,
PM_HOST_ERROR_MSG_LEN);
/* now error message is global */
return TRUE;
@@ -288,8 +286,9 @@ int Pm_HasHostError(PortMidiStream * stream) {
PmError Pm_Initialize( void ) {
- pm_hosterror_text[0] = 0; /* the null string */
if (!pm_initialized) {
+ pm_hosterror = FALSE;
+ pm_hosterror_text[0] = 0; /* the null string */
pm_init();
pm_initialized = TRUE;
}
@@ -300,6 +299,13 @@ PmError Pm_Initialize( void ) {
PmError Pm_Terminate( void ) {
if (pm_initialized) {
pm_term();
+ // if there are no devices, descriptors might still be NULL
+ if (descriptors != NULL) {
+ free(descriptors);
+ descriptors = NULL;
+ }
+ pm_descriptor_index = 0;
+ pm_descriptor_max = 0;
pm_initialized = FALSE;
}
return pmNoError;
@@ -308,33 +314,23 @@ PmError Pm_Terminate( void ) {
/* Pm_Read -- read up to length longs from source into buffer */
/*
- returns number of longs actually read, or error code
-
- When the reader wants data:
- if overflow_flag:
- do not get anything
- empty the buffer (read_ptr = write_ptr)
- clear overflow_flag
- return pmBufferOverflow
- get data
- return number of messages
-*/
+ * returns number of longs actually read, or error code
+ */
PmError Pm_Read(PortMidiStream *stream, PmEvent *buffer, long length) {
PmInternal *midi = (PmInternal *) stream;
int n = 0;
+#ifndef NEWBUFFER
long head;
+#endif
PmError err = pmNoError;
-
+ pm_hosterror = FALSE;
/* arg checking */
if(midi == NULL)
err = pmBadPtr;
- else if(Pm_HasHostError(midi))
- err = pmHostError;
else if(!descriptors[midi->device_id].pub.opened)
err = pmBadPtr;
else if(!descriptors[midi->device_id].pub.input)
- err = pmBadPtr;
-
+ err = pmBadPtr;
/* First poll for data in the buffer...
* This either simply checks for data, or attempts first to fill the buffer
* with data from the MIDI hardware; this depends on the implementation.
@@ -343,9 +339,26 @@ PmError Pm_Read(PortMidiStream *stream, PmEvent *buffer, long length) {
else err = (*(midi->dictionary->poll))(midi);
if (err != pmNoError) {
+ if (err == pmHostError) {
+ midi->dictionary->host_error(midi, pm_hosterror_text,
+ PM_HOST_ERROR_MSG_LEN);
+ pm_hosterror = TRUE;
+ }
return pm_errmsg(err);
}
+#ifdef NEWBUFFER
+ while (n < length) {
+ PmError err = Pm_Dequeue(midi->queue, buffer++);
+ if (err == pmBufferOverflow) {
+ /* ignore the data we have retreived so far */
+ return pm_errmsg(pmBufferOverflow);
+ } else if (err == 0) { /* empty queue */
+ break;
+ }
+ n++;
+ }
+#else
head = midi->head;
while (head != midi->tail && n < length) {
PmEvent event = midi->buffer[head++];
@@ -359,6 +372,7 @@ PmError Pm_Read(PortMidiStream *stream, PmEvent *buffer, long length) {
midi->overflow = FALSE;
return pm_errmsg(pmBufferOverflow);
}
+#endif
return n;
}
@@ -366,12 +380,14 @@ PmError Pm_Poll( PortMidiStream *stream )
{
PmInternal *midi = (PmInternal *) stream;
PmError err;
+#ifdef NEWBUFFER
+ PmEvent *event;
+#endif
+ pm_hosterror = FALSE;
/* arg checking */
if(midi == NULL)
err = pmBadPtr;
- else if(Pm_HasHostError(midi))
- err = pmHostError;
else if(!descriptors[midi->device_id].pub.opened)
err = pmBadPtr;
else if(!descriptors[midi->device_id].pub.input)
@@ -379,12 +395,41 @@ PmError Pm_Poll( PortMidiStream *stream )
else
err = (*(midi->dictionary->poll))(midi);
- if (err != pmNoError)
+ if (err != pmNoError) {
+ if (err == pmHostError) {
+ midi->dictionary->host_error(midi, pm_hosterror_text,
+ PM_HOST_ERROR_MSG_LEN);
+ pm_hosterror = TRUE;
+ }
return pm_errmsg(err);
- else
- return midi->head != midi->tail;
+ }
+
+#ifdef NEWBUFFER
+ event = (PmEvent *) Pm_QueuePeek(midi->queue);
+ return event != NULL;
+#else
+ return midi->head != midi->tail;
+#endif
+}
+
+
+/* this is called from Pm_Write and Pm_WriteSysEx to issue a
+ * call to the system-dependent end_sysex function and handle
+ * the error return
+ */
+static PmError pm_end_sysex(PmInternal *midi)
+{
+ PmError err = (*midi->dictionary->end_sysex)(midi, 0);
+ midi->sysex_in_progress = FALSE;
+ if (err == pmHostError) {
+ midi->dictionary->host_error(midi, pm_hosterror_text,
+ PM_HOST_ERROR_MSG_LEN);
+ pm_hosterror = TRUE;
+ }
+ return err;
}
+
/* to facilitate correct error-handling, Pm_Write, Pm_WriteShort, and
Pm_WriteSysEx all operate a state machine that "outputs" calls to
write_short, begin_sysex, write_byte, end_sysex, and write_realtime */
@@ -395,21 +440,20 @@ PmError Pm_Write( PortMidiStream *stream, PmEvent *buffer, long length)
PmError err;
int i;
int bits;
-
+
+ pm_hosterror = FALSE;
/* arg checking */
if(midi == NULL)
err = pmBadPtr;
- else if(Pm_HasHostError(midi))
- err = pmHostError;
else if(!descriptors[midi->device_id].pub.opened)
err = pmBadPtr;
else if(!descriptors[midi->device_id].pub.output)
err = pmBadPtr;
else
err = pmNoError;
-
+
if (err != pmNoError) goto pm_write_error;
-
+
if (midi->latency == 0) {
midi->now = 0;
} else {
@@ -420,7 +464,19 @@ PmError Pm_Write( PortMidiStream *stream, PmEvent *buffer, long length)
midi->first_message = FALSE;
}
}
-
+ /* error recovery: when a sysex is detected, we call
+ * dictionary->begin_sysex() followed by calls to
+ * dictionary->write_byte() and dictionary->write_realtime()
+ * until an end-of-sysex is detected, when we call
+ * dictionary->end_sysex(). After an error occurs,
+ * Pm_Write() continues to call functions. For example,
+ * it will continue to call write_byte() even after
+ * an error sending a sysex message, and end_sysex() will be
+ * called when an EOX or non-real-time status is found.
+ * When errors are detected, Pm_Write() returns immediately,
+ * so it is possible that this will drop data and leave
+ * sysex messages in a partially transmitted state.
+ */
for (i = 0; i < length; i++) {
unsigned long msg = buffer[i].message;
bits = 0;
@@ -433,21 +489,21 @@ PmError Pm_Write( PortMidiStream *stream, PmEvent *buffer, long length)
goto pm_write_error;
}
midi->sysex_in_progress = TRUE;
- if ((err = (*midi->dictionary->begin_sysex)(midi,
+ if ((err = (*midi->dictionary->begin_sysex)(midi,
buffer[i].timestamp)) != pmNoError)
goto pm_write_error;
if ((err = (*midi->dictionary->write_byte)(midi, MIDI_SYSEX,
- buffer[i].timestamp)) != pmNoError)
+ buffer[i].timestamp)) != pmNoError)
goto pm_write_error;
bits = 8;
/* fall through to continue sysex processing */
- } else if ((msg & MIDI_STATUS_MASK) &&
+ } else if ((msg & MIDI_STATUS_MASK) &&
(Pm_MessageStatus(msg) != MIDI_EOX)) {
/* a non-sysex message */
if (midi->sysex_in_progress) {
- /* this should be a non-realtime message */
+ /* this should be a realtime message */
if (is_real_time(msg)) {
- if ((err = (*midi->dictionary->write_realtime)(midi,
+ if ((err = (*midi->dictionary->write_realtime)(midi,
&(buffer[i]))) != pmNoError)
goto pm_write_error;
} else {
@@ -459,23 +515,34 @@ PmError Pm_Write( PortMidiStream *stream, PmEvent *buffer, long length)
goto pm_write_error;
}
} else { /* regular short midi message */
- if ((err = (*midi->dictionary->write_short)(midi,
+ if ((err = (*midi->dictionary->write_short)(midi,
&(buffer[i]))) != pmNoError)
goto pm_write_error;
continue;
}
}
if (midi->sysex_in_progress) { /* send sysex bytes until EOX */
+ /* see if we can accelerate data transfer */
+ if (bits == 0 && midi->fill_base && /* 4 bytes to copy */
+ (*midi->fill_offset_ptr) + 4 <= midi->fill_length &&
+ (msg & 0x80808080) == 0) { /* all data */
+ /* copy 4 bytes from msg to fill_base + fill_offset */
+ unsigned char *ptr = midi->fill_base +
+ *(midi->fill_offset_ptr);
+ ptr[0] = msg; ptr[1] = msg >> 8;
+ ptr[2] = msg >> 18; ptr[3] = msg >> 24;
+ (*midi->fill_offset_ptr) += 4;
+ continue;
+ }
+ /* no acceleration, so do byte-by-byte copying */
while (bits < 32) {
unsigned char midi_byte = (unsigned char) (msg >> bits);
- if ((err = (*midi->dictionary->write_byte)(midi, midi_byte,
+ if ((err = (*midi->dictionary->write_byte)(midi, midi_byte,
buffer[i].timestamp)) != pmNoError)
goto pm_write_error;
if (midi_byte == MIDI_EOX) {
- midi->sysex_in_progress = FALSE;
- if ((err = (*midi->dictionary->end_sysex)(midi,
- buffer[i].timestamp)) != pmNoError)
- goto pm_write_error;
+ err = pm_end_sysex(midi);
+ if (err != pmNoError) goto error_exit;
break; /* from while loop */
}
bits += 8;
@@ -487,8 +554,15 @@ PmError Pm_Write( PortMidiStream *stream, PmEvent *buffer, long length)
}
}
/* after all messages are processed, send the data */
- err = (*midi->dictionary->write_flush)(midi);
+ if (!midi->sysex_in_progress)
+ err = (*midi->dictionary->write_flush)(midi, 0);
pm_write_error:
+ if (err == pmHostError) {
+ midi->dictionary->host_error(midi, pm_hosterror_text,
+ PM_HOST_ERROR_MSG_LEN);
+ pm_hosterror = TRUE;
+ }
+error_exit:
return pm_errmsg(err);
}
@@ -496,20 +570,22 @@ pm_write_error:
PmError Pm_WriteShort( PortMidiStream *stream, long when, long msg)
{
PmEvent event;
-
+
event.timestamp = when;
event.message = msg;
return Pm_Write(stream, &event, 1);
}
-PmError Pm_WriteSysEx(PortMidiStream *stream, PmTimestamp when,
+PmError Pm_WriteSysEx(PortMidiStream *stream, PmTimestamp when,
unsigned char *msg)
{
/* allocate buffer space for PM_DEFAULT_SYSEX_BUFFER_SIZE bytes */
/* each PmEvent holds sizeof(PmMessage) bytes of sysex data */
#define BUFLEN (PM_DEFAULT_SYSEX_BUFFER_SIZE / sizeof(PmMessage))
PmEvent buffer[BUFLEN];
+ int buffer_size = 1; /* first time, send 1. After that, it's BUFLEN */
+ PmInternal *midi = (PmInternal *) stream;
/* the next byte in the buffer is represented by an index, bufx, and
a shift in bits */
int shift = 0;
@@ -521,28 +597,58 @@ PmError Pm_WriteSysEx(PortMidiStream *stream, PmTimestamp when,
/* insert next byte into buffer */
buffer[bufx].message |= ((*msg) << shift);
shift += 8;
+ if (*msg++ == MIDI_EOX) break;
if (shift == 32) {
shift = 0;
bufx++;
- if (bufx == BUFLEN) {
- PmError err = Pm_Write(stream, buffer, BUFLEN);
+ if (bufx == buffer_size) {
+ PmError err = Pm_Write(stream, buffer, buffer_size);
+ /* note: Pm_Write has already called errmsg() */
if (err) return err;
/* prepare to fill another buffer */
bufx = 0;
+ buffer_size = BUFLEN;
+ /* optimization: maybe we can just copy bytes */
+ if (midi->fill_base) {
+ PmError err;
+ while (*(midi->fill_offset_ptr) < midi->fill_length) {
+ midi->fill_base[(*midi->fill_offset_ptr)++] = *msg;
+ if (*msg++ == MIDI_EOX) {
+ err = pm_end_sysex(midi);
+ if (err != pmNoError) return pm_errmsg(err);
+ goto end_of_sysex;
+ }
+ }
+ /* I thought that I could do a pm_Write here and
+ * change this if to a loop, avoiding calls in Pm_Write
+ * to the slower write_byte, but since
+ * sysex_in_progress is true, this will not flush
+ * the buffer, and we'll infinite loop: */
+ /* err = Pm_Write(stream, buffer, 0);
+ if (err) return err; */
+ /* instead, the way this works is that Pm_Write calls
+ * write_byte on 4 bytes. The first, since the buffer
+ * is full, will flush the buffer and allocate a new
+ * one. This primes the buffer so
+ * that we can return to the loop above and fill it
+ * efficiently without a lot of function calls.
+ */
+ buffer_size = 1; /* get another message started */
+ }
}
buffer[bufx].message = 0;
buffer[bufx].timestamp = when;
- }
+ }
/* keep inserting bytes until you find MIDI_EOX */
- if (*msg++ == MIDI_EOX) break;
}
-
+end_of_sysex:
/* we're finished sending full buffers, but there may
* be a partial one left.
*/
if (shift != 0) bufx++; /* add partial message to buffer len */
if (bufx) { /* bufx is number of PmEvents to send from buffer */
- return Pm_Write(stream, buffer, bufx);
+ PmError err = Pm_Write(stream, buffer, bufx);
+ if (err) return err;
}
return pmNoError;
}
@@ -554,26 +660,25 @@ PmError Pm_OpenInput(PortMidiStream** stream,
void *inputDriverInfo,
long bufferSize,
PmTimeProcPtr time_proc,
- void *time_info)
-{
+ void *time_info) {
PmInternal *midi;
PmError err = pmNoError;
pm_hosterror = FALSE;
*stream = NULL;
-
+
/* arg checking */
- if (inputDevice < 0 || inputDevice >= pm_descriptor_index)
+ if (inputDevice < 0 || inputDevice >= pm_descriptor_index)
err = pmInvalidDeviceId;
- else if (!descriptors[inputDevice].pub.input)
+ else if (!descriptors[inputDevice].pub.input)
err = pmBadPtr;
else if(descriptors[inputDevice].pub.opened)
err = pmBadPtr;
-
- if (err != pmNoError)
+
+ if (err != pmNoError)
goto error_return;
/* create portMidi internal data */
- midi = (PmInternal *) pm_alloc(sizeof(PmInternal));
+ midi = (PmInternal *) pm_alloc(sizeof(PmInternal));
*stream = midi;
if (!midi) {
err = pmInsufficientMemory;
@@ -589,29 +694,36 @@ PmError Pm_OpenInput(PortMidiStream** stream,
system-specific midi_out_open() method.
*/
if (bufferSize <= 0) bufferSize = 256; /* default buffer size */
+#ifdef NEWBUFFER
+ midi->queue = Pm_QueueCreate(bufferSize, sizeof(PmEvent));
+ if (!midi->queue) {
+#else
else bufferSize++; /* buffer holds N-1 msgs, so increase request by 1 */
- midi->buffer_len = bufferSize; /* portMidi input storage */
- midi->buffer = (PmEvent *) pm_alloc(sizeof(PmEvent) * midi->buffer_len);
- if (!midi->buffer) {
+ midi->buffer = (PmEvent *) pm_alloc(sizeof(PmEvent) * midi->buffer_len);
+ midi->head = 0;
+ midi->tail = 0;
+ midi->overflow = FALSE;
+ if (!midi->buffer) {
+#endif
/* free portMidi data */
*stream = NULL;
- pm_free(midi);
+ pm_free(midi);
err = pmInsufficientMemory;
goto error_return;
}
- midi->head = 0;
- midi->tail = 0;
+ midi->buffer_len = bufferSize; /* portMidi input storage */
midi->latency = 0; /* not used */
- midi->overflow = FALSE;
- midi->flush = FALSE;
midi->sysex_in_progress = FALSE;
- midi->sysex_message = 0;
- midi->sysex_message_count = 0;
+ midi->sysex_message = 0;
+ midi->sysex_message_count = 0;
midi->filters = PM_FILT_ACTIVE;
midi->channel_mask = 0xFFFF;
midi->sync_time = 0;
midi->first_message = TRUE;
midi->dictionary = descriptors[inputDevice].dictionary;
+ midi->fill_base = NULL;
+ midi->fill_offset_ptr = NULL;
+ midi->fill_length = 0;
descriptors[inputDevice].internalDescriptor = midi;
/* open system dependent input device */
err = (*midi->dictionary->open)(midi, inputDriverInfo);
@@ -619,13 +731,21 @@ PmError Pm_OpenInput(PortMidiStream** stream,
*stream = NULL;
descriptors[inputDevice].internalDescriptor = NULL;
/* free portMidi data */
- pm_free(midi->buffer);
+#ifdef NEWBUFFER
+ Pm_QueueDestroy(midi->queue);
+#else
+ pm_free(midi->buffer);
+#endif
pm_free(midi);
} else {
/* portMidi input open successful */
descriptors[inputDevice].pub.opened = TRUE;
}
error_return:
+ /* note: if there is a pmHostError, it is the responsibility
+ * of the system-dependent code (*midi->dictionary->open)()
+ * to set pm_hosterror and pm_hosterror_text
+ */
return pm_errmsg(err);
}
@@ -636,26 +756,25 @@ PmError Pm_OpenOutput(PortMidiStream** stream,
long bufferSize,
PmTimeProcPtr time_proc,
void *time_info,
- long latency)
-{
+ long latency ) {
PmInternal *midi;
PmError err = pmNoError;
pm_hosterror = FALSE;
*stream = NULL;
-
+
/* arg checking */
if (outputDevice < 0 || outputDevice >= pm_descriptor_index)
err = pmInvalidDeviceId;
- else if (!descriptors[outputDevice].pub.output)
- err = pmBadPtr;
+ else if (!descriptors[outputDevice].pub.output)
+ err = pmInvalidDeviceId;
else if (descriptors[outputDevice].pub.opened)
- err = pmBadPtr;
- if (err != pmNoError)
+ err = pmInvalidDeviceId;
+ if (err != pmNoError)
goto error_return;
/* create portMidi internal data */
- midi = (PmInternal *) pm_alloc(sizeof(PmInternal));
- *stream = midi;
+ midi = (PmInternal *) pm_alloc(sizeof(PmInternal));
+ *stream = midi;
if (!midi) {
err = pmInsufficientMemory;
goto error_return;
@@ -666,32 +785,36 @@ PmError Pm_OpenOutput(PortMidiStream** stream,
/* if latency > 0, we need a time reference. If none is provided,
use PortTime library */
if (time_proc == NULL && latency != 0) {
- if (!Pt_Started())
+ if (!Pt_Started())
Pt_Start(1, 0, 0);
/* time_get does not take a parameter, so coerce */
midi->time_proc = (PmTimeProcPtr) Pt_Time;
}
midi->time_info = time_info;
- /* when stream used, this buffer allocated and used by
- winmm_out_open; deleted by winmm_out_close */
midi->buffer_len = bufferSize;
+#ifdef NEWBUFFER
+ midi->queue = NULL; /* unused by output */
+#else
midi->buffer = NULL;
midi->head = 0; /* unused by output */
midi->tail = 0; /* unused by output */
+ midi->overflow = FALSE; /* not used */
+#endif
/* if latency zero, output immediate (timestamps ignored) */
/* if latency < 0, use 0 but don't return an error */
if (latency < 0) latency = 0;
midi->latency = latency;
- midi->overflow = FALSE; /* not used */
- midi->flush = FALSE; /* not used */
midi->sysex_in_progress = FALSE;
midi->sysex_message = 0; /* unused by output */
midi->sysex_message_count = 0; /* unused by output */
midi->filters = 0; /* not used for output */
- midi->channel_mask = 0xFFFF; /* not used for output */
+ midi->channel_mask = 0xFFFF;
midi->sync_time = 0;
midi->first_message = TRUE;
midi->dictionary = descriptors[outputDevice].dictionary;
+ midi->fill_base = NULL;
+ midi->fill_offset_ptr = NULL;
+ midi->fill_length = 0;
descriptors[outputDevice].internalDescriptor = midi;
/* open system dependent output device */
err = (*midi->dictionary->open)(midi, outputDriverInfo);
@@ -699,15 +822,19 @@ PmError Pm_OpenOutput(PortMidiStream** stream,
*stream = NULL;
descriptors[outputDevice].internalDescriptor = NULL;
/* free portMidi data */
- pm_free(midi);
+ pm_free(midi);
} else {
/* portMidi input open successful */
descriptors[outputDevice].pub.opened = TRUE;
}
error_return:
+ /* note: system-dependent code must set pm_hosterror and
+ * pm_hosterror_text if a pmHostError occurs
+ */
return pm_errmsg(err);
}
+
PmError Pm_SetChannelMask(PortMidiStream *stream, int mask)
{
PmInternal *midi = (PmInternal *) stream;
@@ -721,8 +848,8 @@ PmError Pm_SetChannelMask(PortMidiStream *stream, int mask)
return pm_errmsg(err);
}
-PmError Pm_SetFilter(PortMidiStream *stream, long filters)
-{
+
+PmError Pm_SetFilter(PortMidiStream *stream, long filters) {
PmInternal *midi = (PmInternal *) stream;
PmError err = pmNoError;
@@ -737,11 +864,11 @@ PmError Pm_SetFilter(PortMidiStream *stream, long filters)
}
-PmError Pm_Close( PortMidiStream *stream )
-{
+PmError Pm_Close( PortMidiStream *stream ) {
PmInternal *midi = (PmInternal *) stream;
PmError err = pmNoError;
+ pm_hosterror = FALSE;
/* arg checking */
if (midi == NULL) /* midi must point to something */
err = pmBadPtr;
@@ -751,8 +878,8 @@ PmError Pm_Close( PortMidiStream *stream )
/* and the device should be in the opened state */
else if (!descriptors[midi->device_id].pub.opened)
err = pmBadPtr;
-
- if (err != pmNoError)
+
+ if (err != pmNoError)
goto error_return;
/* close the device */
@@ -760,15 +887,21 @@ PmError Pm_Close( PortMidiStream *stream )
/* even if an error occurred, continue with cleanup */
descriptors[midi->device_id].internalDescriptor = NULL;
descriptors[midi->device_id].pub.opened = FALSE;
- pm_free(midi->buffer);
- pm_free(midi);
+#ifdef NEWBUFFER
+ if (midi->queue) Pm_QueueDestroy(midi->queue);
+#else
+ if (midi->buffer) pm_free(midi->buffer);
+#endif
+ pm_free(midi);
error_return:
+ /* system dependent code must set pm_hosterror and
+ * pm_hosterror_text if a pmHostError occurs.
+ */
return pm_errmsg(err);
}
-PmError Pm_Abort( PortMidiStream* stream )
-{
+PmError Pm_Abort( PortMidiStream* stream ) {
PmInternal *midi = (PmInternal *) stream;
PmError err;
/* arg checking */
@@ -780,39 +913,47 @@ PmError Pm_Abort( PortMidiStream* stream )
err = pmBadPtr;
else
err = (*midi->dictionary->abort)(midi);
+
+ if (err == pmHostError) {
+ midi->dictionary->host_error(midi, pm_hosterror_text,
+ PM_HOST_ERROR_MSG_LEN);
+ pm_hosterror = TRUE;
+ }
return pm_errmsg(err);
}
+#ifndef NEWBUFFER
+/* this is apparently an orphan routine -- I can find no reference to it now -RBD */
+
/* in win32 multimedia API (via callbacks) some of these functions used; assume never fail */
long pm_next_time(PmInternal *midi) {
/* arg checking */
assert(midi != NULL);
- assert(!Pm_HasHostError(midi));
-
+
return midi->buffer[midi->head].timestamp;
}
+#endif
+
+
/* pm_channel_filtered returns non-zero if the channel mask is blocking the current channel */
-static int pm_channel_filtered(int status, int mask)
-{
- if ((status & 0xF0) == 0xF0) /* 0xF? messages don't have a channel */
- return 0;
- return !(Pm_Channel(status & 0x0F) & mask);
- /* it'd be easier to return 0 for filtered, 1 for allowed,
- but it would different from the other filtering functions
- */
+#define pm_channel_filtered(status, mask) \
+ ((((status) & 0xF0) != 0xF0) && (!(Pm_Channel((status) & 0x0F) & (mask))))
+
-}
/* The following two functions will checks to see if a MIDI message matches
the filtering criteria. Since the sysex routines only want to filter realtime messages,
we need to have separate routines.
*/
+
/* pm_realtime_filtered returns non-zero if the filter will kill the current message.
Note that only realtime messages are checked here.
*/
-static int pm_realtime_filtered(int status, long filters)
-{
+#define pm_realtime_filtered(status, filters) \
+ ((((status) & 0xF0) == 0xF0) && ((1 << ((status) & 0xF)) & (filters)))
+
+/*
return ((status == MIDI_ACTIVE) && (filters & PM_FILT_ACTIVE))
|| ((status == MIDI_CLOCK) && (filters & PM_FILT_CLOCK))
|| ((status == MIDI_START) && (filters & PM_FILT_PLAY))
@@ -825,16 +966,18 @@ static int pm_realtime_filtered(int status, long filters)
|| ((status == MIDI_SONGPOS) && (filters & PM_FILT_SONG_POSITION))
|| ((status == MIDI_SONGSEL) && (filters & PM_FILT_SONG_SELECT))
|| ((status == MIDI_TUNE) && (filters & PM_FILT_TUNE));
-}
+}*/
+
+
/* pm_status_filtered returns non-zero if a filter will kill the current message, based on status.
Note that sysex and real time are not checked. It is up to the subsystem (winmm, core midi, alsa)
to filter sysex, as it is handled more easily and efficiently at that level.
Realtime message are filtered in pm_realtime_filtered.
*/
+#define pm_status_filtered(status, filters) ((1 << (16 + ((status) >> 4))) & (filters))
-static int pm_status_filtered(int status, long filters)
-{
- status &= 0xF0; /* remove channel information */
+
+/*
return ((status == MIDI_NOTE_ON) && (filters & PM_FILT_NOTE))
|| ((status == MIDI_NOTE_OFF) && (filters & PM_FILT_NOTE))
|| ((status == MIDI_CHANNEL_AT) && (filters & PM_FILT_CHANNEL_AFTERTOUCH))
@@ -844,137 +987,198 @@ static int pm_status_filtered(int status, long filters)
|| ((status == MIDI_PITCHBEND) && (filters & PM_FILT_PITCHBEND));
}
+*/
+
+void pm_flush_sysex(PmInternal *midi, PmTimestamp timestamp)
+{
+ PmEvent event;
+
+ /* there may be nothing in the buffer */
+ if (midi->sysex_message_count == 0) return; /* nothing to flush */
+
+ event.message = midi->sysex_message;
+ event.timestamp = timestamp;
+ /* copied from pm_read_short, avoids filtering */
+ if (Pm_Enqueue(midi->queue, &event) == pmBufferOverflow) {
+ midi->sysex_in_progress = FALSE;
+ }
+ midi->sysex_message_count = 0;
+ midi->sysex_message = 0;
+}
-/* pm_read_short and pm_read_byte
+
+/* pm_read_short and pm_read_bytes
are the interface between system-dependent MIDI input handlers
and the system-independent PortMIDI code.
The input handler MUST obey these rules:
1) all short input messages must be sent to pm_read_short, which
enqueues them to a FIFO for the application.
- 2) eash sysex byte should be reported by calling pm_read_byte
- (which sets midi->sysex_in_progress). After the eox byte,
- pm_read_byte will clear sysex_in_progress and midi->flush
- (Note that the overflow flag is managed by pm_read_short
- and Pm_Read, so the supplier should not read or write it.)
+ 2) eash buffer of sysex bytes should be reported by calling pm_read_bytes
+ (which sets midi->sysex_in_progress). After the eox byte,
+ pm_read_bytes will clear sysex_in_progress
*/
-/* pm_read_short is the place where all input messages arrive from
+/* pm_read_short is the place where all input messages arrive from
system-dependent code such as pmwinmm.c. Here, the messages
- are entered into the PortMidi input buffer.
+ are entered into the PortMidi input buffer.
*/
-
- /* Algorithnm:
- if overflow or flush, return
- ATOMIC:
- enqueue data
- if buffer overflow, set overflow
- if buffer overflow and sysex_in_progress, set flush
- */
void pm_read_short(PmInternal *midi, PmEvent *event)
-{
+{
+#ifndef NEWBUFFER
long tail;
+#endif
int status;
/* arg checking */
assert(midi != NULL);
- assert(!Pm_HasHostError(midi));
/* midi filtering is applied here */
status = Pm_MessageStatus(event->message);
if (!pm_status_filtered(status, midi->filters)
- && !pm_realtime_filtered(status, midi->filters)
+ && (!is_real_time(status) ||
+ !pm_realtime_filtered(status, midi->filters))
&& !pm_channel_filtered(status, midi->channel_mask)) {
/* if sysex is in progress and we get a status byte, it had
better be a realtime message or the starting SYSEX byte;
otherwise, we exit the sysex_in_progress state
*/
- if (midi->sysex_in_progress && (status & MIDI_STATUS_MASK) &&
- !is_real_time(status) && status != MIDI_SYSEX ) {
+ if (midi->sysex_in_progress && (status & MIDI_STATUS_MASK)) {
+ /* two choices: real-time or not. If it's real-time, then
+ * this should be delivered as a sysex byte because it is
+ * embedded in a sysex message
+ */
+ if (is_real_time(status)) {
+ midi->sysex_message |=
+ (status << (8 * midi->sysex_message_count++));
+ if (midi->sysex_message_count == 4) {
+ pm_flush_sysex(midi, event->timestamp);
+ }
+ } else { /* otherwise, it's not real-time. This interrupts
+ * a sysex message in progress */
+ midi->sysex_in_progress = FALSE;
+ }
+#ifdef NEWBUFFER
+ } else if (Pm_Enqueue(midi->queue, event) == pmBufferOverflow) {
midi->sysex_in_progress = FALSE;
- midi->flush = FALSE;
}
-
- /* don't try to do anything more in an overflow state */
- if (midi->overflow || midi->flush) return;
-
- /* insert the message */
- tail = midi->tail;
- midi->buffer[tail++] = *event;
- if (tail == midi->buffer_len) tail = 0;
- if (tail == midi->head || midi->overflow) {
- midi->overflow = TRUE;
- if (midi->sysex_in_progress) midi->flush = TRUE;
- /* drop the rest of the message, this must be cleared
- by caller when EOX is received */
- return;
+#else
+ } else {
+ /* don't try to do anything more in an overflow state */
+ if (midi->overflow) return;
+
+ /* insert the message */
+ tail = midi->tail;
+ midi->buffer[tail++] = *event;
+ if (tail == midi->buffer_len) tail = 0;
+ if (tail == midi->head || midi->overflow) {
+ midi->overflow = TRUE;
+ midi->sysex_in_progress = FALSE;
+ /* drop the rest of the message, this must be cleared
+ by caller when EOX is received */
+ return;
+ }
+ midi->tail = tail; /* complete the write */
}
- midi->tail = tail; /* complete the write */
+#endif
}
}
-
-void pm_flush_sysex(PmInternal *midi, PmTimestamp timestamp)
+/* pm_read_bytes -- read one (partial) sysex msg from MIDI data */
+/*
+ * returns how many bytes processed
+ */
+unsigned int pm_read_bytes(PmInternal *midi, unsigned char *data,
+ int len, PmTimestamp timestamp)
{
+ unsigned int i = 0; /* index into data */
PmEvent event;
-
- /* there may be nothing in the buffer */
- if (midi->sysex_message_count == 0) return; /* nothing to flush */
-
- event.message = midi->sysex_message;
event.timestamp = timestamp;
- pm_read_short(midi, &event);
- midi->sysex_message_count = 0;
- midi->sysex_message = 0;
-}
-
-
-void pm_read_byte(PmInternal *midi, unsigned char byte, PmTimestamp timestamp)
-{
assert(midi);
- assert(!Pm_HasHostError(midi));
- /* here is the logic for controlling sysex_in_progress */
- if (midi->sysex_in_progress) {
- if (byte == MIDI_EOX) midi->sysex_in_progress = FALSE;
- else if (byte == MIDI_SYSEX) {
- /* problem: need to terminate the current sysex and start
- a new one
- */
- pm_flush_sysex(midi, timestamp);
- }
- } else if (byte == MIDI_SYSEX) {
- midi->sysex_in_progress = TRUE;
- } else {
- /* error: we're getting data bytes or EOX but we're no sysex is
- in progress. Drop the data. (Would it be better to report an
- error? Is this a host error or a pmBadData error? Is this
- ever possible?
- */
-#ifdef DEBUG
- printf("PortMidi debug msg: unexpected sysex data or EOX\n");
-#endif
- return;
+ /* note that since buffers may not have multiples of 4 bytes,
+ * pm_read_bytes may be called in the middle of an outgoing
+ * 4-byte PortMidi message. sysex_in_progress indicates that
+ * a sysex has been sent but no eox.
+ */
+ if (len == 0) return 0; /* sanity check */
+ if (!midi->sysex_in_progress) {
+ while (i < len) { /* process all data */
+ unsigned char byte = data[i++];
+ if (byte == MIDI_SYSEX &&
+ !pm_realtime_filtered(byte, midi->filters)) {
+ midi->sysex_in_progress = TRUE;
+ i--; /* back up so code below will get SYSEX byte */
+ break; /* continue looping below to process msg */
+ } else if (byte == MIDI_EOX) {
+ midi->sysex_in_progress = FALSE;
+ return i; /* done with one message */
+ } else if (byte & MIDI_STATUS_MASK) {
+ /* We're getting MIDI but no sysex in progress.
+ * Either the SYSEX status byte was dropped or
+ * the message was filtered. Drop the data, but
+ * send any embedded realtime bytes.
+ */
+ /* assume that this is a real-time message:
+ * it is an error to pass non-real-time messages
+ * to pm_read_bytes
+ */
+ event.message = byte;
+ pm_read_short(midi, &event);
+ }
+ } /* all bytes in the buffer are processed */
}
-
- if (pm_realtime_filtered(byte, midi->filters))
- return;
- /* this awkward expression places the bytes in increasingly higher-
- order bytes of the long message */
- midi->sysex_message |= (byte << (8 * midi->sysex_message_count++));
- if (midi->sysex_message_count == 4 || !midi->sysex_in_progress) {
- pm_flush_sysex(midi, timestamp);
- if (!midi->sysex_in_progress) midi->flush = FALSE;
+ /* Now, i<len implies sysex_in_progress. If sysex_in_progress
+ * becomes false in the loop, there must have been an overflow
+ * and we can just drop all remaining bytes
+ */
+ while (i < len && midi->sysex_in_progress) {
+ if (midi->sysex_message_count == 0 && i <= len - 4 &&
+ ((event.message = (((long) data[i]) |
+ (((long) data[i+1]) << 8) |
+ (((long) data[i+2]) << 16) |
+ (((long) data[i+3]) << 24))) &
+ 0x80808080) == 0) { /* all data, no status */
+ if (Pm_Enqueue(midi->queue, &event) == pmBufferOverflow) {
+ midi->sysex_in_progress = FALSE;
+ }
+ i += 4;
+ } else {
+ while (i < len) {
+ /* send one byte at a time */
+ unsigned char byte = data[i++];
+ if (is_real_time(byte) &&
+ pm_realtime_filtered(byte, midi->filters)) {
+ continue; /* real-time data is filtered, so omit */
+ }
+ midi->sysex_message |=
+ (byte << (8 * midi->sysex_message_count++));
+ if (byte == MIDI_EOX) {
+ midi->sysex_in_progress = FALSE;
+ pm_flush_sysex(midi, event.timestamp);
+ return i;
+ } else if (midi->sysex_message_count == 4) {
+ pm_flush_sysex(midi, event.timestamp);
+ /* after handling at least one non-data byte
+ * and reaching a 4-byte message boundary,
+ * resume trying to send 4 at a time in outer loop
+ */
+ break;
+ }
+ }
+ }
}
+ return i;
}
+#ifndef NEWBUFFER
+/* this code is apparently never called */
int pm_queue_full(PmInternal *midi)
{
long tail;
/* arg checking */
assert(midi != NULL);
- assert(!Pm_HasHostError(midi));
-
+
tail = midi->tail + 1;
if (tail == midi->buffer_len) tail = 0;
return tail == midi->head;
}
-
+#endif