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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
26 #ifdef HAVE_AUDIOUNIT_AUDIOUNIT_H
27 #include <AudioUnit/AudioUnit.h>
28 #include <AudioToolbox/AudioToolbox.h>
30 WINE_DECLARE_DEBUG_CHANNEL(midi
);
32 extern OSStatus
CoreAudio_woAudioUnitIOProc(void *inRefCon
,
33 AudioUnitRenderActionFlags
*ioActionFlags
,
34 const AudioTimeStamp
*inTimeStamp
,
36 UInt32 inNumberFrames
,
37 AudioBufferList
*ioData
);
39 extern OSStatus
CoreAudio_wiAudioUnitIOProc(void *inRefCon
,
40 AudioUnitRenderActionFlags
*ioActionFlags
,
41 const AudioTimeStamp
*inTimeStamp
,
43 UInt32 inNumberFrames
,
44 AudioBufferList
*ioData
);
46 int AudioUnit_CreateDefaultAudioUnit(void *wwo
, AudioUnit
*au
)
50 ComponentDescription desc
;
51 AURenderCallbackStruct callbackStruct
;
53 desc
.componentType
= kAudioUnitType_Output
;
54 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
55 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
56 desc
.componentFlags
= 0;
57 desc
.componentFlagsMask
= 0;
59 comp
= FindNextComponent(NULL
, &desc
);
63 err
= OpenAComponent(comp
, au
);
67 callbackStruct
.inputProc
= CoreAudio_woAudioUnitIOProc
;
68 callbackStruct
.inputProcRefCon
= wwo
;
70 err
= AudioUnitSetProperty( *au
,
71 kAudioUnitProperty_SetRenderCallback
,
72 kAudioUnitScope_Input
,
75 sizeof(callbackStruct
));
76 return (err
== noErr
);
79 int AudioUnit_CloseAudioUnit(AudioUnit au
)
81 OSStatus err
= CloseComponent(au
);
82 return (err
== noErr
);
85 int AudioUnit_InitializeWithStreamDescription(AudioUnit au
, AudioStreamBasicDescription
*stream
)
89 err
= AudioUnitSetProperty(au
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Input
,
90 0, stream
, sizeof(*stream
));
94 ERR("AudioUnitSetProperty return an error %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
98 err
= AudioUnitInitialize(au
);
101 ERR("AudioUnitInitialize return an error %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
107 int AudioUnit_SetVolume(AudioUnit au
, float left
, float right
)
109 OSStatus err
= noErr
;
111 err
= AudioUnitSetParameter(au
, kHALOutputParam_Volume
, kAudioUnitParameterFlag_Output
, 0, left
, 0);
115 ERR("AudioUnitSetParameter return an error %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
121 int AudioUnit_GetVolume(AudioUnit au
, float *left
, float *right
)
123 OSStatus err
= noErr
;
125 err
= AudioUnitGetParameter(au
, kHALOutputParam_Volume
, kAudioUnitParameterFlag_Output
, 0, left
);
128 ERR("AudioUnitGetParameter return an error %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
136 /* FIXME: implement sample rate conversion on input */
137 int AudioUnit_GetInputDeviceSampleRate(void)
139 AudioDeviceID defaultInputDevice
;
144 param
= sizeof(defaultInputDevice
);
145 err
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice
, ¶m
, &defaultInputDevice
);
146 if (err
!= noErr
|| defaultInputDevice
== kAudioDeviceUnknown
)
148 ERR("Couldn't get the default audio input device ID: %08lx\n", err
);
152 param
= sizeof(sampleRate
);
153 err
= AudioDeviceGetProperty(defaultInputDevice
, 0, 1, kAudioDevicePropertyNominalSampleRate
, ¶m
, &sampleRate
);
156 ERR("Couldn't get the device sample rate: %08lx\n", err
);
164 int AudioUnit_CreateInputUnit(void* wwi
, AudioUnit
* out_au
,
165 WORD nChannels
, DWORD nSamplesPerSec
, WORD wBitsPerSample
,
166 UInt32
* outFrameCount
)
168 OSStatus err
= noErr
;
169 ComponentDescription description
;
173 AURenderCallbackStruct callback
;
174 AudioDeviceID defaultInputDevice
;
175 AudioStreamBasicDescription desiredFormat
;
180 ERR("Invalid parameter\n");
184 /* Open the AudioOutputUnit */
185 description
.componentType
= kAudioUnitType_Output
;
186 description
.componentSubType
= kAudioUnitSubType_HALOutput
;
187 description
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
188 description
.componentFlags
= 0;
189 description
.componentFlagsMask
= 0;
191 component
= FindNextComponent(NULL
, &description
);
194 ERR("FindNextComponent(kAudioUnitSubType_HALOutput) failed\n");
198 err
= OpenAComponent(component
, &au
);
199 if (err
!= noErr
|| au
== NULL
)
201 ERR("OpenAComponent failed: %08lx\n", err
);
205 /* Configure the AudioOutputUnit */
206 /* The AUHAL has two buses (AKA elements). Bus 0 is output from the app
207 * to the device. Bus 1 is input from the device to the app. Each bus
208 * has two ends (AKA scopes). Data goes from the input scope to the
209 * output scope. The terminology is somewhat confusing because the terms
210 * "input" and "output" have two meanings. Here's a summary:
212 * Bus 0, input scope: refers to the source of data to be output as sound
213 * Bus 0, output scope: refers to the actual sound output device
214 * Bus 1, input scope: refers to the actual sound input device
215 * Bus 1, output scope: refers to the destination of data received by the input device
218 /* Enable input on the AUHAL */
220 err
= AudioUnitSetProperty(au
, kAudioOutputUnitProperty_EnableIO
, kAudioUnitScope_Input
, 1, ¶m
, sizeof(param
));
223 ERR("Couldn't enable input on AUHAL: %08lx\n", err
);
227 /* Disable Output on the AUHAL */
229 err
= AudioUnitSetProperty(au
, kAudioOutputUnitProperty_EnableIO
, kAudioUnitScope_Output
, 0, ¶m
, sizeof(param
));
232 ERR("Couldn't disable output on AUHAL: %08lx\n", err
);
236 /* Find the default input device */
237 param
= sizeof(defaultInputDevice
);
238 err
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice
, ¶m
, &defaultInputDevice
);
239 if (err
!= noErr
|| defaultInputDevice
== kAudioDeviceUnknown
)
241 ERR("Couldn't get the default audio device ID: %08lx\n", err
);
245 /* Set the current device to the default input device. */
246 err
= AudioUnitSetProperty(au
, kAudioOutputUnitProperty_CurrentDevice
, kAudioUnitScope_Global
, 0, &defaultInputDevice
, sizeof(defaultInputDevice
));
249 ERR("Couldn't set current device of AUHAL to default input device: %08lx\n", err
);
253 /* Setup render callback */
254 /* This will be called when the AUHAL has input data. However, it won't
255 * be passed the data itself. The callback will have to all AudioUnitRender. */
256 callback
.inputProc
= CoreAudio_wiAudioUnitIOProc
;
257 callback
.inputProcRefCon
= wwi
;
258 err
= AudioUnitSetProperty(au
, kAudioOutputUnitProperty_SetInputCallback
, kAudioUnitScope_Global
, 0, &callback
, sizeof(callback
));
261 ERR("Couldn't set input callback of AUHAL: %08lx\n", err
);
265 /* Setup the desired data format. */
266 /* FIXME: implement sample rate conversion on input. We shouldn't set
267 * the mSampleRate of this to the desired sample rate. We need to query
268 * the input device and use that. If they don't match, we need to set up
269 * an AUConverter to do the sample rate conversion on a separate thread. */
270 desiredFormat
.mFormatID
= kAudioFormatLinearPCM
;
271 desiredFormat
.mFormatFlags
= kLinearPCMFormatFlagIsPacked
;
272 if (wBitsPerSample
!= 8)
273 desiredFormat
.mFormatFlags
|= kLinearPCMFormatFlagIsSignedInteger
;
274 desiredFormat
.mSampleRate
= nSamplesPerSec
;
275 desiredFormat
.mChannelsPerFrame
= nChannels
;
276 desiredFormat
.mFramesPerPacket
= 1;
277 desiredFormat
.mBitsPerChannel
= wBitsPerSample
;
278 desiredFormat
.mBytesPerFrame
= desiredFormat
.mBitsPerChannel
* desiredFormat
.mChannelsPerFrame
/ 8;
279 desiredFormat
.mBytesPerPacket
= desiredFormat
.mBytesPerFrame
* desiredFormat
.mFramesPerPacket
;
281 /* Set the AudioOutputUnit output data format */
282 err
= AudioUnitSetProperty(au
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Output
, 1, &desiredFormat
, sizeof(desiredFormat
));
285 ERR("Couldn't set desired input format of AUHAL: %08lx\n", err
);
289 /* Get the number of frames in the IO buffer(s) */
290 param
= sizeof(*outFrameCount
);
291 err
= AudioUnitGetProperty(au
, kAudioDevicePropertyBufferFrameSize
, kAudioUnitScope_Global
, 0, outFrameCount
, ¶m
);
294 ERR("Failed to get audio sample size: %08lx\n", err
);
298 TRACE("Frame count: %lu\n", *outFrameCount
);
300 /* Initialize the AU */
301 err
= AudioUnitInitialize(au
);
304 ERR("Failed to initialize AU: %08lx\n", err
);
321 int SynthUnit_CreateDefaultSynthUnit(AUGraph
*graph
, AudioUnit
*synth
)
324 ComponentDescription desc
;
328 err
= NewAUGraph(graph
);
331 ERR_(midi
)("NewAUGraph return %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
335 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
336 desc
.componentFlags
= 0;
337 desc
.componentFlagsMask
= 0;
339 /* create synth node */
340 desc
.componentType
= kAudioUnitType_MusicDevice
;
341 desc
.componentSubType
= kAudioUnitSubType_DLSSynth
;
343 err
= AUGraphNewNode(*graph
, &desc
, 0, NULL
, &synthNode
);
346 ERR_(midi
)("AUGraphNewNode cannot create synthNode : %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
350 /* create out node */
351 desc
.componentType
= kAudioUnitType_Output
;
352 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
354 err
= AUGraphNewNode(*graph
, &desc
, 0, NULL
, &outNode
);
357 ERR_(midi
)("AUGraphNewNode cannot create outNode %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
361 err
= AUGraphOpen(*graph
);
364 ERR_(midi
)("AUGraphOpen return %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
368 /* connecting the nodes */
369 err
= AUGraphConnectNodeInput(*graph
, synthNode
, 0, outNode
, 0);
372 ERR_(midi
)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
376 /* Get the synth unit */
377 err
= AUGraphGetNodeInfo(*graph
, synthNode
, 0, 0, 0, synth
);
380 ERR_(midi
)("AUGraphGetNodeInfo return %c%c%c%c\n", (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
387 int SynthUnit_Initialize(AudioUnit synth
, AUGraph graph
)
389 OSStatus err
= noErr
;
391 err
= AUGraphInitialize(graph
);
394 ERR_(midi
)("AUGraphInitialize(%p) return %c%c%c%c\n", graph
, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
398 err
= AUGraphStart(graph
);
401 ERR_(midi
)("AUGraphStart(%p) return %c%c%c%c\n", graph
, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
408 int SynthUnit_Close(AUGraph graph
)
410 OSStatus err
= noErr
;
412 err
= AUGraphStop(graph
);
415 ERR_(midi
)("AUGraphStop(%p) return %c%c%c%c\n", graph
, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
419 err
= DisposeAUGraph(graph
);
422 ERR_(midi
)("DisposeAUGraph(%p) return %c%c%c%c\n", graph
, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);