audio_driver_coreaudio.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef AUDIO_DRIVER_COREAUDIO_H
  31. #define AUDIO_DRIVER_COREAUDIO_H
  32. #ifdef COREAUDIO_ENABLED
  33. #include "servers/audio_server.h"
  34. #import <AudioUnit/AudioUnit.h>
  35. #ifdef MACOS_ENABLED
  36. #import <CoreAudio/AudioHardware.h>
  37. #else
  38. #import <AVFoundation/AVFoundation.h>
  39. #endif
  40. class AudioDriverCoreAudio : public AudioDriver {
  41. AudioComponentInstance audio_unit = nullptr;
  42. AudioComponentInstance input_unit = nullptr;
  43. bool active = false;
  44. Mutex mutex;
  45. String output_device_name = "Default";
  46. String input_device_name = "Default";
  47. int mix_rate = 0;
  48. int capture_mix_rate = 0;
  49. unsigned int channels = 2;
  50. unsigned int capture_channels = 2;
  51. unsigned int buffer_frames = 0;
  52. unsigned int capture_buffer_frames = 0;
  53. Vector<int32_t> samples_in;
  54. Vector<int16_t> input_buf;
  55. #ifdef MACOS_ENABLED
  56. PackedStringArray _get_device_list(bool capture = false);
  57. void _set_device(const String &output_device, bool capture = false);
  58. static OSStatus input_device_address_cb(AudioObjectID inObjectID,
  59. UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
  60. void *inClientData);
  61. static OSStatus output_device_address_cb(AudioObjectID inObjectID,
  62. UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,
  63. void *inClientData);
  64. #endif
  65. static OSStatus output_callback(void *inRefCon,
  66. AudioUnitRenderActionFlags *ioActionFlags,
  67. const AudioTimeStamp *inTimeStamp,
  68. UInt32 inBusNumber, UInt32 inNumberFrames,
  69. AudioBufferList *ioData);
  70. static OSStatus input_callback(void *inRefCon,
  71. AudioUnitRenderActionFlags *ioActionFlags,
  72. const AudioTimeStamp *inTimeStamp,
  73. UInt32 inBusNumber, UInt32 inNumberFrames,
  74. AudioBufferList *ioData);
  75. Error init_input_device();
  76. void finish_input_device();
  77. public:
  78. virtual const char *get_name() const override {
  79. return "CoreAudio";
  80. }
  81. virtual Error init() override;
  82. virtual void start() override;
  83. virtual int get_mix_rate() const override;
  84. virtual int get_input_mix_rate() const override;
  85. virtual SpeakerMode get_speaker_mode() const override;
  86. virtual void lock() override;
  87. virtual void unlock() override;
  88. virtual void finish() override;
  89. #ifdef MACOS_ENABLED
  90. virtual PackedStringArray get_output_device_list() override;
  91. virtual String get_output_device() override;
  92. virtual void set_output_device(const String &p_name) override;
  93. virtual PackedStringArray get_input_device_list() override;
  94. virtual String get_input_device() override;
  95. virtual void set_input_device(const String &p_name) override;
  96. #endif
  97. virtual Error input_start() override;
  98. virtual Error input_stop() override;
  99. bool try_lock();
  100. void stop();
  101. AudioDriverCoreAudio();
  102. ~AudioDriverCoreAudio() {}
  103. };
  104. #endif // COREAUDIO_ENABLED
  105. #endif // AUDIO_DRIVER_COREAUDIO_H