Debian Codegraph @

BambooTracker/midi/midi.cpp

raw
1 /*
2 * Copyright (C) 2019-2021 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "midi.hpp"
27 #include <stdio.h>
28 #include "RtMidi.h"
29 #include "utils.hpp"
30
31 namespace
32 {
33 // Defines
34 constexpr int MIDI_BUFFER_SIZE = 8192;
35
36 const std::string MIDI_INP_CLIENT_NAME = "BambooTracker Rx";
37 const std::string MIDI_INP_PORT_NAME = "BambooTracker MIDI In";
38
39 constexpr bool MIDI_INP_IGNORE_SYSEX = false;
40 constexpr bool MIDI_INP_IGNORE_TIME = false;
41 constexpr bool MIDI_INP_IGNORE_SENSE = true;
42 }
43
44 std::unique_ptr<MidiInterface> MidiInterface::instance_;
45
46 MidiInterface& MidiInterface::getInstance()
47 {
48 if (instance_)
49 return *instance_;
50
51 MidiInterface *out = new MidiInterface;
52 instance_.reset(out);
53 return *out;
54 }
55
56 MidiInterface::MidiInterface() : hasOpenInputPort_(false) {}
57
58 MidiInterface::~MidiInterface() = default;
59
60 std::string MidiInterface::currentApiName() const
61 {
62 RtMidi::Api api = inputClient_ ? inputClient_->getCurrentApi()
63 : RtMidi::RTMIDI_DUMMY;
64 return RtMidi::getApiDisplayName(api);
65 }
66
67 std::vector<std::string> MidiInterface::getAvailableApis() const
68 {
69 std::vector<RtMidi::Api> apis;
70 RtMidi::getCompiledApi(apis);
71 std::vector<std::string> list;
72 for (const auto& apiAvailable : apis)
73 list.push_back(RtMidi::getApiDisplayName(apiAvailable));
74 return (list.empty() ? std::vector<std::string>({ "" }) : list);
75 }
76
77 bool MidiInterface::isAvailableApi(const std::string& api) const
78 {
79 const std::vector<std::string> apis = getAvailableApis();
80 return (utils::find(apis, api) != apis.end());
81 }
82
83 bool MidiInterface::switchApi(std::string api, std::string* errDetail)
84 {
85 std::vector<RtMidi::Api> apis;
86 RtMidi::getCompiledApi(apis);
87
88 for (const auto& apiAvailable : apis) {
89 if (api == RtMidi::getApiDisplayName(apiAvailable)) {
90 if (inputClient_ && apiAvailable == inputClient_->getCurrentApi())
91 return true;
92
93 RtMidiIn *inputClient = nullptr;
94 try {
95 inputClient = new RtMidiIn(apiAvailable, MIDI_INP_CLIENT_NAME, MIDI_BUFFER_SIZE);
96 if (errDetail) *errDetail = "";
97 }
98 catch (RtMidiError &error) {
99 error.printMessage();
100 if (errDetail) *errDetail = error.getMessage();
101 }
102
103 if (inputClient) {
104 inputClient->ignoreTypes(MIDI_INP_IGNORE_SYSEX, MIDI_INP_IGNORE_TIME, MIDI_INP_IGNORE_SENSE);
105 inputClient->setCallback(&onMidiInput, this);
106 }
107 inputClient_.reset(inputClient);
108 hasOpenInputPort_ = false;
109
110 return (inputClient != nullptr);
111 }
112 }
113
114 inputClient_ = nullptr;
115 if (errDetail) *errDetail = "No available midi api.";
116 return false;
117 }
118
119 bool MidiInterface::supportsVirtualPort() const
120 {
121 if (!inputClient_) return false;
122
123 switch (inputClient_->getCurrentApi()) {
124 case RtMidi::MACOSX_CORE: case RtMidi::LINUX_ALSA: case RtMidi::UNIX_JACK:
125 return true;
126 default:
127 return false;
128 }
129 }
130
131 bool MidiInterface::supportsVirtualPort(std::string api) const
132 {
133 std::vector<RtMidi::Api> apis;
134 RtMidi::getCompiledApi(apis);
135 RtMidi::Api apiType = RtMidi::RTMIDI_DUMMY;
136 for (const auto& apiAvailable : apis) {
137 if (api == RtMidi::getApiDisplayName(apiAvailable)) {
138 apiType = apiAvailable;
139 break;
140 }
141 }
142
143 switch (apiType) {
144 case RtMidi::MACOSX_CORE: case RtMidi::LINUX_ALSA: case RtMidi::UNIX_JACK:
145 return true;
146 default:
147 return false;
148 }
149 }
150
151 std::vector<std::string> MidiInterface::getRealInputPorts()
152 {
153 if (!inputClient_) return { "" }; // Error
154
155 RtMidiIn &client = *inputClient_;
156 unsigned count = client.getPortCount();
157
158 std::vector<std::string> ports;
159 ports.reserve(count);
160
161 for (unsigned i = 0; i < count; ++i)
162 ports.push_back(client.getPortName(i));
163 return ports;
164 }
165
166 std::vector<std::string> MidiInterface::getRealInputPorts(const std::string& api)
167 {
168 std::vector<RtMidi::Api> apis;
169 RtMidi::getCompiledApi(apis);
170
171 RtMidi::Api apiType = RtMidi::RTMIDI_DUMMY;
172 for (const auto& apiAvailable : apis) {
173 if (api == RtMidi::getApiDisplayName(apiAvailable)) {
174 apiType = apiAvailable;
175 break;
176 }
177 }
178 if (apiType == RtMidi::RTMIDI_DUMMY) {
179 return { "" }; // Error
180 }
181
182 std::vector<std::string> ports;
183 try {
184 auto client = std::make_unique<RtMidiIn>(apiType);
185 unsigned count = client->getPortCount();
186 ports.reserve(count);
187 for (unsigned i = 0; i < count; ++i)
188 ports.push_back(client->getPortName(i));
189 }
190 catch (RtMidiError& error) {
191 error.printMessage();
192 }
193 return ports;
194 }
195
196 void MidiInterface::closeInputPort()
197 {
198 if (!inputClient_) return;
199
200 RtMidiIn &client = *inputClient_;
201 if (hasOpenInputPort_) {
202 client.closePort();
203 hasOpenInputPort_ = false;
204 }
205 }
206
207 bool MidiInterface::openInputPort(unsigned port, std::string* errDetail)
208 {
209 if (!inputClient_) {
210 if (errDetail) *errDetail = "Not opened input client.";
211 hasOpenInputPort_ = false;
212 return false;
213 }
214
215 try {
216 RtMidiIn &client = *inputClient_;
217 closeInputPort();
218
219 if (port == ~0u) {
220 client.openVirtualPort(MIDI_INP_PORT_NAME);
221 hasOpenInputPort_ = true;
222 }
223 else {
224 client.openPort(port, MIDI_INP_PORT_NAME);
225 hasOpenInputPort_ = client.isPortOpen();
226 }
227 if (errDetail) *errDetail = "";
228 return true;
229 }
230 catch (RtMidiError& error) {
231 if (errDetail) *errDetail = error.getMessage();
232 hasOpenInputPort_ = false;
233 return false;
234 }
235 }
236
237 bool MidiInterface::openInputPortByName(const std::string &portName, std::string* errDetail)
238 {
239 std::vector<std::string> ports = getRealInputPorts();
240
241 for (unsigned i = 0, n = ports.size(); i < n; ++i) {
242 if (ports[i] == portName) {
243 return openInputPort(i, errDetail);;
244 }
245 }
246
247 if (errDetail) *errDetail = "There is no port such the name.";
248 return false;
249 }
250
251 void MidiInterface::installInputHandler(InputHandler* handler, void* user_data)
252 {
253 std::lock_guard<std::mutex> lock(inputHandlersMutex_);
254 inputHandlers_.push_back(std::make_pair(handler, user_data));
255 }
256
257 void MidiInterface::uninstallInputHandler(InputHandler* handler, void* user_data)
258 {
259 std::lock_guard<std::mutex> lock(inputHandlersMutex_);
260 for (size_t i = 0, n = inputHandlers_.size(); i < n; ++i) {
261 bool match = inputHandlers_[i].first == handler &&
262 inputHandlers_[i].second == user_data;
263 if (match) {
264 inputHandlers_.erase(inputHandlers_.begin() + i);
265 return;
266 }
267 }
268 }
269
270 void MidiInterface::onMidiInput(double timestamp, std::vector<unsigned char> *message, void *user_data)
271 {
272 MidiInterface *self = reinterpret_cast<MidiInterface *>(user_data);
273
274 std::unique_lock<std::mutex> lock(self->inputHandlersMutex_, std::try_to_lock);
275 if (!lock.owns_lock())
276 return;
277
278 const uint8_t *msg = message->data();
279 size_t len = message->size();
280
281 for (size_t i = 0, n = self->inputHandlers_.size(); i < n; ++i)
282 self->inputHandlers_[i].first(timestamp, msg, len, self->inputHandlers_[i].second);
283 }