1 /****************************************************************************
2 
3     lv2-midifunctions.h - support file for using MIDI in LV2 plugins
4 
5     Copyright (C) 2006  Lars Luthman <lars.luthman@gmail.com>
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU Lesser General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307  USA
20 
21 ****************************************************************************/
22 
23 #ifndef LV2_MIDIFUNCTIONS
24 #define LV2_MIDIFUNCTIONS
25 
26 #include "lv2-miditype.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef struct {
33   LV2_MIDI* midi;
34   uint32_t frame_count;
35   uint32_t position;
36 } LV2_MIDIState;
37 
38 
lv2midi_get_event(LV2_MIDIState * state,double * timestamp,uint32_t * size,unsigned char ** data)39 inline double lv2midi_get_event(LV2_MIDIState* state,
40                                 double* timestamp,
41                                 uint32_t* size,
42                                 unsigned char** data) {
43 
44   if (state->position >= state->midi->size) {
45     state->position = state->midi->size;
46     *timestamp = state->frame_count;
47     *size = 0;
48     *data = NULL;
49     return *timestamp;
50   }
51 
52   *timestamp = *(double*)(state->midi->data + state->position);
53   *size = (uint32_t)*(size_t*)(state->midi->data + state->position + sizeof(double));
54   *data = state->midi->data + state->position +
55     sizeof(double) + sizeof(size_t);
56   return *timestamp;
57 }
58 
59 
lv2midi_step(LV2_MIDIState * state)60 inline double lv2midi_step(LV2_MIDIState* state) {
61 
62   if (state->position >= state->midi->size) {
63     state->position = state->midi->size;
64     return state->frame_count;
65   }
66 
67   state->position += (uint32_t)sizeof(double);
68   size_t size = *(size_t*)(state->midi->data + state->position);
69   state->position += (uint32_t)sizeof(size_t);
70   state->position += (uint32_t)size;
71   return *(double*)(state->midi->data + state->position);
72 }
73 
74 
lv2midi_put_event(LV2_MIDIState * state,double timestamp,uint32_t size,const unsigned char * data)75 inline void lv2midi_put_event(LV2_MIDIState* state,
76                              double timestamp,
77                              uint32_t size,
78                              const unsigned char* data) {
79 
80   if (state->midi->size + sizeof(double) + sizeof(size_t) + size < state->midi->capacity)
81   {
82     *((double*)(state->midi->data + state->midi->size)) = timestamp;
83     state->midi->size += (uint32_t)sizeof(double);
84     *((size_t*)(state->midi->data + state->midi->size)) = size;
85     state->midi->size += (uint32_t)sizeof(size_t);
86     memcpy(state->midi->data + state->midi->size, data, size);
87 
88     state->midi->size += size;
89     state->midi->event_count++;
90   }
91 }
92 
93 #ifdef __cplusplus
94 }  /* extern "C" */
95 #endif
96 
97 #endif
98 
99