~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/AudioUnit/AUGraph.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//
2
2
// AUGraph.cs: AUGraph wrapper class
3
3
//
4
 
// Author:
 
4
// Authors:
5
5
//   AKIHIRO Uehara (u-akihiro@reinforce-lab.com)
 
6
//   Marek Safar (marek.safar@gmail.com)
6
7
//
7
8
// Copyright 2010 Reinforce Lab.
8
9
// Copyright 2010 Novell, Inc.
 
10
// Copyright 2012 Xamarin Inc
9
11
//
10
12
// Permission is hereby granted, free of charge, to any person obtaining
11
13
// a copy of this software and associated documentation files (the
31
33
using System.Text;
32
34
using System.Runtime.InteropServices;
33
35
using MonoMac.AudioToolbox;
 
36
using MonoMac.Foundation;
 
37
using System.Collections.Generic;
34
38
 
35
39
namespace MonoMac.AudioUnit
36
40
{
 
41
        public enum AUGraphError
 
42
        {
 
43
                OK = 0,
 
44
                NodeNotFound                            = -10860,
 
45
                InvalidConnection                       = -10861,
 
46
                OutputNodeError                         = -10862,
 
47
                CannotDoInCurrentContext        = -10863,
 
48
                InvalidAudioUnit                        = -10864,
 
49
 
 
50
                // Values returned & shared with other error enums
 
51
                FormatNotSupported                      = -10868,
 
52
                InvalidElement                          = -10877,               
 
53
        }
 
54
 
37
55
        public class AUGraph: IDisposable 
38
56
        {
39
 
                #region Variables
40
57
                readonly GCHandle gcHandle;
41
 
                readonly IntPtr handle;
42
 
                #endregion
43
 
 
44
 
                #region Properties
45
 
                public event EventHandler<AudioGraphEventArgs> RenderCallback;
46
 
                public IntPtr Handler { get { return handle; } }
47
 
                #endregion
48
 
 
49
 
                #region Constructor
50
 
                private AUGraph(IntPtr ptr)
 
58
                IntPtr handle;
 
59
 
 
60
                internal AUGraph (IntPtr ptr)
51
61
                {
52
62
                        handle = ptr;
53
63
                        
54
 
                        gcHandle = GCHandle.Alloc(this);            
55
64
                        int err = AUGraphAddRenderNotify(handle, renderCallback, GCHandle.ToIntPtr(gcHandle));
56
65
                        if (err != 0)
57
66
                                throw new ArgumentException(String.Format("Error code: {0}", err));
 
67
 
 
68
                        gcHandle = GCHandle.Alloc (this);
 
69
                }
 
70
 
 
71
                public event EventHandler<AudioGraphEventArgs> RenderCallback;
 
72
 
 
73
                [Advice ("Use Handle property instead")]
 
74
                public IntPtr Handler {
 
75
                        get {
 
76
                                return handle;
 
77
                                }
 
78
                }
 
79
 
 
80
                public IntPtr Handle {
 
81
                        get {
 
82
                                return handle;
 
83
                        }
58
84
                }
59
85
 
60
86
                public AUGraph ()
61
87
                {
62
 
                        int err = NewAUGraph  (ref handle);
 
88
                        int err = NewAUGraph (ref handle);
63
89
                        if (err != 0)
64
90
                                throw new InvalidOperationException(String.Format("Cannot create new AUGraph. Error code:", err));
65
91
                }
 
92
 
 
93
                public bool IsInitialized {
 
94
                        get {
 
95
                                bool b;
 
96
                                return AUGraphIsInitialized (handle, out b) == AUGraphError.OK && b;
 
97
                        }
 
98
                }
 
99
 
 
100
                public bool IsOpen {
 
101
                        get {
 
102
                                bool b;
 
103
                                return AUGraphIsOpen (handle, out b) == AUGraphError.OK && b;
 
104
                        }
 
105
                }
 
106
 
 
107
                public bool IsRunning {
 
108
                        get {
 
109
                                bool b;
 
110
                                return AUGraphIsRunning (handle, out b) == AUGraphError.OK && b;
 
111
                        }
 
112
                }
66
113
                
67
 
                #endregion
68
 
                        
69
 
                #region Private methods
70
114
                // callback funtion should be static method and be attatched a MonoPInvokeCallback attribute.        
71
115
                [MonoMac.MonoPInvokeCallback(typeof(AudioUnit.AURenderCallback))]
72
116
                static int renderCallback(IntPtr inRefCon,
93
137
                        
94
138
                        return 0; // noerror
95
139
                }
96
 
                #endregion
97
140
 
98
 
                #region Public methods
99
141
                public void Open ()
100
142
                { 
101
143
                        int err = AUGraphOpen (handle);
109
151
                        return err;
110
152
                }
111
153
                
112
 
                public int AddNode(AudioComponentDescription cd)
 
154
                public int AddNode (AudioComponentDescription description)
113
155
                {
114
 
                        int node = 0;
115
 
                        int err = AUGraphAddNode(handle, cd, ref node);
 
156
                        int node;
 
157
                        var err = AUGraphAddNode (handle, description, out node);
116
158
                        if (err != 0)
117
159
                                throw new ArgumentException(String.Format("Error code:", err));
118
160
                        
119
161
                        return node;
120
162
                }
 
163
 
 
164
                public AUGraphError RemoveNode (int node)
 
165
                {
 
166
                        return AUGraphRemoveNode (handle, node);
 
167
                }
 
168
 
 
169
                public AUGraphError GetCPULoad (out float averageCPULoad)
 
170
                {
 
171
                        return AUGraphGetCPULoad (handle, out averageCPULoad);
 
172
                }
 
173
 
 
174
                public AUGraphError GetMaxCPULoad (out float maxCPULoad)
 
175
                {
 
176
                        return AUGraphGetMaxCPULoad (handle, out maxCPULoad);
 
177
                }
 
178
 
 
179
                public AUGraphError GetNode (uint index, out int node)
 
180
                {
 
181
                        return AUGraphGetIndNode (handle, index, out node);
 
182
                }
 
183
 
 
184
                public AUGraphError GetNodeCount (out int count)
 
185
                {
 
186
                        return AUGraphGetNodeCount (handle, out count);
 
187
                }
121
188
                
122
 
                public AudioUnit GetNodeInfo(int node)
 
189
                public AudioUnit GetNodeInfo (int node)
123
190
                {
124
 
                        int err;
125
 
                        IntPtr ptr = new IntPtr();
126
 
                        unsafe {
127
 
                                err = AUGraphNodeInfo(handle, node, null, (IntPtr)(&ptr));
128
 
                        }
 
191
                        IntPtr ptr;
 
192
                        var err = AUGraphNodeInfo(handle, node, IntPtr.Zero, out ptr);
129
193
                        if (err != 0)
130
194
                                throw new ArgumentException(String.Format("Error code:{0}", err));
131
195
 
132
196
                        if (ptr == IntPtr.Zero)
133
197
                                throw new InvalidOperationException("Can not get object instance");
134
198
                        
135
 
                        return new AudioUnit(ptr);
136
 
                }
137
 
 
138
 
                public void ConnnectNodeInput(int inSourceNode, uint inSourceOutputNumber, int inDestNode, uint inDestInputNumber)
139
 
                {
140
 
                        int err = AUGraphConnectNodeInput(handle,                                    
141
 
                                                          inSourceNode, inSourceOutputNumber,                                    
142
 
                                                          inDestNode, inDestInputNumber                                    
143
 
                                );
144
 
                        if (err != 0)
145
 
                                throw new ArgumentException(String.Format("Error code:", err));            
146
 
                }
147
 
 
148
 
                public void Start()
149
 
                {
150
 
                        AUGraphStart(handle);
151
 
                }
152
 
 
153
 
                public void Stop()
154
 
                {
155
 
                        AUGraphStop(handle);
 
199
                        return new AudioUnit (ptr);
 
200
                }
 
201
 
 
202
                public AUGraphError GetNumberOfInteractions (out uint interactionsCount)
 
203
                {
 
204
                        return AUGraphGetNumberOfInteractions (handle, out interactionsCount);
 
205
                }
 
206
 
 
207
                public AUGraphError GetNumberOfInteractions (int node, out uint interactionsCount)
 
208
                {
 
209
                        return AUGraphCountNodeInteractions (handle, node, out interactionsCount);
 
210
                }
 
211
 
 
212
/*
 
213
                // TODO: requires AudioComponentDescription type to be fixed
 
214
                public AUGraphError GetNodeInfo (int node, out AudioComponentDescription description, out AudioUnit audioUnit)
 
215
                {
 
216
                        IntPtr au;
 
217
                        var res = AUGraphNodeInfo (handle, node, out description, out au);
 
218
                        if (res != AUGraphError.OK) {
 
219
                                audioUnit = null;
 
220
                                return res;
 
221
                        }
 
222
 
 
223
                        audioUnit = new AudioUnit (au);
 
224
                        return res;
 
225
                }
 
226
*/
 
227
                public AUGraphError ConnnectNodeInput (int sourceNode, uint sourceOutputNumber, int destNode, uint destInputNumber)
 
228
                {
 
229
                        return AUGraphConnectNodeInput (handle,
 
230
                                                          sourceNode, sourceOutputNumber,                                    
 
231
                                                          destNode, destInputNumber);
 
232
                }
 
233
 
 
234
                public AUGraphError DisconnectNodeInput (int destNode, uint destInputNumber)
 
235
                {
 
236
                        return AUGraphDisconnectNodeInput (handle, destNode, destInputNumber);
 
237
                }
 
238
 
 
239
                /*
 
240
                // Don't know how to test it yet
 
241
 
 
242
                Dictionary<int, RenderDelegate> nodesCallbacks;
 
243
                static readonly RenderCallbackShared CreateRenderCallback = RenderCallbackImpl;
 
244
 
 
245
                public AUGraphError SetNodeInputCallback (int destNode, uint destInputNumber, RenderDelegate renderDelegate)
 
246
                {
 
247
                        var cb = new AURenderCallbackStruct ();
 
248
                        cb.Proc = CreateRenderCallback;
 
249
                        cb.ProcRefCon = GCHandle.ToIntPtr (gcHandle);
 
250
 
 
251
                        AUGraphError res;
 
252
                        if (nodesCallbacks == null) {
 
253
                                nodesCallbacks = new Dictionary<int, RenderDelegate> ();
 
254
 
 
255
                                res = AUGraphSetNodeInputCallback (handle, destNode, destInputNumber, ref cb);
 
256
                        } else {
 
257
                                res = AUGraphError.OK;
 
258
                        }
 
259
 
 
260
                        nodesCallbacks [destNode] = renderDelegate;
 
261
                        return res;
 
262
                }
 
263
 
 
264
                [MonoPInvokeCallback (typeof (RenderCallbackShared))]
 
265
                static AudioUnitStatus RenderCallbackImpl (IntPtr clientData, ref AudioUnitRenderActionFlags actionFlags, ref AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, IntPtr data)
 
266
                {
 
267
                        GCHandle gch = GCHandle.FromIntPtr (clientData);
 
268
                        var au = (AUGraph) gch.Target;
 
269
                }
 
270
                */
 
271
 
 
272
                public AUGraphError ClearConnections ()
 
273
                {
 
274
                        return AUGraphClearConnections (handle);
 
275
                }
 
276
 
 
277
                public AUGraphError Start()
 
278
                {
 
279
                        return AUGraphStart (handle);
 
280
                }
 
281
 
 
282
                public AUGraphError Stop()
 
283
                {
 
284
                        return AUGraphStop (handle);
156
285
                }
157
286
                
158
 
                public void Initialize()
159
 
                {
160
 
                        int err = AUGraphInitialize(handle);
161
 
                        if (err != 0)
162
 
                                throw new ArgumentException(String.Format("Error code:", err));
163
 
                }
164
 
                #endregion
165
 
 
166
 
                #region IDisposable メンバ (Members)
 
287
                public AUGraphError Initialize()
 
288
                {
 
289
                        return AUGraphInitialize (handle);
 
290
                }
 
291
 
 
292
                public bool Update ()
 
293
                {
 
294
                        bool isUpdated;
 
295
                        return AUGraphUpdate (handle, out isUpdated) == AUGraphError.OK && isUpdated;
 
296
                }
 
297
 
167
298
                public void Dispose()
168
299
                {
169
300
                        Dispose (true);
178
309
                                DisposeAUGraph (handle);
179
310
                                
180
311
                                gcHandle.Free();
 
312
                                handle = IntPtr.Zero;
181
313
                        }
182
314
                }
183
315
 
185
317
                {
186
318
                        Dispose (false);
187
319
                }
188
 
                #endregion
189
320
                        
190
 
                #region Interop
191
321
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "NewAUGraph")]
192
322
                static extern int NewAUGraph(ref IntPtr outGraph);
