-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExampleAudioUnit.m
More file actions
122 lines (95 loc) · 3.57 KB
/
ExampleAudioUnit.m
File metadata and controls
122 lines (95 loc) · 3.57 KB
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
//
// ExampleAudioUnit.m
// AudioUnitExample
//
// Created by Lucius Kwok on 11/28/10.
// Copyright 2010 Felt Tip Inc. All rights reserved.
//
#import "ExampleAudioUnit.h"
#import <CoreAudioKit/CoreAudioKit.h>
#import <AudioUnit/AUCocoaUIView.h>
@implementation ExampleAudioUnit
- (id)initWithAudioUnit:(AudioUnit)anAudioUnit {
self = [super init];
if (self) {
audioUnit = anAudioUnit;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (NSData*)dataForProperty:(AudioUnitPropertyID)property scope:(AudioUnitScope)scope element: (AudioUnitElement)element
{
NSAssert(audioUnit != nil, @"audioUnit should not be nil");
ComponentResult err;
UInt32 dataSize;
Boolean writable;
err = AudioUnitGetPropertyInfo (audioUnit, property, scope, element, &dataSize, &writable);
if (err != noErr)
return nil;
NSMutableData *data = [NSMutableData dataWithLength:dataSize];
err = AudioUnitGetProperty (audioUnit, property, scope, element, [data mutableBytes], &dataSize);
NSAssert1 (err == noErr, @"AudioUnitGetProperty() error: %d", err);
[data setLength: dataSize];
return data;
}
- (BOOL)plugInClassIsValid:(Class) pluginClass {
if ([pluginClass conformsToProtocol:@protocol(AUCocoaUIBase)]) {
if ([pluginClass instancesRespondToSelector:@selector(interfaceVersion)] &&
[pluginClass instancesRespondToSelector:@selector(uiViewForAudioUnit:withSize:)]) {
return YES;
}
}
return NO;
}
- (NSView *)customCocoaUIWithViewSize:(NSSize)viewSize {
NSData *infoData = [self dataForProperty:kAudioUnitProperty_CocoaUI scope:kAudioUnitScope_Global element:0];
if (infoData == nil)
return nil;
const AudioUnitCocoaViewInfo *info = [infoData bytes];
NSURL *cocoaViewPath = (NSURL*)info->mCocoaAUViewBundleLocation;
if (cocoaViewPath == nil)
return nil;
NSString *factoryClassName = (NSString *)info->mCocoaAUViewClass[0];
if (factoryClassName == nil)
return nil;
NSBundle *viewBundle = [NSBundle bundleWithPath:[cocoaViewPath path]];
NSAssert (viewBundle != nil, @"Error loading AU view's bundle");
Class factoryClass = [viewBundle classNamed:factoryClassName];
NSAssert (factoryClass != nil, @"Error getting AU view's factory class from bundle");
// make sure 'factoryClass' implements the AUCocoaUIBase protocol
NSAssert ([self plugInClassIsValid:factoryClass], @"AU view's factory class does not properly implement the AUCocoaUIBase protocol");
// make a factory
id factoryInstance = [[[factoryClass alloc] init] autorelease];
NSAssert (factoryInstance != nil, @"Could not create an instance of the AU view factory");
// make a view
NSView *result = [factoryInstance uiViewForAudioUnit:audioUnit withSize:viewSize];
// clean up
[cocoaViewPath release];
NSUInteger i;
NSUInteger count = ([infoData length] - sizeof(CFURLRef)) / sizeof(CFStringRef);
for (i = 0; i < count; i++)
CFRelease(info->mCocoaAUViewClass[i]);
return result;
}
- (NSView *)genericCocoaUI {
AUGenericView *view = nil;
// Check that class is available if CoreAudioKit was weak-linked.
if (objc_getClass ("AUGenericView") != nil) {
view = [[[AUGenericView alloc] initWithAudioUnit: audioUnit] autorelease];
[view setShowsExpertParameters:NO];
}
return view;
}
- (NSView *)cocoaViewWithSize:(NSSize)viewSize {
NSView *customView = [self customCocoaUIWithViewSize:viewSize];
if (customView == nil) {
customView = [self genericCocoaUI];
}
NSAssert (customView != nil, @"Could not get a Cocoa UI view.");
[customView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
customView.frame = NSMakeRect(0, 0, viewSize.width, viewSize.height);
return customView;
}
@end