djm-fix.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Jan 2021 17:11:57 +0100
branchv_0
changeset 13 334b727f7516
parent 12 15d87fdd6e6c
permissions -rw-r--r--
improved logging and error messages
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
 */
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#include <memory>
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <iostream>
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    20
#include <chrono>
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    21
#include <thread>
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    22
#include <csignal>
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    23
#include <atomic>
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include "DJMFix.h"
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    26
#include "AlsaBridge.h"
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    27
#include "Logger.h"
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
5
ef8f4023e32e sending and receiving MIDI messages through ALSA (the dirty way)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    29
static std::atomic<bool> run{true};
2
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    30
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    31
void interrupt(int signal) {
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    32
	run = false;
f34476ab597f background thread + AlsaBridge skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    33
}
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
10
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    35
/**
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    36
 * The support for Pioneer DJ DJM-250MK2 (an external USB sound card / mixer) was added to the Linux (kernel) by these patches:
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    37
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    38
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=73d8c94084341e2895169a0462dbc18167f01683 (playback)
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    39
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=14335d8b9e1a2bf006f9d969a103f9731cabb210 (recording)
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    40
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=cdc01a1558dedcee3daee7e1802d0349a07edb87 (mixer setup)
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    41
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    42
 * These patches are enough for playback and for recording from post CH faders.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    43
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    44
 * However this mixer is somehow incapacitated and if we want to record the raw signal from the PHONO or LINE channels,
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    45
 * we only get silence. This feature is important for DVS (Digital Vinyl Systems) setups where
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    46
 * the timecode signal from special control vinyls flows from mixer to the computer
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    47
 * where it is interpreted in a software like MIXXX and used for controlling the playback of files on our computer.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    48
 * The signal (usually music) from these files flows back to the mixer and then to speakers and headphones.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    49
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    50
 * To make this work and enjoy all the features of the device we have bought, we need to tell the mixer that we
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    51
 * want the signal instead of silence on given channels. And this is the purpose of the djm-fix utility and
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    52
 * it is done by sending some magic packet to the mixer.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    53
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    54
 * Implementation of this magic in the AlsaBridge.cpp file is based on publicly available documentation
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    55
 * that can be found at <https://mixb.me/CDJHidProtocol/hid-analysis/handshake.html>.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    56
 * This page pointed me to the proper hash function (according to the constants, it is bit uncommon but publicly known Fowler–Noll–Vo hash function, FNV)
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
 * and some magic bits. I wrote this standalone C++ program that talks with the mixer over MIDI SysEx messages and does the magic.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    58
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    59
 * When this program is started, it finds the mixer and makes it fully working.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    60
 * It needs to be running all the time, otherwise we will get silence on the PHONO/LINE channels again.
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    61
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    62
 * Install dependencies:
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    63
 *   apt install mercurial make pkg-config g++ libasound2-dev    # in Debian or Ubuntu (it will be similar in other distributions)
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    65
 * Download djm-fix:
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    66
 *   hg clone https://hg.frantovo.cz/midi/djm-fix/               # primary source
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    67
 *   hg clone https://hg.globalcode.info/midi/djm-fix/           # or we can use this mirror
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    68
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    69
 * Compile:
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    70
 *   make                                                        # we can skip this step, it will be compiled on the first run
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    72
 * Run:
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    73
 *   make run                                                    # in most cases
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    74
 *   build/djm-fix 'Pioneer DJ.*'                                # or provide custom name pattern (regular expression) to select the proper card
10
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    75
 *
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    76
 * Stop:
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    77
 *   press Ctrl+C
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    78
 * 
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    79
 * Look for updates in the Mercurial repositories and at <https://blog.frantovo.cz/c/387/>.
10
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    80
 */
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    81
0
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
int main(int argc, char**argv) {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    83
	using L = djmfix::logging::Level;
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    84
	std::unique_ptr<djmfix::logging::Logger> logger(djmfix::logging::create(std::cerr, L::INFO));
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    85
	try {
13
334b727f7516 improved logging and error messages
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    86
		logger->log(L::INFO, "DJM-Fix started.");
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    87
		std::string cardNamePattern = argc == 2 ? argv[1] : "Pioneer DJ.*";
10
4d95b089457d documentation
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    88
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    89
		signal(SIGINT, interrupt);
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    90
		std::unique_ptr<djmfix::DJMFix> djmFix(djmfix::create(logger.get()));
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    91
		std::unique_ptr<djmfix::alsa::AlsaBridge> alsaBridge(djmfix::alsa::create(djmFix.get(), cardNamePattern, logger.get()));
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    93
		alsaBridge->start();
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    94
		while (run) std::this_thread::sleep_for(std::chrono::milliseconds(100));
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    95
		
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    96
		std::cerr << std::endl;
13
334b727f7516 improved logging and error messages
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    97
		logger->log(L::INFO, "DJM-Fix stopping.");
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    98
		
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    99
		alsaBridge->stop();
1
98274757fcf6 more bones
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   101
		return 0;
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   102
	} catch (const std::exception& e) {
12
15d87fdd6e6c use Logger instead of messing with STDIO directly
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   103
		logger->log(L::SEVERE, e.what());
11
5b351628a377 Find card by a name pattern (regular expression) instead using hardcoded name.
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   104
	}
0
4ee5349be21d project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
}