LV2_MIDI | Helper functions

lv2-midifunctions.h

Go to the documentation of this file.
00001 /****************************************************************************
00002     
00003     lv2-midifunctions.h - support file for using MIDI in LV2 plugins
00004     
00005     Copyright (C) 2006-2007  Lars Luthman <lars.luthman@gmail.com>
00006     
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU Lesser General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011     
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016     
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307  USA
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 

Generated on Thu Feb 8 12:45:15 2007 by  doxygen 1.5.1