Skip to content
0.2.0-dev Download ZIP
131 lines ·5.0 KB Raw
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#pragma once
32
33#ifdef COREAUDIO_ENABLED
34
35#include "core/os/mutex.h"
36#include "servers/audio/audio_driver.h"
37
38#import <AudioUnit/AudioUnit.h>
39#ifdef MACOS_ENABLED
40#import <CoreAudio/AudioHardware.h>
41#else
42#import <AVFoundation/AVFoundation.h>
43#endif
44
45class AudioDriverCoreAudio : public AudioDriver {
46 AudioComponentInstance audio_unit = nullptr;
47 AudioComponentInstance input_unit = nullptr;
48
49 bool active = false;
50 Mutex mutex;
51
52 String output_device_name = "Default";
53 String input_device_name = "Default";
54
55 int mix_rate = 0;
56 int capture_mix_rate = 0;
57 unsigned int channels = 2;
58 unsigned int capture_channels = 2;
59 unsigned int buffer_frames = 0;
60 unsigned int capture_buffer_frames = 0;
61
62 Vector<int32_t> samples_in;
63 unsigned int buffer_size = 0;
64
65#ifdef MACOS_ENABLED
66 PackedStringArray _get_device_list(bool capture = false);
67 void _set_device(const String &output_device, bool capture = false);
68
69 static OSStatus input_device_address_cb(AudioObjectID inObjectID,
70 UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
71 void *inClientData);
72
73 static OSStatus output_device_address_cb(AudioObjectID inObjectID,
74 UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
75 void *inClientData);
76#endif
77
78 static OSStatus output_callback(void *inRefCon,
79 AudioUnitRenderActionFlags *ioActionFlags,
80 const AudioTimeStamp *inTimeStamp,
81 UInt32 inBusNumber, UInt32 inNumberFrames,
82 AudioBufferList *ioData);
83
84 static OSStatus input_callback(void *inRefCon,
85 AudioUnitRenderActionFlags *ioActionFlags,
86 const AudioTimeStamp *inTimeStamp,
87 UInt32 inBusNumber, UInt32 inNumberFrames,
88 AudioBufferList *ioData);
89
90 Error init_input_device();
91 void finish_input_device();
92 void _release_output_unit();
93
94public:
95 virtual const char *get_name() const override {
96 return "CoreAudio";
97 }
98
99 virtual Error init() override;
100 virtual void start() override;
101 virtual bool can_output_device_sleep() const override { return true; }
102 virtual bool set_output_device_sleep(bool p_enable) override;
103 virtual int get_mix_rate() const override;
104 virtual int get_input_mix_rate() const override;
105 virtual SpeakerMode get_speaker_mode() const override;
106
107 virtual void lock() override;
108 virtual void unlock() override;
109 virtual void finish() override;
110
111#ifdef MACOS_ENABLED
112 virtual PackedStringArray get_output_device_list() override;
113 virtual String get_output_device() override;
114 virtual void set_output_device(const String &p_name) override;
115
116 virtual PackedStringArray get_input_device_list() override;
117 virtual String get_input_device() override;
118 virtual void set_input_device(const String &p_name) override;
119#endif
120
121 virtual Error input_start() override;
122 virtual Error input_stop() override;
123
124 bool try_lock();
125 void stop();
126
127 AudioDriverCoreAudio();
128 ~AudioDriverCoreAudio() {}
129};
130
131#endif // COREAUDIO_ENABLED