Merge tag 'v1.9.22' into LADI/main
[jack2.git] / common / JackMidiPort.h
blobd43c95aeff597e606817960f17fc7160a14bb5d0
1 /*
2 Copyright (C) 2007 Dmitry Baikov
3 Original JACK MIDI API implementation Copyright (C) 2004 Ian Esten
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #ifndef __JackMidiPort__
22 #define __JackMidiPort__
24 #include "types.h"
25 #include "JackConstants.h"
26 #include "JackPlatformPlug.h"
27 #include <stddef.h>
29 /** Type for raw event data contained in @ref jack_midi_event_t. */
30 typedef unsigned char jack_midi_data_t;
32 /** A Jack MIDI event. */
33 struct jack_midi_event_t
35 jack_nframes_t time; /**< Sample index at which event is valid */
36 size_t size; /**< Number of bytes of data in \a buffer */
37 jack_midi_data_t *buffer; /**< Raw MIDI data */
40 /** A Jack MIDI port type. */
41 #define JACK_DEFAULT_MIDI_TYPE "8 bit raw midi"
43 namespace Jack
46 struct SERVER_EXPORT JackMidiEvent
48 // Most MIDI events are < 4 bytes in size, so we can save a lot, storing them inplace.
49 enum { INLINE_SIZE_MAX = sizeof(jack_shmsize_t) };
51 uint32_t time;
52 jack_shmsize_t size;
53 union {
54 jack_shmsize_t offset;
55 jack_midi_data_t data[INLINE_SIZE_MAX];
58 jack_midi_data_t* GetData(void* buffer)
60 if (size <= INLINE_SIZE_MAX) {
61 return data;
62 } else {
63 return (jack_midi_data_t*)buffer + offset;
69 * To store events with arbitrarily sized payload, but still have O(1) indexed access
70 * we use a trick here:
71 * Events are stored in an linear array from the beginning of the buffer,
72 * but their data (if not inlined) is stored from the end of the same buffer.
75 struct SERVER_EXPORT JackMidiBuffer
77 enum { MAGIC = 0x900df00d };
79 uint32_t magic;
80 jack_shmsize_t buffer_size;
81 jack_nframes_t nframes;
82 jack_shmsize_t write_pos; //!< data write position from the end of the buffer.
83 uint32_t event_count;
84 uint32_t lost_events;
86 JackMidiEvent events[1]; // Using 0 size does not compile with older GCC versions, so use 1 here.
88 int IsValid() const
90 return magic == MAGIC;
92 void Reset(jack_nframes_t nframes);
93 jack_shmsize_t MaxEventSize() const;
95 // checks only size constraints.
96 jack_midi_data_t* ReserveEvent(jack_nframes_t time, jack_shmsize_t size);
99 void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes);
101 } // namespace Jack
103 #endif