Hard Core Audio
[ardour.git] / libs / backends / coreaudio / coremidi_io.h
1 /*
2  * Copyright (C) 2015 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 #include <CoreServices/CoreServices.h>
20 #include <CoreAudio/CoreAudio.h>
21 #include <AudioUnit/AudioUnit.h>
22
23 #include <AudioUnit/AudioUnit.h>
24 #include <AudioToolbox/AudioToolbox.h>
25
26 #include <map>
27 #include <vector>
28 #include <string>
29
30 #include <boost/shared_ptr.hpp>
31 #include "pbd/ringbuffer.h"
32
33 typedef struct _CoreMIDIPacket { 
34         MIDITimeStamp timeStamp; 
35         UInt16 length; 
36         Byte data[256]; 
37 #if 0 // unused
38         _CoreMIDIPacket (MIDITimeStamp t, Byte *d, UInt16 l)
39                 : timeStamp(t)
40                 , length (l)
41         {
42                 if (l > 256) {
43                         length = 256;
44                 }
45                 if (length > 0) {
46                         memcpy(data, d, length);
47                 }
48         }
49 #endif
50         _CoreMIDIPacket (const MIDIPacket *other) 
51                 : timeStamp(other->timeStamp)
52                 , length (other->length)
53         {
54                 if (length > 0) {
55                         memcpy(data, other->data, length);
56                 }
57         }
58 } CoreMIDIPacket;
59
60 typedef std::vector<boost::shared_ptr<CoreMIDIPacket> > CoreMIDIQueue;
61
62 class CoreMidiIo {
63 public:
64         CoreMidiIo (void);
65         ~CoreMidiIo (void);
66
67         void start ();
68         void stop ();
69
70         void start_cycle ();
71
72         int send_event (uint32_t, double, const uint8_t *, const size_t);
73         size_t recv_event (uint32_t, double, uint64_t &, uint8_t *, size_t &);
74
75         uint32_t n_midi_inputs (void) const { return _n_midi_in; }
76         uint32_t n_midi_outputs (void) const { return _n_midi_out; }
77         std::string port_name (uint32_t, bool input);
78
79         void notify_proc (const MIDINotification *message);
80
81         void set_enabled (bool yn = true) { _enabled = yn; }
82         bool enabled (void) const { return _active && _enabled; }
83
84         void set_port_changed_callback (void (changed_callback (void*)), void *arg) {
85                 _changed_callback = changed_callback;
86                 _changed_arg = arg;
87         }
88
89 private:
90         void discover ();
91         void cleanup ();
92
93         MIDIClientRef     _midi_client;
94         MIDIEndpointRef * _input_endpoints;
95         MIDIEndpointRef * _output_endpoints;
96         MIDIPortRef     * _input_ports;
97         MIDIPortRef     * _output_ports;
98         CoreMIDIQueue   * _input_queue;
99
100         RingBuffer<uint8_t> ** _rb;
101
102         uint32_t          _n_midi_in;
103         uint32_t          _n_midi_out;
104
105         MIDITimeStamp     _time_at_cycle_start;
106         bool              _active; // internal deactivate during discovery etc
107         bool              _enabled; // temporary disable, e.g. during freewheeli
108         bool              _run; // general status
109
110         void (* _changed_callback) (void*);
111         void  * _changed_arg;
112
113         pthread_mutex_t _discovery_lock;
114 };