OSX上的AVAudioSession替代品以获得音频驱动程序采样率

mok*_*oka 5 audio macos cocoa avfoundation ios

在IOS上,您可以[[AVAudioSession sharedInstance] sampleRate];用来检索音频驱动程序使用的当前采样率。AVAudioSession在OSX上不存在,所以我想知道如何在OSX上实现相同的目的,因为我在该主题上找不到很多东西。

谢谢

mok*_*oka 4

好的,

经过一些更深入的研究后,音频硬件服务似乎可以在 OSX 上发挥作用。这是一些示例代码:

//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
Run Code Online (Sandbox Code Playgroud)

不幸的是,虽然不像 IOS 版本那么简单,但也能完成工作!这将为您提供可以在 OSX Audio-MIDI-Setup中更改的采样率设置。