Merge branch 'master' into no-self-connect
[jack2.git] / common / JackMidiAPI.cpp
blobb06a93a22c9ce12a2fd3efd5b4dc7f87c1cdffbb
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 "JackCompilerDeps.h"
24 #include <errno.h>
25 #include <string.h>
26 #include "JackSystemDeps.h"
28 #ifdef __cplusplus
29 extern "C"
31 #endif
33 EXPORT jack_nframes_t jack_midi_get_event_count(void* port_buffer);
35 EXPORT int jack_midi_event_get(jack_midi_event_t* event,
36 void* port_buffer, jack_nframes_t event_index);
38 EXPORT void jack_midi_clear_buffer(void* port_buffer);
40 EXPORT size_t jack_midi_max_event_size(void* port_buffer);
42 EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
43 jack_nframes_t time, size_t data_size);
45 EXPORT int jack_midi_event_write(void* port_buffer,
46 jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
48 EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
50 #ifdef __cplusplus
52 #endif
54 using namespace Jack;
56 EXPORT
57 jack_nframes_t jack_midi_get_event_count(void* port_buffer)
59 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
60 if (!buf || !buf->IsValid())
61 return 0;
62 return buf->event_count;
65 EXPORT
66 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, jack_nframes_t event_index)
68 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
69 if (!buf || !buf->IsValid())
70 return -EINVAL;
71 if (event_index >= buf->event_count)
72 return -ENOBUFS;
73 JackMidiEvent* ev = &buf->events[event_index];
74 event->time = ev->time;
75 event->size = ev->size;
76 event->buffer = ev->GetData(buf);
77 return 0;
80 EXPORT
81 void jack_midi_clear_buffer(void* port_buffer)
83 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
84 if (buf && buf->IsValid())
85 buf->Reset(buf->nframes);
88 EXPORT
89 size_t jack_midi_max_event_size(void* port_buffer)
91 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
92 if (buf && buf->IsValid())
93 return buf->MaxEventSize();
94 return 0;
97 EXPORT
98 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
100 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
101 if (!buf && !buf->IsValid())
102 return 0;
103 if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
104 return 0;
105 return buf->ReserveEvent(time, data_size);
108 EXPORT
109 int jack_midi_event_write(void* port_buffer,
110 jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
112 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
113 if (!buf && !buf->IsValid())
114 return -EINVAL;
115 if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
116 return -EINVAL;
117 jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
118 if (!dest)
119 return -ENOBUFS;
120 memcpy(dest, data, data_size);
121 return 0;
124 EXPORT
125 jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer)
127 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
128 if (buf && buf->IsValid())
129 return buf->lost_events;
130 return 0;