Remove all use of nframes_t.
[ardour.git] / libs / midi++2 / midi++ / port.h
1 /*
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.
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 <jack/types.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/crossthread.h"
29 #include "pbd/signals.h"
30 #include "pbd/ringbuffer.h"
31
32 #include "evoral/Event.hpp"
33 #include "evoral/EventRingBuffer.hpp"
34
35 #include "midi++/types.h"
36 #include "midi++/parser.h"
37
38 namespace MIDI {
39
40 class Channel;
41 class PortRequest;
42
43 class Port {
44   public:
45         enum Flags {
46                 IsInput = JackPortIsInput,
47                 IsOutput = JackPortIsOutput,
48         };
49         
50         Port (std::string const &, Flags, jack_client_t *);
51         Port (const XMLNode&, jack_client_t *);
52         ~Port ();
53
54         XMLNode& get_state () const;
55         void set_state (const XMLNode&);
56
57         // FIXME: make Manager a friend of port so these can be hidden?
58
59         /* Only for use by MidiManager.  Don't ever call this. */
60         void cycle_start (pframes_t nframes);
61         /* Only for use by MidiManager.  Don't ever call this. */
62         void cycle_end ();
63
64         /** Write a message to port.
65          * @param msg Raw MIDI message to send
66          * @param msglen Size of @a msg
67          * @param timestamp Time stamp in frames of this message (relative to cycle start)
68          * @return number of bytes successfully written
69          */
70         int write (byte *msg, size_t msglen, timestamp_t timestamp);
71
72         /** Read raw bytes from a port.
73          * @param buf memory to store read data in
74          * @param bufsize size of @a buf
75          * @return number of bytes successfully read, negative if error
76          */
77         int read (byte *buf, size_t bufsize);
78
79         void parse (framecnt_t timestamp);
80         
81         /** Write a message to port.
82          * @return true on success.
83          * FIXME: describe semantics here
84          */
85         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
86                 return !(write (msg, len, timestamp) == (int) len);
87         } 
88
89         bool clock (timestamp_t timestamp);
90
91         /* select(2)/poll(2)-based I/O */
92
93         /** Get the file descriptor for port.
94          * @return File descriptor, or -1 if not selectable. 
95          */
96         int selectable () const {
97                 return xthread.selectable();
98         }
99
100         Channel *channel (channel_t chn) { 
101                 return _channel[chn&0x7F];
102         }
103         
104         Parser* parser () {
105                 return _parser;
106         }
107         
108         const char *name () const   { return _tagname.c_str(); }
109         bool   ok ()   const        { return _ok; }
110
111         bool receives_input () const {
112                 return _flags == IsInput;
113         }
114
115         bool sends_output () const {
116                 return _flags == IsOutput;
117         }
118
119         struct Descriptor {
120             std::string tag;
121             Flags flags;
122
123             Descriptor (const XMLNode&);
124             XMLNode& get_state();
125         };
126
127         pframes_t nframes_this_cycle() const { return _nframes_this_cycle; }
128
129         void reestablish (jack_client_t *);
130         void reconnect ();
131
132         static void set_process_thread (pthread_t);
133         static pthread_t get_process_thread () { return _process_thread; }
134         static bool is_process_thread();
135
136         static std::string state_node_name;
137         
138         static PBD::Signal0<void> MakeConnections;
139         static PBD::Signal0<void> JackHalted;
140
141 private:        
142         bool             _ok;
143         bool             _currently_in_cycle;
144         pframes_t        _nframes_this_cycle;
145         std::string      _tagname;
146         size_t           _number;
147         Channel          *_channel[16];
148         Parser           *_parser;
149
150         int create_port ();
151
152         jack_client_t* _jack_client;
153         jack_port_t*   _jack_port;
154         framecnt_t     _last_read_index;
155         timestamp_t    _last_write_timestamp;
156
157         /** Channel used to signal to the MidiControlUI that input has arrived */
158         CrossThreadChannel xthread;
159         
160         std::string _connections;
161         PBD::ScopedConnection connect_connection;
162         PBD::ScopedConnection halt_connection;
163         void flush (void* jack_port_buffer);
164         void jack_halted ();
165         void make_connections ();
166         void init (std::string const &, Flags);
167
168         static pthread_t _process_thread;
169
170         RingBuffer< Evoral::Event<double> > output_fifo;
171         Evoral::EventRingBuffer<timestamp_t> input_fifo;
172
173         Glib::Mutex output_fifo_lock;
174
175         Flags _flags;
176 };
177
178 struct PortSet {
179     PortSet (std::string str) : owner (str) { }
180     
181     std::string owner;
182     std::list<XMLNode> ports;
183 };
184
185 std::ostream & operator << ( std::ostream & os, const Port & port );
186
187 } // namespace MIDI
188
189 #endif // __libmidi_port_h__