src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 06 Oct 2020 01:31:22 +0200
branchv_0
changeset 20 ccc4e509c1f0
parent 19 dcd98589f4b8
child 21 70b252d02a92
permissions -rw-r--r--
use signal instead of sleep while waiting in attribute()

/**
 * 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 <pthread.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;

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

			if (pthread_mutex_trylock(&processingLock) == 0) {
				pthread_cond_signal(&processingDone);
				pthread_mutex_unlock(&processingLock);
			}

			return 0;
		}

		int syncCallback(jack_transport_state_t state, jack_position_t* position) {
			return true;
		}

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

		static int syncCallback(jack_transport_state_t state, jack_position_t* position, void* instance) {
			return static_cast<RealTimeContext*> (instance)->syncCallback(state, position);
		}

	} realTimeContext;

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

		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;

		bool hasAttribute(const relpipe::common::type::StringX& name) {
			for (auto a : attributes) if (a.getAttributeName() == name) return true;
			return false;
		}

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

	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:

	JackHandler(Configuration& configuration) : configuration(configuration) {
		pthread_mutex_unlock(&realTimeContext.processingLock);

		// 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) 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);
		jack_set_sync_callback(realTimeContext.jackClient, RealTimeContext::syncCallback, &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) 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 destinationPort : configuration.portsToConnect) {
			int error = jack_connect(realTimeContext.jackClient, jackPortName, convertor.to_bytes(destinationPort).c_str());
			if (error) failInConstructor(L"Connection to the JACK port failed: " + destinationPort);
		}

		// 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);

	}

	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
		relationContext = RelationContext();
		relationContext.attributes = attributes;

		// TODO: configurable relation name

		if (!relationContext.hasAttribute(L"raw")) {
			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 RelationalReaderValueHandler

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

		const auto attributeName = rel.attributes[rec.attributeIndex].getAttributeName();

		if (attributeName == L"event") rec.event = rec.parseEventType(value);
		else if (attributeName == L"channel") rec.channel = std::stoi(value);
		else if (attributeName == L"controller_id") rec.controllerId = std::stoi(value);
		else if (attributeName == L"controller_value") rec.controllerValue = std::stoi(value);
		else if (attributeName == L"note_on") rec.noteOn = value == L"true";
		else if (attributeName == L"note_pitch") rec.notePitch = std::stoi(value);
		else if (attributeName == L"note_velocity") rec.noteVelocity = std::stoi(value);
		else if (attributeName == L"raw") rec.raw = value;

		rec.attributeIndex++;

		if (rec.attributeIndex == rel.attributes.size()) {

			while (continueProcessing && jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) waitForRTCycle(); // should not happen, the real-time thread should be faster; see also note in endOfPipe()
			if (!continueProcessing) return;

			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: optionally mute all; probably enabled by default

		// Wait until the ring buffer is empty (messages dequeued from the buffer) and real-time cycle was finished (messages passed to JACK)
		while (continueProcessing && jack_ringbuffer_read_space(realTimeContext.ringBuffer)) pthread_cond_wait(&realTimeContext.processingDone, &realTimeContext.processingLock);

		// There might be a (rare) race condition.
		// Between jack_ringbuffer_read_space() and pthread_cond_wait() the real-time thread might finish the cycle and we will miss the pthread_cond_signal()
		// and will sleep until the next cycle. In endOfPipe() it is not a big problem.
		// But missing the cycle in attribute() might be worse.
		// However it should not happen due to the buffer size
		// and amount of work done in the real-time thread (message copying) vs. amount of work done between the jack_ringbuffer_write_space() and pthread_cond_wait() calls (nothing).
	}

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

	virtual ~JackHandler() {
		finalize();
	}

};

}
}
}