audiounit.c 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Wine Driver for CoreAudio / AudioUnit
 *
 * Copyright 2005, 2006 Emmanuel Maillard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
 */

#include "config.h"

23 24
#define ULONG CoreFoundation_ULONG
#define HRESULT CoreFoundation_HRESULT
25
#ifndef HAVE_AUDIOUNIT_AUDIOCOMPONENT_H
26
#include <CoreServices/CoreServices.h>
27
#endif
28
#include <AudioUnit/AudioUnit.h>
29
#include <AudioToolbox/AudioToolbox.h>
30 31
#undef ULONG
#undef HRESULT
32

33
#undef DPRINTF
34
#undef STDMETHODCALLTYPE
35
#include "coreaudio.h"
36 37
#include "wine/debug.h"

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifndef HAVE_AUDIOUNIT_AUDIOCOMPONENT_H
/* Define new AudioComponent Manager functions for compatibility's sake */
typedef Component AudioComponent;
typedef ComponentDescription AudioComponentDescription;
typedef ComponentInstance AudioComponentInstance;

static inline AudioComponent AudioComponentFindNext(AudioComponent ac, AudioComponentDescription *desc)
{
    return FindNextComponent(ac, desc);
}

static inline OSStatus AudioComponentInstanceNew(AudioComponent ac, AudioComponentInstance *aci)
{
    return OpenAComponent(ac, aci);
}

static inline OSStatus AudioComponentInstanceDispose(AudioComponentInstance aci)
{
    return CloseComponent(aci);
}
#endif

60 61 62 63 64 65 66 67 68 69 70 71
#ifndef HAVE_AUGRAPHADDNODE
static inline OSStatus AUGraphAddNode(AUGraph graph, const AudioComponentDescription *desc, AUNode *node)
{
    return AUGraphNewNode(graph, desc, 0, NULL, node);
}

static inline OSStatus AUGraphNodeInfo(AUGraph graph, AUNode node, AudioComponentDescription *desc, AudioUnit *au)
{
    return AUGraphGetNodeInfo(graph, node, desc, 0, NULL, au);
}
#endif

72
WINE_DEFAULT_DEBUG_CHANNEL(wave);
73
WINE_DECLARE_DEBUG_CHANNEL(midi);
74

75 76 77 78 79 80 81 82 83 84 85 86 87
static const char *streamDescription(const AudioStreamBasicDescription* stream)
{
    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",
        stream->mSampleRate,
        wine_dbgstr_fourcc(stream->mFormatID),
        stream->mFormatFlags,
        stream->mBytesPerPacket,
        stream->mFramesPerPacket,
        stream->mBytesPerFrame,
        stream->mChannelsPerFrame,
        stream->mBitsPerChannel);
}

88 89
int AudioUnit_CloseAudioUnit(AudioUnit au)
{
90
    OSStatus err = AudioComponentInstanceDispose(au);
91 92 93
    return (err == noErr);
}

94
int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *stream)
95 96 97
{
    OSStatus err = noErr;
        
98 99
    TRACE("input format: %s\n", streamDescription(stream));

100
    err = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
101 102
                                0, stream, sizeof(*stream));

103 104
    if (err != noErr)
    {
105
        ERR("AudioUnitSetProperty return an error %s\n", wine_dbgstr_fourcc(err));
106 107 108 109 110 111
        return 0;
    }
    
    err = AudioUnitInitialize(au);
    if (err != noErr)
    {
112
        ERR("AudioUnitInitialize return an error %s\n", wine_dbgstr_fourcc(err));
113 114 115 116 117 118 119 120
        return 0;
    }
    return 1;
}

int AudioUnit_SetVolume(AudioUnit au, float left, float right)
{
    OSStatus err = noErr;
121 122 123
    static int once;

    if (!once++) FIXME("independent left/right volume not implemented (%f, %f)\n", left, right);
124 125 126 127 128
   
    err = AudioUnitSetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left, 0);
                                
    if (err != noErr)
    {
129
        ERR("AudioUnitSetParameter return an error %s\n", wine_dbgstr_fourcc(err));
130 131 132 133 134 135 136 137
        return 0;
    }
    return 1;
}

int AudioUnit_GetVolume(AudioUnit au, float *left, float *right)
{
    OSStatus err = noErr;
138 139 140
    static int once;

    if (!once++) FIXME("independent left/right volume not implemented\n");
141 142 143 144
    
    err = AudioUnitGetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left);
    if (err != noErr)
    {
145
        ERR("AudioUnitGetParameter return an error %s\n", wine_dbgstr_fourcc(err));
146 147 148 149 150
        return 0;
    }
    *right = *left;
    return 1;
}
151 152


