2 Copyright (C) 2004 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "pbd/failed_constructor.h"
25 #include "pbd/error.h"
26 #include "pbd/xml++.h"
28 #include "midi++/types.h"
29 #include "midi++/alsa_sequencer.h"
36 #define TR_FN() (cerr << __FUNCTION__ << endl)
37 #define TR_VAL(v) (cerr << __FILE__ " " << __LINE__ << " " #v "=" << v << endl)
47 snd_seq_t* ALSA_SequencerMidiPort::seq = 0;
49 ALSA_SequencerMidiPort::ALSA_SequencerMidiPort (const XMLNode& node)
57 Descriptor desc (node);
59 if (!seq && init_client (desc.device) < 0) {
64 if (0 <= (err = create_ports (desc)) &&
65 0 <= (err = snd_midi_event_new (1024, &decoder)) && // Length taken from ARDOUR::Session::midi_read ()
66 0 <= (err = snd_midi_event_new (64, &encoder))) { // Length taken from ARDOUR::Session::mmc_buffer
67 snd_midi_event_init (decoder);
68 snd_midi_event_init (encoder);
76 ALSA_SequencerMidiPort::~ALSA_SequencerMidiPort ()
79 snd_midi_event_free (decoder);
82 snd_midi_event_free (encoder);
85 snd_seq_delete_port (seq, port_id);
90 ALSA_SequencerMidiPort::selectable () const
93 if (0 <= snd_seq_poll_descriptors (seq, pfd, 1, POLLIN | POLLOUT)) {
100 ALSA_SequencerMidiPort::write (byte *msg, size_t msglen, timestamp_t ignored)
105 snd_midi_event_reset_encode (encoder);
106 int nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
108 while (0 < nwritten) {
109 if (0 <= (R = snd_seq_event_output (seq, &SEv)) &&
110 0 <= (R = snd_seq_drain_output (seq))) {
111 bytes_written += nwritten;
112 totwritten += nwritten;
114 output_parser->raw_preparse (*output_parser, msg, nwritten);
115 for (int i = 0; i < nwritten; i++) {
116 output_parser->scanner (msg[i]);
118 output_parser->raw_postparse (*output_parser, msg, nwritten);
128 nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
140 ALSA_SequencerMidiPort::read (byte *buf, size_t max)
145 if (0 <= (err = snd_seq_event_input (seq, &ev))) {
147 err = snd_midi_event_decode (decoder, buf, max, ev);
154 input_parser->raw_preparse (*input_parser, buf, err);
155 for (int i = 0; i < err; i++) {
156 input_parser->scanner (buf[i]);
158 input_parser->raw_postparse (*input_parser, buf, err);
161 return -ENOENT == err ? 0 : err;
165 ALSA_SequencerMidiPort::create_ports (const Port::Descriptor& desc)
168 unsigned int caps = 0;
170 if (desc.mode == O_WRONLY || desc.mode == O_RDWR)
171 caps |= SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
172 if (desc.mode == O_RDONLY || desc.mode == O_RDWR)
173 caps |= SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
175 if (0 <= (err = snd_seq_create_simple_port (seq, desc.tag.c_str(), caps,
176 (SND_SEQ_PORT_TYPE_MIDI_GENERIC|
177 SND_SEQ_PORT_TYPE_SOFTWARE|
178 SND_SEQ_PORT_TYPE_APPLICATION)))) {
182 snd_seq_ev_clear (&SEv);
183 snd_seq_ev_set_source (&SEv, port_id);
184 snd_seq_ev_set_subs (&SEv);
185 snd_seq_ev_set_direct (&SEv);
194 ALSA_SequencerMidiPort::init_client (std::string name)
196 static bool called = false;
204 if (snd_seq_open (&seq, "default", SND_SEQ_OPEN_DUPLEX, 0) >= 0) {
205 snd_seq_set_client_name (seq, name.c_str());
208 warning << "The ALSA MIDI system is not available. No ports based on it will be created"
215 ALSA_SequencerMidiPort::discover (vector<PortSet>& ports)
219 snd_seq_client_info_t *client_info;
220 snd_seq_port_info_t *port_info;
222 snd_seq_client_info_alloca (&client_info);
223 snd_seq_port_info_alloca (&port_info);
224 snd_seq_client_info_set_client (client_info, -1);
226 while (snd_seq_query_next_client(seq, client_info) >= 0) {
230 if ((alsa_client = snd_seq_client_info_get_client(client_info)) <= 0) {
234 snd_seq_port_info_set_client(port_info, alsa_client);
235 snd_seq_port_info_set_port(port_info, -1);
238 snprintf (client, sizeof (client), "%d:%s", alsa_client, snd_seq_client_info_get_name(client_info));
240 ports.push_back (PortSet (client));
242 while (snd_seq_query_next_port(seq, port_info) >= 0) {
245 int type = snd_seq_port_info_get_type(pinfo);
246 if (!(type & SND_SEQ_PORT_TYPE_PORT)) {
251 unsigned int port_capability = snd_seq_port_info_get_capability(port_info);
253 if ((port_capability & SND_SEQ_PORT_CAP_NO_EXPORT) == 0) {
255 int alsa_port = snd_seq_port_info_get_port(port_info);
258 snprintf (port, sizeof (port), "%d:%s", alsa_port, snd_seq_port_info_get_name(port_info));
262 if (port_capability & SND_SEQ_PORT_CAP_READ) {
263 if (port_capability & SND_SEQ_PORT_CAP_WRITE) {
268 } else if (port_capability & SND_SEQ_PORT_CAP_WRITE) {
269 if (port_capability & SND_SEQ_PORT_CAP_READ) {
276 XMLNode node (X_("MIDI-port"));
277 node.add_property ("device", client);
278 node.add_property ("tag", port);
279 node.add_property ("mode", mode);
280 node.add_property ("type", "alsa/sequencer");
282 ports.back().ports.push_back (node);
292 ALSA_SequencerMidiPort::get_connections (vector<SequencerPortAddress>& connections, int dir) const
294 snd_seq_query_subscribe_t *subs;
295 snd_seq_addr_t seq_addr;
297 snd_seq_query_subscribe_alloca (&subs);
299 // Get port connections...
302 snd_seq_query_subscribe_set_type(subs, SND_SEQ_QUERY_SUBS_WRITE);
304 snd_seq_query_subscribe_set_type(subs, SND_SEQ_QUERY_SUBS_READ);
307 snd_seq_query_subscribe_set_index(subs, 0);
308 seq_addr.client = snd_seq_client_id (seq);
309 seq_addr.port = port_id;
310 snd_seq_query_subscribe_set_root(subs, &seq_addr);
312 while (snd_seq_query_port_subscribers(seq, subs) >= 0) {
314 if (snd_seq_query_subscribe_get_time_real (subs)) {
315 /* interesting connection */
317 seq_addr = *snd_seq_query_subscribe_get_addr (subs);
319 connections.push_back (SequencerPortAddress (seq_addr.client,
323 snd_seq_query_subscribe_set_index(subs, snd_seq_query_subscribe_get_index(subs) + 1);
328 ALSA_SequencerMidiPort::get_state () const
330 XMLNode& root (Port::get_state ());
331 vector<SequencerPortAddress> connections;
335 get_connections (connections, 1);
337 if (!connections.empty()) {
339 sub = new XMLNode (X_("connections"));
341 for (vector<SequencerPortAddress>::iterator i = connections.begin(); i != connections.end(); ++i) {
342 XMLNode* cnode = new XMLNode (X_("read"));
343 snprintf (buf, sizeof (buf), "%d:%d", i->first, i->second);
344 cnode->add_property ("dest", buf);
345 sub->add_child_nocopy (*cnode);
349 connections.clear ();
350 get_connections (connections, 0);
352 if (!connections.empty()) {
354 sub = new XMLNode (X_("connections"));
356 for (vector<SequencerPortAddress>::iterator i = connections.begin(); i != connections.end(); ++i) {
357 XMLNode* cnode = new XMLNode (X_("write"));
358 snprintf (buf, sizeof (buf), "%d:%d", i->first, i->second);
359 cnode->add_property ("dest", buf);
360 sub->add_child_nocopy (*cnode);
365 root.add_child_nocopy (*sub);
372 ALSA_SequencerMidiPort::set_state (const XMLNode& node)
374 Port::set_state (node);
376 XMLNodeList children (node.children());
377 XMLNodeIterator iter;
379 for (iter = children.begin(); iter != children.end(); ++iter) {
381 if ((*iter)->name() == X_("connections")) {
383 XMLNodeList gchildren ((*iter)->children());
384 XMLNodeIterator gciter;
386 for (gciter = gchildren.begin(); gciter != gchildren.end(); ++gciter) {
389 if ((prop = (*gciter)->property ("dest")) != 0) {
393 if (sscanf (prop->value().c_str(), "%d:%d", &client, &port) == 2) {
395 snd_seq_port_subscribe_t *sub;
396 snd_seq_addr_t seq_addr;
398 snd_seq_port_subscribe_alloca(&sub);
400 if ((*gciter)->name() == X_("write")) {
402 seq_addr.client = snd_seq_client_id (seq);
403 seq_addr.port = port_id;
404 snd_seq_port_subscribe_set_sender(sub, &seq_addr);
406 seq_addr.client = client;
407 seq_addr.port = port;
408 snd_seq_port_subscribe_set_dest(sub, &seq_addr);
412 seq_addr.client = snd_seq_client_id (seq);
413 seq_addr.port = port_id;
414 snd_seq_port_subscribe_set_dest(sub, &seq_addr);
416 seq_addr.client = client;
417 seq_addr.port = port;
418 snd_seq_port_subscribe_set_sender(sub, &seq_addr);
421 snd_seq_subscribe_port (seq, sub);