Ardour  9.0-pre0-582-g084a23a80d
coremidi_io.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2017 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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __libbackend_coremidi_io_h__
20 #define __libbackend_coremidi_io_h__
21 
22 #include <CoreServices/CoreServices.h>
23 #include <CoreAudio/CoreAudio.h>
24 #include <AudioUnit/AudioUnit.h>
25 
26 #include <AudioUnit/AudioUnit.h>
27 #include <AudioToolbox/AudioToolbox.h>
28 
29 #include <map>
30 #include <memory>
31 #include <vector>
32 #include <string>
33 
34 #include "pbd/ringbuffer.h"
35 
36 namespace ARDOUR {
37 
38 typedef struct _CoreMIDIPacket {
39  MIDITimeStamp timeStamp;
40  UInt16 length;
41  Byte data[1024];
42 #if 0 // unused
43  _CoreMIDIPacket (MIDITimeStamp t, Byte *d, UInt16 l)
44  : timeStamp(t)
45  , length (l)
46  {
47  if (l > 256) {
48  length = 256;
49  }
50  if (length > 0) {
51  memcpy(data, d, length);
52  }
53  }
54 #endif
55  _CoreMIDIPacket (const MIDIPacket *other)
56  : timeStamp(other->timeStamp)
57  , length (other->length)
58  {
59  assert (length <= 1024);
60  if (length > 0) {
61  memcpy(data, other->data, length);
62  }
63  }
65 
66 typedef std::vector<std::shared_ptr<CoreMIDIPacket> > CoreMIDIQueue;
67 
68 class CoreMidiIo {
69 public:
70  CoreMidiIo (void);
71  ~CoreMidiIo (void);
72 
73  void start ();
74  void stop ();
75 
76  void start_cycle (MIDITimeStamp, double cycle_ns);
77 
78  int send_event (uint32_t, double, const uint8_t *, const size_t);
79  size_t recv_event (uint32_t, uint64_t &, uint8_t *, size_t &);
80 
81  uint32_t n_midi_inputs (void) const { return _n_midi_in; }
82  uint32_t n_midi_outputs (void) const { return _n_midi_out; }
83  std::string port_id (uint32_t, bool input);
84  std::string port_name (uint32_t, bool input);
85 
86  void notify_proc (const MIDINotification *message);
87 
88  void set_enabled (bool yn = true) { _enabled = yn; }
89  bool enabled (void) const { return _active && _enabled; }
90 
91  void set_port_changed_callback (void (changed_callback (void*)), void *arg) {
92  _changed_callback = changed_callback;
93  _changed_arg = arg;
94  }
95 
96 private:
97  void discover ();
98  void cleanup ();
99 
100  MIDIClientRef _midi_client;
101  MIDIEndpointRef * _input_endpoints;
102  MIDIEndpointRef * _output_endpoints;
103  MIDIPortRef * _input_ports;
104  MIDIPortRef * _output_ports;
106 
108 
109  uint32_t _n_midi_in;
110  uint32_t _n_midi_out;
111 
112  MIDITimeStamp _send_start;
113  MIDITimeStamp _recv_start;
114  MIDITimeStamp _recv_end;
115  bool _active; // internal deactivate during discovery etc
116  bool _enabled; // temporary disable, e.g. during freewheeli
117  bool _run; // general status
118 
119  void (* _changed_callback) (void*);
120  void * _changed_arg;
121 
122  pthread_mutex_t _discovery_lock;
123 };
124 
125 } // namespace
126 
127 #endif /* __libbackend_coremidi_io */
PBD::RingBuffer< uint8_t > ** _rb
Definition: coremidi_io.h:107
bool enabled(void) const
Definition: coremidi_io.h:89
std::string port_id(uint32_t, bool input)
int send_event(uint32_t, double, const uint8_t *, const size_t)
MIDIEndpointRef * _input_endpoints
Definition: coremidi_io.h:101
void set_port_changed_callback(void(changed_callback(void *)), void *arg)
Definition: coremidi_io.h:91
MIDITimeStamp _recv_end
Definition: coremidi_io.h:114
MIDIClientRef _midi_client
Definition: coremidi_io.h:100
MIDIPortRef * _input_ports
Definition: coremidi_io.h:103
CoreMIDIQueue * _input_queue
Definition: coremidi_io.h:105
MIDIEndpointRef * _output_endpoints
Definition: coremidi_io.h:102
void start_cycle(MIDITimeStamp, double cycle_ns)
pthread_mutex_t _discovery_lock
Definition: coremidi_io.h:122
uint32_t n_midi_inputs(void) const
Definition: coremidi_io.h:81
void notify_proc(const MIDINotification *message)
std::string port_name(uint32_t, bool input)
uint32_t n_midi_outputs(void) const
Definition: coremidi_io.h:82
size_t recv_event(uint32_t, uint64_t &, uint8_t *, size_t &)
MIDITimeStamp _send_start
Definition: coremidi_io.h:112
void(* _changed_callback)(void *)
Definition: coremidi_io.h:119
uint32_t _n_midi_out
Definition: coremidi_io.h:110
MIDIPortRef * _output_ports
Definition: coremidi_io.h:104
void set_enabled(bool yn=true)
Definition: coremidi_io.h:88
MIDITimeStamp _recv_start
Definition: coremidi_io.h:113
std::vector< std::shared_ptr< CoreMIDIPacket > > CoreMIDIQueue
Definition: coremidi_io.h:66
struct ARDOUR::_CoreMIDIPacket CoreMIDIPacket
_CoreMIDIPacket(const MIDIPacket *other)
Definition: coremidi_io.h:55
MIDITimeStamp timeStamp
Definition: coremidi_io.h:39