JackCoreAudioDriver.h 6.19 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
29
30
31
32
33

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"

namespace Jack
{

#define kVersion 102

34
35
36
37
38
39
typedef	UInt8	CAAudioHardwareDeviceSectionID;
#define	kAudioDeviceSectionInput	((CAAudioHardwareDeviceSectionID)0x01)
#define	kAudioDeviceSectionOutput	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionGlobal	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionWildcard	((CAAudioHardwareDeviceSectionID)0xFF)

sletz's avatar
sletz committed
40
41
/*!
\brief The CoreAudio driver.
sletz's avatar
sletz committed
42

sletz's avatar
sletz committed
43
44
45
\todo hardware monitoring
*/

46
class JackCoreAudioDriver : public JackAudioDriver
sletz's avatar
sletz committed
47
48
49
{

    private:
sletz's avatar
sletz committed
50
51

        AudioUnit fAUHAL;
sletz's avatar
sletz committed
52
53
54
55
56

        AudioBufferList* fJackInputData;
        AudioBufferList* fDriverOutputData;

        AudioDeviceID fDeviceID;
57
        AudioObjectID fPluginID;
sletz's avatar
sletz committed
58

sletz's avatar
sletz committed
59
60
        AudioUnitRenderActionFlags* fActionFags;
        AudioTimeStamp* fCurrentTime;
sletz's avatar
sletz committed
61
62
63

        bool fState;

sletz's avatar
sletz committed
64
        // Initial state
sletz's avatar
sletz committed
65
66
67
68
69
70
71
72
73
74
        bool fCapturing;
        bool fPlaying;

        int fInChannels;
        int fOutChannels;

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

        bool fMonitor;
75
        float fIOUsage;
76
        float fComputationGrain;
77
78
    
        /*    
79
80
81
    #ifdef MAC_OS_X_VERSION_10_5
        AudioDeviceIOProcID fMesureCallbackID;
    #endif
82
        */
83
    
sletz's avatar
sletz committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        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
100
101
102
103
104
105
106
107
108
109
110
                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
111
112
113
114
115
        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);
116
        OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
sletz's avatar
sletz committed
117
118

        // Setup
119
        OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, AudioDeviceID* outAggregateDevice);
120
121
        OSStatus DestroyAggregateDevice();
        
sletz's avatar
sletz committed
122
123
124
125
126
127
128
129
130
131
132
133
134
        int SetupDevices(const char* capture_driver_uid,
                         const char* playback_driver_uid,
                         char* capture_driver_name,
                         char* playback_driver_name);

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

135
        int SetupBuffers(int inchannels);
sletz's avatar
sletz committed
136
137
        void DisposeBuffers();

sletz's avatar
sletz committed
138
        int SetupBufferSizeAndSampleRate(jack_nframes_t buffer_size, jack_nframes_t samplerate);
sletz's avatar
sletz committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152

        int OpenAUHAL(bool capturing,
                      bool playing,
                      int inchannels,
                      int outchannels,
                      int in_nChannels,
                      int out_nChannels,
                      jack_nframes_t nframes,
                      jack_nframes_t samplerate,
                      bool strict);
        void CloseAUHAL();

        int AddListeners();
        void RemoveListeners();
sletz's avatar
sletz committed
153
154
155

    public:

sletz's avatar
sletz committed
156
        JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
sletz's avatar
sletz committed
157
158
        virtual ~JackCoreAudioDriver();

sletz's avatar
sletz committed
159
160
        int Open(jack_nframes_t buffer_size,
                 jack_nframes_t samplerate,
sletz's avatar
sletz committed
161
162
                 bool capturing,
                 bool playing,
sletz's avatar
sletz committed
163
164
165
166
167
168
                 int chan_in,
                 int chan_out,
                 bool monitor,
                 const char* capture_driver_name,
                 const char* playback_driver_name,
                 jack_nframes_t capture_latency,
169
                 jack_nframes_t playback_latency,
170
171
                 int async_output_latency,
                 int computation_grain);
sletz's avatar
sletz committed
172
        int Close();
sletz's avatar
sletz committed
173
174
175
176
177
178
179
180
181

        int Attach();

        int Start();
        int Stop();

        int Read();
        int Write();

182
183
184
185
186
187
        // BufferSize can be changed
        bool IsFixedBufferSize()
        {
            return false;
        }

188
        int SetBufferSize(jack_nframes_t buffer_size);
sletz's avatar
sletz committed
189
190
191
192
193
};

} // end of namespace

#endif