Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioSession alternative on OSX to get audio driver sample rate

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

like image 502
moka Avatar asked Aug 28 '14 19:08

moka


1 Answers

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.

like image 198
moka Avatar answered Oct 02 '22 13:10

moka