audio_driver_coreaudio.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* audio_driver_coreaudio.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #ifdef COREAUDIO_ENABLED
  31. #ifndef AUDIO_DRIVER_COREAUDIO_H
  32. #define AUDIO_DRIVER_COREAUDIO_H
  33. #include "servers/audio_server.h"
  34. #include <AudioUnit/AudioUnit.h>
  35. #ifdef OSX_ENABLED
  36. #include <CoreAudio/AudioHardware.h>
  37. #endif
  38. class AudioDriverCoreAudio : public AudioDriver {
  39. AudioComponentInstance audio_unit;
  40. AudioComponentInstance input_unit;
  41. bool active;
  42. Mutex *mutex;
  43. String device_name;
  44. String capture_device_name;
  45. int mix_rate;
  46. unsigned int channels;
  47. unsigned int capture_channels;
  48. unsigned int buffer_frames;
  49. Vector<int32_t> samples_in;
  50. Vector<int16_t> input_buf;
  51. #ifdef OSX_ENABLED
  52. Array _get_device_list(bool capture = false);
  53. void _set_device(const String &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 capture_init();
  72. void capture_finish();
  73. public:
  74. const char *get_name() const {
  75. return "CoreAudio";
  76. };
  77. virtual Error init();
  78. virtual void start();
  79. virtual int get_mix_rate() const;
  80. virtual SpeakerMode get_speaker_mode() const;
  81. virtual void lock();
  82. virtual void unlock();
  83. virtual void finish();
  84. virtual Error capture_start();
  85. virtual Error capture_stop();
  86. bool try_lock();
  87. void stop();
  88. #ifdef OSX_ENABLED
  89. virtual Array get_device_list();
  90. virtual String get_device();
  91. virtual void set_device(String device);
  92. virtual Array capture_get_device_list();
  93. virtual void capture_set_device(const String &p_name);
  94. virtual String capture_get_device();
  95. #endif
  96. AudioDriverCoreAudio();
  97. ~AudioDriverCoreAudio();
  98. };
  99. #endif
  100. #endif