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 __libardour_async_midiport_h__
20 #define __libardour_async_midiport_h__
25 #include <boost/function.hpp>
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"
34 #include "midi++/types.h"
35 #include "midi++/parser.h"
36 #include "midi++/port.h"
38 #include "ardour/event_ring_buffer.h"
39 #include "ardour/libardour_visibility.h"
40 #include "ardour/midi_port.h"
44 class LIBARDOUR_API AsyncMIDIPort : public ARDOUR::MidiPort, public MIDI::Port {
47 AsyncMIDIPort (std::string const &, PortFlags);
50 bool flush_at_cycle_start () const { return _flush_at_cycle_start; }
51 void set_flush_at_cycle_start (bool en) { _flush_at_cycle_start = en; }
53 /* called from an RT context */
54 void cycle_start (pframes_t nframes);
55 void cycle_end (pframes_t nframes);
57 /* called from non-RT context */
58 void parse (samplecnt_t timestamp);
59 int write (const MIDI::byte *msg, size_t msglen, MIDI::timestamp_t timestamp);
60 int read (MIDI::byte *buf, size_t bufsize);
61 /* waits for output to be cleared */
62 void drain (int check_interval_usecs, int total_usecs_to_wait);
64 /* clears async request communication channel */
69 CrossThreadChannel& xthread() {
73 /* Not selectable; use ios() */
74 int selectable() const { return -1; }
75 void set_timer (boost::function<samplecnt_t (void)>&);
77 static void set_process_thread (pthread_t);
78 static pthread_t get_process_thread () { return _process_thread; }
79 static bool is_process_thread();
82 bool _currently_in_cycle;
83 MIDI::timestamp_t _last_write_timestamp;
84 bool _flush_at_cycle_start;
86 boost::function<samplecnt_t (void)> timer;
87 PBD::RingBuffer< Evoral::Event<double> > output_fifo;
88 EventRingBuffer<MIDI::timestamp_t> input_fifo;
89 Glib::Threads::Mutex output_fifo_lock;
90 CrossThreadChannel _xthread;
94 /** Channel used to signal to the MidiControlUI that input has arrived */
96 std::string _connections;
97 PBD::ScopedConnection connect_connection;
98 PBD::ScopedConnection halt_connection;
100 void make_connections ();
101 void init (std::string const &, Flags);
103 void flush_output_fifo (pframes_t);
105 static pthread_t _process_thread;
108 } // namespace ARDOUR
110 #endif /* __libardour_async_midiport_h__ */