DJMFix.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Jan 2021 15:45:12 +0100
branchv_0
changeset 12 15d87fdd6e6c
parent 8 87dfa7c89294
child 13 334b727f7516
permissions -rw-r--r--
use Logger instead of messing with STDIO directly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * DJM-Fix
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4ee5349be21d project 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
4ee5349be21d project 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
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
4ee5349be21d project 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,
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
4ee5349be21d project 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
4ee5349be21d project 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/>.
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    17
#include <iostream>
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    18
#include <sstream>
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    19
#include <iomanip>
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    20
#include <thread>
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    21
#include <mutex>
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    22
#include <atomic>
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    23
#include <chrono>
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    24
#include <stdexcept>
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    25
#include <vector>
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
#include "DJMFix.h"
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    28
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
namespace djmfix {
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    31
using L = djmfix::logging::Level;
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    32
using Bytes = std::vector<uint8_t>;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    33
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
class DJMFixImpl : public DJMFix {
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
private:
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    36
	MidiSender* midiSender;
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    37
	djmfix::logging::Logger* logger;
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    38
	std::thread keepAliveThread;
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    39
	std::recursive_mutex midiMutex;
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    40
	std::atomic<bool> running{false};
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    41
	std::atomic<bool> stopped{false};
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    42
	std::atomic<bool> sendKeepAlive{false};
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    43
	Bytes seed2;
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    45
	void run() {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
		while (!stopped) {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    47
			logger->log(L::FINE, "DJMFixImpl::run()");
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    48
			if (sendKeepAlive) send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
			std::this_thread::sleep_for(std::chrono::milliseconds(200));
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
		}
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    53
	void send(const MidiMessage& midiMessage) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    54
		std::lock_guard<std::recursive_mutex> lock(midiMutex);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    55
		midiSender->send(midiMessage);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    56
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    58
	std::string toString(const Bytes& midiMessage) {
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
		std::stringstream result;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
		return result.str();
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
	}
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
	Bytes normalize(const Bytes& data) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
		if (data.size() % 2) throw std::invalid_argument("data before normalization must have even number of bytes");
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    66
		Bytes result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
		result.reserve(data.size() / 2);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    68
		for (size_t i = 0; i < data.size() / 2; i++) result.push_back((data[i * 2] & 0x0F) << 4 | (data[i * 2 + 1] & 0x0F));
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    69
		return result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    70
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    72
	Bytes denormalize(const Bytes& data) {
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    73
		Bytes result;
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    74
		result.reserve(data.size()*2);
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
		for (size_t i = 0; i < data.size(); i++) {
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    76
			result.push_back(data[i] >> 4);
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
			result.push_back(data[i] & 0x0F);
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    78
		}
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    79
		return result;
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    80
	}
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    81
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    82
	uint32_t fnv32hash(const Bytes& buff) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    83
		uint32_t hash = 0x811c9dc5;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    84
		for (uint8_t b : buff) hash = ((b^hash) * 0x1000193);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    85
		return hash;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    86
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    87
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    88
	Bytes toBytes(const uint32_t value) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    89
		Bytes result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    90
		result.reserve(4);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    91
		result.push_back(value >> 24);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    92
		result.push_back(value >> 16);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    93
		result.push_back(value >> 8);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    94
		result.push_back(value >> 0);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    95
		return result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    96
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    97
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    98
	bool equals(Bytes a, Bytes b) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    99
		if (a.size() != b.size()) return false;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   100
		for (size_t i = 0; i < a.size(); i++) if (a[i] != b[i]) return false;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   101
		return true;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   102
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   103
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   104
	template<typename T> std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c = {}) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   105
		std::vector<T> result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   106
		result.reserve(a.size() + b.size() + c.size());
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   107
		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i]);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   108
		for (size_t i = 0; i < b.size(); i++) result.push_back(b[i]);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   109
		for (size_t i = 0; i < c.size(); i++) result.push_back(c[i]);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   110
		return result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   111
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   112
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   113
	template<typename T> std::vector<T> xOR(const std::vector<T>& a, const std::vector<T>& b) {
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   114
		if (a.size() != b.size()) throw std::invalid_argument("xor: both must be the same length");
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   115
		std::vector<T> result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   116
		result.reserve(a.size());
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   117
		for (size_t i = 0; i < a.size(); i++) result.push_back(a[i] ^ b[i]);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   118
		return result;
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   119
	}
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   120
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
public:
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   123
	DJMFixImpl(djmfix::logging::Logger* logger) : logger(logger ? logger : djmfix::logging::blackhole()) {
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   124
	}
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   125
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   126
	virtual ~DJMFixImpl() override {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   127
		logger->log(L::FINE, "~DJMFixImpl()");
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   128
		if (running) stop();
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   129
	}
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   130
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   131
	void setMidiSender(MidiSender* midiSender) {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   132
		logger->log(L::FINE, "DJMFixImpl::setMidiSender()");
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   133
		this->midiSender = midiSender;
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   134
	}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   135
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   136
	virtual void receive(const MidiMessage& midiMessage) override {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   137
		logger->log(L::INFO, "received message: size = " + std::to_string(midiMessage.size()) + " data = " + toString(midiMessage));
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   138
		std::lock_guard<std::recursive_mutex> lock(midiMutex);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   139
7
889b4b8737bd send the second message as a response (instead of after a fixed delay)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   140
889b4b8737bd send the second message as a response (instead of after a fixed delay)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   141
		if (midiMessage.size() == 12 && midiMessage[9] == 0x11) {
889b4b8737bd send the second message as a response (instead of after a fixed delay)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   142
			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});
