Fix crash with clap plugins without MIDI input
[carla.git] / source / libjack / libjack_midi.cpp
blobd8a5a0b61b06a355d499be968b8468d2ae91dbe5
1 /*
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 // --------------------------------------------------------------------------------------------------------------------
24 CARLA_PLUGIN_EXPORT
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)
32 return 0;
34 const JackMidiPortBufferOnStack* const jmidibuf((const JackMidiPortBufferOnStack*)jbasebuf);
36 return jmidibuf->count;
39 CARLA_PLUGIN_EXPORT
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)
50 return ENODATA;
52 std::memcpy(ev, &jmidibuf->events[index], sizeof(jack_midi_event_t));
53 return 0;
56 CARLA_PLUGIN_EXPORT
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)
64 return;
66 JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
68 jmidibuf->count = 0;
69 jmidibuf->bufferPoolPos = 0;
70 std::memset(jmidibuf->bufferPool, 0, JackMidiPortBufferBase::kBufferPoolSize);
73 CARLA_PLUGIN_EXPORT
74 void jack_midi_reset_buffer(void* buf)
76 jack_midi_clear_buffer(buf);
79 CARLA_PLUGIN_EXPORT
80 size_t jack_midi_max_event_size(void*)
82 return JackMidiPortBufferBase::kMaxEventSize;
85 CARLA_PLUGIN_EXPORT
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)
94 return nullptr;
96 // broken jack applications, wow...
97 if (size == 0)
98 return nullptr;
100 JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
102 if (jmidibuf->count >= JackMidiPortBufferBase::kMaxEventCount)
103 return nullptr;
104 if (jmidibuf->bufferPoolPos + size >= JackMidiPortBufferBase::kBufferPoolSize)
105 return nullptr;
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);
112 return jmdata;
115 CARLA_PLUGIN_EXPORT
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)
124 return EINVAL;
126 // broken jack applications, wow...
127 if (size == 0)
128 return EINVAL;
130 JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
132 if (jmidibuf->count >= JackMidiPortBufferBase::kMaxEventCount)
133 return ENOBUFS;
134 if (jmidibuf->bufferPoolPos + size >= JackMidiPortBufferBase::kBufferPoolSize)
135 return ENOBUFS;
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);
142 return 0;
145 CARLA_PLUGIN_EXPORT
146 uint32_t jack_midi_get_lost_event_count(void*)
148 return 0;
151 // --------------------------------------------------------------------------------------------------------------------