winecoreaudio: Fix declaration of CoreAudio_MIDIRelease to match definition.
[wine] / dlls / winecoreaudio.drv / audiounit.c
1 /*
2  * Wine Driver for CoreAudio / AudioUnit
3  *
4  * Copyright 2005, 2006 Emmanuel Maillard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_AUDIOUNIT_AUDIOUNIT_H
24
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>
30 #undef ULONG
31 #undef HRESULT
32
33 #undef DPRINTF
34 #undef STDMETHODCALLTYPE
35 #include "coreaudio.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(wave);
39 WINE_DECLARE_DEBUG_CHANNEL(midi);
40
41 static const char *streamDescription(const AudioStreamBasicDescription* stream)
42 {
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",
44         stream->mSampleRate,
45         wine_dbgstr_fourcc(stream->mFormatID),
46         stream->mFormatFlags,
47         stream->mBytesPerPacket,
48         stream->mFramesPerPacket,
49         stream->mBytesPerFrame,
50         stream->mChannelsPerFrame,
51         stream->mBitsPerChannel);
52 }
53
54 extern OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon, 
55                                 AudioUnitRenderActionFlags *ioActionFlags, 
56                                 const AudioTimeStamp *inTimeStamp, 
57                                 UInt32 inBusNumber, 
58                                 UInt32 inNumberFrames, 
59                                 AudioBufferList *ioData);
60
61 extern OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
62                                 AudioUnitRenderActionFlags *ioActionFlags,
63                                 const AudioTimeStamp *inTimeStamp,
64                                 UInt32 inBusNumber,
65                                 UInt32 inNumberFrames,
66                                 AudioBufferList *ioData);
67
68 int AudioUnit_CreateDefaultAudioUnit(void *wwo, AudioUnit *au)
69 {
70     OSStatus err;
71     Component comp;
72     ComponentDescription desc;
73     AURenderCallbackStruct callbackStruct;
74
75     TRACE("\n");
76     
77     desc.componentType = kAudioUnitType_Output;
78     desc.componentSubType = kAudioUnitSubType_DefaultOutput;
79     desc.componentManufacturer = kAudioUnitManufacturer_Apple;
80     desc.componentFlags = 0;
81     desc.componentFlagsMask = 0;
82
83     comp = FindNextComponent(NULL, &desc);
84     if (comp == NULL)
85         return 0;
86     
87     err = OpenAComponent(comp, au);
88     if (comp == NULL)
89         return 0;
90         
91     callbackStruct.inputProc = CoreAudio_woAudioUnitIOProc;
92     callbackStruct.inputProcRefCon = wwo;
93
94     err = AudioUnitSetProperty( *au, 
95                                 kAudioUnitProperty_SetRenderCallback, 
96                                 kAudioUnitScope_Input,
97                                 0, 
98                                 &callbackStruct, 
99                                 sizeof(callbackStruct));
100     return (err == noErr);
101 }
102
103 int AudioUnit_CloseAudioUnit(AudioUnit au)
104 {
105     OSStatus err = CloseComponent(au);
106     return (err == noErr);
107 }
108
109 int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *stream)
110 {
111     OSStatus err = noErr;
112         
113     TRACE("input format: %s\n", streamDescription(stream));
114
115     err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
116                                 0, stream, sizeof(*stream));
117
118     if (err != noErr)
119     {
120         ERR("AudioUnitSetProperty return an error %s\n", wine_dbgstr_fourcc(err));
121         return 0;
122     }
123     
124     err = AudioUnitInitialize(au);
125     if (err != noErr)
126     {
127         ERR("AudioUnitInitialize return an error %s\n", wine_dbgstr_fourcc(err));
128         return 0;
129     }
130     return 1;
131 }
132
133 int AudioUnit_SetVolume(AudioUnit au, float left, float right)
134 {
135     OSStatus err = noErr;
136     FIXME("independent left/right volume not implemented (%f, %f)\n", left, right);
137    
138     err = AudioUnitSetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left, 0);
139                                 
140     if (err != noErr)
141     {
142         ERR("AudioUnitSetParameter return an error %s\n", wine_dbgstr_fourcc(err));
143         return 0;
144     }
145     return 1;
146 }
147
148 int AudioUnit_GetVolume(AudioUnit au, float *left, float *right)
149 {
150     OSStatus err = noErr;
151     FIXME("independent left/right volume not implemented\n");
152     
153     err = AudioUnitGetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left);
154     if (err != noErr)
155     {
156         ERR("AudioUnitGetParameter return an error %s\n", wine_dbgstr_fourcc(err));
157         return 0;
158     }
159     *right = *left;
160     return 1;
161 }
162
163
164 /* FIXME: implement sample rate conversion on input */
165 int AudioUnit_GetInputDeviceSampleRate(void)
166 {
167     AudioDeviceID               defaultInputDevice;
168     UInt32                      param;
169     Float64                     sampleRate;
170     OSStatus                    err;
171
172     param = sizeof(defaultInputDevice);
173     err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &param, &defaultInputDevice);
174     if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
175     {
176         ERR("Couldn't get the default audio input device ID: %08lx\n", err);
177         return 0;
178     }
179
180     param = sizeof(sampleRate);
181     err = AudioDeviceGetProperty(defaultInputDevice, 0, 1, kAudioDevicePropertyNominalSampleRate, &param, &sampleRate);
182     if (err != noErr)
183     {
184         ERR("Couldn't get the device sample rate: %08lx\n", err);
185         return 0;
186     }
187
188     return sampleRate;
189 }
190
191
192 int AudioUnit_CreateInputUnit(void* wwi, AudioUnit* out_au,
193         WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample,
194         UInt32* outFrameCount)
195 {
196     OSStatus                    err = noErr;
197     ComponentDescription        description;
198     Component                   component;
199     AudioUnit                   au;
200     UInt32                      param;
201     AURenderCallbackStruct      callback;
202     AudioDeviceID               defaultInputDevice;
203     AudioStreamBasicDescription desiredFormat;
204
205
206     if (!outFrameCount)
207     {
208         ERR("Invalid parameter\n");
209         return 0;
210     }
211
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;
218
219     component = FindNextComponent(NULL, &description);
220     if (!component)
221     {
222         ERR("FindNextComponent(kAudioUnitSubType_HALOutput) failed\n");
223         return 0;
224     }
225
226     err = OpenAComponent(component, &au);
227     if (err != noErr || au == NULL)
228     {
229         ERR("OpenAComponent failed: %08lx\n", err);
230         return 0;
231     }
232
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:
239      *
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
244      */
245
246     /* Enable input on the AUHAL */
247     param = 1;
248     err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &param, sizeof(param));
249     if (err != noErr)
250     {
251         ERR("Couldn't enable input on AUHAL: %08lx\n", err);
252         goto error;
253     }
254
255     /* Disable Output on the AUHAL */
256     param = 0;
257     err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &param, sizeof(param));
258     if (err != noErr)
259     {
260         ERR("Couldn't disable output on AUHAL: %08lx\n", err);
261         goto error;
262     }
263
264     /* Find the default input device */
265     param = sizeof(defaultInputDevice);
266     err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &param, &defaultInputDevice);
267     if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
268     {
269         ERR("Couldn't get the default audio device ID: %08lx\n", err);
270         goto error;
271     }
272
273     /* Set the current device to the default input device. */
274     err = AudioUnitSetProperty(au, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &defaultInputDevice, sizeof(defaultInputDevice));
275     if (err != noErr)
276     {
277         ERR("Couldn't set current device of AUHAL to default input device: %08lx\n", err);
278         goto error;
279     }
280
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));
287     if (err != noErr)
288     {
289         ERR("Couldn't set input callback of AUHAL: %08lx\n", err);
290         goto error;
291     }
292
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;
308
309     /* Set the AudioOutputUnit output data format */
310     err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &desiredFormat, sizeof(desiredFormat));
311     if (err != noErr)
312     {
313         ERR("Couldn't set desired input format of AUHAL: %08lx\n", err);
314         goto error;
315     }
316
317     /* Get the number of frames in the IO buffer(s) */
318     param = sizeof(*outFrameCount);
319     err = AudioUnitGetProperty(au, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, outFrameCount, &param);
320     if (err != noErr)
321     {
322         ERR("Failed to get audio sample size: %08lx\n", err);
323         goto error;
324     }
325
326     TRACE("Frame count: %lu\n", *outFrameCount);
327
328     /* Initialize the AU */
329     err = AudioUnitInitialize(au);
330     if (err != noErr)
331     {
332         ERR("Failed to initialize AU: %08lx\n", err);
333         goto error;
334     }
335
336     *out_au = au;
337
338     return 1;
339
340 error:
341     if (au)
342         CloseComponent(au);
343     return 0;
344 }
345
346 /*
347  *  MIDI Synth Unit
348  */
349 int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth)
350 {
351     OSStatus err;
352     ComponentDescription desc;
353     AUNode synthNode;
354     AUNode outNode;
355
356     err = NewAUGraph(graph);
357     if (err != noErr)
358     {
359         ERR_(midi)("NewAUGraph return %s\n", wine_dbgstr_fourcc(err));
360         return 0;
361     }
362
363     desc.componentManufacturer = kAudioUnitManufacturer_Apple;
364     desc.componentFlags = 0;
365     desc.componentFlagsMask = 0;
366
367     /* create synth node */
368     desc.componentType = kAudioUnitType_MusicDevice;
369     desc.componentSubType = kAudioUnitSubType_DLSSynth;
370
371     err = AUGraphNewNode(*graph, &desc, 0, NULL, &synthNode);
372     if (err != noErr)
373     {
374         ERR_(midi)("AUGraphNewNode cannot create synthNode : %s\n", wine_dbgstr_fourcc(err));
375         return 0;
376     }
377
378     /* create out node */
379     desc.componentType = kAudioUnitType_Output;
380     desc.componentSubType = kAudioUnitSubType_DefaultOutput;
381
382     err = AUGraphNewNode(*graph, &desc, 0, NULL, &outNode);
383     if (err != noErr)
384     {
385         ERR_(midi)("AUGraphNewNode cannot create outNode %s\n", wine_dbgstr_fourcc(err));
386         return 0;
387     }
388
389     err = AUGraphOpen(*graph);
390     if (err != noErr)
391     {
392         ERR_(midi)("AUGraphOpen return %s\n", wine_dbgstr_fourcc(err));
393         return 0;
394     }
395
396     /* connecting the nodes */
397     err = AUGraphConnectNodeInput(*graph, synthNode, 0, outNode, 0);
398     if (err != noErr)
399     {
400         ERR_(midi)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %s\n", wine_dbgstr_fourcc(err));
401         return 0;
402     }
403
404     /* Get the synth unit */
405     err = AUGraphGetNodeInfo(*graph, synthNode, 0, 0, 0, synth);
406     if (err != noErr)
407     {
408         ERR_(midi)("AUGraphGetNodeInfo return %s\n", wine_dbgstr_fourcc(err));
409         return 0;
410     }
411
412     return 1;
413 }
414
415 int SynthUnit_Initialize(AudioUnit synth, AUGraph graph)
416 {
417     OSStatus err = noErr;
418
419     err = AUGraphInitialize(graph);
420     if (err != noErr)
421     {
422         ERR_(midi)("AUGraphInitialize(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
423         return 0;
424     }
425
426     err = AUGraphStart(graph);
427     if (err != noErr)
428     {
429         ERR_(midi)("AUGraphStart(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
430         return 0;
431     }
432
433     return 1;
434 }
435
436 int SynthUnit_Close(AUGraph graph)
437 {
438     OSStatus err = noErr;
439
440     err = AUGraphStop(graph);
441     if (err != noErr)
442     {
443         ERR_(midi)("AUGraphStop(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
444         return 0;
445     }
446
447     err = DisposeAUGraph(graph);
448     if (err != noErr)
449     {
450         ERR_(midi)("DisposeAUGraph(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
451         return 0;
452     }
453
454     return 1;
455 }
456
457 #endif