summaryrefslogtreecommitdiff
path: root/common/JackMidiPort.h
blob: d43c95aeff597e606817960f17fc7160a14bb5d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Copyright (C) 2007 Dmitry Baikov
Original JACK MIDI API implementation Copyright (C) 2004 Ian Esten

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#ifndef __JackMidiPort__
#define __JackMidiPort__

#include "types.h"
#include "JackConstants.h"
#include "JackPlatformPlug.h"
#include <stddef.h>

/** Type for raw event data contained in @ref jack_midi_event_t. */
typedef unsigned char jack_midi_data_t;

/** A Jack MIDI event. */
struct jack_midi_event_t
{
    jack_nframes_t    time;   /**< Sample index at which event is valid */
    size_t            size;   /**< Number of bytes of data in \a buffer */
    jack_midi_data_t *buffer; /**< Raw MIDI data */
};

/** A Jack MIDI port type. */
#define JACK_DEFAULT_MIDI_TYPE "8 bit raw midi"

namespace Jack
{

struct SERVER_EXPORT JackMidiEvent
{
    // Most MIDI events are < 4 bytes in size, so we can save a lot, storing them inplace.
    enum { INLINE_SIZE_MAX = sizeof(jack_shmsize_t) };

    uint32_t time;
    jack_shmsize_t size;
    union {
        jack_shmsize_t offset;
        jack_midi_data_t data[INLINE_SIZE_MAX];
    };

    jack_midi_data_t* GetData(void* buffer)
    {
        if (size <= INLINE_SIZE_MAX) {
            return data;
        } else {
            return (jack_midi_data_t*)buffer + offset;
        }
    }
};

/*
 * To store events with arbitrarily sized payload, but still have O(1) indexed access
 * we use a trick here:
 * Events are stored in an linear array from the beginning of the buffer,
 * but their data (if not inlined) is stored from the end of the same buffer.
 */

struct SERVER_EXPORT JackMidiBuffer
{
    enum { MAGIC = 0x900df00d };

    uint32_t magic;
    jack_shmsize_t buffer_size;
    jack_nframes_t nframes;
    jack_shmsize_t write_pos; //!< data write position from the end of the buffer.
    uint32_t event_count;
    uint32_t lost_events;

    JackMidiEvent events[1]; // Using 0 size does not compile with older GCC versions, so use 1 here.

    int IsValid() const
    {
        return magic == MAGIC;
    }
    void Reset(jack_nframes_t nframes);
    jack_shmsize_t MaxEventSize() const;

    // checks only size constraints.
    jack_midi_data_t* ReserveEvent(jack_nframes_t time, jack_shmsize_t size);
};

void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes);

} // namespace Jack

#endif