3 * Mac OS 9 implementation
7 * Written by Dominic Mazzoni
9 * PortMixer is intended to work side-by-side with PortAudio,
10 * the Portable Real-Time Audio Library by Ross Bencina and
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:
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
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.
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.
41 #include "portaudio.h"
43 #include "portmixer.h"
45 #define PA_MAX_NUM_HOST_BUFFERS (16) /* Do not exceed!! */
47 typedef struct MultiBuffer
49 char *buffers[PA_MAX_NUM_HOST_BUFFERS];
56 /* Define structure to contain all Macintosh specific data. */
57 typedef struct PaHostSoundControl
59 UInt64 pahsc_EntryCount;
60 double pahsc_InverseMicrosPerHostBuffer; /* 1/Microseconds of real-time audio per user buffer. */
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;
67 SPB pahsc_InputParams;
68 SICompletionUPP pahsc_InputCompletionProc;
69 MultiBuffer pahsc_InputMultiBuffer;
70 int32 pahsc_BytesPerInputHostBuffer;
71 int32 pahsc_InputRefNum;
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. */
89 typedef struct PxSource
103 int Px_GetNumMixers( void *pa_stream )
108 const char *Px_GetMixerName( void *pa_stream, int index )
110 return "Mac Sound Manager";
113 PxMixer *Px_OpenMixer( void *pa_stream, int index )
116 internalPortAudioStream *past;
117 PaHostSoundControl *macInfo;
123 info = (PxInfo *)malloc(sizeof(PxInfo));
124 past = (internalPortAudioStream *) pa_stream;
125 macInfo = (PaHostSoundControl *) past->past_DeviceData;
127 info->input = &macInfo->pahsc_InputParams;
128 info->inputRefNum = macInfo->pahsc_InputRefNum;
129 info->output = macInfo->pahsc_Channel;
131 info->numSources = 0;
132 info->sources = NULL;
134 err = SPBGetDeviceInfo (info->inputRefNum, siInputSourceNames, &h);
136 return (PxMixer *)info;
141 data = (unsigned char *)*h;
142 info->numSources = ((short *)data)[0];
143 if (info->numSources <= 0 || info->numSources > 50) {
145 return (PxMixer *)info;
148 info->sources = (PxSource *)malloc(info->numSources * sizeof(PxSource));
150 for(i=0; i<info->numSources; i++) {
154 info->numSources = 0;
156 info->sources = NULL;
158 return (PxMixer *)info;
162 info->sources[i].name[j] = *data++;
164 info->sources[i].name[len] = 0;
168 return (PxMixer *)info;
172 Px_CloseMixer() closes a mixer opened using Px_OpenMixer and frees any
173 memory associated with it.
176 void Px_CloseMixer(PxMixer *mixer)
178 PxInfo *info = (PxInfo *)mixer;
186 Master (output) volume
189 PxVolume Px_GetMasterVolume( PxMixer *mixer )
191 PxInfo *info = (PxInfo *)mixer;
196 void Px_SetMasterVolume( PxMixer *mixer, PxVolume volume )
198 PxInfo *info = (PxInfo *)mixer;
205 int Px_SupportsPCMOutputVolume( PxMixer* mixer )
210 PxVolume Px_GetPCMOutputVolume( PxMixer *mixer )
212 PxInfo *info = (PxInfo *)mixer;
217 cmd.cmd = getVolumeCmd;
219 cmd.param2 = (long)&packedVol;
221 err = SndDoImmediate(info->output, &cmd);
225 return ((packedVol & 0xFFFF) + ((packedVol & 0xFFFF0000) >> 16) / 2.0) / 256.0;
228 void Px_SetPCMOutputVolume( PxMixer *mixer, PxVolume volume )
230 PxInfo *info = (PxInfo *)mixer;
235 packedVol = (unsigned long)volume * 256.0;
236 packedVol += (packedVol << 16);
240 cmd.param2 = packedVol;
241 err = SndDoImmediate(info->output, &cmd);
248 int Px_GetNumOutputVolumes( PxMixer *mixer )
250 PxInfo *info = (PxInfo *)mixer;
255 const char *Px_GetOutputVolumeName( PxMixer *mixer, int i )
257 PxInfo *info = (PxInfo *)mixer;
262 PxVolume Px_GetOutputVolume( PxMixer *mixer, int i )
264 PxInfo *info = (PxInfo *)mixer;
269 void Px_SetOutputVolume( PxMixer *mixer, int i, PxVolume volume )
271 PxInfo *info = (PxInfo *)mixer;
278 int Px_GetNumInputSources( PxMixer *mixer )
280 PxInfo *info = (PxInfo *)mixer;
282 return info->numSources;
285 const char *Px_GetInputSourceName( PxMixer *mixer, int i)
287 PxInfo *info = (PxInfo *)mixer;
289 if (i >= 0 && i < info->numSources)
290 return info->sources[i].name;
295 int Px_GetCurrentInputSource( PxMixer *mixer )
297 PxInfo *info = (PxInfo *)mixer;
301 err = SPBGetDeviceInfo (info->inputRefNum, siInputSource, &selected);
308 void Px_SetCurrentInputSource( PxMixer *mixer, int i )
310 PxInfo *info = (PxInfo *)mixer;
311 short selected = i+1;
314 err = SPBSetDeviceInfo (info->inputRefNum, siInputSource, &selected);
321 PxVolume Px_GetInputVolume( PxMixer *mixer )
323 PxInfo *info = (PxInfo *)mixer;
329 err = SPBGetDeviceInfo(info->inputRefNum, siInputGain, (Ptr)&fixedGain);
333 vol = (fixedGain / 65536.0) - 0.5;
340 void Px_SetInputVolume( PxMixer *mixer, PxVolume volume )
342 PxInfo *info = (PxInfo *)mixer;
347 fixedGain = (Fixed)((volume + 0.5) * 65536.0);
348 err = SPBSetDeviceInfo(info->inputRefNum, siInputGain, (Ptr)&fixedGain);
356 int Px_SupportsOutputBalance( PxMixer *mixer )
361 PxBalance Px_GetOutputBalance( PxMixer *mixer )
366 void Px_SetOutputBalance( PxMixer *mixer, PxBalance balance )
374 int Px_SupportsPlaythrough( PxMixer *mixer )
376 PxInfo *info = (PxInfo *)mixer;
378 return (info->input != NULL);
381 PxVolume Px_GetPlaythrough( PxMixer *mixer )
383 PxInfo *info = (PxInfo *)mixer;
387 err = SPBGetDeviceInfo(info->inputRefNum, siPlayThruOnOff, (Ptr)&level);
396 return (PxVolume)(level / 7.0);
399 void Px_SetPlaythrough( PxMixer *mixer, PxVolume volume )
401 PxInfo *info = (PxInfo *)mixer;
403 short level = (int)(volume * 7.0 + 0.5);
405 err = SPBSetDeviceInfo(info->inputRefNum, siPlayThruOnOff, (Ptr)&level);