1 /*************************************************************************/
2 /*  audio_driver_coreaudio.h                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifdef COREAUDIO_ENABLED
32 
33 #ifndef AUDIO_DRIVER_COREAUDIO_H
34 #define AUDIO_DRIVER_COREAUDIO_H
35 
36 #include "servers/audio_server.h"
37 
38 #include <AudioUnit/AudioUnit.h>
39 #ifdef OSX_ENABLED
40 #include <CoreAudio/AudioHardware.h>
41 #endif
42 
43 class AudioDriverCoreAudio : public AudioDriver {
44 
45 	AudioComponentInstance audio_unit;
46 	AudioComponentInstance input_unit;
47 
48 	bool active;
49 	Mutex *mutex;
50 
51 	String device_name;
52 	String capture_device_name;
53 
54 	int mix_rate;
55 	unsigned int channels;
56 	unsigned int capture_channels;
57 	unsigned int buffer_frames;
58 
59 	Vector<int32_t> samples_in;
60 	Vector<int16_t> input_buf;
61 
62 #ifdef OSX_ENABLED
63 	Array _get_device_list(bool capture = false);
64 	void _set_device(const String &device, bool capture = false);
65 
66 	static OSStatus input_device_address_cb(AudioObjectID inObjectID,
67 			UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
68 			void *inClientData);
69 
70 	static OSStatus output_device_address_cb(AudioObjectID inObjectID,
71 			UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
72 			void *inClientData);
73 #endif
74 
75 	static OSStatus output_callback(void *inRefCon,
76 			AudioUnitRenderActionFlags *ioActionFlags,
77 			const AudioTimeStamp *inTimeStamp,
78 			UInt32 inBusNumber, UInt32 inNumberFrames,
79 			AudioBufferList *ioData);
80 
81 	static OSStatus input_callback(void *inRefCon,
82 			AudioUnitRenderActionFlags *ioActionFlags,
83 			const AudioTimeStamp *inTimeStamp,
84 			UInt32 inBusNumber, UInt32 inNumberFrames,
85 			AudioBufferList *ioData);
86 
87 	Error capture_init();
88 	void capture_finish();
89 
90 public:
get_name()91 	const char *get_name() const {
92 		return "CoreAudio";
93 	};
94 
95 	virtual Error init();
96 	virtual void start();
97 	virtual int get_mix_rate() const;
98 	virtual SpeakerMode get_speaker_mode() const;
99 
100 	virtual void lock();
101 	virtual void unlock();
102 	virtual void finish();
103 
104 	virtual Error capture_start();
105 	virtual Error capture_stop();
106 
107 	bool try_lock();
108 	void stop();
109 
110 #ifdef OSX_ENABLED
111 	virtual Array get_device_list();
112 	virtual String get_device();
113 	virtual void set_device(String device);
114 
115 	virtual Array capture_get_device_list();
116 	virtual void capture_set_device(const String &p_name);
117 	virtual String capture_get_device();
118 #endif
119 
120 	AudioDriverCoreAudio();
121 	~AudioDriverCoreAudio();
122 };
123 
124 #endif
125 
126 #endif
127