link to the JACK library, connect to the JACK daemon v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 29 Sep 2020 20:41:10 +0200
branchv_0
changeset 1 7f3ab657dc50
parent 0 3bc3b33ccb02
child 2 01553c2ffa1d
link to the JACK library, connect to the JACK daemon
src/CMakeLists.txt
src/JackHandler.h
--- a/src/CMakeLists.txt	Tue Sep 29 20:10:29 2020 +0200
+++ b/src/CMakeLists.txt	Tue Sep 29 20:41:10 2020 +0200
@@ -17,7 +17,7 @@
 
 # Relpipe libraries:
 INCLUDE(FindPkgConfig)
-pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-cli.cpp)
+pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-cli.cpp jack)
 include_directories(${RELPIPE_LIBS_INCLUDE_DIRS})
 link_directories(${RELPIPE_LIBS_LIBRARY_DIRS})
 
--- a/src/JackHandler.h	Tue Sep 29 20:10:29 2020 +0200
+++ b/src/JackHandler.h	Tue Sep 29 20:41:10 2020 +0200
@@ -23,6 +23,11 @@
 #include <sstream>
 #include <locale>
 #include <codecvt>
+#include <sys/mman.h>
+
+#include <jack/jack.h>
+#include <jack/midiport.h>
+#include <jack/ringbuffer.h>
 
 #include <relpipe/common/type/typedefs.h>
 #include <relpipe/reader/TypeId.h>
@@ -40,9 +45,30 @@
 private:
 	Configuration& configuration;
 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding
+
+	jack_client_t* jackClient = nullptr;
+	jack_port_t* jackPort = nullptr;
+	//jack_ringbuffer_t* ringBuffer = nullptr;
 public:
 
 	JackHandler(Configuration& configuration) : configuration(configuration) {
+		// Initialize JACK connection:
+		std::string clientName = "relpipe-out-jack";
+		jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
+		if (jackClient == nullptr) throw JackException(L"Could not create JACK client.");
+
+		//ringBuffer = jack_ringbuffer_create(RING_BUFFER_SIZE * sizeof (MidiMessage));
+
+		//jack_set_process_callback(jackClient, relpipe::in::jack::enqueueMessage, this);
+		// TODO: report also other events (connections etc.)
+
+		jackPort = jack_port_register(jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
+		if (jackPort == nullptr) throw JackException(L"Could not register port.");
+
+		if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");
+
+		int jackError = jack_activate(jackClient);
+		if (jackError) throw JackException(L"Could not activate client.");
 	}
 
 	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
@@ -57,6 +83,13 @@
 		
 	}
 
+	virtual ~JackHandler() {
+		// Close JACK connection:
+		jack_deactivate(jackClient);
+		jack_client_close(jackClient);
+		//jack_ringbuffer_free(ringBuffer);
+	}
+
 };
 
 }