supernova: fix for small audio vector sizes
[supercollider.git] / lang / LangPrimSource / SC_CoreAudioPrim.cpp
blob332baa1f4dfcee22bc09051237854212ed1315c4
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;
41 // unsigned long count;
42 UInt32 count;
43 OSStatus err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &count, 0);
44 AudioDeviceID *devices = (AudioDeviceID*)malloc(count);
45 err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &count, devices);
46 if (err!=kAudioHardwareNoError)
48 free(devices);
49 return 0;
52 numDevices = count / sizeof(AudioDeviceID);
54 int i;
56 if (type<BOTH)
58 for (i=0; i<numDevices; i++)
60 Boolean writeable;
61 err = AudioDeviceGetPropertyInfo(devices[i], 0, type, kAudioDevicePropertyStreams, &count, &writeable);
62 if (err!=kAudioHardwareNoError)
64 free(devices);
65 return 0;
67 if (!count) devices[i] = 0;
68 else num++;
71 else num = numDevices;
73 PyrObject* devArray = newPyrArray(g->gc, num * sizeof(PyrObject), 0, true);
74 SetObject(a, devArray);
76 int j = 0;
77 for (i=0; i<numDevices; i++)
79 if (!devices[i]) continue;
81 err = AudioDeviceGetPropertyInfo(devices[i], 0, false, kAudioDevicePropertyDeviceName, &count, 0);
82 if (err != kAudioHardwareNoError)
84 break;
87 char *name = (char*)malloc(count);
88 err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &count, name);
89 if (err != kAudioHardwareNoError)
91 free(name);
92 break;
95 PyrString *string = newPyrString(g->gc, name, 0, true);
96 SetObject(devArray->slots+j, string);
97 devArray->size++;
98 g->gc->GCWrite(devArray, (PyrObject*)string);
100 free(name);
101 j++;
104 free(devices);
105 return 1;
108 int prListAudioDevices(struct VMGlobals *g, int numArgsPushed)
110 int in = 0;
111 int out = 0;
112 slotIntVal(g->sp, &out);
113 slotIntVal(g->sp-1, &in);
115 int type;
116 if (in && out) type = BOTH;
117 else if (in) type = IN;
118 else type = OUT;
120 if (listDevices(g, type)) return errNone;
121 return errFailed;
124 void initCoreAudioPrimitives()
126 definePrimitive(nextPrimitiveIndex(), 0, "_ListAudioDevices", prListAudioDevices, 3, 0);