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