3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <alsa/asoundlib.h>
27 #include "AlsaBridge.h"
33 using L = djmfix::logging::Level;
35 class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender {
37 djmfix::DJMFix* djmFix;
38 djmfix::logging::Logger* logger;
40 snd_rawmidi_t* output;
41 std::thread receivingThread;
42 std::recursive_mutex midiMutex;
43 std::atomic<bool> stopped{false};
45 std::string findDeviceName(std::regex cardNamePattern) {
47 std::vector<int> cardNumbers;
49 logger->log(L::INFO, "Looking for available cards:");
51 for (int card = -1; snd_card_next(&card) == 0 && card >= 0;) {
52 char* longName = nullptr;
53 snd_card_get_longname(card, &longName);
55 std::stringstream logMessage;
56 logMessage << " - card: #" << card << ": '" << longName << "'";
58 if (std::regex_match(longName, cardNamePattern)) {
59 cardNumbers.push_back(card);
60 logMessage << " [matches]";
63 logger->log(L::INFO, logMessage.str());
68 if (cardNumbers.size() == 1) {
69 logger->log(L::INFO, "Going to fix card #" + std::to_string(cardNumbers[0]));
70 return "hw:" + std::to_string(cardNumbers[0]);
71 } else if (cardNumbers.empty()) {
72 throw std::invalid_argument("No card with matching name found. Is the card connected? Maybe try to provide different name pattern.");
74 throw std::invalid_argument("Multiple cards with matching name found. Please provide a name pattern that matches only one card");
81 std::lock_guard<std::recursive_mutex> lock(midiMutex);
84 ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer));
85 if (length > 0 && length <= sizeof (buffer)) {
86 // TODO: multiple messages combined together?
87 djmFix->receive(MidiMessage(buffer, buffer + length));
90 std::this_thread::sleep_for(std::chrono::milliseconds(100));
95 AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& cardNamePattern, djmfix::logging::Logger* logger) : djmFix(djmFix), logger(logger ? logger : djmfix::logging::blackhole()) {
96 if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge");
98 std::string deviceName = findDeviceName(std::regex(cardNamePattern));
100 int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK);
101 if (error) throw std::invalid_argument("unable to open ALSA device");
104 djmFix->setMidiSender(this);
107 virtual ~AlsaBridgeImpl() {
108 // TODO: do not use raw/exclusive access to the MIDI device
109 snd_rawmidi_close(input);
110 snd_rawmidi_close(output);
111 logger->log(L::FINE, "~AlsaBridgeImpl()");
114 virtual void start() override {
116 receivingThread = std::thread(&AlsaBridgeImpl::run, this);
119 virtual void stop() override {
121 receivingThread.join();
125 virtual void send(MidiMessage midiMessage) override {
126 std::lock_guard<std::recursive_mutex> lock(midiMutex);
127 ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size());
128 logger->log(L::INFO, "AlsaBridgeImpl::send(): length = " + std::to_string(length));
133 AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName, djmfix::logging::Logger* logger) {
134 return new AlsaBridgeImpl(djmFix, deviceName, logger);