13 #ifndef _AudioHardware_h_ 14 #define _AudioHardware_h_ 19 #include <CoreAudio/AudioHardware.h> 20 #include <AudioUnit/AudioUnit.h> 21 #include <CoreServices/CoreServices.h> 22 #include <AudioToolbox/DefaultAudioOutput.h> 23 #include <AudioToolbox/AudioConverter.h> 50 if (devid != kAudioDeviceUnknown)
54 for (j = 0; j < i; j++)
55 printf(
"dev 0x%x: %g\n", (
unsigned)
mDevId, list[j]);
72 return mDevId != kAudioDeviceUnknown;
83 AudioDeviceID dev_id = kAudioDeviceUnknown;
84 UInt32 parm_size =
sizeof(dev_id);
85 OSStatus st = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &parm_size, &dev_id);
86 if (st != kAudioHardwareNoError)
87 printf(
"AudioDeviceGetProperty->kAudioHardwarePropertyDefaultOutputDevice failed: %d\n", (
int)st);
94 AudioDeviceID dev_id = kAudioDeviceUnknown;
95 UInt32 parm_size =
sizeof(dev_id);
96 OSStatus st = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &parm_size, &dev_id);
97 if (st != kAudioHardwareNoError)
98 printf(
"AudioDeviceGetProperty->kAudioHardwarePropertyDefaultInputDevice failed: %d\n", (
int)st);
106 UInt32 sz =
sizeof(actual);
107 OSStatus st = AudioDeviceGetProperty(
mDevId, 0,
mIsInput, kAudioDevicePropertyActualSampleRate, &sz, &actual);
108 if (st != kAudioHardwareNoError)
109 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyActualSampleRate failed: %d\n", (
unsigned)
mDevId, (
int)st);
117 UInt32 sz =
sizeof(actual);
118 OSStatus st = AudioDeviceGetProperty(
mDevId, 0,
mIsInput, kAudioDevicePropertyLatency, &sz, &actual);
119 if (st != kAudioHardwareNoError)
120 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyLatency failed: %d\n", (
unsigned)
mDevId, (
int)st);
136 for (j = 0; j < sz; j++)
138 double p = buffs[j] / sr;
139 rating = (int)(10000 * (p - (
int)p));
142 if ((rating < prev) && (p >= 1))
148 printf(
"[0x%x]RT: %d (%g) %g\n", (
unsigned)
mDevId, rating, buffs[j], sr);
156 AudioValueRange buffRanges[bufLen];
158 OSStatus st = AudioDeviceGetPropertyInfo(
mDevId, 0,
mIsInput, kAudioDevicePropertyAvailableNominalSampleRates, &sz, NULL);
159 if (st != kAudioHardwareNoError)
161 printf(
"[0x%x]AudioDeviceGetPropertyInfo->kAudioDevicePropertyAvailableNominalSampleRates failed: %d\n", (
unsigned)
mDevId, (
int)st);
165 if (sz >
sizeof(buffRanges))
166 return -(int)(sz /
sizeof(buffRanges[0]));
168 st = AudioDeviceGetProperty(
mDevId, 0,
mIsInput, kAudioDevicePropertyAvailableNominalSampleRates, &sz, &buffRanges);
169 if (st != kAudioHardwareNoError)
171 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyAvailableNominalSampleRates failed: %d\n", (
unsigned)
mDevId, (
int)st);
176 for (i = 0; i < sz /
sizeof(buffRanges[0]); i++)
178 pBuff[i] = buffRanges[i].mMinimum;
185 UInt32 sz =
sizeof(desc);
186 OSStatus st = AudioDeviceGetProperty(
mDevId, 0,
mIsInput, kAudioDevicePropertyStreamFormat, &sz, &desc);
187 if (st != kAudioHardwareNoError)
189 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyStreamFormat failed: %d\n", (
unsigned)
mDevId, (
int)st);
197 UInt32 sz =
sizeof(desc);
198 OSStatus st = AudioDeviceSetProperty(
mDevId, NULL, 0,
mIsInput, kAudioDevicePropertyStreamFormat, sz, &desc);
199 if (st != kAudioHardwareNoError)
201 printf(
"[0x%x]AudioDeviceSetProperty->kAudioDevicePropertyStreamFormat failed: %d\n", (
unsigned)
mDevId, (
int)st);
209 printf(
"%smSampleRate = %g\n", header, desc.mSampleRate);
210 printf(
"%smFormatID = %s\n", header, (
char*)&desc.mFormatID);
211 printf(
"%smFormatFlags = 0x%08lX\n", header, desc.mFormatFlags);
212 printf(
"%smBytesPerPacket = %ld\n", header, desc.mBytesPerPacket);
213 printf(
"%smFramesPerPacket = %ld\n", header, desc.mFramesPerPacket);
214 printf(
"%smChannelsPerFrame = %ld\n", header, desc.mChannelsPerFrame);
215 printf(
"%smBytesPerFrame = %ld\n", header, desc.mBytesPerFrame);
216 printf(
"%smBitsPerChannel = %ld\n", header, desc.mBitsPerChannel);
221 return (desc.mSampleRate == desc2.mSampleRate &&
222 desc.mFormatID == desc2.mFormatID &&
223 desc.mFormatFlags == desc2.mFormatFlags &&
224 desc.mBytesPerPacket == desc2.mBytesPerPacket &&
225 desc.mFramesPerPacket == desc2.mFramesPerPacket &&
226 desc.mChannelsPerFrame == desc2.mChannelsPerFrame &&
227 desc.mBytesPerFrame == desc2.mBytesPerFrame &&
228 desc.mBitsPerChannel == desc2.mBitsPerChannel);
234 UInt32 sz =
sizeof(sr);
235 OSStatus st = AudioDeviceGetProperty(
mDevId, 0,
mIsInput, kAudioDevicePropertyBufferSize, &sz, &sr);
236 if (st != kAudioHardwareNoError)
237 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyBufferSize failed: %d\n", (
unsigned)
mDevId, (
int)st);
245 UInt32 sz =
sizeof(sr);
246 OSStatus st = AudioDeviceSetProperty(
mDevId, NULL, 0,
mIsInput, kAudioDevicePropertyBufferSize, sz, &sr);
247 if (st != kAudioHardwareNoError)
249 printf(
"[0x%x]AudioDeviceGetProperty->kAudioDevicePropertyBufferSize failed: %d\n", (
unsigned)
mDevId, (
int)st);
unsigned getBufferSize() const
Definition: MpCoreAudioHardware.h:231
bool setStreamFormat(const AudioStreamBasicDescription &desc)
Definition: MpCoreAudioHardware.h:195
AudioDeviceID getAudioDeviceID() const
Definition: MpCoreAudioHardware.h:60
int getAvailableSampleRates(double *pBuff, const int bufLen) const
Definition: MpCoreAudioHardware.h:154
double getSuitableSampleRateFor(double sr)
Definition: MpCoreAudioHardware.h:125
bool IsValid() const
Definition: MpCoreAudioHardware.h:70
bool getStreamFormat(AudioStreamBasicDescription &desc) const
Definition: MpCoreAudioHardware.h:183
CoreAudioHardware(AudioDeviceID devid, bool isInput)
Definition: MpCoreAudioHardware.h:45
static void dumpAudioStreamBasicDescription(const char *header, const AudioStreamBasicDescription &desc)
Definition: MpCoreAudioHardware.h:207
bool IsInput() const
Definition: MpCoreAudioHardware.h:65
static CoreAudioHardware fromDefaultOutput()
Definition: MpCoreAudioHardware.h:81
bool setBufferSize(unsigned sr)
Definition: MpCoreAudioHardware.h:243
AudioDeviceID mDevId
Device handle.
Definition: MpCoreAudioHardware.h:256
int getLatency() const
Definition: MpCoreAudioHardware.h:114
static CoreAudioHardware fromDefaultInput()
Definition: MpCoreAudioHardware.h:92
Container for CoreAudio hardware device specific.
Definition: MpCoreAudioHardware.h:40
double getActualSampleRate() const
Definition: MpCoreAudioHardware.h:103
bool mIsInput
Whether input or output operations.
Definition: MpCoreAudioHardware.h:257
static bool isEqualAudioStreamBasicDescription(const AudioStreamBasicDescription &desc, const AudioStreamBasicDescription &desc2)
Definition: MpCoreAudioHardware.h:219
static CoreAudioHardware invalid()
Definition: MpCoreAudioHardware.h:76