AlsaBridge.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 20 Dec 2020 01:00:15 +0100
branchv_0
changeset 7 889b4b8737bd
parent 5 ef8f4023e32e
child 9 ee976a1d1f0a
permissions -rw-r--r--
send the second message as a response (instead of after a fixed delay)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * DJM-Fix
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <iostream>
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    18
#include <stdexcept>
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    19
#include <thread>
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    20
#include <atomic>
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
3
e238528eb19c link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    22
#include <alsa/asoundlib.h>
e238528eb19c link to ALSA (libasound)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    23
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include "AlsaBridge.h"
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace djmfix {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace alsa {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	djmfix::DJMFix* djmFix;
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    32
	snd_rawmidi_t* input;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    33
	snd_rawmidi_t* output;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    34
	std::thread receivingThread;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
	std::atomic<bool> stopped{false};
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    36
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    37
	void run() {
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    38
		while (!stopped) {
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
			// TODO: poll
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
			uint8_t buffer[256];
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
			ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer));
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
			if (length > 0 && length <= sizeof (buffer)) {
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
				// TODO: multiple messages combined together?
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
				djmFix->receive(MidiMessage(buffer, buffer + length));
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
			}
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
			std::this_thread::sleep_for(std::chrono::milliseconds(100));
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
		}
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
	}
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
public:
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
	AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& deviceName) : djmFix(djmFix) {
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
		if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge");
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
		int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK);
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
		if (error) throw std::invalid_argument("unable to open ALSA device");
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		djmFix->setMidiSender(this);
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	virtual ~AlsaBridgeImpl() {
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
		// TODO: do not use raw/exclusive access to the device
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
		snd_rawmidi_close(input);
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
		snd_rawmidi_close(output);
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		std::cerr << "~AlsaBridgeImpl()" << std::endl; // TODO: do not mess STDIO
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	virtual void start() override {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		djmFix->start();
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
		receivingThread = std::thread(&AlsaBridgeImpl::run, this);
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	virtual void stop() override {
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
		stopped = true;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
		receivingThread.join();
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		djmFix->stop();
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	virtual void send(MidiMessage midiMessage) override {
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
		ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size());
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
		std::cerr << "AlsaBridgeImpl::send(): length = " << length << std::endl; // TODO: do not mess STDIO
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
};
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName) {
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    88
	return new AlsaBridgeImpl(djmFix, deviceName);
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}