Ardour  9.0-pre0-582-g084a23a80d
audio_unit.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
24 #include <atomic>
25 #include <cstdint>
26 #include <list>
27 #include <map>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 #include "ardour/plugin.h"
34 
35 #include <AudioUnit/AudioUnit.h>
36 #include <AudioUnit/AudioUnitProperties.h>
37 #include "AUParamInfo.h"
38 
39 
40 class CAComponent;
41 class CAAudioUnit;
42 class CAComponentDescription;
43 struct AudioBufferList;
44 
45 namespace ARDOUR {
46 
47 class AudioEngine;
48 class Session;
49 
51  // additional fields to make operations more efficient
52  AudioUnitParameterID id;
53  AudioUnitScope scope;
54  AudioUnitElement element;
56  AudioUnitParameterUnit au_unit;
57 };
58 
60 {
61  public:
62  AUPlugin (AudioEngine& engine, Session& session, std::shared_ptr<CAComponent> comp);
63  AUPlugin (const AUPlugin& other);
64  virtual ~AUPlugin ();
65 
66  std::string unique_id () const;
67  const char * label () const;
68  const char * name () const { return _info->name.c_str(); }
69  const char * maker () const { return _info->creator.c_str(); }
70  uint32_t parameter_count () const;
71  float default_value (uint32_t port);
72  void set_parameter (uint32_t which, float val, sampleoffset_t);
73  float get_parameter (uint32_t which) const;
74 
75  PluginOutputConfiguration possible_output () const { return _output_configs; }
76 
77  int get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
78  uint32_t nth_parameter (uint32_t which, bool& ok) const;
79  void activate ();
80  void deactivate ();
81  void flush ();
82  int set_block_size (pframes_t nframes);
83  void set_non_realtime (bool);
84 
86  samplepos_t start, samplepos_t end, double speed,
87  ChanMapping const& in, ChanMapping const& out,
88  pframes_t nframes, samplecnt_t offset);
89  std::set<Evoral::Parameter> automatable() const;
91  IOPortDescription describe_io_port (DataType dt, bool input, uint32_t id) const;
92  std::string state_node_name () const { return "audiounit"; }
93 
94  bool parameter_is_audio (uint32_t) const;
95  bool parameter_is_control (uint32_t) const;
96  bool parameter_is_input (uint32_t) const;
97  bool parameter_is_output (uint32_t) const;
98 
100 
101  int set_state(const XMLNode& node, int);
102 
104  std::string current_preset() const;
105 
106  bool has_editor () const;
107 
108  bool match_variable_io (ChanCount& in, ChanCount& aux_in, ChanCount& out);
109  bool reconfigure_io (ChanCount in, ChanCount aux_in, ChanCount out);
110 
114 
115  void set_fixed_size_buffers (bool yn) {
116  _requires_fixed_size_buffers = yn;
117  }
118 
119  std::shared_ptr<CAAudioUnit> get_au () { return unit; }
120  std::shared_ptr<CAComponent> get_comp () const { return comp; }
121 
122  OSStatus render_callback(AudioUnitRenderActionFlags *ioActionFlags,
123  const AudioTimeStamp *inTimeStamp,
124  UInt32 inBusNumber,
125  UInt32 inNumberSamples,
126  AudioBufferList* ioData);
127 
128  /* "host" callbacks */
129 
130  OSStatus get_beat_and_tempo_callback (Float64* outCurrentBeat,
131  Float64* outCurrentTempo);
132 
133  OSStatus get_musical_time_location_callback (UInt32* outDeltaSampleOffsetToNextBeat,
134  Float32* outTimeSig_Numerator,
135  UInt32* outTimeSig_Denominator,
136  Float64* outCurrentMeasureDownBeat);
137 
138  OSStatus get_transport_state_callback (Boolean* outIsPlaying,
139  Boolean* outTransportStateChanged,
140  Float64* outCurrentSampleInTimeLine,
141  Boolean* outIsCycling,
142  Float64* outCycleStartBeat,
143  Float64* outCycleEndBeat);
144 
145  /* this MUST be called from thread in which you want to receive notifications
146  * about parameter changes.
147  */
148  int create_parameter_listener (AUEventListenerProc callback, void *arg, float interval_secs);
149 
150  /* these can be called from any thread but SHOULD be called from the same thread
151  * that will receive parameter change notifications.
152  */
153  int listen_to_parameter (uint32_t param_id);
154  int end_listen_to_parameter (uint32_t param_id);
155 
156 
157  protected:
158  std::string do_save_preset (std::string name);
159  void do_remove_preset (std::string);
160 
161  private:
163  void find_presets ();
164 
165  std::shared_ptr<CAComponent> comp;
166  std::shared_ptr<CAAudioUnit> unit;
167 
170  int32_t input_channels;
172  std::vector<std::pair<int,int> > io_configs;
174  mutable std::atomic<unsigned int> _current_latency;
176  AudioBufferList* buffers;
180 
181  /* despite all the cool work that apple did on their AU preset
182  system, they left factory presets and user presets as two
183  entirely different kinds of things, handled by two entirely
184  different parts of the API. Resolve this.
185  */
186 
187  /* XXX these two maps should really be shared across all instances of this AUPlugin */
188 
189  typedef std::map<std::string,std::string> UserPresetMap;
191  typedef std::map<std::string,int> FactoryPresetMap;
193 
197 
200 
203 
204  uint32_t *bus_inputs;
205  uint32_t *bus_inused;
206  uint32_t *bus_outputs;
207  std::vector <std::string> _bus_name_in;
208  std::vector <std::string> _bus_name_out;
209 
210  int set_stream_format (int scope, uint32_t bus, AudioStreamBasicDescription&);
212  void add_state (XMLNode *) const;
213 
214  typedef std::map<uint32_t, uint32_t> ParameterMap;
216  uint32_t input_maxbuf;
222 
223  std::vector<AUParameterDescriptor> descriptors;
224  AUEventListenerRef _parameter_listener;
226  void init ();
227 
229 
234 
235  static void _parameter_change_listener (void* /*arg*/, void* /*src*/, const AudioUnitEvent* event, UInt64 host_time, Float32 new_value);
236  void parameter_change_listener (void* /*arg*/, void* /*src*/, const AudioUnitEvent* event, UInt64 host_time, Float32 new_value);
237 };
238 
240 public:
241  AUPluginInfo (std::shared_ptr<CAComponentDescription>);
243 
245 
246  std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;
247 
248  bool needs_midi_input () const;
251 
252  /* note: AU's have an explicit way to prompt for instrument/fx category */
253  bool is_effect () const;
254  bool is_instrument () const;
255  bool is_utility () const;
256 
257  bool reconfigurable_io() const { return true; }
258  uint32_t max_configurable_outputs () const { return max_outputs; }
259 
260  UInt32 version;
261  uint32_t max_outputs;
262  std::vector<std::pair<int,int> > io_configs;
263 
264  static std::string convert_old_unique_id (std::string const&);
265 
266 private:
267  std::shared_ptr<CAComponentDescription> descriptor;
268 };
269 
270 typedef std::shared_ptr<AUPluginInfo> AUPluginInfoPtr;
271 
272 } // namespace ARDOUR
273 
bool is_utility() const
bool needs_midi_input() const
bool is_effect_without_midi_input() const
std::shared_ptr< CAComponentDescription > descriptor
Definition: audio_unit.h:267
bool reconfigurable_io() const
Definition: audio_unit.h:257
bool is_instrument() const
bool is_effect() const
std::vector< std::pair< int, int > > io_configs
Definition: audio_unit.h:262
AUPluginInfo(std::shared_ptr< CAComponentDescription >)
static std::string convert_old_unique_id(std::string const &)
bool is_effect_with_midi_input() const
uint32_t max_configurable_outputs() const
Definition: audio_unit.h:258
std::vector< Plugin::PresetRecord > get_presets(bool user_only) const
PluginPtr load(Session &session)
const char * label() const
bool parameter_is_control(uint32_t) const
std::string unique_id() const
uint32_t * bus_outputs
Definition: audio_unit.h:206
void set_parameter(uint32_t which, float val, sampleoffset_t)
void parameter_change_listener(void *, void *, const AudioUnitEvent *event, UInt64 host_time, Float32 new_value)
bool _requires_fixed_size_buffers
Definition: audio_unit.h:175
void set_fixed_size_buffers(bool yn)
Definition: audio_unit.h:115
void set_info(PluginInfoPtr)
samplepos_t transport_sample
Definition: audio_unit.h:230
uint32_t configured_input_busses
Definition: audio_unit.h:201
bool requires_fixed_size_buffers() const
void set_non_realtime(bool)
int connect_and_run(BufferSet &bufs, samplepos_t start, samplepos_t end, double speed, ChanMapping const &in, ChanMapping const &out, pframes_t nframes, samplecnt_t offset)
std::string describe_parameter(Evoral::Parameter)
float default_value(uint32_t port)
static void _parameter_change_listener(void *, void *, const AudioUnitEvent *event, UInt64 host_time, Float32 new_value)
const char * maker() const
Definition: audio_unit.h:69
samplecnt_t plugin_latency() const
FactoryPresetMap factory_preset_map
Definition: audio_unit.h:192
std::shared_ptr< CAComponent > comp
Definition: audio_unit.h:165
samplecnt_t input_offset
Definition: audio_unit.h:217
int32_t input_channels
Definition: audio_unit.h:170
std::vector< AUParameterDescriptor > descriptors
Definition: audio_unit.h:223
PluginOutputConfiguration _output_configs
Definition: audio_unit.h:179
UserPresetMap user_preset_map
Definition: audio_unit.h:190
ChanMapping const * input_map
Definition: audio_unit.h:220
int set_block_size(pframes_t nframes)
std::string do_save_preset(std::string name)
IOPortDescription describe_io_port(DataType dt, bool input, uint32_t id) const
bool parameter_is_input(uint32_t) const
ChanCount output_streams() const
uint32_t configured_output_busses
Definition: audio_unit.h:202
AUPlugin(const AUPlugin &other)
std::shared_ptr< CAComponent > get_comp() const
Definition: audio_unit.h:120
uint32_t * bus_inputs
Definition: audio_unit.h:204
void discover_parameters()
BufferSet * input_buffers
Definition: audio_unit.h:219
OSStatus get_transport_state_callback(Boolean *outIsPlaying, Boolean *outTransportStateChanged, Float64 *outCurrentSampleInTimeLine, Boolean *outIsCycling, Float64 *outCycleStartBeat, Float64 *outCycleEndBeat)
void add_state(XMLNode *) const
bool match_variable_io(ChanCount &in, ChanCount &aux_in, ChanCount &out)
std::vector< std::pair< int, int > > io_configs
Definition: audio_unit.h:172
bool load_preset(PresetRecord)
UInt32 global_elements
Definition: audio_unit.h:194
void discover_factory_presets()
uint32_t nth_parameter(uint32_t which, bool &ok) const
samplecnt_t _last_nframes
Definition: audio_unit.h:173
int get_parameter_descriptor(uint32_t which, ParameterDescriptor &) const
void do_remove_preset(std::string)
int set_stream_format(int scope, uint32_t bus, AudioStreamBasicDescription &)
AUPlugin(AudioEngine &engine, Session &session, std::shared_ptr< CAComponent > comp)
int listen_to_parameter(uint32_t param_id)
OSStatus get_musical_time_location_callback(UInt32 *outDeltaSampleOffsetToNextBeat, Float32 *outTimeSig_Numerator, UInt32 *outTimeSig_Denominator, Float64 *outCurrentMeasureDownBeat)
pframes_t preset_holdoff
Definition: audio_unit.h:233
samplecnt_t * cb_offsets
Definition: audio_unit.h:218
PluginOutputConfiguration possible_output() const
Definition: audio_unit.h:75
uint32_t parameter_count() const
uint32_t input_maxbuf
Definition: audio_unit.h:216
uint32_t * bus_inused
Definition: audio_unit.h:205
std::map< std::string, int > FactoryPresetMap
Definition: audio_unit.h:191
int end_listen_to_parameter(uint32_t param_id)
std::map< uint32_t, uint32_t > ParameterMap
Definition: audio_unit.h:214
std::vector< std::string > _bus_name_in
Definition: audio_unit.h:207
bool parameter_is_output(uint32_t) const
bool reconfigure_io(ChanCount in, ChanCount aux_in, ChanCount out)
bool parameter_is_audio(uint32_t) const
UInt32 input_elements
Definition: audio_unit.h:196
std::set< Evoral::Parameter > automatable() const
ParameterMap parameter_map
Definition: audio_unit.h:215
OSStatus render_callback(AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberSamples, AudioBufferList *ioData)
bool has_editor() const
UInt32 output_elements
Definition: audio_unit.h:195
float last_transport_speed
Definition: audio_unit.h:232
samplecnt_t samples_processed
Definition: audio_unit.h:221
int set_state(const XMLNode &node, int)
void * _parameter_listener_arg
Definition: audio_unit.h:225
std::shared_ptr< CAAudioUnit > get_au()
Definition: audio_unit.h:119
ChanCount input_streams() const
OSStatus get_beat_and_tempo_callback(Float64 *outCurrentBeat, Float64 *outCurrentTempo)
std::string state_node_name() const
Definition: audio_unit.h:92
float get_parameter(uint32_t which) const
AUEventListenerRef _parameter_listener
Definition: audio_unit.h:224
std::map< std::string, std::string > UserPresetMap
Definition: audio_unit.h:189
std::vector< std::string > _bus_name_out
Definition: audio_unit.h:208
std::atomic< unsigned int > _current_latency
Definition: audio_unit.h:174
int32_t output_channels
Definition: audio_unit.h:171
std::string current_preset() const
std::shared_ptr< CAAudioUnit > unit
Definition: audio_unit.h:166
float transport_speed
Definition: audio_unit.h:231
virtual ~AUPlugin()
AudioBufferList * buffers
Definition: audio_unit.h:176
const char * name() const
Definition: audio_unit.h:68
int create_parameter_listener(AUEventListenerProc callback, void *arg, float interval_secs)
Definition: xml++.h:114
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBARDOUR_API
void session(lua_State *L)
PBD::PropertyDescriptor< timepos_t > start
uint32_t pframes_t
std::shared_ptr< Plugin > PluginPtr
Definition: plugin.h:62
Temporal::samplecnt_t samplecnt_t
Temporal::sampleoffset_t sampleoffset_t
Temporal::samplepos_t samplepos_t
std::shared_ptr< AUPluginInfo > AUPluginInfoPtr
Definition: audio_unit.h:270
std::shared_ptr< PluginInfo > PluginInfoPtr
Definition: plugin.h:65
std::set< uint32_t > PluginOutputConfiguration
Definition: plugin.h:67
DebugBits AudioEngine
Definition: session.h:1349
AudioUnitParameterUnit au_unit
Definition: audio_unit.h:56
AudioUnitParameterID id
Definition: audio_unit.h:52
AudioUnitElement element
Definition: audio_unit.h:54