author | František Kučera <franta-hg@frantovo.cz> |
Mon, 04 Jan 2021 13:38:08 +0100 | |
branch | v_0 |
changeset 11 | 5b351628a377 |
parent 9 | ee976a1d1f0a |
child 12 | 15d87fdd6e6c |
permissions | -rw-r--r-- |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* DJM-Fix |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#include <iostream> |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
18 |
#include <stdexcept> |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
19 |
#include <thread> |
9 | 20 |
#include <mutex> |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
21 |
#include <atomic> |
11
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
22 |
#include <regex> |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
|
3
e238528eb19c
link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
24 |
#include <alsa/asoundlib.h> |
e238528eb19c
link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
25 |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include "AlsaBridge.h" |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
namespace djmfix { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
namespace alsa { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
private: |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
djmfix::DJMFix* djmFix; |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
34 |
snd_rawmidi_t* input; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
35 |
snd_rawmidi_t* output; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
36 |
std::thread receivingThread; |
9 | 37 |
std::recursive_mutex midiMutex; |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
38 |
std::atomic<bool> stopped{false}; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
39 |
|
11
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
40 |
std::string findDeviceName(std::regex cardNamePattern) { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
41 |
|
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
42 |
std::vector<int> cardNumbers; |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
43 |
|
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
44 |
std::cerr << "Looking for available cards:" << std::endl; // TODO: do not mess STDIO |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
45 |
|
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
46 |
for (int card = -1; snd_card_next(&card) == 0 && card >= 0;) { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
47 |
char* longName = nullptr; |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
48 |
snd_card_get_longname(card, &longName); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
49 |
std::cerr << "card: #" << card << ": '" << longName << "'"; // TODO: do not mess STDIO |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
50 |
if (std::regex_match(longName, cardNamePattern)) { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
51 |
cardNumbers.push_back(card); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
52 |
std::cerr << " [matches]"; // TODO: do not mess STDIO |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
53 |
} |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
54 |
std::cerr << std::endl; |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
55 |
free(longName); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
56 |
} |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
57 |
|
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
58 |
if (cardNumbers.size() == 1) { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
59 |
std::cerr << "Going to fix card #" << cardNumbers[0] << std::endl; // TODO: do not mess STDIO |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
60 |
return "hw:" + std::to_string(cardNumbers[0]); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
61 |
} else if (cardNumbers.empty()) { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
62 |
throw std::invalid_argument("No card with matching name found. Is the card connected? Maybe try to provide different name pattern."); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
63 |
} else { |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
64 |
throw std::invalid_argument("Multiple cards with matching name found. Please provide a name pattern that matches only one card"); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
65 |
} |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
66 |
} |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
67 |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
68 |
void run() { |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
while (!stopped) { |
9 | 70 |
{ |
71 |
std::lock_guard<std::recursive_mutex> lock(midiMutex); |
|
72 |
// TODO: poll |
|
73 |
uint8_t buffer[256]; |
|
74 |
ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer)); |
|
75 |
if (length > 0 && length <= sizeof (buffer)) { |
|
76 |
// TODO: multiple messages combined together? |
|
77 |
djmFix->receive(MidiMessage(buffer, buffer + length)); |
|
78 |
} |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
} |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
81 |
} |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
82 |
} |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
public: |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
|
11
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
85 |
AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& cardNamePattern) : djmFix(djmFix) { |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
86 |
if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge"); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
87 |
|
11
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
88 |
std::string deviceName = findDeviceName(std::regex(cardNamePattern)); |
5b351628a377
Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
89 |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
90 |
int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
91 |
if (error) throw std::invalid_argument("unable to open ALSA device"); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
92 |
|
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
93 |
|
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
djmFix->setMidiSender(this); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
virtual ~AlsaBridgeImpl() { |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
98 |
// TODO: do not use raw/exclusive access to the device |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
99 |
snd_rawmidi_close(input); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
100 |
snd_rawmidi_close(output); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
std::cerr << "~AlsaBridgeImpl()" << std::endl; // TODO: do not mess STDIO |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
virtual void start() override { |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
djmFix->start(); |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
106 |
receivingThread = std::thread(&AlsaBridgeImpl::run, this); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
virtual void stop() override { |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
110 |
stopped = true; |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
111 |
receivingThread.join(); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
djmFix->stop(); |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
virtual void send(MidiMessage midiMessage) override { |
9 | 116 |
std::lock_guard<std::recursive_mutex> lock(midiMutex); |
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
117 |
ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size()); |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
118 |
std::cerr << "AlsaBridgeImpl::send(): length = " << length << std::endl; // TODO: do not mess STDIO |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
}; |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
|
5
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
123 |
AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName) { |
ef8f4023e32e
sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
124 |
return new AlsaBridgeImpl(djmFix, deviceName); |
2
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
|
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
} |
f34476ab597f
background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
} |