src/JackCommand.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 14 Oct 2020 17:45:48 +0200
branchv_0
changeset 16 79520ded76f0
parent 15 463ce61415f1
child 17 4e6b33312c5a
permissions -rw-r--r--
fix bash-completion (uuid) + connect/disconnect error message (to/from)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
c8c8ec34120f first dirty version, writes MIDI events from JACK
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
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    17
#pragma once
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    19
#include <memory>
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    20
#include <atomic>
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <cstdlib>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <cstring>
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    23
#include <sstream>
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    24
#include <sys/mman.h>
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <unistd.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <pthread.h>
2
e5f0d3f92eb4 pass RelationalWriter instead of std::ostream to the JackCommand
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    27
#include <functional>
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
#include <iomanip>
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    29
#include <regex>
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <jack/jack.h>
15
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    32
#include <jack/metadata.h>
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    33
#include <jack/uuid.h>
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <jack/midiport.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <jack/ringbuffer.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    37
#include <relpipe/common/type/typedefs.h>
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include <relpipe/writer/RelationalWriter.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
#include <relpipe/writer/RelpipeWriterException.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
#include <relpipe/writer/AttributeMetadata.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
#include <relpipe/writer/Factory.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
#include <relpipe/writer/TypeId.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
#include <relpipe/cli/CLI.h>
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    45
#include "Configuration.h"
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
#include "JackException.h"
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
namespace relpipe {
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
namespace in {
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
namespace jack {
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
int enqueueMessage(jack_nframes_t frames, void* arg);
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
class JackCommand {
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
private:
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    56
	Configuration& configuration;
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    57
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    58
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
	std::atomic<bool> continueProcessing{true};
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    61
	int maxJackPortConnections = 0;
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    63
	/**
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    64
	 * Is passed through the ring buffer
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    65
	 * from the the jack-writing thread (callback) to the relpipe-writing thread.
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    66
	 */
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
	struct MidiMessage {
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    68
		uint8_t buffer[4096] = {0};
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
		uint32_t size;
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		uint32_t time;
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
	};
3
0222c20f590f report also unknown messages
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    72
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    73
	/**
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    74
	 * JACK callbacks (called from the real-time thread)
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    75
	 */
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    76
	class RealTimeContext {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    77
	public:
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    78
		jack_client_t* jackClient = nullptr;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    79
		jack_port_t* jackPort = nullptr;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    80
		jack_ringbuffer_t* ringBuffer = nullptr;
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    82
		pthread_mutex_t processingLock = PTHREAD_MUTEX_INITIALIZER;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    83
		pthread_cond_t processingDone = PTHREAD_COND_INITIALIZER;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    84
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    85
		const int RING_BUFFER_SIZE = 100;
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    87
		int processCallback(jack_nframes_t frames) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    88
			void* buffer = jack_port_get_buffer(jackPort, frames);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    89
			if (buffer == nullptr) throw JackException(L"Unable to get port buffer."); // TODO: exception in RT callback?
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    90
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    91
			for (jack_nframes_t i = 0, eventCount = jack_midi_get_event_count(buffer); i < eventCount; i++) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    92
				jack_midi_event_t event;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    93
				int noData = jack_midi_event_get(&event, buffer, i);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    94
				if (noData) continue;
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    96
				if (event.size > sizeof (MidiMessage::buffer)) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    97
					// TODO: should not printf in RT callback:
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    98
					fwprintf(stderr, L"Error: MIDI message was too large → skipping event. Maximum allowed size: %lu bytes.\n", sizeof (MidiMessage::buffer));
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    99
				} else if (jack_ringbuffer_write_space(ringBuffer) >= sizeof (MidiMessage)) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   100
					MidiMessage m;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   101
					m.time = event.time;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   102
					m.size = event.size;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   103
					memcpy(m.buffer, event.buffer, event.size);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   104
					jack_ringbuffer_write(ringBuffer, (const char *) &m, sizeof (MidiMessage));
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   105
				} else {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   106
					// TODO: should not printf in RT callback:
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   107
					fwprintf(stderr, L"Error: ring buffer is full → skipping event.\n");
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   108
				}
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
			}
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   110
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   111
			// TODO: just count skipped events and bytes and report them in next successful message instead of printing to STDERR
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   112
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   113
			if (pthread_mutex_trylock(&processingLock) == 0) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   114
				pthread_cond_signal(&processingDone);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   115
				pthread_mutex_unlock(&processingLock);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   116
			}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   117
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   118
			return 0;
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   119
		}
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   121
		static int processCallback(jack_nframes_t frames, void* instance) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   122
			return static_cast<RealTimeContext*> (instance)->processCallback(frames);
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   123
		}
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   124
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   125
	} realTimeContext;
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   127
	static void writeRecord(std::shared_ptr<relpipe::writer::RelationalWriter> writer,
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   128
			relpipe::common::type::StringX eventType, relpipe::common::type::Integer channel,
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   129
			relpipe::common::type::Boolean noteOn, relpipe::common::type::Integer pitch, relpipe::common::type::Integer velocity,
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   130
			relpipe::common::type::Integer controllerId, relpipe::common::type::Integer value,
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   131
			relpipe::common::type::StringX raw) {
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   132
		writer->writeAttribute(eventType);
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   133
		writer->writeAttribute(&channel, typeid (channel));
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   134
		writer->writeAttribute(&noteOn, typeid (noteOn));
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   135
		writer->writeAttribute(&pitch, typeid (pitch));
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   136
		writer->writeAttribute(&velocity, typeid (velocity));
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   137
		writer->writeAttribute(&controllerId, typeid (controllerId));
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   138
		writer->writeAttribute(&value, typeid (value));
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   139
		writer->writeAttribute(&raw, typeid (raw));
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   140
	}
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   141
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   142
	void processMessage(std::shared_ptr<relpipe::writer::RelationalWriter> writer, MidiMessage* event) {
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   143
		if (event->size == 0) {
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   144
			return;
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
		} else {
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   146
			uint8_t type = event->buffer[0] & 0xF0;
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   147
			uint8_t channel = event->buffer[0] & 0x0F;
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   148
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   149
			// TODO: write timestamp, message number
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   150
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   151
			if ((type == 0x90 || type == 0x80) && event->size == 3) {
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   152
				writeRecord(writer, L"note", channel, type == 0x90, event->buffer[1], event->buffer[2], 0, 0, toHex(event));
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   153
			} else if (type == 0xB0 && event->size == 3) {
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   154
				writeRecord(writer, L"control", channel, false, 0, 0, event->buffer[1], event->buffer[2], toHex(event));
5
40dd6deafaca recognize SysEx (System Exclusive) messages
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   155
			} else if (event->buffer[0] == 0xF0) {
40dd6deafaca recognize SysEx (System Exclusive) messages
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   156
				writeRecord(writer, L"sysex", channel, false, 0, 0, 0, 0, toHex(event));
3
0222c20f590f report also unknown messages
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   157
			} else {
6
b81bff3ebc4c fix unknown message output
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   158
				writeRecord(writer, L"unknown", channel, false, 0, 0, 0, 0, toHex(event));
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   159
			}
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
		}
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
	}
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   163
	relpipe::common::type::StringX toHex(MidiMessage* event) {
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   164
		std::wstringstream result;
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   165
		result << std::hex << std::setfill(L'0');
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   166
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   167
		for (size_t i = 0; i < event->size && i < sizeof (event->buffer); i++) {
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   168
			if (i > 0) result << L' ';
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   169
			result << std::setw(2) << event->buffer[i];
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   170
			// result << ("0123456789abcdef"[event->buffer[i] >> 4]);
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   171
			// result << ("0123456789abcdef"[event->buffer[i] & 0xf]);
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   172
		}
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   173
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   174
		return result.str();
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   175
	}
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   176
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   177
	void listPorts(std::shared_ptr<relpipe::writer::RelationalWriter> writer) {
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   178
		using namespace relpipe::writer;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   179
		vector<AttributeMetadata> metadata;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   180
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   181
		metadata.push_back({L"name", TypeId::STRING});
15
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   182
		metadata.push_back({L"uuid", TypeId::STRING});
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   183
		metadata.push_back({L"input", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   184
		metadata.push_back({L"output", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   185
		metadata.push_back({L"physical", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   186
		metadata.push_back({L"terminal", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   187
		metadata.push_back({L"mine", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   188
		metadata.push_back({L"midi", TypeId::BOOLEAN});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   189
		metadata.push_back({L"type", TypeId::STRING});
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   190
		writer->startRelation(L"port", metadata, true);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   191
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   192
		const char** portNames = jack_get_ports(realTimeContext.jackClient, nullptr, nullptr, 0);
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   193
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   194
		std::regex midiTypePattern(".*midi$");
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   195
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   196
		for (const char** portName = portNames; *portName; portName++) {
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   197
			jack_port_t* port = jack_port_by_name(realTimeContext.jackClient, *portName);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   198
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   199
			const char* portType = jack_port_type(port);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   200
			int portFlags = jack_port_flags(port);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   201
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   202
			bool isInput = portFlags & JackPortFlags::JackPortIsInput;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   203
			bool isOuputput = portFlags & JackPortFlags::JackPortIsOutput;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   204
			bool isPhysical = portFlags & JackPortFlags::JackPortIsPhysical;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   205
			bool isTerminal = portFlags & JackPortFlags::JackPortIsTerminal;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   206
			bool isMine = jack_port_is_mine(realTimeContext.jackClient, port);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   207
			bool isMidi = std::regex_search(portType, midiTypePattern);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   208
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   209
			writer->writeAttribute(convertor.from_bytes(*portName));
15
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   210
			writer->writeAttribute(convertor.from_bytes(uuidToString(jack_port_uuid(port))));
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   211
			writer->writeAttribute(&isInput, typeid (isInput));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   212
			writer->writeAttribute(&isOuputput, typeid (isOuputput));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   213
			writer->writeAttribute(&isPhysical, typeid (isPhysical));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   214
			writer->writeAttribute(&isTerminal, typeid (isTerminal));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   215
			writer->writeAttribute(&isMine, typeid (isMine));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   216
			writer->writeAttribute(&isMidi, typeid (isMidi));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   217
			writer->writeAttribute(convertor.from_bytes(portType));
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   218
		}
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   219
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   220
		jack_free(portNames);
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   221
	}
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   222
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   223
	void listConnections(std::shared_ptr<relpipe::writer::RelationalWriter> writer) {
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   224
		using namespace relpipe::writer;
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   225
		vector<AttributeMetadata> metadata;
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   226
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   227
		metadata.push_back({L"event", TypeId::STRING});
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   228
		metadata.push_back({L"source_port", TypeId::STRING});
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   229
		metadata.push_back({L"destination_port", TypeId::STRING});
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   230
		writer->startRelation(L"connection", metadata, true);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   231
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   232
		const relpipe::common::type::StringX event = L"connect";
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   233
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   234
		const char** sourcePortNames = jack_get_ports(realTimeContext.jackClient, nullptr, nullptr, JackPortFlags::JackPortIsOutput);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   235
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   236
		for (const char** sourcePortName = sourcePortNames; *sourcePortName; sourcePortName++) {
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   237
			jack_port_t* sourcePort = jack_port_by_name(realTimeContext.jackClient, *sourcePortName);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   238
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   239
			const char** destinationPortNames = jack_port_get_all_connections(realTimeContext.jackClient, sourcePort);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   240
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   241
			for (const char** destinationPortName = destinationPortNames; destinationPortNames && *destinationPortName; destinationPortName++) {
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   242
				writer->writeAttribute(event);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   243
				writer->writeAttribute(convertor.from_bytes(*sourcePortName));
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   244
				writer->writeAttribute(convertor.from_bytes(*destinationPortName));
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   245
			}
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   246
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   247
			jack_free(destinationPortNames);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   248
		}
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   249
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   250
		jack_free(sourcePortNames);
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   251
	}
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   252
15
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   253
	std::string uuidToString(jack_uuid_t uuid) {
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   254
		char buffer[JACK_UUID_STRING_SIZE] = {0};
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   255
		jack_uuid_unparse(uuid, buffer);
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   256
		return buffer;
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   257
	}
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   258
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   259
	void listProperties(std::shared_ptr<relpipe::writer::RelationalWriter> writer) {
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   260
		using namespace relpipe::writer;
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   261
		vector<AttributeMetadata> metadata;
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   262
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   263
		metadata.push_back({L"uuid", TypeId::STRING});
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   264
		//metadata.push_back({L"port", TypeId::STRING}); // TODO: port name
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   265
		metadata.push_back({L"name", TypeId::STRING});
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   266
		metadata.push_back({L"type", TypeId::STRING});
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   267
		metadata.push_back({L"value", TypeId::STRING});
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   268
		writer->startRelation(L"property", metadata, true);
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   269
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   270
		jack_description_t* descriptions = nullptr;
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   271
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   272
		int descriptionsCount = jack_get_all_properties(&descriptions);
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   273
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   274
		for (int i = 0; i < descriptionsCount; i++) {
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   275
			jack_description_t* description = &descriptions[i];
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   276
			for (uint32_t j = 0; j < description->property_cnt; j++) {
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   277
				jack_property_t* property = &description->properties[j];
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   278
				writer->writeAttribute(convertor.from_bytes(uuidToString(description->subject)));
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   279
				//writer->writeAttribute(L"TODO: port name"); // TODO: port name
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   280
				writer->writeAttribute(convertor.from_bytes(property->key));
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   281
				writer->writeAttribute(convertor.from_bytes(property->type));
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   282
				writer->writeAttribute(convertor.from_bytes(property->data)); // TODO: format value (some in HEX), according to .type
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   283
			}
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   284
			jack_free_description(description, false);
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   285
		}
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   286
		jack_free(descriptions);
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   287
	}
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   288
14
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   289
	void applyConnectionRecipes() {
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   290
		for (Configuration::ConnectionRecipe recipe : configuration.connectionRecipes) {
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   291
			auto operation = recipe.connected ? jack_connect : jack_disconnect;
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   292
			int result = operation(realTimeContext.jackClient, convertor.to_bytes(recipe.sourcePort).c_str(), convertor.to_bytes(recipe.destinationPort).c_str());
16
79520ded76f0 fix bash-completion (uuid) + connect/disconnect error message (to/from)
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   293
			if (result != 0 && result != EEXIST) std::wcerr << L"Unable to " << (recipe.connected ? L"connect" : L"disconnect") << L": „" << recipe.sourcePort << (recipe.connected ? L"“ to: „" : L"“ from: „") << recipe.destinationPort << L"“." << std::endl;
14
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   294
		}
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   295
	}
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   296
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   297
	static void jackErrorCallback(const char * message) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   298
		std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   299
		std::wcerr << L"JACK: " << convertor.from_bytes(message) << std::endl;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   300
	}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   301
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   302
	void finalize() {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   303
		// Close JACK connection:
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   304
		jack_deactivate(realTimeContext.jackClient);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   305
		jack_client_close(realTimeContext.jackClient);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   306
		jack_ringbuffer_free(realTimeContext.ringBuffer);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   307
		pthread_mutex_unlock(&realTimeContext.processingLock);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   308
	}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   309
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   310
	void failInConstructor(const relpipe::common::type::StringX& errorMessage) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   311
		finalize();
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   312
		throw JackException(errorMessage);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   313
	}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   314
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   315
	/**
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   316
	 * Wait for the signal that is emitted at the end of the real-time processCallback() cycle.
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   317
	 */
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   318
	void waitForRTCycle() {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   319
		pthread_cond_wait(&realTimeContext.processingDone, &realTimeContext.processingLock);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   320
	}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   321
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   322
public:
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   323
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   324
	JackCommand(Configuration& configuration) : configuration(configuration) {
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   325
		pthread_mutex_lock(&realTimeContext.processingLock);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   326
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   327
		// Initialize JACK connection:
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   328
		std::string clientName = convertor.to_bytes(configuration.client);
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   329
		realTimeContext.jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   330
		if (realTimeContext.jackClient == nullptr) failInConstructor(L"Could not create JACK client.");
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   331
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   332
		realTimeContext.ringBuffer = jack_ringbuffer_create(realTimeContext.RING_BUFFER_SIZE * sizeof (MidiMessage));
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   333
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   334
		jack_set_process_callback(realTimeContext.jackClient, RealTimeContext::processCallback, &realTimeContext);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   335
		// TODO: report also other events (connections etc.)
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   336
		jack_set_error_function(jackErrorCallback);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   337
		jack_set_info_function(jackErrorCallback);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   338
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   339
		realTimeContext.jackPort = jack_port_register(realTimeContext.jackClient, convertor.to_bytes(configuration.port).c_str(), JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   340
		if (realTimeContext.jackPort == nullptr) failInConstructor(L"Could not register the JACK port.");
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   341
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   342
		if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   343
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   344
		int jackError = jack_activate(realTimeContext.jackClient);
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   345
		if (jackError) failInConstructor(L"Could not activate the JACK client.");
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   346
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   347
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   348
		// Connect to configured destination ports:
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   349
		const char* jackPortName = jack_port_name(realTimeContext.jackPort);
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   350
		for (auto sourcePort : configuration.connectTo) {
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   351
			int error = jack_connect(realTimeContext.jackClient, convertor.to_bytes(sourcePort).c_str(), jackPortName);
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   352
			if (error) failInConstructor(L"Connection to the JACK port failed: " + sourcePort);
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   353
		}
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   354
8
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   355
	}
8ef1980db907 configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   356
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   357
	void processJackStream(std::shared_ptr<relpipe::writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   358
		// Relation headers:
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   359
		using namespace relpipe::writer;
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   360
		vector<AttributeMetadata> metadata;
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   361
14
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   362
		applyConnectionRecipes();
cde9bb07ea0a add options --connect-ports and --disconnect-ports to allow connecting and disconnecting arbitrary JACK ports
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   363
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   364
		if (configuration.listPorts) listPorts(writer);
13
326935d1bfab add option --list-connections for listing JACK connections
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   365
		if (configuration.listConnections) listConnections(writer);
15
463ce61415f1 partial support for metadata: --list-properties
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   366
		if (configuration.listProperties) listProperties(writer);
11
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   367
		if (!configuration.listMidiMessages) return;
07247893054e add options --list-jack-ports and --list-midi-messages useful for bash-completion
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   368
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   369
		metadata.push_back({L"event", TypeId::STRING});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   370
		metadata.push_back({L"channel", TypeId::INTEGER});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   371
		metadata.push_back({L"note_on", TypeId::BOOLEAN});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   372
		metadata.push_back({L"note_pitch", TypeId::INTEGER});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   373
		metadata.push_back({L"note_velocity", TypeId::INTEGER});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   374
		metadata.push_back({L"controller_id", TypeId::INTEGER});
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   375
		metadata.push_back({L"controller_value", TypeId::INTEGER});
4
30da4232cdbc add raw MIDI data in HEX
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   376
		metadata.push_back({L"raw", TypeId::STRING});
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   377
		writer->startRelation(L"midi", metadata, true);
2
e5f0d3f92eb4 pass RelationalWriter instead of std::ostream to the JackCommand
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   378
		relationalWriterFlush();
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   379
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   380
		// Process messages from the ring buffer queue:
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   381
		while (continueProcessing) {
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   382
			while (jack_ringbuffer_read_space(realTimeContext.ringBuffer) >= sizeof (MidiMessage)) {
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   383
				MidiMessage m;
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   384
				jack_ringbuffer_read(realTimeContext.ringBuffer, (char*) &m, sizeof (MidiMessage));
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   385
				processMessage(writer, &m);
2
e5f0d3f92eb4 pass RelationalWriter instead of std::ostream to the JackCommand
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   386
				relationalWriterFlush();
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   387
			}
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   388
			waitForRTCycle();
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   389
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   390
			// Once the Configuration::requiredJackConnections count was reached, we will disconnect if the count drops under this level.
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   391
			if (configuration.requiredConnections) {
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   392
				int currentConnectionCount = jack_port_connected(realTimeContext.jackPort);
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   393
				if (currentConnectionCount > maxJackPortConnections) maxJackPortConnections = currentConnectionCount;
12
e8aae4d42c01 simplify CLI options: --client --connect-to --required-connections --list-ports + add --port
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   394
				else if (maxJackPortConnections >= configuration.requiredConnections && currentConnectionCount < configuration.requiredConnections) break;
10
ded44e94147c add --jack-connect-to-port and --required-jack-connections options
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   395
			}
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   396
		}
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   397
	}
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   398
9
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   399
	void finish(int sig) {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   400
		continueProcessing = false;
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   401
	}
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   402
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   403
	virtual ~JackCommand() {
0d362165241e refactoring, syncrhonize code structure with JackHandler.cpp (relpipe-out-jack)
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   404
		finalize();
1
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   405
	}
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   406
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   407
};
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   408
001b956610ca separate JackCommand class
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   409
}
0
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   410
}
c8c8ec34120f first dirty version, writes MIDI events from JACK
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   411
}