*** NEW CODING POLICY ***
[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
32 #include "ardour/plugin.h"
33
34 #include <AudioUnit/AudioUnit.h>
35 #include <appleutility/AUParamInfo.h>
36
37 #include <boost/shared_ptr.hpp>
38
39 class CAComponent;
40 class CAAudioUnit;
41 class CAComponentDescription;
42 struct AudioBufferList;
43
44 namespace ARDOUR {
45
46 class AudioEngine;
47 class Session;
48
49 struct AUParameterDescriptor : public Plugin::ParameterDescriptor {
50         // additional fields to make operations more efficient
51         AudioUnitParameterID id;
52         AudioUnitScope scope;
53         AudioUnitElement element;
54         float default_value;
55         bool automatable;
56         AudioUnitParameterUnit unit;
57 };
58
59 class AUPlugin : public ARDOUR::Plugin
60 {
61   public:
62         AUPlugin (AudioEngine& engine, Session& session, boost::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         nframes_t signal_latency () const;
73         void set_parameter (uint32_t which, float val);
74         float get_parameter (uint32_t which) const;
75     
76         int get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
77         uint32_t nth_parameter (uint32_t which, bool& ok) const;
78         void activate ();
79         void deactivate ();
80         void set_block_size (nframes_t nframes);
81     
82         int connect_and_run (BufferSet& bufs, uint32_t& in, uint32_t& out, nframes_t nframes, nframes_t offset);
83         
84         std::set<uint32_t> automatable() const;
85         string describe_parameter (uint32_t);
86         string state_node_name () const { return "audiounit"; }
87         void print_parameter (uint32_t, char*, uint32_t len) const;
88     
89         bool parameter_is_audio (uint32_t) const;
90         bool parameter_is_control (uint32_t) const;
91         bool parameter_is_input (uint32_t) const;
92         bool parameter_is_output (uint32_t) const;
93     
94         XMLNode& get_state();
95         int set_state(const XMLNode& node);
96         
97         bool save_preset (string name);
98         bool load_preset (const string preset_label);
99         std::vector<std::string> get_presets ();
100     
101         bool has_editor () const;
102         
103         bool reconfigurable_io() const { return true; }
104         bool can_support_io_configuration (const ChanCount& in, ChanCount& out) const;
105         int32_t count_for_configuration (const ChanCount& in, ChanCount out) const;
106         bool configure_io (ChanCount in, ChanCount& out);
107         ChanCount output_streams() const;
108         ChanCount input_streams() const;
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         int32_t input_channels;
124         int32_t output_channels;
125         std::vector<std::pair<int,int> > io_configs;
126         AudioBufferList* buffers;
127         
128         UInt32 global_elements;
129         UInt32 output_elements;
130         UInt32 input_elements;
131         
132         int set_output_format (AudioStreamBasicDescription&);
133         int set_input_format (AudioStreamBasicDescription&);
134         int set_stream_format (int scope, uint32_t cnt, AudioStreamBasicDescription&);
135         int _set_block_size (nframes_t nframes);
136         void discover_parameters ();
137
138         std::vector<std::pair<uint32_t, uint32_t> > parameter_map;
139         uint32_t current_maxbuf;
140         nframes_t current_offset;
141         nframes_t cb_offset;
142         vector<Sample*>* current_buffers;
143         nframes_t frames_processed;
144         
145         std::vector<AUParameterDescriptor> descriptors;
146         void init ();
147
148 };
149         
150 typedef boost::shared_ptr<AUPlugin> AUPluginPtr;
151
152 struct AUPluginCachedInfo { 
153         std::vector<std::pair<int,int> > io_configs;
154 };
155
156 class AUPluginInfo : public PluginInfo {
157   public:       
158          AUPluginInfo (boost::shared_ptr<CAComponentDescription>);
159         ~AUPluginInfo ();
160
161         PluginPtr load (Session& session);
162
163         AUPluginCachedInfo cache;
164
165         static PluginInfoList discover ();
166         static void get_names (CAComponentDescription&, std::string& name, Glib::ustring& maker);
167         static std::string stringify_descriptor (const CAComponentDescription&);
168
169         static int load_cached_info ();
170
171   private:
172         boost::shared_ptr<CAComponentDescription> descriptor;
173         UInt32 version;
174
175         static void discover_music (PluginInfoList&);
176         static void discover_fx (PluginInfoList&);
177         static void discover_by_description (PluginInfoList&, CAComponentDescription&);
178         static Glib::ustring au_cache_path ();
179
180         typedef std::map<std::string,AUPluginCachedInfo> CachedInfoMap;
181         static CachedInfoMap cached_info;
182         
183         static bool cached_io_configuration (const std::string&, UInt32, CAComponent&, AUPluginCachedInfo&, const std::string& name);
184         static void add_cached_info (const std::string&, AUPluginCachedInfo&);
185         static void save_cached_info ();
186 };
187
188 typedef boost::shared_ptr<AUPluginInfo> AUPluginInfoPtr;
189
190 } // namespace ARDOUR
191
192 #endif // __ardour_audio_unit_h__