add option --list-connections for listing JACK connections
the output can be later sent to the relpipe-out-jack to restore the connection graph
/**
* Relational pipes
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <atomic>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <sys/mman.h>
#include <unistd.h>
#include <pthread.h>
#include <functional>
#include <iomanip>
#include <regex>
#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>
#include <relpipe/common/type/typedefs.h>
#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>
#include <relpipe/cli/CLI.h>
#include "Configuration.h"
#include "JackException.h"
namespace relpipe {
namespace in {
namespace jack {
int enqueueMessage(jack_nframes_t frames, void* arg);
class JackCommand {
private:
Configuration& configuration;
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
std::atomic<bool> continueProcessing{true};
int maxJackPortConnections = 0;
/**
* Is passed through the ring buffer
* from the the jack-writing thread (callback) to the relpipe-writing thread.
*/
struct MidiMessage {
uint8_t buffer[4096] = {0};
uint32_t size;
uint32_t time;
};
/**
* JACK callbacks (called from the real-time thread)
*/
class RealTimeContext {
public:
jack_client_t* jackClient = nullptr;
jack_port_t* jackPort = nullptr;
jack_ringbuffer_t* ringBuffer = nullptr;
pthread_mutex_t processingLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t processingDone = PTHREAD_COND_INITIALIZER;
const int RING_BUFFER_SIZE = 100;
int processCallback(jack_nframes_t frames) {
void* buffer = jack_port_get_buffer(jackPort, frames);
if (buffer == nullptr) throw JackException(L"Unable to get port buffer."); // TODO: exception in RT callback?
for (jack_nframes_t i = 0, eventCount = jack_midi_get_event_count(buffer); i < eventCount; i++) {
jack_midi_event_t event;
int noData = jack_midi_event_get(&event, buffer, i);
if (noData) continue;
if (event.size > sizeof (MidiMessage::buffer)) {
// TODO: should not printf in RT callback:
fwprintf(stderr, L"Error: MIDI message was too large → skipping event. Maximum allowed size: %lu bytes.\n", sizeof (MidiMessage::buffer));
} else if (jack_ringbuffer_write_space(ringBuffer) >= sizeof (MidiMessage)) {
MidiMessage m;
m.time = event.time;
m.size = event.size;
memcpy(m.buffer, event.buffer, event.size);
jack_ringbuffer_write(ringBuffer, (const char *) &m, sizeof (MidiMessage));
} else {
// TODO: should not printf in RT callback:
fwprintf(stderr, L"Error: ring buffer is full → skipping event.\n");
}
}
// TODO: just count skipped events and bytes and report them in next successful message instead of printing to STDERR
if (pthread_mutex_trylock(&processingLock) == 0) {
pthread_cond_signal(&processingDone);
pthread_mutex_unlock(&processingLock);
}
return 0;
}
static int processCallback(jack_nframes_t frames, void* instance) {
return static_cast<RealTimeContext*> (instance)->processCallback(frames);
}
} realTimeContext;
static void writeRecord(std::shared_ptr<relpipe::writer::RelationalWriter> writer,
relpipe::common::type::StringX eventType, relpipe::common::type::Integer channel,
relpipe::common::type::Boolean noteOn, relpipe::common::type::Integer pitch, relpipe::common::type::Integer velocity,
relpipe::common::type::Integer controllerId, relpipe::common::type::Integer value,
relpipe::common::type::StringX raw) {
writer->writeAttribute(eventType);
writer->writeAttribute(&channel, typeid (channel));
writer->writeAttribute(¬eOn, typeid (noteOn));
writer->writeAttribute(&pitch, typeid (pitch));
writer->writeAttribute(&velocity, typeid (velocity));
writer->writeAttribute(&controllerId, typeid (controllerId));
writer->writeAttribute(&value, typeid (value));
writer->writeAttribute(&raw, typeid (raw));
}
void processMessage(std::shared_ptr<relpipe::writer::RelationalWriter> writer, MidiMessage* event) {
if (event->size == 0) {
return;
} else {
uint8_t type = event->buffer[0] & 0xF0;
uint8_t channel = event->buffer[0] & 0x0F;
// TODO: write timestamp, message number
if ((type == 0x90 || type == 0x80) && event->size == 3) {
writeRecord(writer, L"note", channel, type == 0x90, event->buffer[1], event->buffer[2], 0, 0, toHex(event));
} else if (type == 0xB0 && event->size == 3) {
writeRecord(writer, L"control", channel, false, 0, 0, event->buffer[1], event->buffer[2], toHex(event));
} else if (event->buffer[0] == 0xF0) {
writeRecord(writer, L"sysex", channel, false, 0, 0, 0, 0, toHex(event));
} else {
writeRecord(writer, L"unknown", channel, false, 0, 0, 0, 0, toHex(event));
}
}
}
relpipe::common::type::StringX toHex(MidiMessage* event) {
std::wstringstream result;
result << std::hex << std::setfill(L'0');
for (size_t i = 0; i < event->size && i < sizeof (event->buffer); i++) {
if (i > 0) result << L' ';
result << std::setw(2) << event->buffer[i];
// result << ("0123456789abcdef"[event->buffer[i] >> 4]);
// result << ("0123456789abcdef"[event->buffer[i] & 0xf]);
}
return result.str();
}
void listPorts(std::shared_ptr<relpipe::writer::RelationalWriter> writer) {
using namespace relpipe::writer;
vector<AttributeMetadata> metadata;
metadata.push_back({L"name", TypeId::STRING});
metadata.push_back({L"input", TypeId::BOOLEAN});
metadata.push_back({L"output", TypeId::BOOLEAN});
metadata.push_back({L"physical", TypeId::BOOLEAN});
metadata.push_back({L"terminal", TypeId::BOOLEAN});
metadata.push_back({L"mine", TypeId::BOOLEAN});
metadata.push_back({L"midi", TypeId::BOOLEAN});
metadata.push_back({L"type", TypeId::STRING});
writer->startRelation(L"port", metadata, true);
const char** portNames = jack_get_ports(realTimeContext.jackClient, nullptr, nullptr, 0);
std::regex midiTypePattern(".*midi$");
for (const char** portName = portNames; *portName; portName++) {
jack_port_t* port = jack_port_by_name(realTimeContext.jackClient, *portName);
const char* portType = jack_port_type(port);
int portFlags = jack_port_flags(port);
bool isInput = portFlags & JackPortFlags::JackPortIsInput;
bool isOuputput = portFlags & JackPortFlags::JackPortIsOutput;
bool isPhysical = portFlags & JackPortFlags::JackPortIsPhysical;
bool isTerminal = portFlags & JackPortFlags::JackPortIsTerminal;
bool isMine = jack_port_is_mine(realTimeContext.jackClient, port);
bool isMidi = std::regex_search(portType, midiTypePattern);
writer->writeAttribute(convertor.from_bytes(*portName));
writer->writeAttribute(&isInput, typeid (isInput));
writer->writeAttribute(&isOuputput, typeid (isOuputput));
writer->writeAttribute(&isPhysical, typeid (isPhysical));
writer->writeAttribute(&isTerminal, typeid (isTerminal));
writer->writeAttribute(&isMine, typeid (isMine));
writer->writeAttribute(&isMidi, typeid (isMidi));
writer->writeAttribute(convertor.from_bytes(portType));
}
jack_free(portNames);
}
void listConnections(std::shared_ptr<relpipe::writer::RelationalWriter> writer) {
using namespace relpipe::writer;
vector<AttributeMetadata> metadata;
metadata.push_back({L"event", TypeId::STRING});
metadata.push_back({L"source_port", TypeId::STRING});
metadata.push_back({L"destination_port", TypeId::STRING});
writer->startRelation(L"connection", metadata, true);
const relpipe::common::type::StringX event = L"connect";
const char** sourcePortNames = jack_get_ports(realTimeContext.jackClient, nullptr, nullptr, JackPortFlags::JackPortIsOutput);
for (const char** sourcePortName = sourcePortNames; *sourcePortName; sourcePortName++) {
jack_port_t* sourcePort = jack_port_by_name(realTimeContext.jackClient, *sourcePortName);
const char** destinationPortNames = jack_port_get_all_connections(realTimeContext.jackClient, sourcePort);
for (const char** destinationPortName = destinationPortNames; destinationPortNames && *destinationPortName; destinationPortName++) {
writer->writeAttribute(event);
writer->writeAttribute(convertor.from_bytes(*sourcePortName));
writer->writeAttribute(convertor.from_bytes(*destinationPortName));
}
jack_free(destinationPortNames);
}
jack_free(sourcePortNames);
}
static void jackErrorCallback(const char * message) {
std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
std::wcerr << L"JACK: " << convertor.from_bytes(message) << std::endl;
}
void finalize() {
// Close JACK connection:
jack_deactivate(realTimeContext.jackClient);
jack_client_close(realTimeContext.jackClient);
jack_ringbuffer_free(realTimeContext.ringBuffer);
pthread_mutex_unlock(&realTimeContext.processingLock);
}
void failInConstructor(const relpipe::common::type::StringX& errorMessage) {
finalize();
throw JackException(errorMessage);
}
/**
* Wait for the signal that is emitted at the end of the real-time processCallback() cycle.
*/
void waitForRTCycle() {
pthread_cond_wait(&realTimeContext.processingDone, &realTimeContext.processingLock);
}
public:
JackCommand(Configuration& configuration) : configuration(configuration) {
pthread_mutex_lock(&realTimeContext.processingLock);
// Initialize JACK connection:
std::string clientName = convertor.to_bytes(configuration.client);
realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
if (realTimeContext.jackClient == nullptr) failInConstructor(L"Could not create JACK client.");
realTimeContext.ringBuffer = jack_ringbuffer_create(realTimeContext.RING_BUFFER_SIZE * sizeof (MidiMessage));
jack_set_process_callback(realTimeContext.jackClient, RealTimeContext::processCallback, &realTimeContext);
// TODO: report also other events (connections etc.)
jack_set_error_function(jackErrorCallback);
jack_set_info_function(jackErrorCallback);
realTimeContext.jackPort = jack_port_register(realTimeContext.jackClient, convertor.to_bytes(configuration.port).c_str(), JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
if (realTimeContext.jackPort == nullptr) failInConstructor(L"Could not register the JACK port.");
if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");
int jackError = jack_activate(realTimeContext.jackClient);
if (jackError) failInConstructor(L"Could not activate the JACK client.");
// Connect to configured destination ports:
const char* jackPortName = jack_port_name(realTimeContext.jackPort);
for (auto sourcePort : configuration.connectTo) {
int error = jack_connect(realTimeContext.jackClient, convertor.to_bytes(sourcePort).c_str(), jackPortName);
if (error) failInConstructor(L"Connection to the JACK port failed: " + sourcePort);
}
}
void processJackStream(std::shared_ptr<relpipe::writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
// Relation headers:
using namespace relpipe::writer;
vector<AttributeMetadata> metadata;
if (configuration.listPorts) listPorts(writer);
if (configuration.listConnections) listConnections(writer);
if (!configuration.listMidiMessages) return;
metadata.push_back({L"event", TypeId::STRING});
metadata.push_back({L"channel", TypeId::INTEGER});
metadata.push_back({L"note_on", TypeId::BOOLEAN});
metadata.push_back({L"note_pitch", TypeId::INTEGER});
metadata.push_back({L"note_velocity", TypeId::INTEGER});
metadata.push_back({L"controller_id", TypeId::INTEGER});
metadata.push_back({L"controller_value", TypeId::INTEGER});
metadata.push_back({L"raw", TypeId::STRING});
writer->startRelation(L"midi", metadata, true);
relationalWriterFlush();
// Process messages from the ring buffer queue:
while (continueProcessing) {
while (jack_ringbuffer_read_space(realTimeContext.ringBuffer) >= sizeof (MidiMessage)) {
MidiMessage m;
jack_ringbuffer_read(realTimeContext.ringBuffer, (char*) &m, sizeof (MidiMessage));
processMessage(writer, &m);
relationalWriterFlush();
}
waitForRTCycle();
// Once the Configuration::requiredJackConnections count was reached, we will disconnect if the count drops under this level.
if (configuration.requiredConnections) {
int currentConnectionCount = jack_port_connected(realTimeContext.jackPort);
if (currentConnectionCount > maxJackPortConnections) maxJackPortConnections = currentConnectionCount;
else if (maxJackPortConnections >= configuration.requiredConnections && currentConnectionCount < configuration.requiredConnections) break;
}
}
}
void finish(int sig) {
continueProcessing = false;
}
virtual ~JackCommand() {
finalize();
}
};
}
}
}