summaryrefslogtreecommitdiff
path: root/libs/ardour/audio_unit.cc
blob: 6879123ff1a24398d14a89293b49bdd36361af20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
    Copyright (C) 2006 Paul Davis 
	Written by Taybin Rutkin
	
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <pbd/transmitter.h>
#include <pbd/xml++.h>

#include <ardour/audioengine.h>
#include <ardour/audio_unit.h>
#include <ardour/session.h>
#include <ardour/utils.h>

#include <appleutility/CAAudioUnit.h>

#include <CoreServices/CoreServices.h>
#include <AudioUnit/AudioUnit.h>

#include "i18n.h"

using namespace std;
using namespace PBD;
using namespace ARDOUR;

AUPlugin::AUPlugin (AudioEngine& engine, Session& session, CAComponent* _comp)
	:
	Plugin (engine, session),
	comp (_comp),
	unit (new CAAudioUnit)
{			
	OSErr err = CAAudioUnit::Open (*comp, *unit);
	if (err != noErr) {
		error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
		delete unit;
		delete comp;
		throw failed_constructor ();
	}
	
	unit->Initialize ();
}

AUPlugin::~AUPlugin ()
{
	if (unit) {
		unit->Uninitialize ();
		delete unit;
	}
	
	if (comp) {
		delete comp;
	}
	
	if (in_list) {
		delete in_list;
	}
	
	if (out_list) {
		delete out_list;
	}
}

AUPluginInfo::~AUPluginInfo ()
{
	if (desc) {
		delete desc;
	}
}

uint32_t
AUPlugin::unique_id () const
{
	return 0;
}

const char *
AUPlugin::label () const
{
	return "AUPlugin label";
}

const char *
AUPlugin::maker () const
{
	return "AUplugin maker";
}

uint32_t
AUPlugin::parameter_count () const
{
	return 0;
}

float
AUPlugin::default_value (uint32_t port)
{
	// AudioUnits don't have default values.  Maybe presets though?
	return 0;
}

nframes_t
AUPlugin::latency () const
{
	return unit->Latency ();
}

void
AUPlugin::set_parameter (uint32_t which, float val)
{
	unit->SetParameter (parameter_map[which].first, parameter_map[which].second, 0, val);
}

float
AUPlugin::get_parameter (uint32_t which) const
{
	float outValue = 0.0;
	
	unit->GetParameter(parameter_map[which].first, parameter_map[which].second, 0, outValue);
	
	return outValue;
}

int
AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const
{
	return 0;
}

uint32_t
AUPlugin::nth_parameter (uint32_t which, bool& ok) const
{
	return 0;
}

void
AUPlugin::activate ()
{
	unit->GlobalReset ();
}

void
AUPlugin::deactivate ()
{
	// not needed.  GlobalReset () takes care of it.
}

void
AUPlugin::set_block_size (nframes_t nframes)
{
	
}

int
AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
{
	AudioUnitRenderActionFlags flags = 0;
	AudioTimeStamp ts;
	
	AudioBufferList abl;
	abl.mNumberBuffers = 1;
	abl.mBuffers[0].mNumberChannels = 1;
	abl.mBuffers[0].mDataByteSize = nframes * sizeof(Sample);
	abl.mBuffers[0].mData = &bufs[0];
	
	
	unit->Render (&flags, &ts, 0, 0, &abl);
	
	return 0;
}

set<uint32_t>
AUPlugin::automatable() const
{
	set<uint32_t> automates;
	
	return automates;
}

string
AUPlugin::describe_parameter (uint32_t)
{
	return "";
}

void
AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
{
	
}

bool
AUPlugin::parameter_is_audio (uint32_t) const
{
	return false;
}

bool
AUPlugin::parameter_is_control (uint32_t) const
{
	return false;
}

bool
AUPlugin::parameter_is_input (uint32_t) const
{
	return false;
}

