2 * Carla JACK API for external applications
3 * Copyright (C) 2016-2022 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or 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 General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "libjack.hpp"
20 CARLA_BACKEND_USE_NAMESPACE
22 // --------------------------------------------------------------------------------------------------------------------
25 jack_nframes_t
jack_midi_get_event_count(void* buf
)
27 const JackMidiPortBufferBase
* const jbasebuf((const JackMidiPortBufferBase
*)buf
);
28 CARLA_SAFE_ASSERT_RETURN(jbasebuf
!= nullptr, 0);
29 CARLA_SAFE_ASSERT_RETURN(jbasebuf
->isInput
, 0);
31 if (jbasebuf
->isDummy
)
34 const JackMidiPortBufferOnStack
* const jmidibuf((const JackMidiPortBufferOnStack
*)jbasebuf
);
36 return jmidibuf
->count
;
40 int jack_midi_event_get(jack_midi_event_t
* ev
, void* buf
, uint32_t index
)
42 const JackMidiPortBufferBase
* const jbasebuf((const JackMidiPortBufferBase
*)buf
);
43 CARLA_SAFE_ASSERT_RETURN(jbasebuf
!= nullptr, EFAULT
);
44 CARLA_SAFE_ASSERT_RETURN(jbasebuf
->isInput
, EFAULT
);
45 CARLA_SAFE_ASSERT_RETURN(!jbasebuf
->isDummy
, EFAULT
);
47 const JackMidiPortBufferOnStack
* const jmidibuf((const JackMidiPortBufferOnStack
*)jbasebuf
);
49 if (index
>= jmidibuf
->count
)
52 std::memcpy(ev
, &jmidibuf
->events
[index
], sizeof(jack_midi_event_t
));
57 void jack_midi_clear_buffer(void* buf
)
59 JackMidiPortBufferBase
* const jbasebuf((JackMidiPortBufferBase
*)buf
);
60 CARLA_SAFE_ASSERT_RETURN(jbasebuf
!= nullptr,);
61 CARLA_SAFE_ASSERT_RETURN(! jbasebuf
->isInput
,);
63 if (jbasebuf
->isDummy
)
66 JackMidiPortBufferOnStack
* const jmidibuf((JackMidiPortBufferOnStack
*)jbasebuf
);
69 jmidibuf
->bufferPoolPos
= 0;
70 std::memset(jmidibuf
->bufferPool
, 0, JackMidiPortBufferBase::kBufferPoolSize
);
74 void jack_midi_reset_buffer(void* buf
)
76 jack_midi_clear_buffer(buf
);
80 size_t jack_midi_max_event_size(void*)
82 return JackMidiPortBufferBase::kMaxEventSize
;
86 jack_midi_data_t
* jack_midi_event_reserve(void* buf
, jack_nframes_t frame
, size_t size
)
88 JackMidiPortBufferBase
* const jbasebuf((JackMidiPortBufferBase
*)buf
);
89 CARLA_SAFE_ASSERT_RETURN(jbasebuf
!= nullptr, nullptr);
90 CARLA_SAFE_ASSERT_RETURN(! jbasebuf
->isInput
, nullptr);
91 CARLA_SAFE_ASSERT_RETURN(size
< JackMidiPortBufferBase::kMaxEventSize
, nullptr);
93 if (jbasebuf
->isDummy
)
96 // broken jack applications, wow...
100 JackMidiPortBufferOnStack
* const jmidibuf((JackMidiPortBufferOnStack
*)jbasebuf
);
102 if (jmidibuf
->count
>= JackMidiPortBufferBase::kMaxEventCount
)
104 if (jmidibuf
->bufferPoolPos
+ size
>= JackMidiPortBufferBase::kBufferPoolSize
)
107 jack_midi_data_t
* const jmdata
= jmidibuf
->bufferPool
+ jmidibuf
->bufferPoolPos
;
108 jmidibuf
->bufferPoolPos
+= size
;
110 jmidibuf
->events
[jmidibuf
->count
++] = { frame
, size
, jmdata
};
111 std::memset(jmdata
, 0, size
);
116 int jack_midi_event_write(void* buf
, jack_nframes_t frame
, const jack_midi_data_t
* data
, size_t size
)
118 JackMidiPortBufferBase
* const jbasebuf((JackMidiPortBufferBase
*)buf
);
119 CARLA_SAFE_ASSERT_RETURN(jbasebuf
!= nullptr, EFAULT
);
120 CARLA_SAFE_ASSERT_RETURN(! jbasebuf
->isInput
, EINVAL
);
121 CARLA_SAFE_ASSERT_RETURN(size
< JackMidiPortBufferBase::kMaxEventSize
, ENOBUFS
);
123 if (jbasebuf
->isDummy
)
126 // broken jack applications, wow...
130 JackMidiPortBufferOnStack
* const jmidibuf((JackMidiPortBufferOnStack
*)jbasebuf
);
132 if (jmidibuf
->count
>= JackMidiPortBufferBase::kMaxEventCount
)
134 if (jmidibuf
->bufferPoolPos
+ size
>= JackMidiPortBufferBase::kBufferPoolSize
)
137 jack_midi_data_t
* const jmdata
= jmidibuf
->bufferPool
+ jmidibuf
->bufferPoolPos
;
138 jmidibuf
->bufferPoolPos
+= size
;
140 jmidibuf
->events
[jmidibuf
->count
++] = { frame
, size
, jmdata
};
141 std::memcpy(jmdata
, data
, size
);
146 uint32_t jack_midi_get_lost_event_count(void*)
151 // --------------------------------------------------------------------------------------------------------------------