Skip to content

Commit

Permalink
Generic initialize of audio devices
Browse files Browse the repository at this point in the history
More generic solution to initialize audio device profiles
Finds default audio device and initializes its profiles

Signed-off-by: Jon Sahlberg <[email protected]>
  • Loading branch information
josa41 authored and brianmcgillion committed Nov 21, 2024
1 parent 2cfd015 commit 1f0be75
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions modules/common/services/audio.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,76 @@ in
# Start pipewire on system boot
systemd.services.pipewire.wantedBy = [ "multi-user.target" ];

systemd.services."pulseaudio-set-profile" =
systemd.services."initialize-audio-profile" =
let
pulse-set-profile = pkgs.writeShellScriptBin "pulseaudio-set-profile" ''
${pkgs.pulseaudio}/bin/pactl set-card-profile "alsa_card.pci-0000_00_07.0-platform-skl_hda_dsp_generic" "HiFi (Headphones, Mic1, Mic2)"
${pkgs.pulseaudio}/bin/pactl set-card-profile "alsa_card.pci-0000_00_07.0-platform-skl_hda_dsp_generic" "HiFi (Mic1, Mic2, Speaker)"
'';
initialize-audio-profile = pkgs.writeShellApplication {
name = "initialize-audio-profile";
runtimeInputs = [
pkgs.pulseaudio
pkgs.jq
];
text = ''
function setProfile() {
if [[ -n "$1" ]] && [[ -n "$2" ]]; then
echo "Setting audio device profile: ($1 - $2)"
pactl set-card-profile "$1" "$2"
fi
}
function findAudioProfiles() {
local audio_device_name active_profile profile_list
if [[ -n "$1" ]]; then
audio_device_name=$(jq --raw-output --argjson index "$1" '.[$index].name' <<< "$pactl_data")
active_profile=$(jq --raw-output --argjson index "$1" '.[$index].active_profile ' <<< "$pactl_data")
if [[ -n "$audio_device_name" ]]; then
echo "Found the default audio device: $audio_device_name"
profile_list=$(jq --raw-output --argjson index "$1" '.[$index].profiles | keys[]' <<< "$pactl_data")
echo "With profiles: $profile_list"
while IFS= read -r profile; do
setProfile "$audio_device_name" "$profile"
done <<< "$profile_list"
echo "Reset the original active profile."
if [[ -n "$active_profile" ]]; then
setProfile "$audio_device_name" "$active_profile"
fi
fi
fi
}
pactl_data=$(pactl --format=json list cards)
if [[ -z "$pactl_data" ]]; then
pulse_address="''${PULSE_SERVER:-localhost}"
echo "Error connecting to Pulseaudio service at: \"$pulse_address\""
exit 1
fi
default_device_id=$(pactl --format=json info short | jq '.default_sink_name | split(".") | .[1:-1] | join(".")')
echo "Default audio device name: $default_device_id";
audio_device_count=$(jq '. | length' <<< "$pactl_data")
echo "Audio device count: $audio_device_count"
for ((index = 0; index < audio_device_count; index++)); do
device_id=$(jq --argjson index $index '.[$index].name | split(".") | .[1:] | join(".")' <<< "$pactl_data")
echo "Found audio device with id: $device_id"
if [[ "$device_id" == "$default_device_id" ]]; then
findAudioProfiles "$index"
fi
done
'';
};
in
{
enable = true;
description = "Force selection of Pulseaudio integrated mic profile";
path = [ pulse-set-profile ];
description = "Initialize default audio device profiles";
wantedBy = [ "multi-user.target" ];
after = [ "pipewire.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
StandardOutput = "journal";
StandardError = "journal";
Environment = "PULSE_SERVER=tcp:localhost:${toString cfg.pulseaudioTcpControlPort}";
ExecStart = "${pulse-set-profile}/bin/pulseaudio-set-profile";
ExecStart = "${initialize-audio-profile}/bin/initialize-audio-profile";
Restart = "on-failure";
RestartSec = "1";
};
Expand Down

0 comments on commit 1f0be75

Please sign in to comment.