bool
AUPlugin::parameter_is_output (uint32_t) const
{
	return false;
}

XMLNode&
AUPlugin::get_state()
{
	XMLNode* root = new XMLNode (state_node_name());
	
	return *root;
}

int
AUPlugin::set_state(const XMLNode& node)
{
	return -1;
}

bool
AUPlugin::save_preset (string name)
{
	return false;
}

bool
AUPlugin::load_preset (const string preset_label)
{
	return false;
}

vector<string>
AUPlugin::get_presets ()
{
	vector<string> presets;
	
	return presets;
}

bool
AUPlugin::has_editor () const
{
	return false;
}

PluginPtr
AUPluginInfo::load (Session& session)
{
	try {
		PluginPtr plugin;

		CAComponent* comp = new CAComponent(*desc);
		
		if (!comp->IsValid()) {
			error << ("AudioUnit: not a valid Component") << endmsg;
		} else {
			plugin.reset (new AUPlugin (session.engine(), session, comp));
		}
		
		plugin->set_info(PluginInfoPtr(new AUPluginInfo(*this)));
		return plugin;
	}

	catch (failed_constructor &err) {
		return PluginPtr ((Plugin*) 0);
	}
}

PluginInfoList
AUPluginInfo::discover ()
{
	PluginInfoList plugs;

	CAComponentDescription desc;
	desc.componentFlags = 0;
	desc.componentFlagsMask = 0;
	desc.componentSubType = 0;
	desc.componentManufacturer = 0;
	desc.componentType = kAudioUnitType_Effect;

	Component comp = 0;

	comp = FindNextComponent (NULL, &desc);
	while (comp != NULL) {
		CAComponentDescription temp;
		GetComponentInfo (comp, &temp, NULL, NULL, NULL);
		
		AUPluginInfoPtr plug(new AUPluginInfo);
		plug->name = AUPluginInfo::get_name (temp);
		plug->type = ARDOUR::AudioUnit;
		plug->n_inputs = 0;
		plug->n_outputs = 0;
		// plug->setup_nchannels (temp);
		plug->category = "AudioUnit";
		plug->desc = new CAComponentDescription(temp);

		plugs.push_back(plug);
		
		comp = FindNextComponent (comp, &desc);
	}

	return plugs;
}

string
AUPluginInfo::get_name (CAComponentDescription& comp_desc)
{
	CFStringRef itemName = NULL;
	// Marc Poirier -style item name
	CAComponent auComponent (comp_desc);
	if (auComponent.IsValid()) {
		CAComponentDescription dummydesc;
		Handle nameHandle = NewHandle(sizeof(void*));
		if (nameHandle != NULL) {
			OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
			if (err == noErr) {
				ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
				if (nameString != NULL) {
					itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
				}
			}
			DisposeHandle(nameHandle);
		}
	}
    
	// if Marc-style fails, do the original way
	if (itemName == NULL) {
		CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
		CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
		CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
    
		itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
			compTypeString, compManufacturerString, compSubTypeString);
    
		if (compTypeString != NULL)
			CFRelease(compTypeString);
		if (compSubTypeString != NULL)
			CFRelease(compSubTypeString);
		if (compManufacturerString != NULL)
			CFRelease(compManufacturerString);
	}
	
	return CFStringRefToStdString(itemName);
}

void
AUPluginInfo::setup_nchannels (CAComponentDescription& comp_desc)
{
	CAAudioUnit unit;
	
	CAAudioUnit::Open (comp_desc, unit);
	
	if (unit.SupportsNumChannels()) {
		n_inputs = n_outputs = 0;
	} else {
		AUChannelInfo cinfo;
		size_t info_size = sizeof(cinfo);
		OSStatus err = AudioUnitGetProperty (unit.AU(), kAudioUnitProperty_SupportedNumChannels, kAudioUnitScope_Global,
                                             0, &cinfo, &info_size);
	}
}