make ALSA MIDI I/O work with timestamps; more MTC debug tracing
[ardour.git] / libs / midi++2 / midi++ / port.h
1 /*
2     Copyright (C) 1998-99 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.
7
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.
12
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.
16
17 */
18
19 #ifndef  __libmidi_port_h__
20 #define  __libmidi_port_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include <sigc++/sigc++.h>
26 #include "pbd/xml++.h"
27
28 #include "midi++/types.h"
29 #include "midi++/parser.h"
30
31 namespace MIDI {
32
33 class Channel;
34 class PortRequest;
35
36 class Port : public sigc::trackable {
37   public:
38         enum Type {
39                 Unknown,
40                 JACK_Midi,
41                 ALSA_RawMidi,
42                 ALSA_Sequencer,
43                 CoreMidi_MidiPort,
44                 Null,
45                 FIFO
46         };
47
48
49         Port (const XMLNode&);
50         virtual ~Port ();
51
52         virtual XMLNode& get_state () const;
53         virtual void set_state (const XMLNode&);
54
55         // FIXME: make Manager a friend of port so these can be hidden?
56
57         /* Only for use by MidiManager.  Don't ever call this. */
58         virtual void cycle_start(nframes_t nframes);
59         /* Only for use by MidiManager.  Don't ever call this. */
60         virtual void cycle_end();
61
62         /** Write a message to port.
63          * @param msg Raw MIDI message to send
64          * @param msglen Size of @a msg
65          * @param timestamp Time stamp in frames of this message (relative to cycle start)
66          * @return number of bytes successfully written
67          */
68         virtual int write (byte *msg, size_t msglen, timestamp_t timestamp) = 0;        
69
70         /** Read raw bytes from a port.
71          * @param buf memory to store read data in
72          * @param bufsize size of @a buf
73          * @return number of bytes successfully read, negative if error
74          */
75         virtual int read (byte *buf, size_t bufsize) = 0;
76
77         void parse (nframes_t timestamp);
78         
79         /** Write a message to port.
80          * @return true on success.
81          * FIXME: describe semantics here
82          */
83         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
84                 return !(write (msg, len, timestamp) == (int) len);
85         } 
86
87         int three_byte_msg (byte a, byte b, byte c, timestamp_t timestamp) {
88                 byte msg[3];
89
90                 msg[0] = a;
91                 msg[1] = b;
92                 msg[2] = c;
93
94                 return !(write (msg, 3, timestamp) == 3);
95         } 
96         
97         bool clock (timestamp_t timestamp);
98         
99         /* slowdown i/o to a loop of single byte emissions
100            interspersed with a busy loop of 10000 * this value.
101
102            This may be ignored by a particular instance
103            of this virtual class. See FD_MidiPort for an 
104            example of where it used.  
105         */
106
107         void set_slowdown (size_t n) { slowdown = n; }
108
109         /* select(2)/poll(2)-based I/O */
110
111         /** Get the file descriptor for port.
112          * @return File descriptor, or -1 if not selectable. 
113          */
114         virtual int selectable() const = 0;
115
116         static void gtk_read_callback (void *ptr, int fd, int cond);
117         static void write_callback (byte *msg, unsigned int len, void *);
118         
119         Channel *channel (channel_t chn) { 
120                 return _channel[chn&0x7F];
121         }
122         
123         Parser *input()     { return input_parser; }
124         Parser *output()    { return output_parser; }
125
126         void iostat (int *written, int *read, 
127                      const size_t **in_counts,
128                      const size_t **out_counts) {
129
130                 *written = bytes_written;
131                 *read = bytes_read;
132                 if (input_parser) {
133                         *in_counts = input_parser->message_counts();
134                 } else {
135                         *in_counts = 0;
136                 }
137                 if (output_parser) {
138                         *out_counts = output_parser->message_counts();
139                 } else {
140                         *out_counts = 0;
141                 }
142         }
143         
144         const char *device () const { return _devname.c_str(); }
145         const char *name () const   { return _tagname.c_str(); }
146         Type   type () const        { return _type; }
147         int    mode () const        { return _mode; }
148         bool   ok ()   const        { return _ok; }
149
150         struct Descriptor {
151             std::string tag;
152             std::string device;
153             int mode;
154             Port::Type type;
155
156             Descriptor (const XMLNode&);
157             XMLNode& get_state();
158         };
159
160   protected:
161         bool             _ok;
162         bool             _currently_in_cycle;
163         nframes_t        _nframes_this_cycle;
164         Type             _type;
165         std::string      _devname;
166         std::string      _tagname;
167         int              _mode;
168         size_t           _number;
169         Channel          *_channel[16];
170         sigc::connection thru_connection;
171         unsigned int     bytes_written;
172         unsigned int     bytes_read;
173         Parser           *input_parser;
174         Parser           *output_parser;
175         size_t           slowdown;
176
177         virtual std::string get_typestring () const = 0;
178
179   private:
180         static size_t nports;
181 };
182
183 struct PortSet {
184     PortSet (std::string str) : owner (str) { }
185     
186     std::string owner;
187     std::list<XMLNode> ports;
188 };
189
190 std::ostream & operator << ( std::ostream & os, const Port & port );
191
192 } // namespace MIDI
193
194 #endif // __libmidi_port_h__