djm-fix.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Jan 2021 00:15:56 +0100
branchv_0
changeset 10 4d95b089457d
parent 5 ef8f4023e32e
child 11 5b351628a377
permissions -rw-r--r--
documentation
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@0
    17
franta-hg@1
    18
#include <memory>
franta-hg@1
    19
#include <iostream>
franta-hg@2
    20
#include <chrono>
franta-hg@2
    21
#include <thread>
franta-hg@2
    22
#include <csignal>
franta-hg@5
    23
#include <atomic>
franta-hg@1
    24
franta-hg@1
    25
#include "DJMFix.h"
franta-hg@2
    26
#include "AlsaBridge.h"
franta-hg@2
    27
franta-hg@5
    28
static std::atomic<bool> run{true};
franta-hg@2
    29
franta-hg@2
    30
void interrupt(int signal) {
franta-hg@2
    31
	run = false;
franta-hg@2
    32
	std::cerr << "interrupt()" << std::endl; // TODO: do not mess STDIO
franta-hg@2
    33
}
franta-hg@1
    34
franta-hg@10
    35
/**
franta-hg@10
    36
 * The support for Pioneer DJ DJM-250MK2 (an external USB sound card / mixer) was added to the Linux (kernel) by these patches:
franta-hg@10
    37
 *
franta-hg@10
    38
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=73d8c94084341e2895169a0462dbc18167f01683 (playback)
franta-hg@10
    39
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=14335d8b9e1a2bf006f9d969a103f9731cabb210 (recording)
franta-hg@10
    40
 *  - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/sound/usb?id=cdc01a1558dedcee3daee7e1802d0349a07edb87 (mixer setup)
franta-hg@10
    41
 *
franta-hg@10
    42
 * These patches are enough for playback and for recording from post CH faders.
franta-hg@10
    43
 *
franta-hg@10
    44
 * However this mixer is somehow incapacitated and if we want to record the raw signal from the PHONO or LINE channels,
franta-hg@10
    45
 * we only get silence. This feature is important for DVS (Digital Vinyl Systems) setups where
franta-hg@10
    46
 * the timecode signal from special control vinyls flows from mixer to the computer
franta-hg@10
    47
 * where it is interpreted in a software like MIXXX and used for controlling the playback of files on our computer.
franta-hg@10
    48
 * The signal (usually music) from these files flows back to the mixer and then to speakers and headphones.
franta-hg@10
    49
 *
franta-hg@10
    50
 * To make this work and enjoy all the features of the device we have bought, we need to tell the mixer that we
franta-hg@10
    51
 * want the signal instead of silence on given channels. And this is the purpose of the djm-fix utility and
franta-hg@10
    52
 * it is done by sending some magic packet to the mixer.
franta-hg@10
    53
 *
franta-hg@10
    54
 * Implementation of this magic in the AlsaBridge.cpp file is based on publicly available documentation
franta-hg@10
    55
 * that can be found at <https://mixb.me/CDJHidProtocol/hid-analysis/handshake.html>.
franta-hg@10
    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)
franta-hg@10
    57
 * and some magic bits. I wrote this standalone C++ program that talks with the mixer over MIDI SysEx messages and does the magic.
franta-hg@10
    58
 *
franta-hg@10
    59
 * When this program is started, it finds the mixer and makes it fully working.
franta-hg@10
    60
 * It needs to be running all the time, otherwise we will get silence on the PHONO/LINE channels again.
franta-hg@10
    61
 *
franta-hg@10
    62
 * Install dependencies:
franta-hg@10
    63
 *   apt install mercurial make pkg-config g++ libasound2-dev    # in Debian or Ubuntu (it will be similar in other distributions)
franta-hg@10
    64
 *
franta-hg@10
    65
 * Download djm-fix:
franta-hg@10
    66
 *   hg clone https://hg.frantovo.cz/midi/djm-fix/               # primary source
franta-hg@10
    67
 *   hg clone https://hg.globalcode.info/midi/djm-fix/           # or we can use this mirror
franta-hg@10
    68
 *
franta-hg@10
    69
 * Compile:
franta-hg@10
    70
 *   make                                                        # we can skip this step, it will be compiled on the first run
franta-hg@10
    71
 *
franta-hg@10
    72
 * Run:
franta-hg@10
    73
 *   make run
franta-hg@10
    74
 *
franta-hg@10
    75
 * Stop:
franta-hg@10
    76
 *   press Ctrl+C
franta-hg@10
    77
 */
franta-hg@10
    78
franta-hg@0
    79
int main(int argc, char**argv) {
franta-hg@5
    80
	std::string deviceName = argc == 2 ? argv[1] : "hw:1"; // FIXME: parse CLI options + automatic device search
franta-hg@10
    81
franta-hg@2
    82
	signal(SIGINT, interrupt);
franta-hg@2
    83
	std::unique_ptr<djmfix::DJMFix> djmFix(djmfix::create());
franta-hg@5
    84
	std::unique_ptr<djmfix::alsa::AlsaBridge> alsaBridge(djmfix::alsa::create(djmFix.get(), deviceName));
franta-hg@1
    85
franta-hg@2
    86
	alsaBridge->start();
franta-hg@2
    87
	while (run) std::this_thread::sleep_for(std::chrono::milliseconds(100));
franta-hg@2
    88
	alsaBridge->stop();
franta-hg@1
    89
franta-hg@0
    90
	return 0;
franta-hg@0
    91
}