audio_driver_coreaudio.h 4.7 KB

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