Skip to content

Commit d4a7ae8

Browse files
committed
Sound (macOS): detecting volume more aggressively
1 parent 04da7ce commit d4a7ae8

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Bugfixes:
77
* Fix flatpak package count (#441)
88
* Don't print white color blocks with `--pipe` (#450)
99
* Fix iTerm being detected as iTermServer-* sometimes
10+
* Fix sound device volume being incorrectly detected as muted sometimes (Sound)
1011

1112
Logo:
1213
* Update Windows 11 ASCII logo to look more visually consistent (#445)

src/detection/sound/sound.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "fastfetch.h"
77

8+
#define FF_SOUND_VOLUME_UNKNOWN 255
9+
810
typedef struct FFSoundDevice
911
{
1012
FFstrbuf identifier;

src/detection/sound/sound_apple.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* de
4444
FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices);
4545
device->main = deviceId == mainDeviceId;
4646
device->active = false;
47-
device->volume = 0;
47+
device->volume = FF_SOUND_VOLUME_UNKNOWN;
4848
ffStrbufInitF(&device->identifier, "%u", (unsigned) deviceId);
4949
ffStrbufInit(&device->name);
5050

@@ -67,6 +67,34 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* de
6767
kAudioObjectPropertyElementMain
6868
}, 0, NULL, &dataSize, &volume) == kAudioHardwareNoError)
6969
device->volume = (uint8_t) (volume * 100 + 0.5);
70+
else
71+
{
72+
// Try detecting volume from channels
73+
uint32_t channels[2];
74+
dataSize = sizeof(channels);
75+
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
76+
kAudioDevicePropertyPreferredChannelsForStereo,
77+
kAudioObjectPropertyScopeOutput,
78+
kAudioObjectPropertyElementMain
79+
}, 0, NULL, &dataSize, channels) == kAudioHardwareNoError)
80+
{
81+
dataSize = sizeof(volume);
82+
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
83+
kAudioDevicePropertyVolumeScalar,
84+
kAudioObjectPropertyScopeOutput,
85+
channels[0]
86+
}, 0, NULL, &dataSize, &volume) == kAudioHardwareNoError)
87+
{
88+
float temp;
89+
if (AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){
90+
kAudioDevicePropertyVolumeScalar,
91+
kAudioObjectPropertyScopeOutput,
92+
channels[1]
93+
}, 0, NULL, &dataSize, &temp) == kAudioHardwareNoError)
94+
device->volume = (uint8_t) ((volume + temp) / 2 * 100 + 0.5);
95+
}
96+
}
97+
}
7098
}
7199

72100
CFStringRef name;

src/detection/sound/sound_windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const char* ffDetectSound(FF_MAYBE_UNUSED const FFinstance* instance, FF_MAYBE_U
6262
FFSoundDevice* device = (FFSoundDevice*) ffListAdd(devices);
6363
device->main = wcscmp(mainDeviceId, immDeviceId) == 0;
6464
device->active = !!(immState & DEVICE_STATE_ACTIVE);
65-
device->volume = 0;
65+
device->volume = FF_SOUND_VOLUME_UNKNOWN;
6666
ffStrbufInit(&device->identifier);
6767
ffStrbufInit(&device->name);
6868

src/modules/sound.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ static void printDevice(FFinstance* instance, const FFSoundDevice* device, uint8
1111
ffPrintLogoAndKey(instance, FF_SOUND_MODULE_NAME, index, &instance->config.sound.key);
1212
ffStrbufWriteTo(&device->name, stdout);
1313

14-
if(device->volume > 0)
15-
printf(" (%d%%)", device->volume);
16-
else
17-
fputs(" (muted)", stdout);
14+
if(device->volume != FF_SOUND_VOLUME_UNKNOWN)
15+
{
16+
if(device->volume > 0)
17+
printf(" (%d%%)", device->volume);
18+
else
19+
fputs(" (muted)", stdout);
20+
}
1821

1922
if(device->main && index > 0)
2023
fputs(" (*)", stdout);

0 commit comments

Comments
 (0)