Jack2 1.9.12

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 void jack_midi_reset_buffer(void* port_buffer);
39
40 LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
41
42 LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
43 jack_nframes_t time, size_t data_size);
44
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);
47
48 LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
49
50 #ifdef __cplusplus
51 }
52 #endif
53
54 using namespace Jack;
55
56 LIB_EXPORT
57 uint32_t jack_midi_get_event_count(void* port_buffer)
58 {
59 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
60 if (!buf || !buf->IsValid()) {
61 return 0;
62 }
63 return buf->event_count;
64 }
65
66 LIB_EXPORT
67 int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
68 {
69 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
70 if (!buf || !buf->IsValid()) {
71 return -EINVAL;
72 }
73 if (event_index >= buf->event_count) {
74 return -ENOBUFS;
75 }
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;
81 }
82
83 LIB_EXPORT
84 void jack_midi_clear_buffer(void* port_buffer)
85 {
86 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
87 if (buf && buf->IsValid()) {
88 buf->Reset(buf->nframes);
89 }
90 }
91
92 LIB_EXPORT
93 void jack_midi_reset_buffer(void* port_buffer)
94 {
95 MidiBufferInit(port_buffer, BUFFER_SIZE_MAX, BUFFER_SIZE_MAX);
96 }
97
98 LIB_EXPORT
99 size_t jack_midi_max_event_size(void* port_buffer)
100 {
101 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
102 if (buf && buf->IsValid()) {
103 return buf->MaxEventSize();
104 }
105 return 0;
106 }
107
108 LIB_EXPORT
109 jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
110 {
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;
115 }
116 if (! buf->IsValid()) {
117 jack_error("jack_midi_event_reserve: port buffer is invalid");
118 return 0;
119 }
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;
124 }
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;
129 }
130 return buf->ReserveEvent(time, data_size);
131 }
132
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)
136 {
137 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
138 if (!buf || !buf->IsValid()) {
139 return -EINVAL;
140 }
141 if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
142 return -EINVAL;
143 }
144 jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
145 if (!dest) {
146 return -ENOBUFS;
147 }
148 memcpy(dest, data, data_size);
149 return 0;
150 }
151
152 LIB_EXPORT
153 uint32_t jack_midi_get_lost_event_count(void* port_buffer)
154 {
155 JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
156 if (buf && buf->IsValid()) {
157 return buf->lost_events;
158 }
159 return 0;
160 }
jack_midi_clear_buffer
LIB_EXPORT void jack_midi_clear_buffer(void *port_buffer)
Definition: JackMidiAPI.cpp:84
jack_midi_reset_buffer
LIB_EXPORT void jack_midi_reset_buffer(void *port_buffer)
Definition: JackMidiAPI.cpp:93
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_midi_event_t
Definition: JackMidiPort.h:33
jack_midi_get_lost_event_count
LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void *port_buffer)
Definition: JackMidiAPI.cpp:153
jack_midi_max_event_size
LIB_EXPORT size_t jack_midi_max_event_size(void *port_buffer)
Definition: JackMidiAPI.cpp:99
jack_midi_get_event_count
LIB_EXPORT uint32_t jack_midi_get_event_count(void *port_buffer)
Definition: JackMidiAPI.cpp:57
Jack::JackMidiEvent
Definition: JackMidiPort.h:46
jack_midi_event_get
LIB_EXPORT int jack_midi_event_get(jack_midi_event_t *event, void *port_buffer, uint32_t event_index)
Definition: JackMidiAPI.cpp:67
Jack::JackMidiBuffer
Definition: JackMidiPort.h:75
jack_midi_event_write
LIB_EXPORT int jack_midi_event_write(void *port_buffer, jack_nframes_t time, const jack_midi_data_t *data, size_t data_size)
Definition: JackMidiAPI.cpp:134
jack_midi_event_reserve
LIB_EXPORT jack_midi_data_t * jack_midi_event_reserve(void *port_buffer, jack_nframes_t time, size_t data_size)
Definition: JackMidiAPI.cpp:109