alsa: fix the poll interval
[jack2.git] / common / JackMidiAPI.cpp
blob61d77e125f8a068654b6a9c480c31db00e98ac7e
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 void jack_midi_reset_buffer(void* port_buffer);
40 LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
42 LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
43 jack_nframes_t time, size_t data_size);
45 LIB_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 LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
50 #ifdef __cplusplus
52 #endif
54 using namespace Jack;
56 LIB_EXPORT
57 uint32_t jack_midi_get_event_count(void* port_buffer)
59 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
60 if (!buf || !buf->IsValid()) {
61 return 0;
63 return buf->event_count;
66 LIB_EXPORT
67 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
69 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
70 if (!buf || !buf->IsValid()) {
71 return -EINVAL;
73 if (event_index >= buf->event_count) {
74 return -ENOBUFS;
76 JackMidiEvent* ev = &buf->events[event_index];
77 event->time = ev->time;
78 event->size = ev->size;
79 event->buffer = ev->GetData(buf);
80 return 0;
83 LIB_EXPORT
84 void jack_midi_clear_buffer(void* port_buffer)
86 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
87 if (buf && buf->IsValid()) {
88 buf->Reset(buf->nframes);
92 LIB_EXPORT
93 void jack_midi_reset_buffer(void* port_buffer)
95 MidiBufferInit(port_buffer, BUFFER_SIZE_MAX, BUFFER_SIZE_MAX);
98 LIB_EXPORT
99 size_t jack_midi_max_event_size(void* port_buffer)
101 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
102 if (buf && buf->IsValid()) {
103 return buf->MaxEventSize();
105 return 0;
108 LIB_EXPORT
109 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
111 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
112 if (! buf) {
113 jack_error("jack_midi_event_reserve: port buffer is set to NULL");
114 return 0;
116 if (! buf->IsValid()) {
117 jack_error("jack_midi_event_reserve: port buffer is invalid");
118 return 0;
120 if (time >= buf->nframes) {
121 jack_error("jack_midi_event_reserve: time parameter is out of range "
122 "(%lu >= %lu)", time, buf->nframes);
123 return 0;
125 if (buf->event_count && (buf->events[buf->event_count - 1].time > time)) {
126 jack_error("jack_midi_event_reserve: time parameter is earlier than "
127 "last reserved event");
128 return 0;
130 return buf->ReserveEvent(time, data_size);
133 LIB_EXPORT
134 int jack_midi_event_write(void* port_buffer,
135 jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
137 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
138 if (!buf || !buf->IsValid()) {
139 return -EINVAL;
141 if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
142 return -EINVAL;
144 jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
145 if (!dest) {
146 return -ENOBUFS;
148 memcpy(dest, data, data_size);
149 return 0;
152 LIB_EXPORT
153 uint32_t jack_midi_get_lost_event_count(void* port_buffer)
155 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
156 if (buf && buf->IsValid()) {
157 return buf->lost_events;
159 return 0;