-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJackEngine.cpp
More file actions
135 lines (105 loc) · 4.05 KB
/
JackEngine.cpp
File metadata and controls
135 lines (105 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright (C) 2016 Mikko Tamminen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Note.h"
#include "JackEngine.h"
#include <stdio.h>
#include <iostream>
#include <jack/jack.h>
#include <jack/midiport.h>
#include "IJackEngineCallbackProvider.h"
void* outbuf;
void* inbuf;
void* trigbuf;
jack_client_t *client;
jack_port_t *input_port;
jack_port_t *output_port;
jack_port_t *trigger_port;
const char *client_name;
const char *server_name;
int process(jack_nframes_t nframes, void* arg);
const char* JackEngine::getName() {return client_name;}
void play_fn(Note* note, int offset) {
jack_midi_data_t data[] = {(unsigned char)(note->channel | note->type),(unsigned char)note->note, (unsigned char)note->velocity};
jack_midi_event_write(outbuf, offset, data, 3);
}
JackEngine::JackEngine(IJackEngineCallbackProvider *callbackProvider) {
this->callbackProvider = callbackProvider;
}
int Process_Callback(jack_nframes_t x, void* p)
{
return static_cast<JackEngine*>(p)->process(x);
}
void JackEngine::init() {
std::cout << "Connecting to Jack..." << std::endl;
client_name = "JackSeq";
server_name = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
/* open a client connection to the JACK server */
client = jack_client_open(client_name, options, &status, server_name);
if (client == NULL) {
std::cout << "Jack connection failed :(" << std::endl;
exit(1);
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name(client);
}
std::cout << "Client name: " << client_name << std::endl;
jack_set_process_callback(client, Process_Callback, this);
/* create the ports */
input_port = jack_port_register(client, "MIDI IN",
JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput, 0);
output_port = jack_port_register(client, "MIDI OUT",
JACK_DEFAULT_MIDI_TYPE,
JackPortIsOutput, 0);
trigger_port = jack_port_register(client, "TRIGGER",
JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput, 0);
if ((input_port == NULL) || (output_port == NULL) || (trigger_port == NULL)) {
std::cout << "No Jack Ports available" << std::endl;
exit(1);
}
if (jack_activate(client)) {
std::cout << "Cannot activate client" << std::endl;
exit(1);
}
std::cout << "Connected!" << client_name << std::endl;
}
void JackEngine::shutdown() {
jack_client_close(client);
}
int JackEngine::process(jack_nframes_t nframes) {
inbuf = jack_port_get_buffer(input_port, nframes);
for (uint32_t i = 0; i < jack_midi_get_event_count(inbuf); i++) {
jack_midi_event_t event;
jack_midi_event_get(&event, inbuf, i);
Note *n = new Note(event.buffer);
this->callbackProvider->JackEngineNoteHandler(n, event.time);
}
trigbuf = jack_port_get_buffer(trigger_port, nframes);
for (uint32_t i = 0; i < jack_midi_get_event_count(trigbuf); i++) {
jack_midi_event_t event;
jack_midi_event_get(&event, trigbuf, i);
Note *n = new Note(event.buffer);
this->callbackProvider->JackEngineTriggerHandler(n, event.time);
}
this->callbackProvider->JackEngineTickHandler(nframes);
outbuf = jack_port_get_buffer(output_port, nframes);
jack_midi_clear_buffer(outbuf);
this->callbackProvider->JackEnginePlayFunctionHandler(play_fn);
this->callbackProvider->JackEnginePostTickHandler();
return 0;
}