audiounit.c 5.33 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 32
#undef ULONG
#undef HRESULT
#undef STDMETHODCALLTYPE
33
#include "coreaudio.h"
34 35
#include "wine/debug.h"

36
#ifndef HAVE_AUDIOUNIT_AUDIOCOMPONENT_H
37
/* Define new AudioComponent Manager types for compatibility's sake */
38 39 40
typedef ComponentDescription AudioComponentDescription;
#endif

41 42 43 44 45 46 47 48 49 50 51 52
#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

53
WINE_DEFAULT_DEBUG_CHANNEL(wave);
54
WINE_DECLARE_DEBUG_CHANNEL(midi);
55 56 57 58

int AudioUnit_SetVolume(AudioUnit au, float left, float right)
{
    OSStatus err = noErr;
59 60 61
    static int once;

    if (!once++) FIXME("independent left/right volume not implemented (%f, %f)\n", left, right);
62 63 64 65 66
   
    err = AudioUnitSetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left, 0);
                                
    if (err != noErr)
    {
67
        ERR("AudioUnitSetParameter return an error %s\n", wine_dbgstr_fourcc(err));
68 69 70 71 72 73 74 75
        return 0;
    }
    return 1;
}

int AudioUnit_GetVolume(AudioUnit au, float *left, float *right)
{
    OSStatus err = noErr;
76 77 78
    static int once;

    if (!once++) FIXME("independent left/right volume not implemented\n");
79 80 81 82
    
    err = AudioUnitGetParameter(au, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, left);
    if (err != noErr)
    {
83
        ERR("AudioUnitGetParameter return an error %s\n", wine_dbgstr_fourcc(err));
84 85 86 87 88
        return 0;
    }
    *right = *left;
    return 1;
}
89 90


91 92 93 94 95 96
/*
 *  MIDI Synth Unit
 */
int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth)
{
    OSStatus err;
97
    AudioComponentDescription desc;
98 99 100 101 102 103
    AUNode synthNode;
    AUNode outNode;

    err = NewAUGraph(graph);
    if (err != noErr)
    {
104
        ERR_(midi)("NewAUGraph return %s\n", wine_dbgstr_fourcc(err));
105 106 107 108 109 110 111 112 113 114 115
        return 0;
    }

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

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

116
    err = AUGraphAddNode(*graph, &desc, &synthNode);
117 118
    if (err != noErr)
    {
119
        ERR_(midi)("AUGraphAddNode cannot create synthNode : %s\n", wine_dbgstr_fourcc(err));
120 121 122 123 124 125 126
        return 0;
    }

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

127
    err = AUGraphAddNode(*graph, &desc, &outNode);
128 129
    if (err != noErr)
    {
130
        ERR_(midi)("AUGraphAddNode cannot create outNode %s\n", wine_dbgstr_fourcc(err));
131 132 133 134 135 136
        return 0;
    }

    err = AUGraphOpen(*graph);
    if (err != noErr)
    {
137
        ERR_(midi)("AUGraphOpen return %s\n", wine_dbgstr_fourcc(err));
138 139 140 141 142 143 144
        return 0;
    }

    /* connecting the nodes */
    err = AUGraphConnectNodeInput(*graph, synthNode, 0, outNode, 0);
    if (err != noErr)
    {
145
        ERR_(midi)("AUGraphConnectNodeInput cannot connect synthNode to outNode : %s\n", wine_dbgstr_fourcc(err));
146 147 148 149
        return 0;
    }

    /* Get the synth unit */
150
    err = AUGraphNodeInfo(*graph, synthNode, 0, synth);
151 152
    if (err != noErr)
    {
153
        ERR_(midi)("AUGraphNodeInfo return %s\n", wine_dbgstr_fourcc(err));
154 155 156 157 158 159 160 161 162 163 164 165 166
        return 0;
    }

    return 1;
}

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

    err = AUGraphInitialize(graph);
    if (err != noErr)
    {
167
        ERR_(midi)("AUGraphInitialize(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
168 169 170 171 172 173
        return 0;
    }

    err = AUGraphStart(graph);
    if (err != noErr)
    {
174
        ERR_(midi)("AUGraphStart(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
175 176 177 178 179 180 181 182 183 184 185 186 187
        return 0;
    }

    return 1;
}

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

    err = AUGraphStop(graph);
    if (err != noErr)
    {
188
        ERR_(midi)("AUGraphStop(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
189 190 191 192 193 194
        return 0;
    }

    err = DisposeAUGraph(graph);
    if (err != noErr)
    {
195
        ERR_(midi)("DisposeAUGraph(%p) return %s\n", graph, wine_dbgstr_fourcc(err));
196 197 198 199 200
        return 0;
    }

    return 1;
}