193
323
 
194
324
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphOpen")]
195
325
                static extern int AUGraphOpen(IntPtr inGraph);
196
326
 
197
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphAddNode")]
198
 
                static extern int AUGraphAddNode(IntPtr inGraph, AudioComponentDescription inDescription, ref int outNode);
199
 
        
200
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphNodeInfo")]
201
 
                static extern int AUGraphNodeInfo(IntPtr inGraph, int inNode, AudioComponentDescription outDescription, IntPtr outAudioUnit);
202
 
        
203
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphConnectNodeInput")]
204
 
                static extern int AUGraphConnectNodeInput(IntPtr inGraph, int inSourceNode, uint inSourceOutputNumber, int inDestNode, uint inDestInputNumber);
205
 
        
206
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphInitialize")]
207
 
                static extern int AUGraphInitialize(IntPtr inGraph);
 
327
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
328
                static extern AUGraphError AUGraphAddNode(IntPtr inGraph, AudioComponentDescription inDescription, out int outNode);
 
329
 
 
330
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
331
                static extern AUGraphError AUGraphRemoveNode (IntPtr inGraph, int inNode);
 
332
 
 
333
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
334
                static extern AUGraphError AUGraphGetNodeCount (IntPtr inGraph, out int outNumberOfNodes);
 
