AlsaBridge.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Dec 2020 17:33:16 +0100
branchv_0
changeset 5 ef8f4023e32e
parent 3 e238528eb19c
child 9 ee976a1d1f0a
permissions -rw-r--r--
sending and receiving MIDI messages through ALSA (the dirty way)
     1 /**
     2  * DJM-Fix
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 #include <iostream>
    18 #include <stdexcept>
    19 #include <thread>
    20 #include <atomic>
    21 
    22 #include <alsa/asoundlib.h>
    23 
    24 #include "AlsaBridge.h"
    25 
    26 namespace djmfix {
    27 namespace alsa {
    28 
    29 class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender {
    30 private:
    31 	djmfix::DJMFix* djmFix;
    32 	snd_rawmidi_t* input;
    33 	snd_rawmidi_t* output;
    34 	std::thread receivingThread;
    35 	std::atomic<bool> stopped{false};
    36 
    37 	void run() {
    38 		while (!stopped) {
    39 			// TODO: poll
    40 			uint8_t buffer[256];
    41 			ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer));
    42 			if (length > 0 && length <= sizeof (buffer)) {
    43 				// TODO: multiple messages combined together?
    44 				djmFix->receive(MidiMessage(buffer, buffer + length));
    45 			}
    46 
    47 			std::this_thread::sleep_for(std::chrono::milliseconds(100));
    48 		}
    49 	}
    50 public:
    51 
    52 	AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& deviceName) : djmFix(djmFix) {
    53 		if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge");
    54 
    55 		int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK);
    56 		if (error) throw std::invalid_argument("unable to open ALSA device");
    57 
    58 
    59 		djmFix->setMidiSender(this);
    60 	}
    61 
    62 	virtual ~AlsaBridgeImpl() {
    63 		// TODO: do not use raw/exclusive access to the device
    64 		snd_rawmidi_close(input);
    65 		snd_rawmidi_close(output);
    66 		std::cerr << "~AlsaBridgeImpl()" << std::endl; // TODO: do not mess STDIO
    67 	}
    68 
    69 	virtual void start() override {
    70 		djmFix->start();
    71 		receivingThread = std::thread(&AlsaBridgeImpl::run, this);
    72 	}
    73 
    74 	virtual void stop() override {
    75 		stopped = true;
    76 		receivingThread.join();
    77 		djmFix->stop();
    78 	}
    79 
    80 	virtual void send(MidiMessage midiMessage) override {
    81 		ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size());
    82 		std::cerr << "AlsaBridgeImpl::send(): length = " << length << std::endl; // TODO: do not mess STDIO
    83 	}
    84 
    85 };
    86 
    87 AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName) {
    88 	return new AlsaBridgeImpl(djmFix, deviceName);
    89 }
    90 
    91 }
    92 }