Sound (macOS): detecting volume more aggressively

This commit is contained in:
李通洲
2023-05-26 15:18:59 +08:00
committed by Carter Li
parent 04da7cebdb
commit d4a7ae8b34
5 changed files with 40 additions and 6 deletions
+1
View File
@@ -7,6 +7,7 @@ Bugfixes:
* Fix flatpak package count (#441)
* Don't print white color blocks with `--pipe` (#450)
* Fix iTerm being detected as iTermServer-* sometimes
* Fix sound device volume being incorrectly detected as muted sometimes (Sound)
Logo:
* Update Windows 11 ASCII logo to look more visually consistent (#445)
+2
View File
@@ -5,6 +5,8 @@
#include "fastfetch.h"
#define FF_SOUND_VOLUME_UNKNOWN 255
typedef struct FFSoundDevice
{
FFstrbuf identifier;
+29 -1
View File
@@ -44,7 +44,7 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* de
FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices);
device->main = deviceId == mainDeviceId;
device->active = false;
device->volume = 0;
device->volume = FF_SOUND_VOLUME_UNKNOWN;
ffStrbufInitF(&device->identifier, "%u", (unsigned) deviceId);
ffStrbufInit(&device->name);
@@ -67,6 +67,34 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* de
kAudioObjectPropertyElementMain
}, 0, NULL, &dataSize, &volume) == kAudioHardwareNoError)
device->volume = (uint8_t) (volume * 100 + 0.5);
else
{
// Try detecting volume from channels
uint32_t channels[2];
dataSize = sizeof(channels);
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
kAudioDevicePropertyPreferredChannelsForStereo,
kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMain
}, 0, NULL, &dataSize, channels) == kAudioHardwareNoError)
{
dataSize = sizeof(volume);
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
kAudioDevicePropertyVolumeScalar,
kAudioObjectPropertyScopeOutput,
channels[0]
}, 0, NULL, &dataSize, &volume) == kAudioHardwareNoError)
{
float temp;
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
kAudioDevicePropertyVolumeScalar,
kAudioObjectPropertyScopeOutput,
channels[1]
}, 0, NULL, &dataSize, &temp) == kAudioHardwareNoError)
device->volume = (uint8_t) ((volume + temp) / 2 * 100 + 0.5);
}
}
}
}
CFStringRef name;
+1 -1
View File
@@ -62,7 +62,7 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_U
FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices);
device->main = wcscmp(mainDeviceId, immDeviceId) == 0;
device->active = !!(immState & DEVICE_STATE_ACTIVE);
device->volume = 0;
device->volume = FF_SOUND_VOLUME_UNKNOWN;
ffStrbufInit(&device->identifier);
ffStrbufInit(&device->name);
+7 -4
View File
@@ -11,10 +11,13 @@ static void printDevice(FFinstance* instance, const FFSoundDevice* device, uint8
ffPrintLogoAndKey(instance, FF_SOUND_MODULE_NAME, index, &instance->config.sound.key);
ffStrbufWriteTo(&device->name, stdout);
if(device->volume > 0)
printf(" (%d%%)", device->volume);
else
fputs(" (muted)", stdout);
if(device->volume != FF_SOUND_VOLUME_UNKNOWN)
{
if(device->volume > 0)
printf(" (%d%%)", device->volume);
else
fputs(" (muted)", stdout);
}
if(device->main && index > 0)
fputs(" (*)", stdout);