335
 
 
336
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
337
                static extern AUGraphError AUGraphGetIndNode (IntPtr inGraph, uint inIndex, out int outNode);
 
338
        
 
339
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
340
                static extern AUGraphError AUGraphNodeInfo(IntPtr inGraph, int inNode, IntPtr outDescription, out IntPtr outAudioUnit);
 
341
 
 
342
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
343
                static extern AUGraphError AUGraphNodeInfo (IntPtr inGraph, int inNode, out AudioComponentDescription outDescription, out IntPtr outAudioUnit);
 
344
 
 
345
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
346
                static extern AUGraphError AUGraphClearConnections (IntPtr inGraph);
 
347
        
 
348
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
349
                static extern AUGraphError AUGraphConnectNodeInput (IntPtr inGraph, int inSourceNode, uint inSourceOutputNumber, int inDestNode, uint inDestInputNumber);
 
350
 
 
351
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
352
                static extern AUGraphError AUGraphDisconnectNodeInput (IntPtr inGraph, int inDestNode, uint inDestInputNumber);
 
353
 
 
354
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
355
                static extern AUGraphError AUGraphGetNumberOfInteractions (IntPtr inGraph, out uint outNumInteractions);
 
356
 
 
357
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
358
                static extern AUGraphError AUGraphCountNodeInteractions (IntPtr inGraph, int inNode, out uint outNumInteractions);
 
