/
midi.c
198 lines (167 loc) · 5.85 KB
/
midi.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************
*
* midi.c - MIDI subsystem
*
* Copyright (C) 2014 Ricard Wanderlof <ricard2013@butoba.net>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************/
#include <asoundlib.h>
#include "midi.h"
#include "debug.h"
#include <alloca.h>
/* ALSA related stuff */
int seq_port;
snd_seq_t *seq;
/* Receiver(s) */
static midi_cc_receiver cc_receiver;
/* Initialize ALSA sequencer interface, and create MIDI port */
/* Return list of fds that main loop needs to poll() in order to detect
* activity. */
/* Returned structure pointer is allocated using malloc. */
struct polls *
midi_init_alsa(void)
{
struct polls *polls;
int npfd;
int i;
if (snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
dbgprintf("Couldn't open ALSA sequencer: %s\n", snd_strerror(errno));
return NULL;
}
snd_seq_set_client_name(seq, "Nocturn");
seq_port = snd_seq_create_simple_port(seq, "Nocturn port 1",
SND_SEQ_PORT_CAP_READ |
SND_SEQ_PORT_CAP_WRITE |
SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_APPLICATION);
if (seq_port < 0) {
dbgprintf("Couldn't create sequencer port: %s\n", snd_strerror(errno));
return NULL;
}
/* Fetch poll descriptor(s) for MIDI input (normally only one) */
npfd = snd_seq_poll_descriptors_count(seq, POLLIN);
polls = (struct polls *) malloc(sizeof(struct polls) +
npfd * sizeof(struct pollfd));
polls->npfd = npfd;
snd_seq_poll_descriptors(seq, polls->pollfds, npfd, POLLIN);
snd_seq_nonblock(seq, SND_SEQ_NONBLOCK);
return polls;
}
/* Set up ALSA MIDI subscription according to supplied parameter. */
static int
subscribe(snd_seq_port_subscribe_t *sub)
{
if (snd_seq_get_port_subscription(seq, sub) == 0) {
dbgprintf("Connection between editor and device already established\n");
return 0;
}
if (snd_seq_subscribe_port(seq, sub) < 0) {
dbgprintf("Couldn't estabilsh connection between editor and device\n");
return -1;
}
return 0;
}
/* Make bidirectional MIDI connection to specified remote device */
/* Remote device name is saved after first call (or rather the pointer to it,
* so the actual string must not be deallocated); subsequent calls may use
* NULL as the remote_device. */
int
midi_connect(const char *remote_device)
{
int client;
snd_seq_port_subscribe_t *sub;
snd_seq_addr_t my_addr;
snd_seq_addr_t remote_addr;
static const char *saved_remote_device = "";
if (remote_device)
saved_remote_device = remote_device;
client = snd_seq_client_id(seq);
if (client < 0) {
dbgprintf("Can't get client_id: %d\n", client);
return client;
}
dbgprintf("Client address %d:%d\n", client, seq_port);
snd_seq_port_subscribe_alloca(&sub);
/* My address */
my_addr.client = client;
my_addr.port = seq_port;
/* Other devices address */
if (snd_seq_parse_address(seq, &remote_addr, saved_remote_device) < 0) {
dbgprintf("Can't locate destination device %s\n", saved_remote_device);
return -1;
}
/* We always attempt to set up subscription in both directions, regardless
* of which error occurs when setting up the first direction. */
/* Set up sender and destination in subscription. */
snd_seq_port_subscribe_set_sender(sub, &my_addr);
snd_seq_port_subscribe_set_dest(sub, &remote_addr);
int res = subscribe(sub);
/* And now, connection in other direction. */
snd_seq_port_subscribe_set_sender(sub, &remote_addr);
snd_seq_port_subscribe_set_dest(sub, &my_addr);
int res2 = subscribe(sub);
if (res == 0) res = res2; /* if first subscribe() had no error */
return res;
}
/* Send control change message */
int
midi_send_control_change(int channel, int controller, int value)
{
snd_seq_event_t sendev;
snd_seq_ev_clear(&sendev);
snd_seq_ev_set_subs(&sendev);
snd_seq_ev_set_controller(&sendev, channel - 1, controller, value);
snd_seq_ev_set_direct(&sendev);
int ret = snd_seq_event_output_direct(seq, &sendev);
dbgprintf("Ch %d:CC %d:%d\n", channel, controller, value);
}
/* Handle MIDI input. To be called when poll() call in main loop indicates
* that data is available on our fd(s). */
void
midi_input(void)
{
int midi_status;
snd_seq_event_t *ev;
ssize_t evlen;
while (1)
{
midi_status = snd_seq_event_input(seq, &ev);
dbgprintf("MIDI input status : %d\n", midi_status);
if (midi_status < 0)
break;
evlen = snd_seq_event_length(ev);
dbgprintf("MIDI event length %d\n", evlen);
switch (ev->type) {
case SND_SEQ_EVENT_CONTROLLER:
dbgprintf("CC: ch %d, param %d, val %d\n", ev->data.control.channel + 1,
ev->data.control.param, ev->data.control.value);
if (cc_receiver) cc_receiver(ev->data.control.channel,
ev->data.control.param,
ev->data.control.value);
/* Do something useful here */
break;
default:
break;
}
}
}
void
register_midi_cc_reciver(midi_cc_receiver receiver)
{
cc_receiver = receiver;
}
/**************************** End of file midi.c ****************************/