2 Copyright (C) 1998-2010 Paul Barton-Davis
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #ifndef __libmidi_port_h__
20 #define __libmidi_port_h__
25 #include <jack/types.h>
27 #include "pbd/xml++.h"
28 #include "pbd/crossthread.h"
29 #include "pbd/signals.h"
30 #include "pbd/ringbuffer.h"
32 #include "evoral/Event.hpp"
33 #include "evoral/EventRingBuffer.hpp"
35 #include "midi++/types.h"
36 #include "midi++/parser.h"
45 Port (const XMLNode&, jack_client_t *);
48 XMLNode& get_state () const;
49 void set_state (const XMLNode&);
51 // FIXME: make Manager a friend of port so these can be hidden?
53 /* Only for use by MidiManager. Don't ever call this. */
54 void cycle_start(nframes_t nframes);
55 /* Only for use by MidiManager. Don't ever call this. */
58 /** Write a message to port.
59 * @param msg Raw MIDI message to send
60 * @param msglen Size of @a msg
61 * @param timestamp Time stamp in frames of this message (relative to cycle start)
62 * @return number of bytes successfully written
64 int write (byte *msg, size_t msglen, timestamp_t timestamp);
66 /** Read raw bytes from a port.
67 * @param buf memory to store read data in
68 * @param bufsize size of @a buf
69 * @return number of bytes successfully read, negative if error
71 int read (byte *buf, size_t bufsize);
73 void parse (nframes_t timestamp);
75 /** Write a message to port.
76 * @return true on success.
77 * FIXME: describe semantics here
79 int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
80 return !(write (msg, len, timestamp) == (int) len);
83 bool clock (timestamp_t timestamp);
85 /* select(2)/poll(2)-based I/O */
87 /** Get the file descriptor for port.
88 * @return File descriptor, or -1 if not selectable.
90 int selectable () const {
91 return xthread.selectable();
94 Channel *channel (channel_t chn) {
95 return _channel[chn&0x7F];
98 Parser *input() { return input_parser; }
99 Parser *output() { return output_parser; }
101 const char *name () const { return _tagname.c_str(); }
102 int mode () const { return _mode; }
103 bool ok () const { return _ok; }
109 Descriptor (const XMLNode&);
110 XMLNode& get_state();
113 nframes_t nframes_this_cycle() const { return _nframes_this_cycle; }
115 void reestablish (void *);
118 static void set_process_thread (pthread_t);
119 static pthread_t get_process_thread () { return _process_thread; }
120 static bool is_process_thread();
122 static PBD::Signal0<void> MakeConnections;
123 static PBD::Signal0<void> JackHalted;
127 bool _currently_in_cycle;
128 nframes_t _nframes_this_cycle;
129 std::string _tagname;
132 Channel *_channel[16];
133 Parser *input_parser;
134 Parser *output_parser;
136 static size_t nports;
138 int create_ports(const XMLNode&);
141 jack_client_t* _jack_client;
142 std::string _jack_input_port_name; /// input port name, or empty if there isn't one
143 jack_port_t* _jack_input_port;
144 std::string _jack_output_port_name; /// output port name, or empty if there isn't one
145 jack_port_t* _jack_output_port;
146 nframes_t _last_read_index;
147 timestamp_t _last_write_timestamp;
149 /** Channel used to signal to the MidiControlUI that input has arrived */
150 CrossThreadChannel xthread;
152 std::string _inbound_connections;
153 std::string _outbound_connections;
154 PBD::ScopedConnection connect_connection;
155 PBD::ScopedConnection halt_connection;
156 void flush (void* jack_port_buffer);
158 void make_connections();
160 static pthread_t _process_thread;
162 RingBuffer< Evoral::Event<double> > output_fifo;
163 Evoral::EventRingBuffer<timestamp_t> input_fifo;
165 Glib::Mutex output_fifo_lock;
170 PortSet (std::string str) : owner (str) { }
173 std::list<XMLNode> ports;
176 std::ostream & operator << ( std::ostream & os, const Port & port );
180 #endif // __libmidi_port_h__