AVAudioSession alternative on OSX to get audio driver sample rate

倖福魔咒の 提交于 2019-12-07 04:06:39

问题


on IOS you can use [[AVAudioSession sharedInstance] sampleRate]; to retrieve the current sample rate used by the audio driver. AVAudioSession does not exist on OSX, so I am wondering how to achieve the same thing on OSX, as I could not find much on the topic.

Thanks


回答1:


Okay,

after some more in depth research Audio Hardware Services seems to do the trick on OSX. Here is some example code:

//get the default output device
AudioObjectPropertyAddress addr;
UInt32 size;
AudioDeviceID deviceID = 0;
addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
addr.mScope = kAudioObjectPropertyScopeGlobal;
addr.mElement = 0;
size = sizeof(AudioDeviceID);
err = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL, &size, &deviceID);

//get its sample rate
addr.mSelector = kAudioDevicePropertyNominalSampleRate;
addr.mScope = kAudioObjectPropertyScopeGlobal;
addr.mElement = 0;
size = sizeof(Float64);
Float64 outSampleRate;
err = AudioHardwareServiceGetPropertyData(deviceID, &addr, 0, NULL, &size, &outSampleRate);
//if there is no error, outSampleRate contains the sample rate

Unfortunately, not as easy as the IOS version, but does the job! This will give you the sample rate settings you can change in the OSX Audio-MIDI-Setup.



来源:https://stackoverflow.com/questions/25556680/avaudiosession-alternative-on-osx-to-get-audio-driver-sample-rate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!