src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 03 Oct 2020 18:20:30 +0200
branchv_0
changeset 14 9ad606c80d3b
parent 13 a85c191709e6
child 15 b3239e4ad328
permissions -rw-r--r--
encode MIDI messages + process JACK error output

/**
 * 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 <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <sys/mman.h>
#include <signal.h>
#include <unistd.h>

#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>

#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include "Configuration.h"
#include "JackException.h"

namespace relpipe {
namespace out {
namespace jack {

int dequeueMessages(jack_nframes_t frames, void* arg);

class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:
	Configuration& configuration;
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding

	std::atomic<bool> continueProcessing{true};

	/**
	 * Is passed through the ring buffer
	 * from the relpipe-reading thread to the jack-writing thread (callback).
	 */
	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;

		const int RING_BUFFER_SIZE = 100;

		int processCallback(jack_nframes_t frames) {
			const size_t queuedMessages = jack_ringbuffer_read_space(ringBuffer) / sizeof (MidiMessage);
			void* jackPortBuffer = jack_port_get_buffer(jackPort, frames); // jack_port_get_buffer() must be called outside the loop, otherwise it will multiply the MIDI events
			jack_midi_clear_buffer(jackPortBuffer); // TODO: clean buffer?
			for (size_t i = 0; i < queuedMessages && i < frames; i++) {
				// TODO: correct timing?
				MidiMessage m;
				jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage));
				jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, m.time, m.size);
				memcpy(midiData, m.buffer, m.size);
			}
			return 0;
		}

		static int processCallback(jack_nframes_t frames, void* arg) {
			return static_cast<RealTimeContext*> (arg)->processCallback(frames);
		}

	} realTimeContext;

	/**
	 * Temporary storage of values read from relational input.
	 */
	class RelationContext {
	public:
		bool skip = false;

		size_t indexOfEvent = -1;
		size_t indexOfChannel = -1;
		size_t indexOfNoteOn = -1;
		size_t indexOfNotePitch = -1;
		size_t indexOfNoteVelocity = -1;
		size_t indexOfControllerId = -1;
		size_t indexOfControllerValue = -1;
		size_t indexOfRaw = -1;

		size_t attributeCount = 0;

		class RecordContext {
		public:

			enum class Event {
				NOTE,
				CONTROL,
				SYSEX,
				UNKNOWN
			};

			static Event parseEventType(const relpipe::common::type::StringX name) {
				if (name == L"note") return Event::NOTE;
				else if (name == L"control") return Event::CONTROL;
				else if (name == L"sysex") return Event::SYSEX;
				else return Event::UNKNOWN;
			}

			Event event = Event::UNKNOWN;
			relpipe::common::type::Integer channel;
			relpipe::common::type::Boolean noteOn;
			relpipe::common::type::Integer notePitch;
			relpipe::common::type::Integer noteVelocity;
			relpipe::common::type::Integer controllerId;
			relpipe::common::type::Integer controllerValue;
			relpipe::common::type::StringX raw;

			size_t attributeIndex = 0;

		} recordContext;

	} relationContext;

	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;
	}

