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