1/**************************************************************************/
2/* audio_driver_coreaudio.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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#ifndef AUDIO_DRIVER_COREAUDIO_H
32#define AUDIO_DRIVER_COREAUDIO_H
33
34#ifdef COREAUDIO_ENABLED
35
36#include "servers/audio_server.h"
37
38#import <AudioUnit/AudioUnit.h>
39#ifdef MACOS_ENABLED
40#import <CoreAudio/AudioHardware.h>
41#endif
42
43class AudioDriverCoreAudio : public AudioDriver {
44 AudioComponentInstance audio_unit = nullptr;
45 AudioComponentInstance input_unit = nullptr;
46
47 bool active = false;
48 Mutex mutex;
49
50 String output_device_name = "Default";
51 String input_device_name = "Default";
52
53 int mix_rate = 0;
54 unsigned int channels = 2;
55 unsigned int capture_channels = 2;
56 unsigned int buffer_frames = 0;
57
58 Vector<int32_t> samples_in;
59 Vector<int16_t> input_buf;
60
61#ifdef MACOS_ENABLED
62 PackedStringArray _get_device_list(bool capture = false);
63 void _set_device(const String &output_device, bool capture = false);
64
65 static OSStatus input_device_address_cb(AudioObjectID inObjectID,
66 UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
67 void *inClientData);
68
69 static OSStatus output_device_address_cb(AudioObjectID inObjectID,
70 UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
71 void *inClientData);
72#endif
73
74 static OSStatus output_callback(void *inRefCon,
75 AudioUnitRenderActionFlags *ioActionFlags,
76 const AudioTimeStamp *inTimeStamp,
77 UInt32 inBusNumber, UInt32 inNumberFrames,
78 AudioBufferList *ioData);
79
80 static OSStatus input_callback(void *inRefCon,
81 AudioUnitRenderActionFlags *ioActionFlags,
82 const AudioTimeStamp *inTimeStamp,
83 UInt32 inBusNumber, UInt32 inNumberFrames,
84 AudioBufferList *ioData);
85
86 Error init_input_device();
87 void finish_input_device();
88
89public:
90 virtual const char *get_name() const override {
91 return "CoreAudio";
92 };
93
94 virtual Error init() override;
95 virtual void start() override;
96 virtual int get_mix_rate() const override;
97 virtual SpeakerMode get_speaker_mode() const override;
98
99 virtual void lock() override;
100 virtual void unlock() override;
101 virtual void finish() override;
102
103#ifdef MACOS_ENABLED
104 virtual PackedStringArray get_output_device_list() override;
105 virtual String get_output_device() override;
106 virtual void set_output_device(const String &p_name) override;
107
108 virtual PackedStringArray get_input_device_list() override;
109 virtual String get_input_device() override;
110 virtual void set_input_device(const String &p_name) override;
111#endif
112
113 virtual Error input_start() override;
114 virtual Error input_stop() override;
115
116 bool try_lock();
117 void stop();
118
119 AudioDriverCoreAudio();
120 ~AudioDriverCoreAudio() {}
121};
122
123#endif // COREAUDIO_ENABLED
124
125#endif // AUDIO_DRIVER_COREAUDIO_H
126