src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 17 Oct 2020 19:36:30 +0200
branchv_0
changeset 27 45a1742b9854
parent 25 e0627da77dad
permissions -rw-r--r--
timing (first version)

/**
 * 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 <codecvt>
#include <sys/mman.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};
		size_t size;
		/**
		 * Time in micro seconds;
		 * starts at the beginning of the playback.
		 */
		relpipe::common::type::Integer time;
	};

	/**
	 * JACK callbacks (called from the real-time thread)
	 */
	class RealTimeContext {
	private:
		jack_nframes_t startFrame = 0;
	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) {
			jack_nframes_t lastFrame = jack_last_frame_time(jackClient);

			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);
			MidiMessage m;
			while (jack_ringbuffer_peek(ringBuffer, (char*) &m, sizeof (m)) == sizeof (m)) {
				if (startFrame == 0) startFrame = lastFrame;

				jack_nframes_t eventFrame = std::max(0L, (m.time * jack_get_sample_rate(jackClient) / 1000 / 1000) - (lastFrame - startFrame));
				// If std::max() does its job, the message comes from the past and missed its cycle → we will send it now rather than lose it completely.

				if (eventFrame < frames) {
					jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, eventFrame, m.size);
					if (midiData) {
						memcpy(midiData, m.buffer, m.size);
						jack_ringbuffer_read_advance(ringBuffer, sizeof (m));
					} // else = error: not enough space; will be kept in the ring buffer and sent in the next cycle
				} else {
					/**
					 * This message does not belong to this cycle.
					 * Its time will come later.
					 * For now, it stays in the ring buffer.
					 */
					break;
				}
			}

			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,
				CONNECT,
				DISCONNECT,
				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 if (name == L"connect") return Event::CONNECT;
				else if (name == L"disconnect") return Event::DISCONNECT;
				else return Event::UNKNOWN;
			}

			Event event = Event::UNKNOWN;
			relpipe::common::type::Integer time;
			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;
			relpipe::common::type::StringX connectionSourcePort;
			relpipe::common::type::StringX connectionDestinationPort;

			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_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);
		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, convertor.to_bytes(configuration.port).c_str(), 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.connectTo) {
			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.requiredConnections) 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"event")) {
			relationContext.skip = true;
			fwprintf(stderr, L"Relation „%ls“ will be ignored because mandatory attribute „event“ 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"time") rec.time = std::stoll(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;
		else if (attributeName == L"source_port") rec.connectionSourcePort = value;
		else if (attributeName == L"destination_port") rec.connectionDestinationPort = value;

		rec.attributeIndex++;

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

			while (continueProcessing && jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) waitForRTCycle(); // wait, if we are faster than the real-time thread
			if (!continueProcessing) return;

			MidiMessage m;

			m.time = rec.time;
			m.size = 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 if (rec.event == RelationContext::RecordContext::Event::CONNECT) {
				int result = jack_connect(realTimeContext.jackClient, convertor.to_bytes(rec.connectionSourcePort).c_str(), convertor.to_bytes(rec.connectionDestinationPort).c_str());
				if (result != 0 && result != EEXIST) std::wcerr << L"Unable to connect: „" << rec.connectionSourcePort << L"“ to: „" << rec.connectionDestinationPort << L"“." << std::endl;
			} else if (rec.event == RelationContext::RecordContext::Event::DISCONNECT) {
				int result = jack_disconnect(realTimeContext.jackClient, convertor.to_bytes(rec.connectionSourcePort).c_str(), convertor.to_bytes(rec.connectionDestinationPort).c_str());
				if (result != 0) std::wcerr << L"Unable to disconnect: „" << rec.connectionSourcePort << L"“ from: „" << rec.connectionDestinationPort << L"“." << std::endl;
			} else { // SysEx and other raw messages
				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++;
				}
			}

			if (m.size > 0) 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();
	}

};

}
}
}