scide: implement selectionLength for openDocument
[supercollider.git] / lang / LangPrimSource / SC_CoreAudioPrim.cpp
blobeaebc1146bc307c3f2ac80d27aa2e02c8b240052
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <CoreAudio/AudioHardware.h>
23 #include "SCBase.h"
24 #include "VMGlobals.h"
25 #include "PyrKernel.h"
26 #include "PyrPrimitive.h"
27 #include "GC.h"
29 enum
31 OUT = 0,
32 IN,
33 BOTH
36 int listDevices(struct VMGlobals *g, int type)
38 int numDevices, num = 0;
39 PyrSlot *a = g->sp-2;
40 AudioObjectPropertyAddress propertyAddress;
41 propertyAddress.mSelector = kAudioHardwarePropertyDevices;
42 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
43 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
45 // unsigned long count;
46 UInt32 count;
47 // OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &count, 0);
48 OSStatus err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &count);
49 AudioDeviceID *devices = (AudioDeviceID*)malloc(count);
50 // err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &count, devices);
51 err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &count, devices);
52 if (err!=kAudioHardwareNoError)
54 free(devices);
55 return 0;
58 numDevices = count / sizeof(AudioDeviceID);
60 int i;
62 if (type<BOTH)
64 if(type < IN)
66 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
67 } else {
68 propertyAddress.mScope = kAudioDevicePropertyScopeInput;
71 for (i=0; i<numDevices; i++)
73 Boolean writeable;
74 propertyAddress.mSelector = kAudioDevicePropertyStreams;
75 // err = AudioDeviceGetPropertyInfo(devices[i], 0, type, kAudioDevicePropertyStreams, &count, &writeable);
77 err = AudioObjectGetPropertyDataSize(devices[i], &propertyAddress, 0, NULL, &count);
79 if (err!=kAudioHardwareNoError)
81 free(devices);
82 return 0;
85 err = AudioObjectIsPropertySettable(devices[i], &propertyAddress, &writeable);
87 if (err!=kAudioHardwareNoError)
89 free(devices);
90 return 0;
92 if (!count) devices[i] = 0;
93 else num++;
96 else num = numDevices;
98 PyrObject* devArray = newPyrArray(g->gc, num * sizeof(PyrObject), 0, true);
99 SetObject(a, devArray);
101 int j = 0;
102 for (i=0; i<numDevices; i++)
104 if (!devices[i]) continue;
106 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
107 propertyAddress.mSelector = kAudioDevicePropertyDeviceName;
109 // err = AudioDeviceGetPropertyInfo(devices[i], 0, false, kAudioDevicePropertyDeviceName, &count, 0);
111 err = AudioObjectGetPropertyDataSize(devices[i], &propertyAddress, 0, NULL, &count);
113 if (err != kAudioHardwareNoError)
115 break;
118 char *name = (char*)malloc(count);
119 // err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &count, name);
120 err = AudioObjectGetPropertyData(devices[i], &propertyAddress, 0, NULL, &count, name);
121 if (err != kAudioHardwareNoError)
123 free(name);
124 break;
127 PyrString *string = newPyrString(g->gc, name, 0, true);
128 SetObject(devArray->slots+j, string);
129 devArray->size++;
130 g->gc->GCWrite(devArray, (PyrObject*)string);
132 free(name);
133 j++;
136 free(devices);
137 return 1;
140 int prListAudioDevices(struct VMGlobals *g, int numArgsPushed)
142 int in = 0;
143 int out = 0;
144 slotIntVal(g->sp, &out);
145 slotIntVal(g->sp-1, &in);
147 int type;
148 if (in && out) type = BOTH;
149 else if (in) type = IN;
150 else type = OUT;
152 if (listDevices(g, type)) return errNone;
153 return errFailed;
156 void initCoreAudioPrimitives()
158 definePrimitive(nextPrimitiveIndex(), 0, "_ListAudioDevices", prListAudioDevices, 3, 0);