Debian Codegraph @

BambooTracker/midi/midi.hpp

raw
1 /*
2 * Copyright (C) 2019-2020 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 #pragma once
27
28 #include <string>
29 #include <vector>
30 #include <memory>
31 #include <mutex>
32 #include <cstdint>
33 #include "RtMidi.h"
34
35 class RtMidiIn;
36
37 class MidiInterface
38 {
39 public:
40 static MidiInterface& getInstance();
41 ~MidiInterface();
42
43 std::string currentApiName() const;
44 std::vector<std::string> getAvailableApis() const;
45 bool isAvailableApi(const std::string& api) const;
46 bool switchApi(std::string api, std::string* errDetail = nullptr);
47 bool supportsVirtualPort() const;
48 bool supportsVirtualPort(std::string api) const;
49 std::vector<std::string> getRealInputPorts();
50 std::vector<std::string> getRealInputPorts(const std::string& api);
51
52 void closeInputPort();
53 bool openInputPort(unsigned port, std::string* errDetail = nullptr);
54 bool openInputPortByName(const std::string &portName, std::string* errDetail = nullptr);
55
56 using InputHandler = void(double, const uint8_t*, size_t, void*);
57 void installInputHandler(InputHandler *handler, void *userData);
58 void uninstallInputHandler(InputHandler *handler, void *userData);
59
60 private:
61 static std::unique_ptr<MidiInterface> instance_;
62
63 std::unique_ptr<RtMidiIn> inputClient_;
64 bool hasOpenInputPort_;
65 std::mutex inputHandlersMutex_;
66 std::vector<std::pair<InputHandler*, void*>> inputHandlers_;
67
68 MidiInterface();
69 static void onMidiInput(double timestamp, std::vector<unsigned char> *message, void *userData);
70 };