30
#include "PlatformMidi.h"
34
char* GetInternalErrorStr(INT32 err) {
36
case MIDI_SUCCESS: return "";
37
case MIDI_NOT_SUPPORTED: return "feature not supported";
38
case MIDI_INVALID_DEVICEID: return "invalid device ID";
39
case MIDI_INVALID_HANDLE: return "internal error: invalid handle";
40
case MIDI_OUT_OF_MEMORY: return "out of memory";
48
char* MIDI_IN_InternalGetErrorString(INT32 err) {
49
char* result = GetInternalErrorStr(err);
51
#if USE_PLATFORM_MIDI_IN == TRUE
53
result = MIDI_IN_GetErrorStr(err);
57
result = GetInternalErrorStr(MIDI_NOT_SUPPORTED);
65
char* MIDI_OUT_InternalGetErrorString(INT32 err) {
66
char* result = GetInternalErrorStr(err);
68
#if USE_PLATFORM_MIDI_OUT == TRUE
70
result = MIDI_OUT_GetErrorStr(err);
74
result = GetInternalErrorStr(MIDI_NOT_SUPPORTED);
80
#if USE_MIDI_QUEUE == TRUE
84
MidiMessageQueue* MIDI_CreateQueue(int capacity) {
85
MidiMessageQueue* queue = (MidiMessageQueue*) malloc(sizeof(MidiMessageQueue) + ((capacity-1) * sizeof(MidiMessage)));
87
TRACE0("MIDI_CreateQueue\n");
88
queue->lock = MIDI_CreateLock();
89
queue->capacity = capacity;
92
queue->writeIndex = 0;
97
void MIDI_DestroyQueue(MidiMessageQueue* queue) {
99
void* lock = queue->lock;
103
MIDI_DestroyLock(lock);
104
TRACE0("MIDI_DestroyQueue\n");
110
int MIDI_QueueAddShort(MidiMessageQueue* queue, UINT32 packedMsg, INT64 timestamp, int overwrite) {
112
MIDI_Lock(queue->lock);
113
if (queue->size == queue->capacity) {
114
TRACE0("MIDI_QueueAddShort: overflow\n");
115
if (!overwrite || queue->queue[queue->writeIndex].locked) {
119
queue->readIndex = (queue->readIndex+1) % queue->capacity;
123
TRACE2("MIDI_QueueAddShort. index=%d, size=%d\n", queue->writeIndex, queue->size);
124
queue->queue[queue->writeIndex].type = SHORT_MESSAGE;
125
queue->queue[queue->writeIndex].data.s.packedMsg = packedMsg;
126
queue->queue[queue->writeIndex].timestamp = timestamp;
127
queue->writeIndex = (queue->writeIndex+1) % queue->capacity;
128
MIDI_Unlock(queue->lock);
134
int MIDI_QueueAddLong(MidiMessageQueue* queue, UBYTE* data, UINT32 size,
135
INT32 sysexIndex, INT64 timestamp, int overwrite) {
137
MIDI_Lock(queue->lock);
138
if (queue->size == queue->capacity) {
139
TRACE0("MIDI_QueueAddLong: overflow\n");
140
if (!overwrite || queue->queue[queue->writeIndex].locked) {
144
queue->readIndex = (queue->readIndex+1) % queue->capacity;
148
TRACE2("MIDI_QueueAddLong. index=%d, size=%d\n", queue->writeIndex, queue->size);
150
queue->queue[queue->writeIndex].type = LONG_MESSAGE;
151
queue->queue[queue->writeIndex].data.l.size = size;
152
queue->queue[queue->writeIndex].data.l.data = data;
153
queue->queue[queue->writeIndex].data.l.index = sysexIndex;
154
queue->queue[queue->writeIndex].timestamp = timestamp;
155
queue->writeIndex = (queue->writeIndex+1) % queue->capacity;
156
MIDI_Unlock(queue->lock);
163
MidiMessage* MIDI_QueueRead(MidiMessageQueue* queue) {
164
MidiMessage* msg = NULL;
166
MIDI_Lock(queue->lock);
167
if (queue->size > 0) {
168
msg = &(queue->queue[queue->readIndex]);
169
TRACE2("MIDI_QueueRead. index=%d, size=%d\n", queue->readIndex, queue->size);
172
MIDI_Unlock(queue->lock);
177
void MIDI_QueueRemove(MidiMessageQueue* queue, INT32 onlyLocked) {
179
MIDI_Lock(queue->lock);
180
if (queue->size > 0) {
181
MidiMessage* msg = &(queue->queue[queue->readIndex]);
182
if (!onlyLocked || msg->locked) {
183
TRACE2("MIDI_QueueRemove. index=%d, size=%d\n", queue->readIndex, queue->size);
184
queue->readIndex = (queue->readIndex+1) % queue->capacity;
189
MIDI_Unlock(queue->lock);
193
void MIDI_QueueClear(MidiMessageQueue* queue) {
195
MIDI_Lock(queue->lock);
197
queue->readIndex = 0;
198
queue->writeIndex = 0;
199
MIDI_Unlock(queue->lock);