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