359
        
 
360
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
361
                static extern AUGraphError AUGraphInitialize (IntPtr inGraph);
208
362
        
209
363
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphAddRenderNotify")]
210
364
                static extern int AUGraphAddRenderNotify(IntPtr inGraph, AudioUnit.AURenderCallback inCallback, IntPtr inRefCon );
211
365
        
212
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphStart")]
213
 
                static extern int AUGraphStart(IntPtr inGraph);
214
 
        
215
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphStop")]
216
 
                static extern int AUGraphStop(IntPtr inGraph);
217
 
        
218
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphUninitialize")]
219
 
                static extern int AUGraphUninitialize(IntPtr inGraph);
 
366
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
367
                static extern AUGraphError AUGraphStart (IntPtr inGraph);
 
368
        
 
369
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
370
                static extern AUGraphError AUGraphStop (IntPtr inGraph);
 
371
        
 
372
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
373
                static extern AUGraphError AUGraphUninitialize (IntPtr inGraph);
220
374
        
221
375
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "AUGraphClose")]
222
376
                static extern int AUGraphClose(IntPtr inGraph);
223
377
        
224
 
                [DllImport(MonoMac.Constants.AudioToolboxLibrary, EntryPoint = "DisposeAUGraph")]
225
 
                static extern int DisposeAUGraph(IntPtr inGraph);
226
 
                #endregion
 
378
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
379
                static extern int DisposeAUGraph(IntPtr inGraph);
 
380
 
 
381
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
382
                static extern AUGraphError AUGraphIsOpen (IntPtr inGraph, out bool outIsOpen);
 
383
 
 
384
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
385
                static extern AUGraphError AUGraphIsInitialized (IntPtr inGraph, out bool outIsInitialized);
 
386
 
 
387
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
388
                static extern AUGraphError AUGraphIsRunning (IntPtr inGraph, out bool outIsRunning);
 
389
 
 
390
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
391
                static extern AUGraphError AUGraphGetCPULoad (IntPtr inGraph, out float outAverageCPULoad);
 
392
 
 
393
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
394
                static extern AUGraphError AUGraphGetMaxCPULoad (IntPtr inGraph, out float outMaxLoad);
 
395
 
 
396
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
397
                static extern AUGraphError AUGraphSetNodeInputCallback (IntPtr inGraph, int inDestNode, uint inDestInputNumber, ref AURenderCallbackStruct inInputCallback);
 
398
 
 
399
                [DllImport(MonoMac.Constants.AudioToolboxLibrary)]
 
400
                static extern AUGraphError AUGraphUpdate (IntPtr inGraph, out bool outIsUpdated);
227
401
        }
228
402
}