Debian Codegraph @

3rdparty/iaxclient/lib/portmixer/px_mac/px_mac.c

raw symbols SCIP
1 /*
2 * PortMixer
3 * Mac OS 9 implementation
4 *
5 * Copyright (c) 2002
6 *
7 * Written by Dominic Mazzoni
8 *
9 * PortMixer is intended to work side-by-side with PortAudio,
10 * the Portable Real-Time Audio Library by Ross Bencina and
11 * Phil Burk.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "portaudio.h"
42 #include "pa_host.h"
43 #include "portmixer.h"
44
45 #define PA_MAX_NUM_HOST_BUFFERS (16) /* Do not exceed!! */
46
47 typedef struct MultiBuffer
48 {
49 char *buffers[PA_MAX_NUM_HOST_BUFFERS];
50 int numBuffers;
51 int nextWrite;
52 int nextRead;
53 }
54 MultiBuffer;
55
56 /* Define structure to contain all Macintosh specific data. */
57 typedef struct PaHostSoundControl
58 {
59 UInt64 pahsc_EntryCount;
60 double pahsc_InverseMicrosPerHostBuffer; /* 1/Microseconds of real-time audio per user buffer. */
61
62 /* Use char instead of Boolean for atomic operation. */
63 volatile char pahsc_IsRecording; /* Recording in progress. Set by foreground. Cleared by background. */
64 volatile char pahsc_StopRecording; /* Signal sent to background. */
65 volatile char pahsc_IfInsideCallback;
66 /* Input */
67 SPB pahsc_InputParams;
68 SICompletionUPP pahsc_InputCompletionProc;
69 MultiBuffer pahsc_InputMultiBuffer;
70 int32 pahsc_BytesPerInputHostBuffer;
71 int32 pahsc_InputRefNum;
72 /* Output */
73 CmpSoundHeader pahsc_SoundHeaders[PA_MAX_NUM_HOST_BUFFERS];
74 int32 pahsc_BytesPerOutputHostBuffer;
75 SndChannelPtr pahsc_Channel;
76 SndCallBackUPP pahsc_OutputCompletionProc;
77 int32 pahsc_NumOutsQueued;
78 int32 pahsc_NumOutsPlayed;
79 PaTimestamp pahsc_NumFramesDone;
80 UInt64 pahsc_WhenFramesDoneIncremented;
81 /* Init Time -------------- */
82 int32 pahsc_NumHostBuffers;
83 int32 pahsc_FramesPerHostBuffer;
84 int32 pahsc_UserBuffersPerHostBuffer;
85 int32 pahsc_MinFramesPerHostBuffer; /* Can vary depending on virtual memory usage. */
86 }
87 PaHostSoundControl;
88
89 typedef struct PxSource
90 {
91 char name[256];
92 } PxSource;
93
94 typedef struct PxInfo
95 {
96 SPB *input;
97 int32 inputRefNum;
98 SndChannelPtr output;
99 int32 numSources;
100 PxSource *sources;
101 } PxInfo;
102
103 int Px_GetNumMixers( void *pa_stream )
104 {
105 return 0;
106 }
107
108 const char *Px_GetMixerName( void *pa_stream, int index )
109 {
110 return "Mac Sound Manager";
111 }
112
113 PxMixer *Px_OpenMixer( void *pa_stream, int index )
114 {
115 PxInfo *info;
116 internalPortAudioStream *past;
117 PaHostSoundControl *macInfo;
118 OSErr err;
119 int i, j;
120 Handle h;
121 unsigned char *data;
122
123 info = (PxInfo *)malloc(sizeof(PxInfo));
124 if (info == NULL)
125 return NULL;
126
127 past = (internalPortAudioStream *) pa_stream;
128 macInfo = (PaHostSoundControl *) past->past_DeviceData;
129
130 info->input = &macInfo->pahsc_InputParams;
131 info->inputRefNum = macInfo->pahsc_InputRefNum;
132 info->output = macInfo->pahsc_Channel;
133
134 info->numSources = 0;
135 info->sources = NULL;
136
137 err = SPBGetDeviceInfo (info->inputRefNum, siInputSourceNames, &h);
138 if (err)
139 return (PxMixer *)info;
140
141 HLock(h);
142 HNoPurge(h);
143
144 data = (unsigned char *)*h;
145 info->numSources = ((short *)data)[0];
146 if (info->numSources <= 0 || info->numSources > 50) {
147 HUnlock(h);
148 return (PxMixer *)info;
149 }
150
151 info->sources = (PxSource *)malloc(info->numSources * sizeof(PxSource));
152 data += 2;
153 for(i=0; i<info->numSources; i++) {
154 int len = *data++;
155
156 if (len > 63) {
157 info->numSources = 0;
158 free(info->sources);
159 info->sources = NULL;
160 HUnlock(h);
161 return (PxMixer *)info;
162 }
163
164 for(j=0; j<len; j++)
165 info->sources[i].name[j] = *data++;
166
167 info->sources[i].name[len] = 0;
168 }
169 HUnlock(h);
170
171 return (PxMixer *)info;
172 }
173
174 /*
175 Px_CloseMixer() closes a mixer opened using Px_OpenMixer and frees any
176 memory associated with it.
177 */
178
179 void Px_CloseMixer(PxMixer *mixer)
180 {
181 PxInfo *info = (PxInfo *)mixer;
182
183 if (info->sources)
184 free(info->sources);
185 free(info);
186 }
187
188 /*
189 Master (output) volume
190 */
191
192 PxVolume Px_GetMasterVolume( PxMixer *mixer )
193 {
194 PxInfo *info = (PxInfo *)mixer;
195
196 return 0.0;
197 }
198
199 void Px_SetMasterVolume( PxMixer *mixer, PxVolume volume )
200 {
201 PxInfo *info = (PxInfo *)mixer;
202 }
203
204 /*
205 PCM output volume
206 */
207
208 int Px_SupportsPCMOutputVolume( PxMixer* mixer )
209 {
210 return 1 ;
211 }
212
213 PxVolume Px_GetPCMOutputVolume( PxMixer *mixer )
214 {
215 PxInfo *info = (PxInfo *)mixer;
216 OSErr err;
217 long packedVol;
218 SndCommand cmd;
219
220 cmd.cmd = getVolumeCmd;
221 cmd.param1 = 0;
222 cmd.param2 = (long)&packedVol;
223
224 err = SndDoImmediate(info->output, &cmd);
225 if (err)
226 return 0.0;
227
228 return ((packedVol & 0xFFFF) + ((packedVol & 0xFFFF0000) >> 16) / 2.0) / 256.0;
229 }
230
231 void Px_SetPCMOutputVolume( PxMixer *mixer, PxVolume volume )
232 {
233 PxInfo *info = (PxInfo *)mixer;
234 OSErr err;
235 long packedVol;
236 SndCommand cmd;
237
238 packedVol = (unsigned long)volume * 256.0;
239 packedVol += (packedVol << 16);
240
241 cmd.cmd = volumeCmd;
242 cmd.param1 = 0;
243 cmd.param2 = packedVol;
244 err = SndDoImmediate(info->output, &cmd);
245 }
246
247 /*
248 All output volumes
249 */
250
251 int Px_GetNumOutputVolumes( PxMixer *mixer )
252 {
253 PxInfo *info = (PxInfo *)mixer;
254
255 return 0;
256 }
257
258 const char *Px_GetOutputVolumeName( PxMixer *mixer, int i )
259 {
260 PxInfo *info = (PxInfo *)mixer;
261
262 return NULL;
263 }
264
265 PxVolume Px_GetOutputVolume( PxMixer *mixer, int i )
266 {
267 PxInfo *info = (PxInfo *)mixer;
268
269 return NULL;
270 }
271
272 void Px_SetOutputVolume( PxMixer *mixer, int i, PxVolume volume )
273 {
274 PxInfo *info = (PxInfo *)mixer;
275 }
276
277 /*
278 Input sources
279 */
280
281 int Px_GetNumInputSources( PxMixer *mixer )
282 {
283 PxInfo *info = (PxInfo *)mixer;
284
285 return info->numSources;
286 }
287
288 const char *Px_GetInputSourceName( PxMixer *mixer, int i)
289 {
290 PxInfo *info = (PxInfo *)mixer;
291
292 if (i >= 0 && i < info->numSources)
293 return info->sources[i].name;
294 else
295 return "";
296 }
297
298 int Px_GetCurrentInputSource( PxMixer *mixer )
299 {
300 PxInfo *info = (PxInfo *)mixer;
301 short selected;
302 OSErr err;
303
304 err = SPBGetDeviceInfo (info->inputRefNum, siInputSource, &selected);
305 if (err)
306 return 0;
307
308 return selected - 1;
309 }
310
311 void Px_SetCurrentInputSource( PxMixer *mixer, int i )
312 {
313 PxInfo *info = (PxInfo *)mixer;
314 short selected = i+1;
315 OSErr err;
316
317 err = SPBSetDeviceInfo (info->inputRefNum, siInputSource, &selected);
318 }
319
320 /*
321 Input volume
322 */
323
324 PxVolume Px_GetInputVolume( PxMixer *mixer )
325 {
326 PxInfo *info = (PxInfo *)mixer;
327 Fixed fixedGain;
328 PxVolume vol;
329 OSErr err;
330
331 if (info->input) {
332 err = SPBGetDeviceInfo(info->inputRefNum, siInputGain, (Ptr)&fixedGain);
333 if (err)
334 return 0.0;
335
336 vol = (fixedGain / 65536.0) - 0.5;
337 return vol;
338 }
339
340 return 0.0;
341 }
342
343 void Px_SetInputVolume( PxMixer *mixer, PxVolume volume )
344 {
345 PxInfo *info = (PxInfo *)mixer;
346 Fixed fixedGain;
347 OSErr err;
348
349 if (info->input) {
350 fixedGain = (Fixed)((volume + 0.5) * 65536.0);
351 err = SPBSetDeviceInfo(info->inputRefNum, siInputGain, (Ptr)&fixedGain);
352 }
353 }
354
355 /*
356 Balance
357 */
358
359 int Px_SupportsOutputBalance( PxMixer *mixer )
360 {
361 return 0;
362 }
363
364 PxBalance Px_GetOutputBalance( PxMixer *mixer )
365 {
366 return 0.0;
367 }
368
369 void Px_SetOutputBalance( PxMixer *mixer, PxBalance balance )
370 {
371 }
372
373 /*
374 Playthrough
375 */
376
377 int Px_SupportsPlaythrough( PxMixer *mixer )
378 {
379 PxInfo *info = (PxInfo *)mixer;
380
381 return (info->input != NULL);
382 }
383
384 PxVolume Px_GetPlaythrough( PxMixer *mixer )
385 {
386 PxInfo *info = (PxInfo *)mixer;
387 OSErr err;
388 short level;
389
390 err = SPBGetDeviceInfo(info->inputRefNum, siPlayThruOnOff, (Ptr)&level);
391 if (err)
392 return 0.0;
393
394 if (level < 0)
395 level = 0;
396 if (level > 7)
397 level = 7;
398
399 return (PxVolume)(level / 7.0);
400 }
401
402 void Px_SetPlaythrough( PxMixer *mixer, PxVolume volume )
403 {
404 PxInfo *info = (PxInfo *)mixer;
405 OSErr err;
406 short level = (int)(volume * 7.0 + 0.5);
407
408 err = SPBSetDeviceInfo(info->inputRefNum, siPlayThruOnOff, (Ptr)&level);
409 }