merge new MTDM code from Fons' latest release.
[jack2.git] / common / JackMidiAPI.cpp
blob60c7c4b2fc63572004e3bf8492da921d4b63d9c3
1 /*
2 Copyright (C) 2007 Dmitry Baikov
3 Original JACK MIDI 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 #include "JackError.h"
22 #include "JackMidiPort.h"
23 #include <errno.h>
24 #include <string.h>
26 #ifdef __cplusplus
27 extern "C"
29 #endif
31 LIB_EXPORT uint32_t jack_midi_get_event_count(void* port_buffer);
33 LIB_EXPORT int jack_midi_event_get(jack_midi_event_t* event,
34 void* port_buffer, uint32_t event_index);
36 LIB_EXPORT void jack_midi_clear_buffer(void* port_buffer);
38 LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
40 LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
41 jack_nframes_t time, size_t data_size);
43 LIB_EXPORT int jack_midi_event_write(void* port_buffer,
44 jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
46 LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
48 #ifdef __cplusplus
50 #endif
52 using namespace Jack;
54 LIB_EXPORT
55 uint32_t jack_midi_get_event_count(void* port_buffer)
57 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
58 if (!buf || !buf->IsValid()) {
59 return 0;
61 return buf->event_count;
64 LIB_EXPORT
65 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
67 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
68 if (!buf || !buf->IsValid()) {
69 return -EINVAL;
71 if (event_index >= buf->event_count) {
72 return -ENOBUFS;
74 JackMidiEvent* ev = &buf->events[event_index];
75 event->time = ev->time;
76 event->size = ev->size;
77 event->buffer = ev->GetData(buf);
78 return 0;
81 LIB_EXPORT
82 void jack_midi_clear_buffer(void* port_buffer)
84 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
85 if (buf && buf->IsValid()) {
86 buf->Reset(buf->nframes);
90 LIB_EXPORT
91 size_t jack_midi_max_event_size(void* port_buffer)
93 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
94 if (buf && buf->IsValid())
95 return buf->MaxEventSize();
96 return 0;
99 LIB_EXPORT
100 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
102 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
103 if (! buf) {
104 jack_error("jack_midi_event_reserve: port buffer is set to NULL");
105 return 0;
107 if (! buf->IsValid()) {
108 jack_error("jack_midi_event_reserve: port buffer is invalid");
109 return 0;
111 if (time >= buf->nframes) {
112 jack_error("jack_midi_event_reserve: time parameter is out of range "
113 "(%lu >= %lu)", time, buf->nframes);
114 return 0;
116 if (buf->event_count && (buf->events[buf->event_count - 1].time > time)) {
117 jack_error("jack_midi_event_reserve: time parameter is earlier than "
118 "last reserved event");
119 return 0;
121 return buf->ReserveEvent(time, data_size);
124 LIB_EXPORT
125 int jack_midi_event_write(void* port_buffer,
126 jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
128 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
129 if (!buf && !buf->IsValid()) {
130 return -EINVAL;
132 if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
133 return -EINVAL;
135 jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
136 if (!dest) {
137 return -ENOBUFS;
139 memcpy(dest, data, data_size);
140 return 0;
143 LIB_EXPORT
144 uint32_t jack_midi_get_lost_event_count(void* port_buffer)
146 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
147 if (buf && buf->IsValid())
148 return buf->lost_events;
149 return 0;