src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 29 Sep 2020 20:49:04 +0200
branchv_0
changeset 2 01553c2ffa1d
parent 1 7f3ab657dc50
child 3 baa8055c5b10
permissions -rw-r--r--
configurable JACK client name
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
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
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>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#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
    26
#include <sys/mman.h>
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    28
#include <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
    29
#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
    30
#include <jack/ringbuffer.h>
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/common/type/typedefs.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/TypeId.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/reader/handlers/AttributeMetadata.h>
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "Configuration.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
#include "JackException.h"
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace relpipe {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace out {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace jack {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
private:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	Configuration& configuration;
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	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
    48
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	jack_client_t* jackClient = nullptr;
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	jack_port_t* jackPort = nullptr;
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	//jack_ringbuffer_t* ringBuffer = nullptr;
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	JackHandler(Configuration& configuration) : configuration(configuration) {
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		// Initialize JACK connection:
2
01553c2ffa1d configurable JACK client name
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		std::string clientName = convertor.to_bytes(configuration.jackClientName);
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		if (jackClient == nullptr) throw JackException(L"Could not create JACK client.");
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
		//ringBuffer = jack_ringbuffer_create(RING_BUFFER_SIZE * sizeof (MidiMessage));
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
		//jack_set_process_callback(jackClient, relpipe::in::jack::enqueueMessage, this);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
		// TODO: report also other events (connections etc.)
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
		jackPort = jack_port_register(jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
		if (jackPort == nullptr) throw JackException(L"Could not register port.");
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		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
    69
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		int jackError = jack_activate(jackClient);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		if (jackError) throw JackException(L"Could not activate client.");
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	void attribute(const relpipe::common::type::StringX& value) override {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	void endOfPipe() {
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
1
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
	virtual ~JackHandler() {
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
		// Close JACK connection:
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
		jack_deactivate(jackClient);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
		jack_client_close(jackClient);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
		//jack_ringbuffer_free(ringBuffer);
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
	}
7f3ab657dc50 link to the JACK library, connect to the JACK daemon
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
0
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
};
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
}
3bc3b33ccb02 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}