get started on coreaudio/midi backend
[ardour.git] / libs / backends / coreaudio / coreaudio_pcmio.h
1 /*
2  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <CoreServices/CoreServices.h>
20 #include <CoreAudio/CoreAudio.h>
21 #include <AudioUnit/AudioUnit.h>
22
23 #include <AudioUnit/AudioUnit.h>
24 #include <AudioToolbox/AudioToolbox.h>
25
26 #include <map>
27 #include <string>
28
29 #define AUHAL_OUTPUT_ELEMENT 0
30 #define AUHAL_INPUT_ELEMENT 1
31
32
33 class CoreAudioPCM {
34 public:
35         CoreAudioPCM (void);
36         ~CoreAudioPCM (void);
37
38
39         int      state (void) const { return _state; }
40         uint32_t n_playback_channels (void) const { return _playback_channels; }
41         uint32_t n_capture_channels (void) const { return _capture_channels; }
42
43         void     discover();
44         void     device_list(std::map<size_t, std::string> &devices) const { devices = _devices;}
45
46         void     pcm_stop (void);
47         int      pcm_start (
48                         uint32_t input_device,
49                         uint32_t output_device,
50                         uint32_t sample_rate,
51                         uint32_t samples_per_period,
52                         int (process_callback (void*)),
53                         void * process_arg
54                         );
55
56         void     set_error_callback (
57                         void ( error_callback (void*)),
58                         void * error_arg
59                         ) {
60                 _error_callback = error_callback;
61                 _error_arg = error_arg;
62         }
63
64         // must be called from process_callback;
65         int      get_capture_channel (uint32_t chn, float *input, uint32_t n_samples);
66         int      set_playback_channel (uint32_t chn, const float *input, uint32_t n_samples);
67         uint32_t n_samples() const { return _cur_samples_per_period; };
68
69         // really private
70         OSStatus render_callback (
71                         AudioUnitRenderActionFlags* ioActionFlags,
72                         const AudioTimeStamp* inTimeStamp,
73                         UInt32 inBusNumber,
74                         UInt32 inNumberFrames,
75                         AudioBufferList* ioData);
76
77         void hwPropertyChange();
78
79 private:
80         AudioUnit _auhal;
81         AudioDeviceID* _deviceIDs;
82         AudioBufferList* _inputAudioBufferList;
83         AudioBufferList* _outputAudioBufferList;
84
85         int _state;
86
87         uint32_t _max_samples_per_period;
88         uint32_t _cur_samples_per_period;
89         uint32_t _capture_channels;
90         uint32_t _playback_channels;
91         bool     _in_process;
92         size_t   _numDevices;
93
94         int (* _process_callback) (void*);
95         void * _process_arg;
96
97         void (* _error_callback) (void*);
98         void  * _error_arg;
99
100         std::map<size_t, std::string> _devices;
101         // TODO proper device info struct
102         uint32_t * _device_ins;
103         uint32_t * _device_outs;
104
105 };