Repository navigation
Expand file tree
/
Copy pathmio2jack.c
More file actions
119 lines (95 loc) · 2.79 KB
/
Copy pathmio2jack.c
File metadata and controls
119 lines (95 loc) · 2.79 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
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sndio.h>
#include <jack/jack.h>
#include <jack/midiport.h>
char* mio_port = MIO_PORTANY;
struct mio_hdl* mio_hdl;
char* jack_client_name = "mio2jack";
jack_client_t* jack_client;
jack_port_t* jack_out;
#define MIO_READ_SZ 256
unsigned char* event_buf;
static void quit(int code)
{
if (jack_client) jack_client_close(jack_client);
if (mio_hdl) mio_close(mio_hdl);
if (event_buf) free(event_buf);
exit(code);
}
static void signal_handler(int signal)
{
quit(EXIT_SUCCESS);
}
static void usage(void)
{
extern char* __progname;
fprintf(stderr,
"usage: %s [-h] [-p mio_port] [-n jack_client_name]\n",
__progname);
quit(EXIT_FAILURE);
}
static int jack_process(jack_nframes_t nframes, void* arg)
{
void* jack_out_buf = jack_port_get_buffer(jack_out, nframes);
jack_midi_clear_buffer(jack_out_buf);
/* Read events from sndio and write them to jack. */
size_t read;
while (read = mio_read(mio_hdl, event_buf, MIO_READ_SZ)) {
/* Note that we are in many cases writing multiple -- possibly
* incomplete -- MIDI events to jack with each call to
* jack_midi_event_write. Ideally we'd buffer and split so that one call
* meant one event, but this seems to work for now. */
jack_midi_event_write(jack_out_buf, 0, event_buf, read);
}
return 0;
}
int main(int argc, char* argv[])
{
int ch;
while ((ch = getopt(argc, argv, "p:n:h")) != -1) {
switch (ch) {
case 'p':
mio_port = optarg;
break;
case 'n':
jack_client_name = optarg;
break;
case 'h': /* fallthrough */
default:
usage();
}
}
event_buf = malloc(MIO_READ_SZ);
if (!event_buf) {
perror("malloc");
quit(EXIT_FAILURE);
}
mio_hdl = mio_open(mio_port, MIO_IN, 1);
if (!mio_hdl) {
fprintf(stderr, "couldn't open sndio port: %s\n", mio_port);
quit(EXIT_FAILURE);
}
jack_client = jack_client_open(jack_client_name, JackNullOption, 0);
if (!jack_client) {
fprintf(stderr, "couldn't open jack client\n");
quit(EXIT_FAILURE);
}
jack_set_process_callback(jack_client, jack_process, NULL);
jack_out = jack_port_register(jack_client, "midi_out",
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (!jack_out) {
fprintf(stderr, "couldn't register jack output port\n");
quit(EXIT_FAILURE);
}
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
if (jack_activate(jack_client)) {
fprintf(stderr, "couldn't activate jack client\n");
quit(EXIT_FAILURE);
}
while (1) sleep(1);
/* unreachable */
quit(EXIT_SUCCESS);
}