public:

	JackHandler(Configuration& configuration) : configuration(configuration) {
		// Initialize JACK connection:
		std::string clientName = convertor.to_bytes(configuration.jackClientName);
		realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
		if (realTimeContext.jackClient == nullptr) throw JackException(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, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
		if (realTimeContext.jackPort == nullptr) throw JackException(L"Could not register port.");

		if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");

		int jackError = jack_activate(realTimeContext.jackClient);
		if (jackError) throw JackException(L"Could not activate client.");

		// Wait for a port connection, because it does not make much sense to send MIDI events nowhere:
		while (jack_port_connected(realTimeContext.jackPort) < configuration.requiredJackConnections) usleep(10000);

		// TODO: configurable auto-connection to another client/port
	}

	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
		// TODO: validate metadata and prepare attribute mappings (names and types are important, order does not matter)

		relationContext = RelationContext();
		relationContext.attributeCount = attributes.size();

		for (int i = 0; i < relationContext.attributeCount; i++) {
			const relpipe::common::type::StringX attributeName = attributes[i].getAttributeName();
			if (attributeName == L"event") relationContext.indexOfEvent = i;
			else if (attributeName == L"channel") relationContext.indexOfChannel = i;
			else if (attributeName == L"controller_id") relationContext.indexOfControllerId = i;
			else if (attributeName == L"controller_value") relationContext.indexOfControllerValue = i;
			else if (attributeName == L"note_on") relationContext.indexOfNoteOn = i;
			else if (attributeName == L"note_pitch") relationContext.indexOfNotePitch = i;
			else if (attributeName == L"note_velocity") relationContext.indexOfNoteVelocity = i;
			else if (attributeName == L"raw") relationContext.indexOfRaw = i;
		}

		// TODO: check also data types and skipt relation if important attributes are missing
		// TODO: configurable relation name

		if (relationContext.indexOfRaw == -1) {
			relationContext.skip = true;
			fwprintf(stderr, L"Relation „%ls“ will be ignored because mandatory attribute „raw“ is missing.\n", name.c_str());
		}

	}

	void attribute(const relpipe::common::type::StringX& value) override {
		if (relationContext.skip) return;
		// TODO: switch to RelationalReaderStringHandler
		// TODO: if (continueProcessing) {} ?

		RelationContext& rel = relationContext;
		RelationContext::RecordContext& rec = rel.recordContext;

		if (rel.indexOfEvent == rec.attributeIndex) rec.event = rec.parseEventType(value);
		else if (rel.indexOfChannel == rec.attributeIndex) rec.channel = std::stoi(value);
		else if (rel.indexOfControllerId == rec.attributeIndex) rec.controllerId = std::stoi(value);
		else if (rel.indexOfControllerValue == rec.attributeIndex) rec.controllerValue = std::stoi(value);
		else if (rel.indexOfNoteOn == rec.attributeIndex) rec.noteOn = value == L"true";
		else if (rel.indexOfNotePitch == rec.attributeIndex) rec.notePitch = std::stoi(value);
		else if (rel.indexOfNoteVelocity == rec.attributeIndex) rec.noteVelocity = std::stoi(value);
		else if (rel.indexOfRaw == rec.attributeIndex) rec.raw = value;

		rec.attributeIndex++;

		if (rec.attributeIndex == rel.attributeCount) {

			while (jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) usleep(1000); // should not happen, the real-time thread should be faster

			MidiMessage m;

			// TODO: correct timing?
			m.time = 0;

			if (rec.event == RelationContext::RecordContext::Event::NOTE) {
				m.size = 3;
				m.buffer[0] = (rec.noteOn ? 0x90 : 0x80) | rec.channel;
				m.buffer[1] = rec.notePitch;
				m.buffer[2] = rec.noteVelocity;
			} else if (rec.event == RelationContext::RecordContext::Event::CONTROL) {
				m.size = 3;
				m.buffer[0] = 0xB0 | rec.channel;
				m.buffer[1] = rec.controllerId;
				m.buffer[2] = rec.controllerValue;
			} else { // SysEx and other raw messages
				m.size = 0;
				size_t nibble = 0;
				for (int i = 0; i < rec.raw.size() && m.size < sizeof (m.buffer); i++) {
					wchar_t ch = rec.raw[i];

					if (ch == L' ') continue;
					else if (ch >= L'0' && ch <= L'9') m.buffer[m.size] += (ch - '0') /**/ << (nibble % 2 ? 0 : 4);
					else if (ch >= L'a' && ch <= L'f') m.buffer[m.size] += (ch - 'a' + 10) << (nibble % 2 ? 0 : 4);
					else if (ch >= L'A' && ch <= L'F') m.buffer[m.size] += (ch - 'A' + 10) << (nibble % 2 ? 0 : 4);
					else throw JackException(L"Invalid character in the hexadeximal sequence: „" + std::wstring(1, ch) + L"“.");

					nibble++;
					if (nibble % 2 == 0) m.size++;
				}
			}

			// TODO: if (m.size == 0) fwprintf(stderr, L"Missing data\n");

			jack_ringbuffer_write(realTimeContext.ringBuffer, (const char *) &m, sizeof (m));

			relationContext.recordContext = RelationContext::RecordContext();
		}

	}

	void endOfPipe() {
		// TODO: send optional (configurable) MIDI events

		// Wait until the ring buffer is empty
		while (continueProcessing && jack_ringbuffer_read_space(realTimeContext.ringBuffer)) usleep(1000);
		usleep(1000000);
		// TODO: better waiting (ringBuffer might be empty, but events have not been sent to JACK yet)
		// TODO: optionally mute all; probably enabled by default
	}

	void finish(int sig) {
		continueProcessing = false;
	}

	virtual ~JackHandler() {
		// Close JACK connection:
		jack_deactivate(realTimeContext.jackClient);
		jack_client_close(realTimeContext.jackClient);
		jack_ringbuffer_free(realTimeContext.ringBuffer);
	}

};

}
}
}