889b4b8737bd send the second message as a response (instead of after a fixed delay)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   143
		} else if (midiMessage.size() == 54 && midiMessage[9] == 0x13 && midiMessage[33] == 0x04 && midiMessage[43] == 0x03) {
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   144
			Bytes hash1(midiMessage.begin() + 35, midiMessage.begin() + 35 + 8);
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   145
			seed2 = Bytes(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   146
			hash1 = normalize(hash1);
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   147
			seed2 = normalize(seed2);
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   148
			logger->log(L::INFO, "got message with hash1 = " + toString(hash1) + " and seed2 = " + toString(seed2));
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   149
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   150
			Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   151
			Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   152
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   153
			Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   154
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   155
			if (equals(hash1, hash1check)) {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   156
				logger->log(L::INFO, "hash1 verification: OK");
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   157
				Bytes hash2 = toBytes(fnv32hash(concat(seed2, xOR(seed0, seed2))));
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   158
				send(concat({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x14, 0x38, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x04, 0x0a}, concat(denormalize(hash2),{0x05, 0x16, 0x05, 0x09, 0x0b, 0x05, 0x04, 0x0b, 0x0f, 0x0e, 0x0e, 0x04, 0x04, 0x0a, 0x05, 0x0a, 0x0c, 0x08, 0x0e, 0x04, 0x0c, 0x05, 0xf7})));
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   159
			} else {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   160
				std::stringstream logMessage;
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   161
				logMessage
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   162
						<< "hash1 verification failed: "
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   163
						<< " midiMessage = " << toString(midiMessage)
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   164
						<< " seed0 = " << toString(seed0)
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   165
						<< " seed1 = " << toString(seed1)
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   166
						<< " seed2 = " << toString(seed2)
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   167
						<< " hash1 = " << toString(hash1)
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   168
						<< " hash1check = " << toString(hash1check);
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   169
				logger->log(L::SEVERE, logMessage.str());
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   170
				// TODO: graceful death
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   171
			}
8
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   172
		} else if (midiMessage.size() == 12 && midiMessage[9] == 0x15) {
87dfa7c89294 first working version (still dirty, but working)
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   173
			sendKeepAlive = true;
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   174
		}
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   175
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   176
	}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   177
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   178
	void start() override {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   179
		logger->log(L::FINE, "DJMFixImpl::start()");
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   180
		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   181
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   182
		// TODO: methods for parsing and constructing messages from parts (TLV)
6
bddcf2bf29f2 verify the response from the mixer
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   183
		send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   184
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   185
		keepAliveThread = std::thread(&DJMFixImpl::run, this);
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   186
		running = true;
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   187
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   188
	}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   189
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   190
	void stop() override {
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   191
		stopped = true;
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   192
		keepAliveThread.join();
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   193
		running = false;
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   194
		logger->log(L::FINE, "DJMFixImpl::stop()");
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   195
	}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   196
};
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   197
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   198
DJMFix* create(djmfix::logging::Logger* logger) {
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   199
	return new DJMFixImpl(logger);
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   200
}
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   201
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   202
}