Jack2 1.9.6

JackMidiPort.h

00001 /*
00002 Copyright (C) 2007 Dmitry Baikov
00003 Original JACK MIDI API implementation Copyright (C) 2004 Ian Esten
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU Lesser General Public License as published by
00007 the Free Software Foundation; either version 2.1 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public License
00016 along with this program; if not, write to the Free Software 
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 
00019 */
00020 
00021 #ifndef __JackMidiPort__
00022 #define __JackMidiPort__
00023 
00024 #include "types.h"
00025 #include "JackConstants.h"
00026 #include "JackPlatformPlug.h"
00027 #include <stddef.h>
00028 
00030 typedef unsigned char jack_midi_data_t;
00031 
00033 struct jack_midi_event_t
00034 {
00035     jack_nframes_t    time;   
00036     size_t            size;   
00037     jack_midi_data_t *buffer; 
00038 };
00039 
00041 #define JACK_DEFAULT_MIDI_TYPE "8 bit raw midi"
00042 
00043 namespace Jack
00044 {
00045 
00046 struct SERVER_EXPORT JackMidiEvent
00047 {
00048     // Most MIDI events are < 4 bytes in size, so we can save a lot, storing them inplace.
00049     enum { INLINE_SIZE_MAX = sizeof(jack_shmsize_t) };
00050 
00051     uint32_t time;
00052     jack_shmsize_t size;
00053     union {
00054         jack_shmsize_t   offset;
00055         jack_midi_data_t data[INLINE_SIZE_MAX];
00056     };
00057 
00058     jack_midi_data_t* GetData(void* buffer)
00059     {
00060         if (size <= INLINE_SIZE_MAX)
00061             return data;
00062         else
00063             return (jack_midi_data_t*)buffer + offset;
00064     }
00065 };
00066 
00067 /*
00068  * To store events with arbitrarily sized payload, but still have O(1) indexed access
00069  * we use a trick here:
00070  * Events are stored in an linear array from the beginning of the buffer,
00071  * but their data (if not inlined) is stored from the end of the same buffer.
00072  */
00073 
00074 struct JackMidiBuffer
00075 {
00076     enum { MAGIC = 0x900df00d };
00077 
00078     uint32_t magic;
00079     jack_shmsize_t buffer_size;
00080     jack_nframes_t nframes;
00081     jack_shmsize_t write_pos; 
00082     uint32_t event_count;
00083     uint32_t lost_events;
00084     uint32_t mix_index;
00085 
00086     JackMidiEvent events[1]; // Using 0 size does not compile with older GCC versions, so use 1 here.
00087 
00088     int IsValid() const
00089     {
00090         return magic == MAGIC;
00091     }
00092     void Reset(jack_nframes_t nframes);
00093     jack_shmsize_t MaxEventSize() const;
00094 
00095     // checks only size constraints.
00096     jack_midi_data_t* ReserveEvent(jack_nframes_t time, jack_shmsize_t size);
00097 };
00098 
00099 } // namespace Jack
00100 
00101 #endif