franta-hg@0: /**
franta-hg@0: * DJM-Fix
franta-hg@16: * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@0: *
franta-hg@0: * This program is free software: you can redistribute it and/or modify
franta-hg@0: * it under the terms of the GNU General Public License as published by
franta-hg@0: * the Free Software Foundation, version 3 of the License.
franta-hg@0: *
franta-hg@0: * This program is distributed in the hope that it will be useful,
franta-hg@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@0: * GNU General Public License for more details.
franta-hg@0: *
franta-hg@0: * You should have received a copy of the GNU General Public License
franta-hg@0: * along with this program. If not, see .
franta-hg@0: */
franta-hg@0:
franta-hg@1: #include
franta-hg@1: #include
franta-hg@2: #include
franta-hg@2: #include
franta-hg@2: #include
franta-hg@5: #include
franta-hg@1:
franta-hg@1: #include "DJMFix.h"
franta-hg@2: #include "AlsaBridge.h"
franta-hg@12: #include "Logger.h"
franta-hg@2:
franta-hg@5: static std::atomic run{true};
franta-hg@2:
franta-hg@2: void interrupt(int signal) {
franta-hg@2: run = false;
franta-hg@2: }
franta-hg@1:
franta-hg@10: /**
franta-hg@16: * The support for Pioneer DJ DJM-250MK2 (an external USB sound card / mixer)
franta-hg@16: * was added to the Linux (kernel) by these patches:
franta-hg@10: *
franta-hg@16: * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/...
franta-hg@16: * - ...sound/usb?id=73d8c94084341e2895169a0462dbc18167f01683 (playback)
franta-hg@16: * - ...sound/usb?id=14335d8b9e1a2bf006f9d969a103f9731cabb210 (recording)
franta-hg@16: * - ...sound/usb?id=cdc01a1558dedcee3daee7e1802d0349a07edb87 (mixer setup)
franta-hg@10: *
franta-hg@16: * These patches are enough for playback and for recording from post CH faders.
franta-hg@10: *
franta-hg@16: * However this mixer is somehow incapacitated and if we want to record the raw
franta-hg@16: * signal from the PHONO or LINE channels, we only get silence. This feature is
franta-hg@16: * important for DVS (Digital Vinyl Systems) setups where the timecode signal
franta-hg@16: * from special control vinyls flows from mixer to the computer where it is
franta-hg@16: * interpreted in a software like MIXXX and used for controlling the playback of
franta-hg@16: * files on our computer. The signal (usually music) from these files flows back
franta-hg@16: * to the mixer and then to speakers and headphones.
franta-hg@10: *
franta-hg@16: * To make this work and enjoy all the features of the device we have bought, we
franta-hg@16: * need to tell the mixer that we want the signal instead of silence on given
franta-hg@16: * channels. And this is the purpose of the djm-fix utility and it is done by
franta-hg@16: * sending some magic packet to the mixer.
franta-hg@10: *
franta-hg@18: * Implementation of this magic in the DJMFix.cpp file is based on publicly
franta-hg@16: * available documentation that can be found at:
franta-hg@16: * - https://swiftb0y.github.io/CDJHidProtocol/hid-analysis/handshake.html
franta-hg@16: * - https://mixb.me/CDJHidProtocol/hid-analysis/handshake.html (formerly).
franta-hg@16: * This page pointed me to the proper hash function (according to the constants,
franta-hg@16: * it is bit uncommon but publicly known Fowler–Noll–Vo hash function, FNV) and
franta-hg@16: * some magic bits. I wrote this standalone C++ program that talks with the
franta-hg@16: * mixer over MIDI SysEx messages and does the magic.
franta-hg@10: *
franta-hg@16: * When this program is started, it finds the mixer and makes it fully working.
franta-hg@16: * It needs to be running all the time, otherwise we will get silence
franta-hg@16: * on the PHONO/LINE channels again.
franta-hg@10: *
franta-hg@10: * Install dependencies:
franta-hg@16: * apt install mercurial make pkg-config g++ libasound2-dev
franta-hg@16: * (in Debian or Ubuntu - it will be similar in other distributions)
franta-hg@10: *
franta-hg@10: * Download djm-fix:
franta-hg@16: * hg clone https://hg.frantovo.cz/midi/djm-fix/ # primary source
franta-hg@16: * hg clone https://hg.globalcode.info/midi/djm-fix/ # mirror
franta-hg@10: *
franta-hg@10: * Compile:
franta-hg@16: * make # can be skipped
franta-hg@10: *
franta-hg@10: * Run:
franta-hg@16: * make run # in most cases
franta-hg@16: * build/djm-fix 'Pioneer DJ.*' # with custom name
franta-hg@16: * ^ regular expression to select desired card
franta-hg@10: *
franta-hg@10: * Stop:
franta-hg@10: * press Ctrl+C
franta-hg@11: *
franta-hg@16: * Look for updates in the Mercurial repositories and at:
franta-hg@16: * - https://blog.frantovo.cz/c/387/
franta-hg@18: * - https://blog.frantovo.cz/c/396/
franta-hg@10: */
franta-hg@10:
franta-hg@0: int main(int argc, char**argv) {
franta-hg@12: using L = djmfix::logging::Level;
franta-hg@16: std::unique_ptr
franta-hg@16: logger(djmfix::logging::create(std::cerr, L::INFO));
franta-hg@11: try {
franta-hg@13: logger->log(L::INFO, "DJM-Fix started.");
franta-hg@18: std::string cardNamePattern = argc == 2
franta-hg@18: ? argv[1]
franta-hg@18: : "(Pioneer DJ|AlphaTheta).*";
franta-hg@10:
franta-hg@11: signal(SIGINT, interrupt);
franta-hg@12: std::unique_ptr djmFix(djmfix::create(logger.get()));
franta-hg@16: std::unique_ptr alsaBridge(
franta-hg@16: djmfix::alsa::create(djmFix.get(),
franta-hg@16: cardNamePattern,
franta-hg@16: logger.get()));
franta-hg@1:
franta-hg@11: alsaBridge->start();
franta-hg@11: while (run) std::this_thread::sleep_for(std::chrono::milliseconds(100));
franta-hg@16:
franta-hg@12: std::cerr << std::endl;
franta-hg@13: logger->log(L::INFO, "DJM-Fix stopping.");
franta-hg@16:
franta-hg@11: alsaBridge->stop();
franta-hg@1:
franta-hg@11: return 0;
franta-hg@11: } catch (const std::exception& e) {
franta-hg@12: logger->log(L::SEVERE, e.what());
franta-hg@11: }
franta-hg@0: }