summaryrefslogtreecommitdiff
path: root/libs/midi++2/alsa_sequencer_midiport.cc
blob: 9ffd9f78321ac9f2ff73fc2c190bceff82e5dd84 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
  Copyright (C) 2004 Paul Davis 

  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.

  $Id$
*/

#include <fcntl.h>
#include <cerrno>

#include <pbd/failed_constructor.h>
#include <pbd/error.h>

#include <midi++/types.h>
#include <midi++/alsa_sequencer.h>
#include <midi++/port_request.h>

//#define DOTRACE 1

#ifdef DOTRACE
#define TR_FN() (cerr << __FUNCTION__ << endl)
#define TR_VAL(v) (cerr << __FILE__  " " << __LINE__ << " " #v  "=" << v << endl)
#else
#define TR_FN()
#define TR_VAL(v)
#endif

using namespace std;
using namespace MIDI;
using namespace PBD;

snd_seq_t* ALSA_SequencerMidiPort::seq = 0;

ALSA_SequencerMidiPort::ALSA_SequencerMidiPort (PortRequest &req)
	: Port (req)
	, decoder (0) 
	, encoder (0) 
	, port_id (-1)
{
	TR_FN();
	int err;

	if (!seq && init_client (req.devname) < 0) {
		_ok = false; 

	} else {
		
		if (0 <= (err = CreatePorts (req)) &&
		    0 <= (err = snd_midi_event_new (1024, &decoder)) &&	// Length taken from ARDOUR::Session::midi_read ()
		    0 <= (err = snd_midi_event_new (64, &encoder))) {	// Length taken from ARDOUR::Session::mmc_buffer
			snd_midi_event_init (decoder);
			snd_midi_event_init (encoder);
			_ok = true;
			req.status = PortRequest::OK;
		} else {
			req.status = PortRequest::Unknown;
		}
	}
}

ALSA_SequencerMidiPort::~ALSA_SequencerMidiPort ()
{
	if (decoder) {
		snd_midi_event_free (decoder);
	}
	if (encoder) {
		snd_midi_event_free (encoder);
	}
	if (port_id >= 0) {
		snd_seq_delete_port (seq, port_id);
	}
}

int 
ALSA_SequencerMidiPort::selectable () const
{
	struct pollfd pfd[1];
	if (0 <= snd_seq_poll_descriptors (seq, pfd, 1, POLLIN | POLLOUT)) {
		return pfd[0].fd;
	}
	return -1;
}

int 
ALSA_SequencerMidiPort::write (byte *msg, size_t msglen)	
{
	TR_FN ();
	int R;
	int totwritten = 0;
	snd_midi_event_reset_encode (encoder);
	int nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
	TR_VAL (nwritten);
	while (0 < nwritten) {
		if (0 <= (R = snd_seq_event_output (seq, &SEv))  &&
		    0 <= (R = snd_seq_drain_output (seq))) {
			bytes_written += nwritten;
			totwritten += nwritten;
			if (output_parser) {
				output_parser->raw_preparse (*output_parser, msg, nwritten);
				for (int i = 0; i < nwritten; i++) {
					output_parser->scanner (msg[i]);
				}
				output_parser->raw_postparse (*output_parser, msg, nwritten);
			}
		} else {
			TR_VAL(R);
			return R;
		}

		msglen -= nwritten;
		msg += nwritten;
		if (msglen > 0) {
			nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
			TR_VAL(nwritten);
		}
		else {
			break;
		}
	}

	return totwritten;
}

int 
ALSA_SequencerMidiPort::read (byte *buf, size_t max)
{
	TR_FN();
	int err;
	snd_seq_event_t *ev;
	if (0 <= (err = snd_seq_event_input (seq, &ev))) {
		TR_VAL(err);
		err = snd_midi_event_decode (decoder, buf, max, ev);
	}

	if (err > 0) {
		bytes_read += err;

		if (input_parser) {
			input_parser->raw_preparse (*input_parser, buf, err);
			for (int i = 0; i < err; i++) {
				input_parser->scanner (buf[i]);
			}	
			input_parser->raw_postparse (*input_parser, buf, err);
		}
	}
	return -ENOENT == err ? 0 : err;
}

