46d5f94a91b315368b89ca3d7081371b4bf56ffd
[ardour.git] / libs / ardour / ardour / async_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  __libardour_async_midiport_h__
20 #define  __libardour_async_midiport_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include <boost/function.hpp>
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
34 #include "midi++/types.h"
35 #include "midi++/parser.h"
36 #include "midi++/port.h"
37
38 #include "ardour/event_ring_buffer.h"
39 #include "ardour/libardour_visibility.h"
40 #include "ardour/midi_port.h"
41
42 namespace ARDOUR {
43
44 class LIBARDOUR_API AsyncMIDIPort : public ARDOUR::MidiPort, public MIDI::Port {
45
46         public:
47                 AsyncMIDIPort (std::string const &, PortFlags);
48                 ~AsyncMIDIPort ();
49
50                 /* called from an RT context */
51
52                 void cycle_start (pframes_t nframes);
53                 void cycle_end (pframes_t nframes);
54
55                 /* called from non-RT context */
56                 void parse (framecnt_t timestamp);
57                 int write (const MIDI::byte *msg, size_t msglen, MIDI::timestamp_t timestamp);
58                 int read (MIDI::byte *buf, size_t bufsize);
59                 /* waits for output to be cleared */
60                 void drain (int check_interval_usecs, int total_usecs_to_wait);
61
62                 /* clears async request communication channel */
63                 void clear () {
64                         _xthread.drain ();
65                 }
66
67                 CrossThreadChannel& xthread() {
68                         return _xthread;
69                 }
70
71                 /* Not selectable; use ios() */
72                 int selectable() const { return -1; }
73                 void set_timer (boost::function<framecnt_t (void)>&);
74
75                 static void set_process_thread (pthread_t);
76                 static pthread_t get_process_thread () { return _process_thread; }
77                 static bool is_process_thread();
78
79         private:
80                 bool                    _currently_in_cycle;
81                 MIDI::timestamp_t       _last_write_timestamp;
82                 bool                    have_timer;
83                 boost::function<framecnt_t (void)> timer;
84                 RingBuffer< Evoral::Event<double> > output_fifo;
85                 EventRingBuffer<MIDI::timestamp_t> input_fifo;
86                 Glib::Threads::Mutex output_fifo_lock;
87                 CrossThreadChannel _xthread;
88
89                 int create_port ();
90
91                 /** Channel used to signal to the MidiControlUI that input has arrived */
92
93                 std::string _connections;
94                 PBD::ScopedConnection connect_connection;
95                 PBD::ScopedConnection halt_connection;
96                 void jack_halted ();
97                 void make_connections ();
98                 void init (std::string const &, Flags);
99
100                 void flush_output_fifo (pframes_t);
101
102                 static pthread_t _process_thread;
103 };
104
105 } // namespace ARDOUR
106
107 #endif /* __libardour_async_midiport_h__ */