JackCoreAudioDriver.h 6.91 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __JackCoreAudioDriver__
#define __JackCoreAudioDriver__

#include <AudioToolbox/AudioConverter.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include "JackAudioDriver.h"
#include "JackTime.h"

29
30
31
32
#include <vector>

using namespace std;

sletz's avatar
sletz committed
33
34
35
36
37
namespace Jack
{

#define kVersion 102

38
39
40
41
42
typedef	UInt8	CAAudioHardwareDeviceSectionID;
#define	kAudioDeviceSectionInput	((CAAudioHardwareDeviceSectionID)0x01)
#define	kAudioDeviceSectionOutput	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionGlobal	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionWildcard	((CAAudioHardwareDeviceSectionID)0xFF)
43
44
    
#define WAIT_COUNTER 60
45

sletz's avatar
sletz committed
46
47
/*!
\brief The CoreAudio driver.
sletz's avatar
sletz committed
48

sletz's avatar
sletz committed
49
50
51
\todo hardware monitoring
*/

52
class JackCoreAudioDriver : public JackAudioDriver
sletz's avatar
sletz committed
53
54
55
{

    private:
sletz's avatar
sletz committed
56
57

        AudioUnit fAUHAL;
sletz's avatar
sletz committed
58
59
60
61

        AudioBufferList* fJackInputData;
        AudioBufferList* fDriverOutputData;

62
63
        AudioDeviceID fDeviceID;    // Used "duplex" device
        AudioObjectID fPluginID;    // Used for aggregate device
sletz's avatar
sletz committed
64

sletz's avatar
sletz committed
65
66
        AudioUnitRenderActionFlags* fActionFags;
        AudioTimeStamp* fCurrentTime;
sletz's avatar
sletz committed
67
68

        bool fState;
69
        bool fHogged;
sletz's avatar
sletz committed
70

sletz's avatar
sletz committed
71
        // Initial state
sletz's avatar
sletz committed
72
73
74
75
76
77
78
79
80
81
        bool fCapturing;
        bool fPlaying;

        int fInChannels;
        int fOutChannels;

        char fCaptureUID[256];
        char fPlaybackUID[256];

        bool fMonitor;
82
        float fIOUsage;
83
        float fComputationGrain;
84
        bool fClockDriftCompensate;
85
86
    
        /*    
87
88
89
    #ifdef MAC_OS_X_VERSION_10_5
        AudioDeviceIOProcID fMesureCallbackID;
    #endif
90
        */
91
    
sletz's avatar
sletz committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        static	OSStatus Render(void *inRefCon,
                               AudioUnitRenderActionFlags *ioActionFlags,
                               const AudioTimeStamp *inTimeStamp,
                               UInt32 inBusNumber,
                               UInt32 inNumberFrames,
                               AudioBufferList *ioData);

        static OSStatus MeasureCallback(AudioDeviceID inDevice,
                                        const AudioTimeStamp* inNow,
                                        const AudioBufferList* inInputData,
                                        const AudioTimeStamp* inInputTime,
                                        AudioBufferList* outOutputData,
                                        const AudioTimeStamp* inOutputTime,
                                        void* inClientData);

        static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
sletz's avatar
sletz committed
108
109
110
111
112
113
114
115
116
117
118
                UInt32 inChannel,
                Boolean	isInput,
                AudioDevicePropertyID inPropertyID,
                void* inClientData);

        static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
                                               UInt32 inChannel,
                                               Boolean	isInput,
                                               AudioDevicePropertyID inPropertyID,
                                               void* inClientData);

sletz's avatar
sletz committed
119
120
121
122
123
        OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
        OSStatus GetDefaultDevice(AudioDeviceID* id);
        OSStatus GetDefaultInputDevice(AudioDeviceID* id);
        OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
        OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
124
        OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
125
   
sletz's avatar
sletz committed
126
        // Setup
127
        OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
128
        OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
129
        OSStatus DestroyAggregateDevice();
130
        bool IsAggregateDevice(AudioDeviceID device);
131
        
sletz's avatar
sletz committed
132
133
134
        int SetupDevices(const char* capture_driver_uid,
                         const char* playback_driver_uid,
                         char* capture_driver_name,
135
136
                         char* playback_driver_name,
                         jack_nframes_t samplerate);
sletz's avatar
sletz committed
137
138
139
140
141
142
143
144
145

        int SetupChannels(bool capturing,
                          bool playing,
                          int& inchannels,
                          int& outchannels,
                          int& in_nChannels,
                          int& out_nChannels,
                          bool strict);

146
        int SetupBuffers(int inchannels);
sletz's avatar
sletz committed
147
148
        void DisposeBuffers();

149
150
151
        int SetupBufferSize(jack_nframes_t buffer_size);
        int SetupSampleRate(jack_nframes_t samplerate);
        int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
sletz's avatar
sletz committed
152
153
154
155
156
157
158
159

        int OpenAUHAL(bool capturing,
                      bool playing,
                      int inchannels,
                      int outchannels,
                      int in_nChannels,
                      int out_nChannels,
                      jack_nframes_t nframes,
160
                      jack_nframes_t samplerate);
sletz's avatar
sletz committed
161
162
163
164
        void CloseAUHAL();

        int AddListeners();
        void RemoveListeners();
165
        
166
167
        bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
        bool TakeHog();
sletz's avatar
sletz committed
168
169
170

    public:

sletz's avatar
sletz committed
171
        JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
sletz's avatar
sletz committed
172
173
        virtual ~JackCoreAudioDriver();

sletz's avatar
sletz committed
174
175
        int Open(jack_nframes_t buffer_size,
                 jack_nframes_t samplerate,
sletz's avatar
sletz committed
176
177
                 bool capturing,
                 bool playing,
sletz's avatar
sletz committed
178
179
180
181
182
183
                 int chan_in,
                 int chan_out,
                 bool monitor,
                 const char* capture_driver_name,
                 const char* playback_driver_name,
                 jack_nframes_t capture_latency,
184
                 jack_nframes_t playback_latency,
185
                 int async_output_latency,
186
                 int computation_grain,
187
188
                 bool hogged,
                 bool clock_drift);
sletz's avatar
sletz committed
189
        int Close();
sletz's avatar
sletz committed
190
191
192
193
194
195
196
197
198

        int Attach();

        int Start();
        int Stop();

        int Read();
        int Write();

199
200
201
202
203
204
        // BufferSize can be changed
        bool IsFixedBufferSize()
        {
            return false;
        }

205
        int SetBufferSize(jack_nframes_t buffer_size);
sletz's avatar
sletz committed
206
207
208
209
210
};

} // end of namespace

#endif