Add script to toggle PulseAudio card profile
This commit is contained in:
parent
31dd36c804
commit
e09a8b41f6
1 changed files with 53 additions and 0 deletions
53
toggle_headset
Executable file
53
toggle_headset
Executable file
|
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env bb
|
||||||
|
(ns toggle-headset
|
||||||
|
(:require [clojure.java.shell :refer [sh]]
|
||||||
|
[cheshire.core :as json]))
|
||||||
|
|
||||||
|
(defn get-current-sink-name []
|
||||||
|
(-> (sh "pactl" "--format=json" "list" "sinks")
|
||||||
|
:out
|
||||||
|
(json/parse-string true)
|
||||||
|
(->> (filter #(= (:state %) "RUNNING"))
|
||||||
|
(first)
|
||||||
|
:properties
|
||||||
|
:device.name)))
|
||||||
|
|
||||||
|
(defn get-card-current-profile [card]
|
||||||
|
(-> (sh "pactl" "--format=json" "list" "cards")
|
||||||
|
:out
|
||||||
|
(json/parse-string true)
|
||||||
|
(->> (filter #(= (:name %) card))
|
||||||
|
(first)
|
||||||
|
:active_profile
|
||||||
|
keyword
|
||||||
|
)
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-card-profiles [card]
|
||||||
|
(-> (sh "pactl" "--format=json" "list" "cards")
|
||||||
|
:out
|
||||||
|
(json/parse-string true)
|
||||||
|
(->> (filter #(= (:name %) card))
|
||||||
|
(first)
|
||||||
|
:profiles
|
||||||
|
(map #(assoc (second %) :name (first %)))
|
||||||
|
(filter :available)
|
||||||
|
(sort-by :priority >=)
|
||||||
|
(partition-by (comp pos? :sources))
|
||||||
|
(map first)
|
||||||
|
(take 2)
|
||||||
|
(map :name)
|
||||||
|
)))
|
||||||
|
|
||||||
|
(defn toggle-card-profile [card]
|
||||||
|
(let [current (get-card-current-profile card)
|
||||||
|
profiles (get-card-profiles card)]
|
||||||
|
[profiles current]
|
||||||
|
(if-let [next-profile (some->> profiles
|
||||||
|
(remove (partial = current))
|
||||||
|
first)]
|
||||||
|
(sh "pactl" "set-card-profile" card (name next-profile))
|
||||||
|
(prn "Error selecting next profile"))))
|
||||||
|
|
||||||
|
(toggle-card-profile (get-current-sink-name))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue