Merged revisions 6293,6296-6306,6308 via svnmerge from
[ardour.git] / libs / ardour / ardour / audio_unit.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Written by Taybin Rutkin
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_audio_unit_h__
22 #define __ardour_audio_unit_h__
23
24 #include <stdint.h>
25 #include <boost/shared_ptr.hpp>
26
27 #include <list>
28 #include <set>
29 #include <string>
30 #include <vector>
31 #include <map>
32
33 #include <AudioUnit/AudioUnit.h>
34 #include <appleutility/AUParamInfo.h>
35
36 #include <boost/shared_ptr.hpp>
37
38 #include "ardour/plugin.h"
39 #include "ardour/chan_mapping.h"
40
41 class CAComponent;
42 class CAAudioUnit;
43 class CAComponentDescription;
44 struct AudioBufferList;
45
46 namespace ARDOUR {
47
48 class AudioEngine;
49 class Session;
50
51 struct AUParameterDescriptor : public Plugin::ParameterDescriptor {
52         // additional fields to make operations more efficient
53         AudioUnitParameterID id;
54         AudioUnitScope scope;
55         AudioUnitElement element;
56         float default_value;
57         bool automatable;
58         AudioUnitParameterUnit unit;
59 };
60
61 class AUPlugin : public ARDOUR::Plugin
62 {
63   public:
64         AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAComponent> comp);
65         AUPlugin (const AUPlugin& other);
66         virtual ~AUPlugin ();
67
68         std::string unique_id () const;
69         const char * label () const;
70         const char * name () const { return _info->name.c_str(); }
71         const char * maker () const { return _info->creator.c_str(); }
72         uint32_t parameter_count () const;
73         float default_value (uint32_t port);
74         nframes_t latency () const;
75         void set_parameter (uint32_t which, float val);
76         float get_parameter (uint32_t which) const;
77
78         int get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
79         uint32_t nth_parameter (uint32_t which, bool& ok) const;
80         void activate ();
81         void deactivate ();
82         void set_block_size (nframes_t nframes);
83
84         int connect_and_run (std::vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset);
85         std::set<uint32_t> automatable() const;
86         std::string describe_parameter (uint32_t);
87         std::string state_node_name () const { return "audiounit"; }
88         void print_parameter (uint32_t, char*, uint32_t len) const;
89
90         bool parameter_is_audio (uint32_t) const;
91         bool parameter_is_control (uint32_t) const;
92         bool parameter_is_input (uint32_t) const;
93         bool parameter_is_output (uint32_t) const;
94
95         XMLNode& get_state();
96         int set_state(const XMLNode& node);
97
98         bool save_preset (std::string name);
99         bool load_preset (const std::string preset_label);
100         std::vector<std::string> get_presets ();
101         std::string current_preset() const;
102
103         bool has_editor () const;
104
105         int32_t can_do (int32_t in, int32_t& out);
106         ChanCount output_streams() const;
107         ChanCount input_streams() const;
108         int32_t configure_io (int32_t in, int32_t out);
109
110         boost::shared_ptr<CAAudioUnit> get_au () { return unit; }
111         boost::shared_ptr<CAComponent> get_comp () const { return comp; }
112
113         OSStatus render_callback(AudioUnitRenderActionFlags *ioActionFlags,
114                         const AudioTimeStamp *inTimeStamp,
115                         UInt32                inBusNumber,
116                         UInt32                inNumberFrames,
117                         AudioBufferList*      ioData);
118   private:
119         boost::shared_ptr<CAComponent> comp;
120         boost::shared_ptr<CAAudioUnit> unit;
121
122         bool initialized;
123         ChanCount input_channels;
124         ChanCount output_channels;
125         std::vector<std::pair<int,int> > io_configs;
126         AudioBufferList* buffers;
127
128         /* XXX this should really be shared across all AUPlugin instances */
129
130         typedef std::map<std::string,std::string> PresetMap;
131         PresetMap preset_map;
132
133         UInt32 global_elements;
134         UInt32 output_elements;
135         UInt32 input_elements;
136
137         int set_output_format (AudioStreamBasicDescription&);
138         int set_input_format (AudioStreamBasicDescription&);
139         int set_stream_format (int scope, uint32_t cnt, AudioStreamBasicDescription&);
140         int _set_block_size (nframes_t nframes);
141         void discover_parameters ();
142
143         std::vector<std::pair<uint32_t, uint32_t> > parameter_map;
144         uint32_t current_maxbuf;
145         nframes_t current_offset;
146         nframes_t cb_offset;
147         std::vector<Sample*>* current_buffers;
148         nframes_t frames_processed;
149
150         std::vector<AUParameterDescriptor> descriptors;
151         void init ();
152 };
153
154 typedef boost::shared_ptr<AUPlugin> AUPluginPtr;
155
156 struct AUPluginCachedInfo {
157         std::vector<std::pair<int,int> > io_configs;
158 };
159
160 class AUPluginInfo : public PluginInfo {
161   public:
162         AUPluginInfo (boost::shared_ptr<CAComponentDescription>);
163         ~AUPluginInfo ();
164
165         PluginPtr load (Session& session);
166
167         AUPluginCachedInfo cache;
168
169         static PluginInfoList* discover ();
170         static void get_names (CAComponentDescription&, std::string& name, Glib::ustring& maker);
171         static std::string stringify_descriptor (const CAComponentDescription&);
172
173         static int load_cached_info ();
174
175   private:
176         boost::shared_ptr<CAComponentDescription> descriptor;
177         UInt32 version;
178
179         static void discover_music (PluginInfoList&);
180         static void discover_fx (PluginInfoList&);
181         static void discover_generators (PluginInfoList&);
182         static void discover_by_description (PluginInfoList&, CAComponentDescription&);
183         static Glib::ustring au_cache_path ();
184
185         typedef std::map<std::string,AUPluginCachedInfo> CachedInfoMap;
186         static CachedInfoMap cached_info;
187
188         static bool cached_io_configuration (const std::string&, UInt32, CAComponent&, AUPluginCachedInfo&, const std::string& name);
189         static void add_cached_info (const std::string&, AUPluginCachedInfo&);
190         static void save_cached_info ();
191 };
192
193 typedef boost::shared_ptr<AUPluginInfo> AUPluginInfoPtr;
194
195 } // namespace ARDOUR
196
197 #endif // __ardour_audio_unit_h__