summaryrefslogtreecommitdiff
path: root/distrho/src/CocoaUI/PluginAU_CocoaUI.m
blob: e8e1d7bf133a6a7f4ebdd7518c8ec98ccf260ecd (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
#include <Cocoa/Cocoa.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include <AudioUnit/AUCocoaUIView.h>
#include "DistrhoUIInternal.hpp"

#define MAX_PARAMS 100

START_NAMESPACE_DISTRHO

class UIAu
{
public:
    UIAu(const char* const uiTitle)
        : fUI(this, 0, nullptr, setParameterCallback, setStateCallback, sendNoteCallback, setSizeCallback),
          fHostClosed(false)
    {
        fUI.setWindowTitle(uiTitle);
    }

    ~UIAu()
    {
    }

    void auui_showhide(bool show)
    {
        fUI.setWindowVisible(show);
    }

protected:
    void setParameterValue(const uint32_t rindex, const float value)
    {
        //FIXME fUI.setParameterValue(rindex, value);
    }

    void setState(const char* const key, const char* const value)
    {
    }

    void setSize(const uint width, const uint height)
    {
        fUI.setWindowSize(width, height);
    }

private:
    UIExporter fUI;
    bool fHostClosed;

    // -------------------------------------------------------------------
    // Callbacks

    #define uiPtr ((UIAu*)ptr)

    static void setParameterCallback(void* ptr, uint32_t rindex, float value)
    {
        uiPtr->setParameterValue(rindex, value);
    }

    static void setStateCallback(void* ptr, const char* key, const char* value)
    {
        uiPtr->setState(key, value);
    }

    static void sendNoteCallback(void* ptr, uint8_t channel, uint8_t note, uint8_t velocity)
    {
    }

    static void setSizeCallback(void* ptr, uint width, uint height)
    {
        uiPtr->setSize(width, height);
    }

    #undef uiPtr
};

END_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------

@interface PluginAU_CocoaUI : NSView
{
    AudioUnit mAU;
    AudioUnitParameter mParameter[MAX_PARAMS];
    AUParameterListenerRef mParameterListener;
    UInt32 paramCount;
    UIAu* fUIAu;
}

#pragma mark ____ PUBLIC FUNCTIONS ____
- (void)setAU:(AudioUnit)inAU;

#pragma mark ____ PRIVATE FUNCTIONS
- (void)_synchronizeUIWithParameterValues;
- (void)_addListeners;
- (void)_removeListeners;

#pragma mark ____ LISTENER CALLBACK DISPATCHEE ____
- (void)_parameterListener:(void *)inObject
      parameter:(const AudioUnitParameter *)inParameter
      value:(Float32)inValue;

@end


@implementation PluginAU_CocoaUI

#pragma mark ____ (INIT /) DEALLOC ____
- (void)dealloc {
    [self _removeListeners];
    fUIAu->auui_showhide(false);
    delete fUIAu;
    [super dealloc];
}

#pragma mark ____ PUBLIC FUNCTIONS ____
- (void)setAU:(AudioUnit)inAU {
    // remove previous listeners
    if (mAU) [self _removeListeners];
    mAU = inAU;

    paramCount = 1; //globalUI->dspPtr.getParameterCount();

    UInt32 i;
    for (i = 0; i < paramCount; ++i) {
        mParameter[i].mAudioUnit = inAU;
        mParameter[i].mParameterID = i;
        mParameter[i].mScope = kAudioUnitScope_Global;
        mParameter[i].mElement = 0;
    }

    fUIAu = new UIAu(DISTRHO_PLUGIN_NAME);

    // add new listeners
    [self _addListeners];

    // initial setup
    [self _synchronizeUIWithParameterValues];

    fUIAu->auui_showhide(true);
}

#pragma mark ____ LISTENER CALLBACK DISPATCHER ____
void ParameterListenerDispatcher (void *inRefCon, void *inObject,
                                  const AudioUnitParameter *inParameter,
                                  Float32 inValue)
{
    PluginAU_CocoaUI *SELF = (PluginAU_CocoaUI *)inRefCon;

    [SELF _parameterListener:inObject parameter:inParameter value:inValue];
}

#pragma mark ____ PRIVATE FUNCTIONS ____
- (void)_addListeners {
    NSAssert (AUListenerCreate(ParameterListenerDispatcher, self,
              CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0.100, // 100 ms
              &mParameterListener ) == noErr,
              @"[_addListeners] AUListenerCreate()");

    UInt32 i;
    for (i = 0; i < paramCount; ++i) {
        NSAssert(AUListenerAddParameter(mParameterListener,
                                        NULL, &mParameter[i]) == noErr,
                 @"[_addListeners] AUListenerAddParameter()");
    }
}

- (void)_removeListeners {
    UInt32 i;
    for (i = 0; i < paramCount; ++i) {
        NSAssert (AUListenerRemoveParameter(mParameterListener,
                                            NULL, &mParameter[i]) == noErr,
                  @"[_removeListeners] AUListenerRemoveParameter()");
    }

    NSAssert (AUListenerDispose(mParameterListener) == noErr,
              @"[_removeListeners] AUListenerDispose()");
}

- (void)_synchronizeUIWithParameterValues {
    Float32 value;
    UInt32 i;

    for (i = 0; i < paramCount; ++i) {
        // only has global parameters
        NSAssert (AudioUnitGetParameter(mAU, mParameter[i].mParameterID,
                    kAudioUnitScope_Global, 0, &value) == noErr,
                    @"[synchronizeUIWithParameterValues] (x.1)");
        NSAssert (AUParameterSet(mParameterListener, self, &mParameter[i],
	                         value, 0) == noErr,
                    @"[synchronizeUIWithParameterValues] (x.2)");
        NSAssert (AUParameterListenerNotify (mParameterListener, self,
	                                     &mParameter[i]) == noErr,
                    @"[synchronizeUIWithParameterValues] (x.3)");
    }
}

#pragma mark ____ LISTENER CALLBACK DISPATCHEE ____
- (void)_parameterListener:(void *)inObject
         parameter: (const AudioUnitParameter *)inParameter
         value: (Float32)inValue
{
    //DPF: setUIParameter(inParameter->mParameterID, inValue);
}

@end

@interface PluginAU_CocoaUIFactory : NSObject <AUCocoaUIBase>
{
    IBOutlet PluginAU_CocoaUI *uiFreshlyLoadedView;	
}

- (NSString *) description;	

@end

@implementation PluginAU_CocoaUIFactory

- (unsigned) interfaceVersion {
	return 0;
}

- (NSString *) description {
	return @"DPF: AU UI";
}

- (NSView *)uiViewForAudioUnit:(AudioUnit)inAU withSize:(NSSize)inPreferredSize {
    uiFreshlyLoadedView = [[PluginAU_CocoaUI alloc] init];
    [uiFreshlyLoadedView setAU:inAU];
    NSView *returnView = uiFreshlyLoadedView;
    uiFreshlyLoadedView = nil;
    return [returnView autorelease];
}

@end