00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef LV2_MIDIFUNCTIONS
00029 #define LV2_MIDIFUNCTIONS
00030
00031 #include <string.h>
00032
00033 #include "lv2-miditype.h"
00034
00035
00040 typedef struct {
00041
00043 LV2_MIDI* midi;
00044
00046 uint32_t frame_count;
00047
00049 uint32_t position;
00050
00051 } LV2_MIDIState;
00052
00053
00062 static double lv2midi_get_event(LV2_MIDIState* state,
00063 double* timestamp,
00064 uint32_t* size,
00065 unsigned char** data) {
00066
00067 if (state->position >= state->midi->size) {
00068 state->position = state->midi->size;
00069 *timestamp = state->frame_count;
00070 *size = 0;
00071 *data = NULL;
00072 return *timestamp;
00073 }
00074
00075 *timestamp = *(double*)(state->midi->data + state->position);
00076 *size = *(uint32_t*)(state->midi->data + state->position + sizeof(double));
00077 *data = state->midi->data + state->position +
00078 sizeof(double) + sizeof(uint32_t);
00079 return *timestamp;
00080 }
00081
00082
00086 static double lv2midi_step(LV2_MIDIState* state) {
00087
00088 if (state->position + sizeof(double) + sizeof(uint32_t) >= state->midi->size) {
00089 state->position = state->midi->size;
00090 return state->frame_count;
00091 }
00092
00093 state->position += sizeof(double);
00094 uint32_t size = *(uint32_t*)(state->midi->data + state->position);
00095 state->position += sizeof(uint32_t);
00096 state->position += size;
00097
00098 if (state->position >= state->midi->size)
00099 return state->frame_count;
00100
00101 return *(double*)(state->midi->data + state->position);
00102 }
00103
00104
00109 static int lv2midi_put_event(LV2_MIDIState* state,
00110 double timestamp,
00111 uint32_t size,
00112 const unsigned char* data) {
00113
00114 if (state->midi->capacity - state->midi->size <
00115 sizeof(double) + sizeof(uint32_t) + size)
00116 return -1;
00117
00118 *(double*)(state->midi->data + state->midi->size) = timestamp;
00119 state->midi->size += sizeof(double);
00120 *(uint32_t*)(state->midi->data + state->midi->size) = size;
00121 state->midi->size += sizeof(uint32_t);
00122 memcpy(state->midi->data + state->midi->size, data, size);
00123 state->midi->size += size;
00124
00125 ++state->midi->event_count;
00126
00127 return 0;
00128 }
00129
00130
00131 #endif
00132