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