1 /* 2 File: AudioDevice.h 3 Adapted from the CAPlayThough example 4 Version: 1.2.2 5 6 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 Inc. ("Apple") in consideration of your agreement to the following 8 terms, and your use, installation, modification or redistribution of 9 this Apple software constitutes acceptance of these terms. If you do 10 not agree with these terms, please do not use, install, modify or 11 redistribute this Apple software. 12 13 In consideration of your agreement to abide by the following terms, and 14 subject to these terms, Apple grants you a personal, non-exclusive 15 license, under Apple's copyrights in this original Apple software (the 16 "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 Software, with or without modifications, in source and/or binary forms; 18 provided that if you redistribute the Apple Software in its entirety and 19 without modifications, you must retain this notice and the following 20 text and disclaimers in all such redistributions of the Apple Software. 21 Neither the name, trademarks, service marks or logos of Apple Inc. may 22 be used to endorse or promote products derived from the Apple Software 23 without specific prior written permission from Apple. Except as 24 expressly stated in this notice, no other rights or licenses, express or 25 implied, are granted by Apple herein, including but not limited to any 26 patent rights that may be infringed by your derivative works or by other 27 works in which the Apple Software may be incorporated. 28 29 The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 35 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 POSSIBILITY OF SUCH DAMAGE. 43 44 Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 Copyright (C) 2017 René J.V. Bertin All Rights Reserved. 46 47 */ 48 49 #ifndef __AudioDevice_h__ 50 #define __AudioDevice_h__ 51 52 #include <CoreServices/CoreServices.h> 53 #include <CoreAudio/CoreAudio.h> 54 #include <AvailabilityMacros.h> 55 56 #ifndef DEPRECATED_LISTENER_API 57 # if !(defined(MAC_OS_X_VERSION_10_11) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_11) 58 # define DEPRECATED_LISTENER_API 1 59 # warning "Using the deprecated PropertyListener API; at least it works" 60 # endif 61 #endif 62 63 #if DEPRECATED_LISTENER_API 64 using AudioPropertyListenerProc = AudioDevicePropertyListenerProc; 65 #else 66 using AudioPropertyListenerProc = AudioObjectPropertyListenerProc; 67 #endif 68 69 class AudioDeviceList; 70 71 class AudioDevice { 72 public: 73 74 typedef void (DefaultDeviceChangeHandler)(AudioObjectID newID, void *data); 75 76 AudioDevice(); 77 AudioDevice(AudioObjectID devid, bool isInput=false); 78 AudioDevice(AudioObjectID devid, bool quick, bool isInput); 79 AudioDevice(AudioObjectID devid, AudioPropertyListenerProc lProc, bool isInput=false); 80 ~AudioDevice(); 81 82 void Init(); 83 void Init(AudioPropertyListenerProc lProc); 84 Valid()85 bool Valid() { return mID != kAudioDeviceUnknown; } 86 87 void SetBufferSize(UInt32 size); 88 OSStatus NominalSampleRate(Float64 &sampleRate); 89 inline Float64 ClosestNominalSampleRate(Float64 sampleRate); 90 OSStatus SetNominalSampleRate(Float64 sampleRate, bool force=false); 91 OSStatus ResetNominalSampleRate(bool force=false); 92 OSStatus SetStreamBasicDescription(AudioStreamBasicDescription *desc); 93 int CountChannels(); 94 char *GetName(char *buf=NULL, UInt32 maxlen=0); 95 SetInitialNominalSampleRate(Float64 sampleRate)96 void SetInitialNominalSampleRate(Float64 sampleRate) 97 { 98 mInitialFormat.mSampleRate = sampleRate; 99 } 100 CurrentNominalSampleRate()101 Float64 CurrentNominalSampleRate() 102 { 103 return currentNominalSR; 104 } 105 ID()106 AudioObjectID ID() 107 { 108 return mID; 109 } 110 isDefaultDevice()111 bool isDefaultDevice() 112 { 113 return mDefaultDevice; 114 } setDefaultDevice(bool isDefault)115 void setDefaultDevice(bool isDefault) 116 { 117 mDefaultDevice = isDefault; 118 } 119 120 void installDefaultDeviceChangeHandler(DefaultDeviceChangeHandler *handler, void *data); 121 void callDefaultDeviceChangeHandler(AudioObjectID newID); 122 123 static AudioDevice *GetDefaultDevice(bool forInput, OSStatus &err, AudioDevice *dev=NULL); 124 static AudioDevice *GetDevice(AudioObjectID devId, bool forInput, AudioDevice *dev=NULL, bool quick=false); 125 126 protected: 127 AudioStreamBasicDescription mInitialFormat; 128 AudioPropertyListenerProc listenerProc; 129 OSStatus GetPropertyDataSize( AudioObjectPropertySelector property, UInt32 *size, AudioObjectPropertyAddress *propertyAddress=NULL ); 130 Float64 currentNominalSR; 131 Float64 minNominalSR, maxNominalSR; 132 UInt32 nominalSampleRates; 133 Float64 *nominalSampleRateList = NULL; 134 bool discreteSampleRateList; 135 const AudioObjectID mID; 136 const bool mForInput; 137 UInt32 mSafetyOffset; 138 UInt32 mBufferSizeFrames; 139 AudioStreamBasicDescription mFormat; 140 char mDevName[256] = ""; 141 142 bool mInitialised = false; 143 bool mDefaultDevice = false; 144 145 DefaultDeviceChangeHandler *defDeviceChangeHandler = nullptr; 146 void *defDeviceChangeHanderData = nullptr; 147 148 private: 149 bool gettingDevName = false; 150 151 friend class AudioDeviceList; 152 153 public: 154 UInt32 listenerSilentFor; 155 }; 156 157 158 #endif // __AudioDevice_h__ 159