JackCoreAudioDriver.h 5.78 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
57

        AudioBufferList* fJackInputData;
        AudioBufferList* fDriverOutputData;

        AudioDeviceID fDeviceID;

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

        bool fState;

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

        int fInChannels;
        int fOutChannels;

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

        bool fMonitor;
74
        float fIOUsage;
75
76
77
78
79
        
    #ifdef MAC_OS_X_VERSION_10_5
        AudioDeviceIOProcID fMesureCallbackID;
    #endif
    
sletz's avatar
sletz committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        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
96
97
98
99
100
101
102
103
104
105
106
107
                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
108
109
110
111
112
        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);
113
        OSStatus GetTotalChannels(AudioDeviceID device, int* channelCount, bool isInput);
sletz's avatar
sletz committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

        // Setup
        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);

        int SetupBuffers(int inchannels, int outchannels);
        void DisposeBuffers();

        int SetupBufferSizeAndSampleRate(jack_nframes_t nframes, jack_nframes_t samplerate);

        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
147
148
149

    public:

sletz's avatar
sletz committed
150
        JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
sletz's avatar
sletz committed
151
152
153
154
        virtual ~JackCoreAudioDriver();

        int Open(jack_nframes_t frames_per_cycle,
                 jack_nframes_t rate,
sletz's avatar
sletz committed
155
156
                 bool capturing,
                 bool playing,
sletz's avatar
sletz committed
157
158
159
160
161
162
                 int chan_in,
                 int chan_out,
                 bool monitor,
                 const char* capture_driver_name,
                 const char* playback_driver_name,
                 jack_nframes_t capture_latency,
163
164
                 jack_nframes_t playback_latency,
                 int async_output_latency);
sletz's avatar
sletz committed
165
        int Close();
sletz's avatar
sletz committed
166
167
168
169
170
171
172
173
174

        int Attach();

        int Start();
        int Stop();

        int Read();
        int Write();

175
        int SetBufferSize(jack_nframes_t buffer_size);
sletz's avatar
sletz committed
176
177
178
179
180
};

} // end of namespace

#endif