fix crash when copy'ing latent plugins
[ardour.git] / libs / backends / alsa / alsa_midi.h
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifndef __libbackend_alsa_midi_h__
20 #define __libbackend_alsa_midi_h__
21
22 #include <stdint.h>
23 #include <poll.h>
24 #include <pthread.h>
25
26 #include "pbd/ringbuffer.h"
27 #include "ardour/types.h"
28
29 namespace ARDOUR {
30
31 class AlsaMidiIO {
32 public:
33         AlsaMidiIO ();
34         virtual ~AlsaMidiIO ();
35
36         int state (void) const { return _state; }
37         int start ();
38         int stop ();
39
40         void setup_timing (const size_t samples_per_period, const float samplerate);
41         void sync_time(uint64_t);
42
43         virtual void* main_process_thread () = 0;
44
45         const std::string & name () const { return _name; }
46
47 protected:
48         pthread_t _main_thread;
49         pthread_mutex_t _notify_mutex;
50         pthread_cond_t _notify_ready;
51
52         int  _state;
53         bool  _running;
54
55         int _npfds;
56         struct pollfd *_pfds;
57
58         double _sample_length_us;
59         double _period_length_us;
60         size_t _samples_per_period;
61         uint64_t _clock_monotonic;
62
63         struct MidiEventHeader {
64                 uint64_t time;
65                 size_t size;
66                 MidiEventHeader(const uint64_t t, const size_t s)
67                         : time(t)
68                         , size(s) {}
69         };
70
71         RingBuffer<uint8_t>* _rb;
72
73         std::string _name;
74
75         virtual void init (const char *device_name, const bool input) = 0;
76
77 };
78
79 class AlsaMidiOut : virtual public AlsaMidiIO
80 {
81 public:
82         AlsaMidiOut ();
83
84         int send_event (const pframes_t, const uint8_t *, const size_t);
85 };
86
87 class AlsaMidiIn : virtual public AlsaMidiIO
88 {
89 public:
90         AlsaMidiIn ();
91
92         size_t recv_event (pframes_t &, uint8_t *, size_t &);
93
94 protected:
95         int queue_event (const uint64_t, const uint8_t *, const size_t);
96 };
97
98 } // namespace
99
100 #endif