2005-05-13 16:47:18 -04:00
|
|
|
/*
|
|
|
|
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>
|
|
|
|
|
2007-01-28 12:44:13 -05:00
|
|
|
#include <pbd/failed_constructor.h>
|
|
|
|
#include <pbd/error.h>
|
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
#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;
|
2007-01-28 12:44:13 -05:00
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
snd_seq_t* ALSA_SequencerMidiPort::seq = 0;
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
ALSA_SequencerMidiPort::ALSA_SequencerMidiPort (PortRequest &req)
|
|
|
|
: Port (req)
|
|
|
|
, decoder (0)
|
|
|
|
, encoder (0)
|
2007-01-28 12:44:13 -05:00
|
|
|
, port_id (-1)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
TR_FN();
|
|
|
|
int err;
|
2007-01-28 12:44:13 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ALSA_SequencerMidiPort::~ALSA_SequencerMidiPort ()
|
|
|
|
{
|
2007-01-28 12:44:13 -05:00
|
|
|
if (decoder) {
|
2005-05-13 16:47:18 -04:00
|
|
|
snd_midi_event_free (decoder);
|
2007-01-28 12:44:13 -05:00
|
|
|
}
|
|
|
|
if (encoder) {
|
2005-05-13 16:47:18 -04:00
|
|
|
snd_midi_event_free (encoder);
|
2007-01-28 12:44:13 -05:00
|
|
|
}
|
|
|
|
if (port_id >= 0) {
|
|
|
|
snd_seq_delete_port (seq, port_id);
|
|
|
|
}
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2007-01-28 12:44:13 -05:00
|
|
|
int
|
|
|
|
ALSA_SequencerMidiPort::selectable () const
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
if (0 <= snd_seq_poll_descriptors (seq, pfd, 1, POLLIN | POLLOUT)) {
|
|
|
|
return pfd[0].fd;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-08 19:46:42 -04:00
|
|
|
int ALSA_SequencerMidiPort::write (byte *msg, size_t msglen, timestamp_t timestamp)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
TR_FN ();
|
|
|
|
int R;
|
2006-01-05 23:59:17 -05:00
|
|
|
int totwritten = 0;
|
2005-05-13 16:47:18 -04:00
|
|
|
snd_midi_event_reset_encode (encoder);
|
|
|
|
int nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
|
|
|
|
TR_VAL (nwritten);
|
2006-01-05 23:59:17 -05:00
|
|
|
while (0 < nwritten) {
|
2005-05-13 16:47:18 -04:00
|
|
|
if (0 <= (R = snd_seq_event_output (seq, &SEv)) &&
|
|
|
|
0 <= (R = snd_seq_drain_output (seq))) {
|
|
|
|
bytes_written += nwritten;
|
2006-01-05 23:59:17 -05:00
|
|
|
totwritten += nwritten;
|
2005-05-13 16:47:18 -04:00
|
|
|
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;
|
|
|
|
}
|
2006-01-05 23:59:17 -05:00
|
|
|
|
|
|
|
msglen -= nwritten;
|
|
|
|
msg += nwritten;
|
|
|
|
if (msglen > 0) {
|
|
|
|
nwritten = snd_midi_event_encode (encoder, msg, msglen, &SEv);
|
|
|
|
TR_VAL(nwritten);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totwritten;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2007-01-28 12:44:13 -05:00
|
|
|
<<<<<<< .working
|
2006-06-08 19:46:42 -04:00
|
|
|
int ALSA_SequencerMidiPort::read (byte *buf, size_t max, timestamp_t timestamp)
|
2007-01-28 12:44:13 -05:00
|
|
|
=======
|
|
|
|
int
|
|
|
|
ALSA_SequencerMidiPort::read (byte *buf, size_t max)
|
|
|
|
>>>>>>> .merge-right.r1393
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-01-28 12:44:13 -05:00
|
|
|
int
|
|
|
|
ALSA_SequencerMidiPort::CreatePorts (PortRequest &req)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
int err;
|
2007-01-28 12:44:13 -05:00
|
|
|
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))) {
|
|
|
|
|
|
|
|
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;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
2007-01-28 12:44:13 -05:00
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-01-28 12:44:13 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|