Jack2  1.9.9
JackMidiAPI.cpp
1 /*
2 Copyright (C) 2007 Dmitry Baikov
3 Original JACK MIDI implementation Copyright (C) 2004 Ian Esten
4 
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.
9 
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.
14 
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.
18 
19 */
20 
21 #include "JackError.h"
22 #include "JackMidiPort.h"
23 #include <errno.h>
24 #include <string.h>
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31  LIB_EXPORT uint32_t jack_midi_get_event_count(void* port_buffer);
32 
33  LIB_EXPORT int jack_midi_event_get(jack_midi_event_t* event,
34  void* port_buffer, uint32_t event_index);
35 
36  LIB_EXPORT void jack_midi_clear_buffer(void* port_buffer);
37 
38  LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
39 
40  LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
41  jack_nframes_t time, size_t data_size);
42 
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);
45 
46  LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
47 
48 #ifdef __cplusplus
49 }
50 #endif
51 
52 using namespace Jack;
53 
54 LIB_EXPORT
55 uint32_t jack_midi_get_event_count(void* port_buffer)
56 {
57  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
58  if (!buf || !buf->IsValid()) {
59  return 0;
60  }
61  return buf->event_count;
62 }
63 
64 LIB_EXPORT
65 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
66 {
67  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
68  if (!buf || !buf->IsValid()) {
69  return -EINVAL;
70  }
71  if (event_index >= buf->event_count) {
72  return -ENOBUFS;
73  }
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;
79 }
80 
81 LIB_EXPORT
82 void jack_midi_clear_buffer(void* port_buffer)
83 {
84  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
85  if (buf && buf->IsValid()) {
86  buf->Reset(buf->nframes);
87  }
88 }
89 
90 LIB_EXPORT
91 size_t jack_midi_max_event_size(void* port_buffer)
92 {
93  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
94  if (buf && buf->IsValid())
95  return buf->MaxEventSize();
96  return 0;
97 }
98 
99 LIB_EXPORT
100 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
101 {
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;
106  }
107  if (! buf->IsValid()) {
108  jack_error("jack_midi_event_reserve: port buffer is invalid");
109  return 0;
110  }
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;
115  }
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;
120  }
121  return buf->ReserveEvent(time, data_size);
122 }
123 
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)
127 {
128  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
129  if (!buf && !buf->IsValid()) {
130  return -EINVAL;
131  }
132  if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
133  return -EINVAL;
134  }
135  jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
136  if (!dest) {
137  return -ENOBUFS;
138  }
139  memcpy(dest, data, data_size);
140  return 0;
141 }
142 
143 LIB_EXPORT
144 uint32_t jack_midi_get_lost_event_count(void* port_buffer)
145 {
146  JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
147  if (buf && buf->IsValid())
148  return buf->lost_events;
149  return 0;
150 }
LIB_EXPORT void jack_midi_clear_buffer(void *port_buffer)
Definition: JackMidiAPI.cpp:82
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void *port_buffer)
LIB_EXPORT size_t jack_midi_max_event_size(void *port_buffer)
Definition: JackMidiAPI.cpp:91
LIB_EXPORT uint32_t jack_midi_get_event_count(void *port_buffer)
Definition: JackMidiAPI.cpp:55
LIB_EXPORT int jack_midi_event_get(jack_midi_event_t *event, void *port_buffer, uint32_t event_index)
Definition: JackMidiAPI.cpp:65
LIB_EXPORT int jack_midi_event_write(void *port_buffer, jack_nframes_t time, const jack_midi_data_t *data, size_t data_size)
LIB_EXPORT jack_midi_data_t * jack_midi_event_reserve(void *port_buffer, jack_nframes_t time, size_t data_size)