2 * Wine Driver for CoreAudio / AudioUnit
4 * Copyright 2005, 2006 Emmanuel Maillard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifdef HAVE_AUDIOUNIT_AUDIOUNIT_H
25 #define ULONG CoreFoundation_ULONG
26 #define HRESULT CoreFoundation_HRESULT
27 #include <CoreServices/CoreServices.h>
28 #include <AudioUnit/AudioUnit.h>
29 #include <AudioToolbox/AudioToolbox.h>
34 #undef STDMETHODCALLTYPE
35 #include "coreaudio.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(wave);
39 WINE_DECLARE_DEBUG_CHANNEL(midi);
41 static const char *streamDescription(const AudioStreamBasicDescription* stream)
43 return wine_dbg_sprintf("\n mSampleRate : %f\n mFormatID : %s\n mFormatFlags : %lX\n mBytesPerPacket : %lu\n mFramesPerPacket : %lu\n mBytesPerFrame : %lu\n mChannelsPerFrame : %lu\n mBitsPerChannel : %lu\n",
45 wine_dbgstr_fourcc(stream->mFormatID),
47 stream->mBytesPerPacket,
48 stream->mFramesPerPacket,
49 stream->mBytesPerFrame,
50 stream->mChannelsPerFrame,
51 stream->mBitsPerChannel);
54 extern OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
55 AudioUnitRenderActionFlags *ioActionFlags,
56 const AudioTimeStamp *inTimeStamp,
58 UInt32 inNumberFrames,
59 AudioBufferList *ioData);
61 extern OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
62 AudioUnitRenderActionFlags *ioActionFlags,
63 const AudioTimeStamp *inTimeStamp,
65 UInt32 inNumberFrames,
66 AudioBufferList *ioData);
68 int AudioUnit_CreateDefaultAudioUnit(void *wwo, AudioUnit *au)
72 ComponentDescription desc;
73 AURenderCallbackStruct callbackStruct;
77 desc.componentType = kAudioUnitType_Output;
78 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
79 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
80 desc.componentFlags = 0;
81 desc.componentFlagsMask = 0;
83 comp = FindNextComponent(NULL, &desc);
87 err = OpenAComponent(comp, au);
91 callbackStruct.inputProc = CoreAudio_woAudioUnitIOProc;
92 callbackStruct.inputProcRefCon = wwo;
94 err = AudioUnitSetProperty( *au,
95 kAudioUnitProperty_SetRenderCallback,
96 kAudioUnitScope_Input,
99 sizeof(callbackStruct));
100 return (err == noErr);
103 int AudioUnit_CloseAudioUnit(AudioUnit au)
105 OSStatus err = CloseComponent(au);
106 return (err == noErr);
109 int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *stream)
111 OSStatus err = noErr;
113 TRACE("input format: %s\n", streamDescription(stream));
115 err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
116 0, stream, sizeof(*stream));
120 ERR("AudioUnitSetProperty return an error %s\n", wine_dbgstr_fourcc(err));
124 err = AudioUnitInitialize(au);
127 ERR("AudioUnitInitialize return an error %s\n", wine_dbgstr_fourcc(err));
133 int AudioUnit_SetVolume(AudioUnit au, float left, float right)
135 OSStatus err = noErr;
136 FIXME("independent left/right volume not implemented (%f, %f)\n", left, right);
138 err = AudioUnitSetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left, 0);
142 ERR("AudioUnitSetParameter return an error %s\n", wine_dbgstr_fourcc(err));
148 int AudioUnit_GetVolume(AudioUnit au, float *left, float *right)
150 OSStatus err = noErr;
151 FIXME("independent left/right volume not implemented\n");
153 err = AudioUnitGetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left);
156 ERR("AudioUnitGetParameter return an error %s\n", wine_dbgstr_fourcc(err));
164 /* FIXME: implement sample rate conversion on input */
165 int AudioUnit_GetInputDeviceSampleRate(void)
167 AudioDeviceID defaultInputDevice;
172 param = sizeof(defaultInputDevice);
173 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, ¶m, &defaultInputDevice);
174 if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
176 ERR("Couldn't get the default audio input device ID: %08lx\n", err);
180 param = sizeof(sampleRate);
181 err = AudioDeviceGetProperty(defaultInputDevice, 0, 1, kAudioDevicePropertyNominalSampleRate, ¶m, &sampleRate);
184 ERR("Couldn't get the device sample rate: %08lx\n", err);
192 int AudioUnit_CreateInputUnit(void* wwi, AudioUnit* out_au,
193 WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample,
194 UInt32* outFrameCount)
196 OSStatus err = noErr;
197 ComponentDescription description;
201 AURenderCallbackStruct callback;
202 AudioDeviceID defaultInputDevice;
203 AudioStreamBasicDescription desiredFormat;
208 ERR("Invalid parameter\n");
212 /* Open the AudioOutputUnit */
213 description.componentType = kAudioUnitType_Output;
214 description.componentSubType = kAudioUnitSubType_HALOutput;
215 description.componentManufacturer = kAudioUnitManufacturer_Apple;
216 description.componentFlags = 0;
217 description.componentFlagsMask = 0;
219 component = FindNextComponent(NULL, &description);
222 ERR("FindNextComponent(kAudioUnitSubType_HALOutput) failed\n");
226 err = OpenAComponent(component, &au);
227 if (err != noErr || au == NULL)
229 ERR("OpenAComponent failed: %08lx\n", err);
233 /* Configure the AudioOutputUnit */
234 /* The AUHAL has two buses (AKA elements). Bus 0 is output from the app
235 * to the device. Bus 1 is input from the device to the app. Each bus
236 * has two ends (AKA scopes). Data goes from the input scope to the
237 * output scope. The terminology is somewhat confusing because the terms
238 * "input" and "output" have two meanings. Here's a summary:
240 * Bus 0, input scope: refers to the source of data to be output as sound
241 * Bus 0, output scope: refers to the actual sound output device
242 * Bus 1, input scope: refers to the actual sound input device
243 * Bus 1, output scope: refers to the destination of data received by the input device
246 /* Enable input on the AUHAL */
248 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, ¶m, sizeof(param));
251 ERR("Couldn't enable input on AUHAL: %08lx\n", err);
255 /* Disable Output on the AUHAL */
257 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, ¶m, sizeof(param));
260 ERR("Couldn't disable output on AUHAL: %08lx\n", err);
264 /* Find the default input device */
265 param = sizeof(defaultInputDevice);
266 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, ¶m, &defaultInputDevice);
267 if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
269 ERR("Couldn't get the default audio device ID: %08lx\n", err);
273 /* Set the current device to the default input device. */
274 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &defaultInputDevice, sizeof(defaultInputDevice));
277 ERR("Couldn't set current device of AUHAL to default input device: %08lx\n", err);
281 /* Setup render callback */
282 /* This will be called when the AUHAL has input data. However, it won't
283 * be passed the data itself. The callback will have to all AudioUnitRender. */
284 callback.inputProc = CoreAudio_wiAudioUnitIOProc;
285 callback.inputProcRefCon = wwi;
286 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback));
289 ERR("Couldn't set input callback of AUHAL: %08lx\n", err);
293 /* Setup the desired data format. */
294 /* FIXME: implement sample rate conversion on input. We shouldn't set
295 * the mSampleRate of this to the desired sample rate. We need to query
296 * the input device and use that. If they don't match, we need to set up
297 * an AUConverter to do the sample rate conversion on a separate thread. */
298 desiredFormat.mFormatID = kAudioFormatLinearPCM;
299 desiredFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked;
300 if (wBitsPerSample != 8)
301 desiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
302 desiredFormat.mSampleRate = nSamplesPerSec;
303 desiredFormat.mChannelsPerFrame = nChannels;
304 desiredFormat.mFramesPerPacket = 1;
305 desiredFormat.mBitsPerChannel = wBitsPerSample;
306 desiredFormat.mBytesPerFrame = desiredFormat.mBitsPerChannel * desiredFormat.mChannelsPerFrame / 8;
307 desiredFormat.mBytesPerPacket = desiredFormat.mBytesPerFrame * desiredFormat.mFramesPerPacket;
309 /* Set the AudioOutputUnit output data format */
310 err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &desiredFormat, sizeof(desiredFormat));
313 ERR("Couldn't set desired input format of AUHAL: %08lx\n", err);
317 /* Get the number of frames in the IO buffer(s) */
318 param = sizeof(*outFrameCount);
319 err = AudioUnitGetProperty(au, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, outFrameCount, ¶m);
322 ERR("Failed to get audio sample size: %08lx\n", err);
326 TRACE("Frame count: %lu\n", *outFrameCount);
328 /* Initialize the AU */
329 err = AudioUnitInitialize(au);
332 ERR("Failed to initialize AU: %08lx\n", err);
349 int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth)
352 ComponentDescription desc;
356 err = NewAUGraph(graph);
359 ERR_(midi)("NewAUGraph return %s\n", wine_dbgstr_fourcc(err));
363 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
364 desc.componentFlags = 0;
365 desc.componentFlagsMask = 0;
367 /* create synth node */
368 desc.componentType = kAudioUnitType_MusicDevice;
369 desc.componentSubType = kAudioUnitSubType_DLSSynth;
371 err = AUGraphNewNode(*graph, &desc, 0, NULL, &synthNode);
374 ERR_(midi)("AUGraphNewNode cannot create synthNode : %s\n", wine_dbgstr_fourcc(err));
378 /* create out node */
379 desc.componentType = kAudioUnitType_Output;
380 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
382 err = AUGraphNewNode(*graph, &desc, 0, NULL, &outNode);
385 ERR_(midi)("AUGraphNewNode cannot create outNode %s\n", wine_dbgstr_fourcc(err));
389 err = AUGraphOpen(*graph);
392 ERR_(midi)("AUGraphOpen return %s\n", wine_dbgstr_fourcc(err));
396 /* connecting the nodes */
397 err = AUGraphConnectNodeInput(*graph, synthNode, 0, outNode, 0);
400 ERR_(midi)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %s\n", wine_dbgstr_fourcc(err));
404 /* Get the synth unit */
405 err = AUGraphGetNodeInfo(*graph, synthNode, 0, 0, 0, synth);
408 ERR_(midi)("AUGraphGetNodeInfo return %s\n", wine_dbgstr_fourcc(err));
415 int SynthUnit_Initialize(AudioUnit synth, AUGraph graph)
417 OSStatus err = noErr;
419 err = AUGraphInitialize(graph);
422 ERR_(midi)("AUGraphInitialize(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
426 err = AUGraphStart(graph);
429 ERR_(midi)("AUGraphStart(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
436 int SynthUnit_Close(AUGraph graph)
438 OSStatus err = noErr;
440 err = AUGraphStop(graph);
443 ERR_(midi)("AUGraphStop(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
447 err = DisposeAUGraph(graph);
450 ERR_(midi)("DisposeAUGraph(%p) return %s\n", graph, wine_dbgstr_fourcc(err));