int 
ALSA_SequencerMidiPort::CreatePorts (PortRequest &req)
{
	int err;
	unsigned int caps = 0;

	if (req.mode == O_WRONLY  ||  req.mode == O_RDWR)
		caps |= SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
	if (req.mode == O_RDONLY  ||  req.mode == O_RDWR)
		caps |= SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
	
	if (0 <= (err = snd_seq_create_simple_port (seq, req.tagname, caps, 
						    (SND_SEQ_PORT_TYPE_MIDI_GENERIC|
						     SND_SEQ_PORT_TYPE_SOFTWARE|
						     SND_SEQ_PORT_TYPE_APPLICATION)))) {
		
		port_id = err;

		snd_seq_ev_clear (&SEv);
		snd_seq_ev_set_source (&SEv, port_id);
		snd_seq_ev_set_subs (&SEv);
		snd_seq_ev_set_direct (&SEv);
		
		return 0;
	}

	return err;
}

int
ALSA_SequencerMidiPort::init_client (std::string name)
{
	static bool called = false;

	if (called) {
		return -1;
	}

	called = true;

	if (snd_seq_open (&seq, "default", SND_SEQ_OPEN_DUPLEX, 0) >= 0) {
		snd_seq_set_client_name (seq, name.c_str());
		return 0;
	} else {
		warning << "The ALSA MIDI system is not available. No ports based on it will be created"
			<< endmsg;
		return -1;
	}
}

int
ALSA_SequencerMidiPort::discover (vector<PortSet>& ports)
{
	 int n = 0;

	 snd_seq_client_info_t *client_info;
	 snd_seq_port_info_t   *port_info;

	 snd_seq_client_info_alloca (&client_info);
	 snd_seq_port_info_alloca (&port_info);
	 snd_seq_client_info_set_client (client_info, -1);

	 while (snd_seq_query_next_client(seq, client_info) >= 0) {

		 int alsa_client;

		 if ((alsa_client = snd_seq_client_info_get_client(client_info)) <= 0) {
			 break;
		 }

		 snd_seq_port_info_set_client(port_info, alsa_client);
		 snd_seq_port_info_set_port(port_info, -1);

		 char client[256];
		 snprintf (client, sizeof (client), "%d:%s", alsa_client, snd_seq_client_info_get_name(client_info));

		 ports.push_back (PortSet (client));

		 while (snd_seq_query_next_port(seq, port_info) >= 0) {

#if 0
			 int type = snd_seq_port_info_get_type(pinfo);
			 if (!(type & SND_SEQ_PORT_TYPE_PORT)) {
				 continue;
			 }
#endif

			 unsigned int port_capability = snd_seq_port_info_get_capability(port_info);

			 if ((port_capability & SND_SEQ_PORT_CAP_NO_EXPORT) == 0) {

				 int alsa_port = snd_seq_port_info_get_port(port_info);

				 char port[256];
				 snprintf (port, sizeof (port), "%d:%s", alsa_port, snd_seq_port_info_get_name(port_info));

				 std::string mode;

				 if (port_capability & SND_SEQ_PORT_CAP_READ) {
					 if (port_capability & SND_SEQ_PORT_CAP_WRITE) {
							 mode = "duplex";
						 } else {
							 mode = "output";
						 } 
					 } else if (port_capability & SND_SEQ_PORT_CAP_WRITE) {
						 if (port_capability & SND_SEQ_PORT_CAP_READ) {
							 mode = "duplex";
						 } else {
							 mode = "input";
						 } 
					 }

					 ports.back().ports.push_back (PortRequest (client, port, mode, "alsa/sequencer"));
					 ++n;
				 }
		 }
	 }
	 
	 return n;
}