audio_driver_coreaudio.h 4.8 KB

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