src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 04 Oct 2020 00:04:28 +0200
branchv_0
changeset 15 b3239e4ad328
parent 14 9ad606c80d3b
child 16 d17d70731446
permissions -rw-r--r--
connect to configured destination ports + syncCallback
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    20
#include <atomic>
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
6
4174fc0c2e7c send some (constant, dummy) MIDI message to JACK
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    22
#include <cstring>
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <vector>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <iostream>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <sstream>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <locale>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <codecvt>
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    28
#include <sys/mman.h>
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    29
#include <signal.h>
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    30
#include <unistd.h>
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
#include <jack/jack.h>
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    33
#include <jack/midiport.h>
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
#include <jack/ringbuffer.h>
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/common/type/typedefs.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include <relpipe/reader/TypeId.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
#include <relpipe/reader/handlers/AttributeMetadata.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
#include "Configuration.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
#include "JackException.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace relpipe {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace out {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
namespace jack {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
int dequeueMessages(jack_nframes_t frames, void* arg);
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
private:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	Configuration& configuration;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
	std::atomic<bool> continueProcessing{true};
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
	/**
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
	 * Is passed through the ring buffer
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
	 * from the relpipe-reading thread to the jack-writing thread (callback).
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    60
	 */
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
	struct MidiMessage {
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    62
		uint8_t buffer[4096] = {0};
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
		uint32_t size;
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
		uint32_t time;
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
	};
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
	/**
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    68
	 * JACK callbacks (called from the real-time thread)
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    69
	 */
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    70
	class RealTimeContext {
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    71
	public:
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    72
		jack_client_t* jackClient = nullptr;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    73
		jack_port_t* jackPort = nullptr;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    74
		jack_ringbuffer_t* ringBuffer = nullptr;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    75
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    76
		const int RING_BUFFER_SIZE = 100;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    77
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    78
		int processCallback(jack_nframes_t frames) {
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    79
			const size_t queuedMessages = jack_ringbuffer_read_space(ringBuffer) / sizeof (MidiMessage);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    80
			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
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    81
			jack_midi_clear_buffer(jackPortBuffer); // TODO: clean buffer?
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    82
			for (size_t i = 0; i < queuedMessages && i < frames; i++) {
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    83
				// TODO: correct timing?
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    84
				MidiMessage m;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    85
				jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage));
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    86
				jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, m.time, m.size);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    87
				memcpy(midiData, m.buffer, m.size);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    88
			}
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    89
			return 0;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    90
		}
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    91
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    92
		int syncCallback(jack_transport_state_t state, jack_position_t* position) {
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    93
			std::wcerr << L"syncCallback()" << std::endl;
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    94
			return true;
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    95
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    96
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    97
		static int processCallback(jack_nframes_t frames, void* instance) {
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    98
			return static_cast<RealTimeContext*> (instance)->processCallback(frames);
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    99
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   100
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   101
		static int syncCallback(jack_transport_state_t state, jack_position_t* position, void* instance) {
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   102
			return static_cast<RealTimeContext*> (instance)->syncCallback(state, position);
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   103
		}
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   104
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   105
	} realTimeContext;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   106
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   107
	/**
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   108
	 * Temporary storage of values read from relational input.
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   109
	 */
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   110
	class RelationContext {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   111
	public:
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   112
		bool skip = false;
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   113
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   114
		size_t indexOfEvent = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   115
		size_t indexOfChannel = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   116
		size_t indexOfNoteOn = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   117
		size_t indexOfNotePitch = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   118
		size_t indexOfNoteVelocity = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   119
		size_t indexOfControllerId = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   120
		size_t indexOfControllerValue = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   121
		size_t indexOfRaw = -1;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   122
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   123
		size_t attributeCount = 0;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   124
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   125
		class RecordContext {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   126
		public:
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   127
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   128
			enum class Event {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   129
				NOTE,
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   130
				CONTROL,
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   131
				SYSEX,
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   132
				UNKNOWN
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   133
			};
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   134
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   135
			static Event parseEventType(const relpipe::common::type::StringX name) {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   136
				if (name == L"note") return Event::NOTE;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   137
				else if (name == L"control") return Event::CONTROL;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   138
				else if (name == L"sysex") return Event::SYSEX;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   139
				else return Event::UNKNOWN;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   140
			}
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   141
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   142
			Event event = Event::UNKNOWN;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   143
			relpipe::common::type::Integer channel;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   144
			relpipe::common::type::Boolean noteOn;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   145
			relpipe::common::type::Integer notePitch;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   146
			relpipe::common::type::Integer noteVelocity;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   147
			relpipe::common::type::Integer controllerId;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   148
			relpipe::common::type::Integer controllerValue;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   149
			relpipe::common::type::StringX raw;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   150
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   151
			size_t attributeIndex = 0;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   152
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   153
		} recordContext;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   154
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   155
	} relationContext;
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   156
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   157
	static void jackErrorCallback(const char * message) {
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   158
		std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   159
		std::wcerr << L"JACK: " << convertor.from_bytes(message) << std::endl;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   160
	}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   161
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
public:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
	JackHandler(Configuration& configuration) : configuration(configuration) {
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   165
		// Initialize JACK connection:
2
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   166
		std::string clientName = convertor.to_bytes(configuration.jackClientName);
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   167
		realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   168
		if (realTimeContext.jackClient == nullptr) throw JackException(L"Could not create JACK client.");
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   169
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   170
		realTimeContext.ringBuffer = jack_ringbuffer_create(realTimeContext.RING_BUFFER_SIZE * sizeof (MidiMessage));
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   171
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   172
		jack_set_process_callback(realTimeContext.jackClient, RealTimeContext::processCallback, &realTimeContext);
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   173
		jack_set_sync_callback(realTimeContext.jackClient, RealTimeContext::syncCallback, &realTimeContext);
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   174
		// TODO: report also other events (connections etc.)
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   175
		jack_set_error_function(jackErrorCallback);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   176
		jack_set_info_function(jackErrorCallback);
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   177
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   178
		realTimeContext.jackPort = jack_port_register(realTimeContext.jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   179
		if (realTimeContext.jackPort == nullptr) throw JackException(L"Could not register port.");
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   180
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   181
		if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   182
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   183
		int jackError = jack_activate(realTimeContext.jackClient);
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   184
		if (jackError) throw JackException(L"Could not activate client.");
5
6be3464ccb2b wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   185
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   186
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   187
		// Connect to configured destination ports:
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   188
		const char* jackPortName = jack_port_name(realTimeContext.jackPort);
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   189
		for (auto destinationPort : configuration.portsToConnect) {
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   190
			int error = jack_connect(realTimeContext.jackClient, jackPortName, convertor.to_bytes(destinationPort).c_str());
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   191
			if (error) throw JackException(L"Connection to the JACK port failed: " + destinationPort);
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   192
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   193
5
6be3464ccb2b wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   194
		// Wait for a port connection, because it does not make much sense to send MIDI events nowhere:
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   195
		while (jack_port_connected(realTimeContext.jackPort) < configuration.requiredJackConnections) usleep(10000);
8
27579c5cdc51 send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   196
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   197
		// TODO: finalize on exceptions
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   201
		// TODO: validate metadata and prepare attribute mappings (names and types are important, order does not matter)
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   202
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   203
		relationContext = RelationContext();
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   204
		relationContext.attributeCount = attributes.size();
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   205
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   206
		for (int i = 0; i < relationContext.attributeCount; i++) {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   207
			const relpipe::common::type::StringX attributeName = attributes[i].getAttributeName();
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   208
			if (attributeName == L"event") relationContext.indexOfEvent = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   209
			else if (attributeName == L"channel") relationContext.indexOfChannel = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   210
			else if (attributeName == L"controller_id") relationContext.indexOfControllerId = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   211
			else if (attributeName == L"controller_value") relationContext.indexOfControllerValue = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   212
			else if (attributeName == L"note_on") relationContext.indexOfNoteOn = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   213
			else if (attributeName == L"note_pitch") relationContext.indexOfNotePitch = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   214
			else if (attributeName == L"note_velocity") relationContext.indexOfNoteVelocity = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   215
			else if (attributeName == L"raw") relationContext.indexOfRaw = i;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   216
		}
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   217
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   218
		// TODO: check also data types and skipt relation if important attributes are missing
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   219
		// TODO: configurable relation name
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   220
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   221
		if (relationContext.indexOfRaw == -1) {
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   222
			relationContext.skip = true;
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   223
			fwprintf(stderr, L"Relation „%ls“ will be ignored because mandatory attribute „raw“ is missing.\n", name.c_str());
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   224
		}
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   225
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
	void attribute(const relpipe::common::type::StringX& value) override {
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   229
		if (relationContext.skip) return;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   230
		// TODO: switch to RelationalReaderStringHandler
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   231
		// TODO: if (continueProcessing) {} ?
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   232
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   233
		RelationContext& rel = relationContext;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   234
		RelationContext::RecordContext& rec = rel.recordContext;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   235
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   236
		if (rel.indexOfEvent == rec.attributeIndex) rec.event = rec.parseEventType(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   237
		else if (rel.indexOfChannel == rec.attributeIndex) rec.channel = std::stoi(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   238
		else if (rel.indexOfControllerId == rec.attributeIndex) rec.controllerId = std::stoi(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   239
		else if (rel.indexOfControllerValue == rec.attributeIndex) rec.controllerValue = std::stoi(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   240
		else if (rel.indexOfNoteOn == rec.attributeIndex) rec.noteOn = value == L"true";
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   241
		else if (rel.indexOfNotePitch == rec.attributeIndex) rec.notePitch = std::stoi(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   242
		else if (rel.indexOfNoteVelocity == rec.attributeIndex) rec.noteVelocity = std::stoi(value);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   243
		else if (rel.indexOfRaw == rec.attributeIndex) rec.raw = value;
7
b2fc99295546 send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   244
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   245
		rec.attributeIndex++;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   246
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   247
		if (rec.attributeIndex == rel.attributeCount) {
12
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   248
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   249
			while (jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) usleep(1000); // should not happen, the real-time thread should be faster
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   250
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   251
			MidiMessage m;
8
27579c5cdc51 send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   252
12
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   253
			// TODO: correct timing?
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   254
			m.time = 0;
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   255
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   256
			if (rec.event == RelationContext::RecordContext::Event::NOTE) {
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   257
				m.size = 3;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   258
				m.buffer[0] = (rec.noteOn ? 0x90 : 0x80) | rec.channel;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   259
				m.buffer[1] = rec.notePitch;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   260
				m.buffer[2] = rec.noteVelocity;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   261
			} else if (rec.event == RelationContext::RecordContext::Event::CONTROL) {
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   262
				m.size = 3;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   263
				m.buffer[0] = 0xB0 | rec.channel;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   264
				m.buffer[1] = rec.controllerId;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   265
				m.buffer[2] = rec.controllerValue;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   266
			} else { // SysEx and other raw messages
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   267
				m.size = 0;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   268
				size_t nibble = 0;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   269
				for (int i = 0; i < rec.raw.size() && m.size < sizeof (m.buffer); i++) {
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   270
					wchar_t ch = rec.raw[i];
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   271
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   272
					if (ch == L' ') continue;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   273
					else if (ch >= L'0' && ch <= L'9') m.buffer[m.size] += (ch - '0') /**/ << (nibble % 2 ? 0 : 4);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   274
					else if (ch >= L'a' && ch <= L'f') m.buffer[m.size] += (ch - 'a' + 10) << (nibble % 2 ? 0 : 4);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   275
					else if (ch >= L'A' && ch <= L'F') m.buffer[m.size] += (ch - 'A' + 10) << (nibble % 2 ? 0 : 4);
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   276
					else throw JackException(L"Invalid character in the hexadeximal sequence: „" + std::wstring(1, ch) + L"“.");
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   277
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   278
					nibble++;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   279
					if (nibble % 2 == 0) m.size++;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   280
				}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   281
			}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   282
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   283
			// TODO: if (m.size == 0) fwprintf(stderr, L"Missing data\n");
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   284
12
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   285
			jack_ringbuffer_write(realTimeContext.ringBuffer, (const char *) &m, sizeof (m));
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   286
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   287
			relationContext.recordContext = RelationContext::RecordContext();
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   288
		}
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   289
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   290
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   291
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   292
	void endOfPipe() {
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   293
		// TODO: send optional (configurable) MIDI events
4
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   294
65dfbf0494a3 pass MIDI messages through the ring buffer (queue) from one thread to another
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   295
		// Wait until the ring buffer is empty
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   296
		while (continueProcessing && jack_ringbuffer_read_space(realTimeContext.ringBuffer)) usleep(1000);
7
b2fc99295546 send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   297
		usleep(1000000);
b2fc99295546 send correct number of messages to JACK + wait in order to be sure that all events were sent
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   298
		// TODO: better waiting (ringBuffer might be empty, but events have not been sent to JACK yet)
8
27579c5cdc51 send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   299
		// TODO: optionally mute all; probably enabled by default
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   300
	}
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   301
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   302
	void finish(int sig) {
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   303
		continueProcessing = false;
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   304
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   306
	virtual ~JackHandler() {
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   307
		// Close JACK connection:
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   308
		jack_deactivate(realTimeContext.jackClient);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   309
		jack_client_close(realTimeContext.jackClient);
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   310
		jack_ringbuffer_free(realTimeContext.ringBuffer);
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   311
	}
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   312
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
};
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   314
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   316
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
}