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)
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 <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
    25
#include <sys/mman.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
    26
#include <unistd.h>
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    27
#include <pthread.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
    28
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
#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
    30
#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
    31
#include <jack/ringbuffer.h>
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/common/type/typedefs.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/reader/TypeId.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/reader/handlers/AttributeMetadata.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include "Configuration.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
#include "JackException.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
namespace relpipe {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace out {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace jack {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    45
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
    46
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
private:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	Configuration& configuration;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	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
    51
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
	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
    53
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    54
	/**
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    55
	 * Is passed through the ring buffer
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
	 * 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
    57
	 */
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
	struct MidiMessage {
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    59
		uint8_t buffer[4096] = {0};
25
e0627da77dad correct types in MidiMessage + do not copy data if jack_midi_event_reserve() failed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    60
		size_t size;
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    61
		/**
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    62
		 * Time in micro seconds;
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    63
		 * starts at the beginning of the playback.
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    64
		 */
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    65
		relpipe::common::type::Integer time;
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
	};
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
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
    68
	/**
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    69
	 * 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
    70
	 */
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    71
	class RealTimeContext {
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    72
	private:
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    73
		jack_nframes_t startFrame = 0;
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    74
	public:
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    75
		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
    76
		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
    77
		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
    78
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    79
		pthread_mutex_t processingLock = PTHREAD_MUTEX_INITIALIZER;
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    80
		pthread_cond_t processingDone = PTHREAD_COND_INITIALIZER;
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    81
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    82
		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
    83
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    84
		int processCallback(jack_nframes_t frames) {
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    85
			jack_nframes_t lastFrame = jack_last_frame_time(jackClient);
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    86
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    87
			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
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    88
			jack_midi_clear_buffer(jackPortBuffer);
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    89
			MidiMessage m;
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    90
			while (jack_ringbuffer_peek(ringBuffer, (char*) &m, sizeof (m)) == sizeof (m)) {
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    91
				if (startFrame == 0) startFrame = lastFrame;
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    92
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    93
				jack_nframes_t eventFrame = std::max(0L, (m.time * jack_get_sample_rate(jackClient) / 1000 / 1000) - (lastFrame - startFrame));
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    94
				// 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.
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    95
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    96
				if (eventFrame < frames) {
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    97
					jack_midi_data_t* midiData = jack_midi_event_reserve(jackPortBuffer, eventFrame, m.size);
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    98
					if (midiData) {
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    99
						memcpy(midiData, m.buffer, m.size);
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   100
						jack_ringbuffer_read_advance(ringBuffer, sizeof (m));
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   101
					} // else = error: not enough space; will be kept in the ring buffer and sent in the next cycle
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   102
				} else {
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   103
					/**
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   104
					 * This message does not belong to this cycle.
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   105
					 * Its time will come later.
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   106
					 * For now, it stays in the ring buffer.
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   107
					 */
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   108
					break;
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   109
				}
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   110
			}
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   111
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   112
			if (pthread_mutex_trylock(&processingLock) == 0) {
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   113
				pthread_cond_signal(&processingDone);
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   114
				pthread_mutex_unlock(&processingLock);
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   115
			}
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   116
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   117
			return 0;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   118
		}
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   119
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   120
		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
   121
			return true;
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   122
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   123
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   124
		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
   125
			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
   126
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   127
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   128
		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
   129
			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
   130
		}
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   131
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   132
	} realTimeContext;
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   133
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   134
	/**
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   135
	 * 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
   136
	 */
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   137
	class RelationContext {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   138
	public:
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   139
		bool skip = false;
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   140
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   141
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   142
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   143
		bool hasAttribute(const relpipe::common::type::StringX& name) {
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   144
			for (auto a : attributes) if (a.getAttributeName() == name) return true;
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   145
			return false;
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   146
		}
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   147
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   148
		class RecordContext {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   149
		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
   150
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   151
			enum class Event {
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   152
				NOTE,
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   153
				CONTROL,
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   154
				SYSEX,
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   155
				CONNECT,
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   156
				DISCONNECT,
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   157
				UNKNOWN
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   158
			};
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   159
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   160
			static Event parseEventType(const relpipe::common::type::StringX& name) {
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   161
				if (name == L"note") return Event::NOTE;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   162
				else if (name == L"control") return Event::CONTROL;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   163
				else if (name == L"sysex") return Event::SYSEX;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   164
				else if (name == L"connect") return Event::CONNECT;
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   165
				else if (name == L"disconnect") return Event::DISCONNECT;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   166
				else return Event::UNKNOWN;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   167
			}
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   168
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   169
			Event event = Event::UNKNOWN;
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   170
			relpipe::common::type::Integer time;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   171
			relpipe::common::type::Integer channel;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   172
			relpipe::common::type::Boolean noteOn;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   173
			relpipe::common::type::Integer notePitch;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   174
			relpipe::common::type::Integer noteVelocity;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   175
			relpipe::common::type::Integer controllerId;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   176
			relpipe::common::type::Integer controllerValue;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   177
			relpipe::common::type::StringX raw;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   178
			relpipe::common::type::StringX connectionSourcePort;
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   179
			relpipe::common::type::StringX connectionDestinationPort;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   180
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   181
			size_t attributeIndex = 0;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   182
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   183
		} recordContext;
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   184
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   185
	} 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
   186
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   187
	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
   188
		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
   189
		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
   190
	}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   191
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   192
	void finalize() {
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   193
		// Close JACK connection:
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   194
		jack_deactivate(realTimeContext.jackClient);
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   195
		jack_client_close(realTimeContext.jackClient);
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   196
		jack_ringbuffer_free(realTimeContext.ringBuffer);
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   197
		pthread_mutex_unlock(&realTimeContext.processingLock);
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   198
	}
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   199
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   200
	void failInConstructor(const relpipe::common::type::StringX& errorMessage) {
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   201
		finalize();
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   202
		throw JackException(errorMessage);
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   203
	}
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   204
20
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   205
	/**
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   206
	 * Wait for the signal that is emitted at the end of the real-time processCallback() cycle.
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   207
	 */
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   208
	void waitForRTCycle() {
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   209
		pthread_cond_wait(&realTimeContext.processingDone, &realTimeContext.processingLock);
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   210
	}
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   211
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
public:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
	JackHandler(Configuration& configuration) : configuration(configuration) {
21
70b252d02a92 refactoring of includes, fix locking
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   215
		pthread_mutex_lock(&realTimeContext.processingLock);
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   216
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   217
		// Initialize JACK connection:
23
d2ad84dcf249 simplify CLI options: --client --connect-to --required-connections + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   218
		std::string clientName = convertor.to_bytes(configuration.client);
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   219
		realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   220
		if (realTimeContext.jackClient == nullptr) failInConstructor(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
   221
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   222
		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
   223
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   224
		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
   225
		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
   226
		// 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
   227
		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
   228
		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
   229
23
d2ad84dcf249 simplify CLI options: --client --connect-to --required-connections + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   230
		realTimeContext.jackPort = jack_port_register(realTimeContext.jackClient, convertor.to_bytes(configuration.port).c_str(), JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   231
		if (realTimeContext.jackPort == nullptr) failInConstructor(L"Could not register the JACK 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
   232
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   233
		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
   234
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   235
		int jackError = jack_activate(realTimeContext.jackClient);
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   236
		if (jackError) failInConstructor(L"Could not activate the JACK client.");
5
6be3464ccb2b wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   237
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   238
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   239
		// Connect to configured destination ports:
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   240
		const char* jackPortName = jack_port_name(realTimeContext.jackPort);
23
d2ad84dcf249 simplify CLI options: --client --connect-to --required-connections + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   241
		for (auto destinationPort : configuration.connectTo) {
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   242
			int error = jack_connect(realTimeContext.jackClient, jackPortName, convertor.to_bytes(destinationPort).c_str());
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   243
			if (error) failInConstructor(L"Connection to the JACK port failed: " + destinationPort);
15
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   244
		}
b3239e4ad328 connect to configured destination ports + syncCallback
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   245
5
6be3464ccb2b wait for a port connection
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   246
		// Wait for a port connection, because it does not make much sense to send MIDI events nowhere:
23
d2ad84dcf249 simplify CLI options: --client --connect-to --required-connections + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   247
		while (jack_port_connected(realTimeContext.jackPort) < configuration.requiredConnections) 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
   248
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   250
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   252
		relationContext = RelationContext();
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   253
		relationContext.attributes = attributes;
11
1c0b9981eb6d move real-time callbacks to a separate context
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   254
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   255
		// TODO: configurable relation name
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   256
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   257
		if (!relationContext.hasAttribute(L"event")) {
13
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   258
			relationContext.skip = true;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   259
			fwprintf(stderr, L"Relation „%ls“ will be ignored because mandatory attribute „event“ 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
   260
		}
a85c191709e6 skip relations with missing mandatory attributes
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   261
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   262
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   263
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   264
	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
   265
		if (relationContext.skip) return;
18
7a74e9f9e674 update TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   266
		// TODO: switch to RelationalReaderValueHandler
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
   267
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   268
		RelationContext& rel = relationContext;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   269
		RelationContext::RecordContext& rec = rel.recordContext;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   270
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   271
		const auto attributeName = rel.attributes[rec.attributeIndex].getAttributeName();
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   272
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   273
		if (attributeName == L"event") rec.event = rec.parseEventType(value);
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   274
		else if (attributeName == L"time") rec.time = std::stoll(value);
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   275
		else if (attributeName == L"channel") rec.channel = std::stoi(value);
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   276
		else if (attributeName == L"controller_id") rec.controllerId = std::stoi(value);
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   277
		else if (attributeName == L"controller_value") rec.controllerValue = std::stoi(value);
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   278
		else if (attributeName == L"note_on") rec.noteOn = value == L"true";
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   279
		else if (attributeName == L"note_pitch") rec.notePitch = std::stoi(value);
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   280
		else if (attributeName == L"note_velocity") rec.noteVelocity = std::stoi(value);
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   281
		else if (attributeName == L"raw") rec.raw = value;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   282
		else if (attributeName == L"source_port") rec.connectionSourcePort = value;
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   283
		else if (attributeName == L"destination_port") rec.connectionDestinationPort = 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
   284
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   285
		rec.attributeIndex++;
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   286
16
d17d70731446 simplify attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   287
		if (rec.attributeIndex == rel.attributes.size()) {
12
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   288
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   289
			while (continueProcessing && jack_ringbuffer_write_space(realTimeContext.ringBuffer) < sizeof (MidiMessage)) waitForRTCycle(); // wait, if we are faster than the real-time thread
20
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   290
			if (!continueProcessing) return;
12
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   291
f56620b95180 wait until there is enough space in the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   292
			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
   293
27
45a1742b9854 timing (first version)
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   294
			m.time = rec.time;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   295
			m.size = 0;
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   296
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   297
			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
   298
				m.size = 3;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   299
				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
   300
				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
   301
				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
   302
			} 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
   303
				m.size = 3;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   304
				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
   305
				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
   306
				m.buffer[2] = rec.controllerValue;
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   307
			} else if (rec.event == RelationContext::RecordContext::Event::CONNECT) {
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   308
				int result = jack_connect(realTimeContext.jackClient, convertor.to_bytes(rec.connectionSourcePort).c_str(), convertor.to_bytes(rec.connectionDestinationPort).c_str());
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   309
				if (result != 0 && result != EEXIST) std::wcerr << L"Unable to connect: „" << rec.connectionSourcePort << L"“ to: „" << rec.connectionDestinationPort << L"“." << std::endl;
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   310
			} else if (rec.event == RelationContext::RecordContext::Event::DISCONNECT) {
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   311
				int result = jack_disconnect(realTimeContext.jackClient, convertor.to_bytes(rec.connectionSourcePort).c_str(), convertor.to_bytes(rec.connectionDestinationPort).c_str());
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   312
				if (result != 0) std::wcerr << L"Unable to disconnect: „" << rec.connectionSourcePort << L"“ from: „" << rec.connectionDestinationPort << L"“." << std::endl;
14
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   313
			} 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
   314
				size_t nibble = 0;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   315
				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
   316
					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
   317
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   318
					if (ch == L' ') continue;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   319
					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
   320
					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
   321
					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
   322
					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
   323
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   324
					nibble++;
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   325
					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
   326
				}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   327
			}
9ad606c80d3b encode MIDI messages + process JACK error output
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   328
24
427b8d799a4a add „connect“ and „disconnect“ events + „source_port“ and „destination_port“ attributes to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   329
			if (m.size > 0) 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
   330
10
6aa881c10efd attribute mapping
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   331
			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
   332
		}
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
   333
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   334
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   335
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   336
	void endOfPipe() {
8
27579c5cdc51 send correct number of messages to the ring buffer
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   337
		// TODO: optionally mute all; probably enabled by default
19
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   338
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   339
		// Wait until the ring buffer is empty (messages dequeued from the buffer) and real-time cycle was finished (messages passed to JACK)
dcd98589f4b8 use signal instead of sleep while waiting at the end
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   340
		while (continueProcessing && jack_ringbuffer_read_space(realTimeContext.ringBuffer)) pthread_cond_wait(&realTimeContext.processingDone, &realTimeContext.processingLock);
20
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   341
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   342
		// There might be a (rare) race condition.
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   343
		// 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()
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   344
		// and will sleep until the next cycle. In endOfPipe() it is not a big problem.
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   345
		// But missing the cycle in attribute() might be worse.
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   346
		// However it should not happen due to the buffer size
ccc4e509c1f0 use signal instead of sleep while waiting in attribute()
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   347
		// 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).
3
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   348
	}
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   349
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   350
	void finish(int sig) {
baa8055c5b10 ring buffer, JACK callback skeleton, signal handler
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   351
		continueProcessing = false;
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   352
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   354
	virtual ~JackHandler() {
17
0fcd9944016a finalize on exceptions in constructor
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   355
		finalize();
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   356
	}
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   357
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   358
};
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   359
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   360
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   361
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   362
}