153 154 155 156 157
/* FIXME: implement sample rate conversion on input */
int AudioUnit_GetInputDeviceSampleRate(void)
{
    AudioDeviceID               defaultInputDevice;
    UInt32                      param;
158
    AudioObjectPropertyAddress  propertyAddress;
159 160 161 162
    Float64                     sampleRate;
    OSStatus                    err;

    param = sizeof(defaultInputDevice);
163 164 165 166
    propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice;
    propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
    propertyAddress.mElement = kAudioObjectPropertyElementMaster;
    err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &param, &defaultInputDevice);
167 168 169 170 171 172 173
    if (err != noErr || defaultInputDevice == kAudioDeviceUnknown)
    {
        ERR("Couldn't get the default audio input device ID: %08lx\n", err);
        return 0;
    }

    param = sizeof(sampleRate);
174 175 176
    propertyAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
    propertyAddress.mScope = kAudioDevicePropertyScopeInput;
    err = AudioObjectGetPropertyData(defaultInputDevice, &propertyAddress, 0, NULL, &param, &sampleRate);
177 178 179 180 181 182 183 184 185
    if (err != noErr)
    {
        ERR("Couldn't get the device sample rate: %08lx\n", err);
        return 0;
    }

    return sampleRate;
}

186 187 188 189 190 191
/*
 *  MIDI Synth Unit
 */
int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth)
{
    OSStatus err;
192
    AudioComponentDescription desc;
193 194 195 196 197 198
    AUNode synthNode;
    AUNode outNode;

    err = NewAUGraph(graph);
    if (err != noErr)
    {
199
        ERR_(midi)("NewAUGraph return %s\n", wine_dbgstr_fourcc(err));
200 201 202 203 204 205 206 207 208 209 210
        return 0;
    }

    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;

    /* create synth node */
    desc.componentType = kAudioUnitType_MusicDevice;
    desc.componentSubType = kAudioUnitSubType_DLSSynth;

211
    err = AUGraphAddNode(*graph, &desc, &synthNode);
212 213
    if (err != noErr)
    {
214
        ERR_(midi)("AUGraphAddNode cannot create synthNode : %s\n", wine_dbgstr_fourcc(err));
215 216 217 218 219 220 221
        return 0;
    }

    /* create out node */
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_DefaultOutput;

222
    err = AUGraphAddNode(*graph, &desc, &outNode);
223 224
    if (err != noErr)
    {
225
        ERR_(midi)("AUGraphAddNode cannot create outNode %s\n", wine_dbgstr_fourcc(err));
226 227 228 229 230 231
        return 0;
    }

    err = AUGraphOpen(*graph);
    if (err != noErr)
    {
232
        ERR_(midi)("AUGraphOpen return %s\n", wine_dbgstr_fourcc(err));
233 234 235 236 237 238 239
        return 0;
    }

    /* connecting the nodes */
    err = AUGraphConnectNodeInput(*graph, synthNode, 0, outNode, 0);
    if (err != noErr)
    {
240
        ERR_(midi)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %s\n", wine_dbgstr_fourcc(err));
241 242 243 244
        return 0;
    }

    /* Get the synth unit */
245
    err = AUGraphNodeInfo(*graph, synthNode, 0, synth);
246 247
    if (err != noErr)
    {
248
        ERR_(midi)("AUGraphNodeInfo return %s\n", wine_dbgstr_fourcc(err));
249 250 251 252 253 254 255 256 257 258 259 260 261
        return 0;
    }

    return 1;
}

int SynthUnit_Initialize(AudioUnit synth, AUGraph graph)
{
    OSStatus err = noErr;

    err = AUGraphInitialize(graph);
    if (err != noErr)
    {
262
        ERR_(midi)("AUGraphInitialize(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
263 264 265 266 267 268
        return 0;
    }

    err = AUGraphStart(graph);
    if (err != noErr)
    {
269
        ERR_(midi)("AUGraphStart(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
270 271 272 273 274 275 276 277 278 279 280 281 282
        return 0;
    }

    return 1;
}

int SynthUnit_Close(AUGraph graph)
{
    OSStatus err = noErr;

    err = AUGraphStop(graph);
    if (err != noErr)
    {
283
        ERR_(midi)("AUGraphStop(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
284 285 286 287 288 289
        return 0;
    }

    err = DisposeAUGraph(graph);
    if (err != noErr)
    {
290
        ERR_(midi)("DisposeAUGraph(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
291 292 293 294 295
        return 0;
    }

    return 1;
}