DJMFix.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Dec 2020 17:33:16 +0100
branchv_0
changeset 5 ef8f4023e32e
parent 2 f34476ab597f
child 6 bddcf2bf29f2
permissions -rw-r--r--
sending and receiving MIDI messages through ALSA (the dirty way)
franta-hg@0
     1
/**
franta-hg@0
     2
 * DJM-Fix
franta-hg@0
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@0
     4
 *
franta-hg@0
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@0
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@0
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@0
     8
 *
franta-hg@0
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@0
    12
 * GNU General Public License for more details.
franta-hg@0
    13
 *
franta-hg@0
    14
 * You should have received a copy of the GNU General Public License
franta-hg@0
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@0
    16
 */
franta-hg@1
    17
#include <iostream>
franta-hg@5
    18
#include <iomanip>
franta-hg@2
    19
#include <thread>
franta-hg@5
    20
#include <atomic>
franta-hg@2
    21
#include <chrono>
franta-hg@2
    22
#include <stdexcept>
franta-hg@1
    23
franta-hg@1
    24
#include "DJMFix.h"
franta-hg@1
    25
franta-hg@1
    26
namespace djmfix {
franta-hg@1
    27
franta-hg@1
    28
class DJMFixImpl : public DJMFix {
franta-hg@1
    29
private:
franta-hg@2
    30
	MidiSender* midiSender;
franta-hg@2
    31
	std::thread keepAliveThread;
franta-hg@5
    32
	std::atomic<bool> running{false};
franta-hg@5
    33
	std::atomic<bool> stopped{false};
franta-hg@2
    34
franta-hg@2
    35
	void run() {
franta-hg@2
    36
		while (!stopped) {
franta-hg@2
    37
			std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
franta-hg@2
    38
			// TODO: send keep-alive messages
franta-hg@2
    39
			std::this_thread::sleep_for(std::chrono::milliseconds(200));
franta-hg@2
    40
		}
franta-hg@2
    41
	}
franta-hg@2
    42
franta-hg@5
    43
	// TODO: remove
franta-hg@5
    44
	std::string toString(const MidiMessage& midiMessage) {
franta-hg@5
    45
		std::stringstream result;
franta-hg@5
    46
		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
franta-hg@5
    47
		return result.str();
franta-hg@5
    48
	}
franta-hg@5
    49
franta-hg@1
    50
public:
franta-hg@1
    51
franta-hg@1
    52
	virtual ~DJMFixImpl() override {
franta-hg@1
    53
		std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
franta-hg@2
    54
		if (running) stop();
franta-hg@2
    55
	}
franta-hg@2
    56
franta-hg@2
    57
	void setMidiSender(MidiSender* midiSender) {
franta-hg@2
    58
		std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
franta-hg@2
    59
		this->midiSender = midiSender;
franta-hg@1
    60
	}
franta-hg@1
    61
franta-hg@1
    62
	virtual void receive(MidiMessage midiMessage) override {
franta-hg@5
    63
		std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
franta-hg@5
    64
franta-hg@5
    65
		if (midiMessage.size() == 54 && midiMessage[9] == 0x13) {
franta-hg@5
    66
			std::cerr << "DJMFixImpl::receive(): got message with HashA and SeedE" << std::endl; // TODO: do not mess STDIO
franta-hg@5
    67
		}
franta-hg@5
    68
franta-hg@1
    69
	}
franta-hg@1
    70
franta-hg@1
    71
	void start() override {
franta-hg@1
    72
		std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
franta-hg@5
    73
		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
franta-hg@5
    74
franta-hg@5
    75
		// TODO: methods for parsing and constructing messages from parts (TLV)
franta-hg@5
    76
		midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
franta-hg@5
    77
		std::this_thread::sleep_for(std::chrono::milliseconds(10));
franta-hg@5
    78
		midiSender->send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x12, 0x2a, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x03, 0x12, 0x02, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0xf7});
franta-hg@2
    79
franta-hg@2
    80
		keepAliveThread = std::thread(&DJMFixImpl::run, this);
franta-hg@2
    81
		running = true;
franta-hg@2
    82
franta-hg@1
    83
	}
franta-hg@1
    84
franta-hg@1
    85
	void stop() override {
franta-hg@2
    86
		stopped = true;
franta-hg@2
    87
		keepAliveThread.join();
franta-hg@2
    88
		running = false;
franta-hg@1
    89
		std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
franta-hg@1
    90
	}
franta-hg@1
    91
};
franta-hg@1
    92
franta-hg@2
    93
DJMFix* create() {
franta-hg@2
    94
	return new DJMFixImpl();
franta-hg@1
    95
}
franta-hg@1
    96
franta-hg@1
    97
}