-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb-autovolume
79 lines (64 loc) · 2.71 KB
/
usb-autovolume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Configuration
readonly DEFAULT_VOLUME=85 # Default volume to set for USB soundcard
# Enable strict error handling
set -euo pipefail
# Function to handle errors
# Logs an error message with the line number and error code, then exits
# Arguments:
# $1 - Line number where the error occurred
# $2 - Error code returned by the command
error_handler() {
local line_no=$1
local error_code=$2
log_message "Error (code: ${error_code}) occurred on line ${line_no}"
exit ${error_code}
}
# Trap any error and pass it to the error handler
trap 'error_handler ${LINENO} $?' ERR
# Log function to write messages to a log file
# This appends messages with a timestamp to /tmp/autovolume-usb.log
# Arguments:
# $1 - Message to log
log_message() {
echo "$1"
echo "$(date) - $1" >> /tmp/autovolume-usb.log
}
# Sleep for a brief moment to ensure the device is fully initialized
sleep 1
# Log the detected soundcards
log_message "$(aplay -l)"
# Check for USB soundcard
# NOTE: If there are multiple USB cards detected, we assume the last entry is the intended one
# This resolves ambiguity if multiple USB soundcards are detected
# TODO - pass env vars from udev so this isnt ambiguous, keep this method as fallback
USB_CARDS=$(aplay -l | grep "card" | grep -i "usb" | awk '{print $2}' | cut -d':' -f1)
if [ -n "$USB_CARDS" ]; then
# If multiple USB soundcards are detected, log a warning and pick the last one
CARD_COUNT=$(echo "$USB_CARDS" | wc -l)
if [ "$CARD_COUNT" -gt 1 ]; then
log_message "Warning: Multiple USB soundcards detected. Using the last detected card."
fi
# Select the last USB soundcard detected
USB_CARD=$(echo "$USB_CARDS" | tail -n 1)
# Output the soundcard index for debugging
log_message "USB audio device detected. Soundcard index: $USB_CARD"
# Validate USB_CARD is numeric
if ! [[ "$USB_CARD" =~ ^[0-9]+$ ]]; then
log_message "Error: Invalid soundcard number: $USB_CARD"
exit 1
fi
# Detect the control name for the USB soundcard, like 'Master', 'Speaker', etc.
CONTROL_NAME=$(amixer -c "$USB_CARD" scontrols | grep -i 'Master\|Speaker\|Headphone\|Front\|Output\|PCM' | head -n 1 | cut -d"'" -f2)
# If no known control found, log an error
if [ -z "$CONTROL_NAME" ]; then
log_message "No valid control name found for USB soundcard (card $USB_CARD)"
exit 1
fi
log_message "Mixer name: $CONTROL_NAME"
# Adjust the volume of the detected soundcard to 85% using amixer
amixer -c "$USB_CARD" sset "$CONTROL_NAME" "$DEFAULT_VOLUME%"
log_message "Volume set to $DEFAULT_VOLUME% on card $USB_CARD, control '$CONTROL_NAME'"
else
log_message "No USB audio device detected."
fi