使用 kAudioDevicePropertyVolumeScalarToDecibels 的意外值

问题描述

我在笔记本电脑的内置音频中使用 kAudioDevicePropertyVolumeScalarToDecibels 获得了意外的音量值。

void volume_test()
{
    AudioObjectPropertyAddress address = {
        .mSelector = kAudioHardwarePropertyDefaultOutputDevice,.mScope = kAudioObjectPropertyScopeGlobal,.mElement = kAudioObjectPropertyElementMaster
    };

    AudioObjectID deviceid = kAudioObjectUnkNown;
    UInt32 dataSize = sizeof(deviceid);
    Osstatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,&address,NULL,&dataSize,&deviceid);
    assert(result == noErr);

    address.mSelector = kAudioDevicePropertyVolumeScalar;
    address.mScope = kAudioObjectPropertyScopeOutput;

    Float32 volumeScalar = 0;
    dataSize = sizeof(volumeScalar);
    result = AudioObjectGetPropertyData(deviceid,&volumeScalar);
    assert(result == noErr);

    address.mSelector = kAudioDevicePropertyVolumeDecibels;

    Float32 volumeDecibels = 0;
    dataSize = sizeof(volumeDecibels);
    result = AudioObjectGetPropertyData(deviceid,&volumeDecibels);
    assert(result == noErr);

    address.mSelector = kAudioDevicePropertyVolumeScalarToDecibels;

    Float32 convertedVolumeDecibels = volumeScalar;
    dataSize = sizeof(convertedVolumeDecibels);
    result = AudioObjectGetPropertyData(deviceid,&convertedVolumeDecibels);
    assert(result == noErr);

    address.mSelector = kAudioDevicePropertyVolumeDecibelsToScalar;

    Float32 convertedVolumeScalar = volumeDecibels;
    dataSize = sizeof(convertedVolumeScalar);
    result = AudioObjectGetPropertyData(deviceid,&convertedVolumeScalar);
    assert(result == noErr);

    NSLog(@"Direct    = %.4f %+2.2f dB",volumeScalar,volumeDecibels);
    NSLog(@"Converted = %.4f %+2.2f dB",convertedVolumeScalar,convertedVolumeDecibels);

    address.mSelector = kAudioDevicePropertyVolumeRangeDecibels;

    AudiovalueRange decibelRange;
    dataSize = sizeof(decibelRange);
    result = AudioObjectGetPropertyData(deviceid,&decibelRange);
    assert(result == noErr);

    NSLog(@"dB range %+2.2f ... %+2.2f",decibelRange.mMinimum,decibelRange.mMaximum);
}

输出为:

Direct    = 0.0620 -47.69 dB
Converted = 0.0620 -59.56 dB
dB range -63.50 ... +0.00

直接使用底层 AudioControl 会发生同样的事情。

作为参考,音频 MIDI 设置显示

Audio MIDI Setup

有趣的是,使用我的外部显示器的音频和来自 kAudioDevicePropertyPreferredChannelsForStereo 的元素(因为它没有主元素),这些值是匹配的。

另外值得注意的是,对于显示音频,kAudioDevicePropertyVolumeDecibelsToScalarTransferFunction5kAudioLevelControlTranferFunction2Over1。尝试为笔记本电脑检索 kAudioDevicePropertyVolumeDecibelsToScalarTransferFunction 失败并显示消息

HALC_ShellObject::GetPropertyData: call to the proxy Failed,Error: 2003332927 (who?)
HALPlugIn::ObjectGetPropertyData: got an error from the plug-in routine,Error: 2003332927 (who?)

这不是不受支持属性的“正常”错误消息。

应该如何使用kAudioDevicePropertyVolumeScalarToDecibels

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)