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 #include <AudioUnit/AudioUnit.h>
26 #include <AudioToolbox/AudioToolbox.h>
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wave);
32 WINE_DECLARE_DEBUG_CHANNEL(midi);
34 extern OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
35 AudioUnitRenderActionFlags *ioActionFlags,
36 const AudioTimeStamp *inTimeStamp,
38 UInt32 inNumberFrames,
39 AudioBufferList *ioData);
41 extern OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
42 AudioUnitRenderActionFlags *ioActionFlags,
43 const AudioTimeStamp *inTimeStamp,
45 UInt32 inNumberFrames,
46 AudioBufferList *ioData);
48 int AudioUnit_CreateDefaultAudioUnit(void *wwo, AudioUnit *au)
52 ComponentDescription desc;
53 AURenderCallbackStruct callbackStruct;
55 desc.componentType = kAudioUnitType_Output;
56 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
57 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
58 desc.componentFlags = 0;
59 desc.componentFlagsMask = 0;
61 comp = FindNextComponent(NULL, &desc);
65 err = OpenAComponent(comp, au);
69 callbackStruct.inputProc = CoreAudio_woAudioUnitIOProc;
70 callbackStruct.inputProcRefCon = wwo;
72 err = AudioUnitSetProperty( *au,
73 kAudioUnitProperty_SetRenderCallback,
74 kAudioUnitScope_Input,
77 sizeof(callbackStruct));
78 return (err == noErr);
81 int AudioUnit_CloseAudioUnit(AudioUnit au)
83 OSStatus err = CloseComponent(au);
84 return (err == noErr);
87 int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *stream)
91 err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
92 0, stream, sizeof(*stream));
96 ERR("AudioUnitSetProperty return an error %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
100 err = AudioUnitInitialize(au);
103 ERR("AudioUnitInitialize return an error %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
109 int AudioUnit_SetVolume(AudioUnit au, float left, float right)
111 OSStatus err = noErr;
113 err = AudioUnitSetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left, 0);
117 ERR("AudioUnitSetParameter return an error %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
123 int AudioUnit_GetVolume(AudioUnit au, float *left, float *right)
125 OSStatus err = noErr;
127 err = AudioUnitGetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left);
130 ERR("AudioUnitGetParameter return an error %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
138 /* FIXME: implement sample rate conversion on input */
139 int AudioUnit_GetInputDeviceSampleRate(void)
141 AudioDeviceID defaultInputDevice;
146 param = sizeof(defaultInputDevice);
147 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, ¶m, &defaultInputDevice);
148 if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
150 ERR("Couldn't get the default audio input device ID: %08lx\n", err);
154 param = sizeof(sampleRate);
155 err = AudioDeviceGetProperty(defaultInputDevice, 0, 1, kAudioDevicePropertyNominalSampleRate, ¶m, &sampleRate);
158 ERR("Couldn't get the device sample rate: %08lx\n", err);
166 int AudioUnit_CreateInputUnit(void* wwi, AudioUnit* out_au,
167 WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample,
168 UInt32* outFrameCount)
170 OSStatus err = noErr;
171 ComponentDescription description;
175 AURenderCallbackStruct callback;
176 AudioDeviceID defaultInputDevice;
177 AudioStreamBasicDescription desiredFormat;
182 ERR("Invalid parameter\n");
186 /* Open the AudioOutputUnit */
187 description.componentType = kAudioUnitType_Output;
188 description.componentSubType = kAudioUnitSubType_HALOutput;
189 description.componentManufacturer = kAudioUnitManufacturer_Apple;
190 description.componentFlags = 0;
191 description.componentFlagsMask = 0;
193 component = FindNextComponent(NULL, &description);
196 ERR("FindNextComponent(kAudioUnitSubType_HALOutput) failed\n");
200 err = OpenAComponent(component, &au);
201 if (err != noErr || au == NULL)
203 ERR("OpenAComponent failed: %08lx\n", err);
207 /* Configure the AudioOutputUnit */
208 /* The AUHAL has two buses (AKA elements). Bus 0 is output from the app
209 * to the device. Bus 1 is input from the device to the app. Each bus
210 * has two ends (AKA scopes). Data goes from the input scope to the
211 * output scope. The terminology is somewhat confusing because the terms
212 * "input" and "output" have two meanings. Here's a summary:
214 * Bus 0, input scope: refers to the source of data to be output as sound
215 * Bus 0, output scope: refers to the actual sound output device
216 * Bus 1, input scope: refers to the actual sound input device
217 * Bus 1, output scope: refers to the destination of data received by the input device
220 /* Enable input on the AUHAL */
222 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, ¶m, sizeof(param));
225 ERR("Couldn't enable input on AUHAL: %08lx\n", err);
229 /* Disable Output on the AUHAL */
231 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, ¶m, sizeof(param));
234 ERR("Couldn't disable output on AUHAL: %08lx\n", err);
238 /* Find the default input device */
239 param = sizeof(defaultInputDevice);
240 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, ¶m, &defaultInputDevice);
241 if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
243 ERR("Couldn't get the default audio device ID: %08lx\n", err);
247 /* Set the current device to the default input device. */
248 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &defaultInputDevice, sizeof(defaultInputDevice));
251 ERR("Couldn't set current device of AUHAL to default input device: %08lx\n", err);
255 /* Setup render callback */
256 /* This will be called when the AUHAL has input data. However, it won't
257 * be passed the data itself. The callback will have to all AudioUnitRender. */
258 callback.inputProc = CoreAudio_wiAudioUnitIOProc;
259 callback.inputProcRefCon = wwi;
260 err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback));
263 ERR("Couldn't set input callback of AUHAL: %08lx\n", err);
267 /* Setup the desired data format. */
268 /* FIXME: implement sample rate conversion on input. We shouldn't set
269 * the mSampleRate of this to the desired sample rate. We need to query
270 * the input device and use that. If they don't match, we need to set up
271 * an AUConverter to do the sample rate conversion on a separate thread. */
272 desiredFormat.mFormatID = kAudioFormatLinearPCM;
273 desiredFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked;
274 if (wBitsPerSample != 8)
275 desiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
276 desiredFormat.mSampleRate = nSamplesPerSec;
277 desiredFormat.mChannelsPerFrame = nChannels;
278 desiredFormat.mFramesPerPacket = 1;
279 desiredFormat.mBitsPerChannel = wBitsPerSample;
280 desiredFormat.mBytesPerFrame = desiredFormat.mBitsPerChannel * desiredFormat.mChannelsPerFrame / 8;
281 desiredFormat.mBytesPerPacket = desiredFormat.mBytesPerFrame * desiredFormat.mFramesPerPacket;
283 /* Set the AudioOutputUnit output data format */
284 err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &desiredFormat, sizeof(desiredFormat));
287 ERR("Couldn't set desired input format of AUHAL: %08lx\n", err);
291 /* Get the number of frames in the IO buffer(s) */
292 param = sizeof(*outFrameCount);
293 err = AudioUnitGetProperty(au, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, outFrameCount, ¶m);
296 ERR("Failed to get audio sample size: %08lx\n", err);
300 TRACE("Frame count: %lu\n", *outFrameCount);
302 /* Initialize the AU */
303 err = AudioUnitInitialize(au);
306 ERR("Failed to initialize AU: %08lx\n", err);
323 int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth)
326 ComponentDescription desc;
330 err = NewAUGraph(graph);
333 ERR_(midi)("NewAUGraph return %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
337 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
338 desc.componentFlags = 0;
339 desc.componentFlagsMask = 0;
341 /* create synth node */
342 desc.componentType = kAudioUnitType_MusicDevice;
343 desc.componentSubType = kAudioUnitSubType_DLSSynth;
345 err = AUGraphNewNode(*graph, &desc, 0, NULL, &synthNode);
348 ERR_(midi)("AUGraphNewNode cannot create synthNode : %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
352 /* create out node */
353 desc.componentType = kAudioUnitType_Output;
354 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
356 err = AUGraphNewNode(*graph, &desc, 0, NULL, &outNode);
359 ERR_(midi)("AUGraphNewNode cannot create outNode %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
363 err = AUGraphOpen(*graph);
366 ERR_(midi)("AUGraphOpen return %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
370 /* connecting the nodes */
371 err = AUGraphConnectNodeInput(*graph, synthNode, 0, outNode, 0);
374 ERR_(midi)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
378 /* Get the synth unit */
379 err = AUGraphGetNodeInfo(*graph, synthNode, 0, 0, 0, synth);
382 ERR_(midi)("AUGraphGetNodeInfo return %c%c%c%c\n", (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
389 int SynthUnit_Initialize(AudioUnit synth, AUGraph graph)
391 OSStatus err = noErr;
393 err = AUGraphInitialize(graph);
396 ERR_(midi)("AUGraphInitialize(%p) return %c%c%c%c\n", graph, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
400 err = AUGraphStart(graph);
403 ERR_(midi)("AUGraphStart(%p) return %c%c%c%c\n", graph, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
410 int SynthUnit_Close(AUGraph graph)
412 OSStatus err = noErr;
414 err = AUGraphStop(graph);
417 ERR_(midi)("AUGraphStop(%p) return %c%c%c%c\n", graph, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
421 err = DisposeAUGraph(graph);
424 ERR_(midi)("DisposeAUGraph(%p) return %c%c%c%c\n", graph, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);