This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio
executable file
·138 lines (121 loc) · 4.18 KB
/
audio
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# By MrRob0-X t.me/@MrRobin_Ho_Od
# Define color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
NC="\033[0m" # No Color
# Configuration Variables
pulse_ip="127.0.0.1"
pulse_port="8000"
audio_rate="48000"
audio_format="s16le"
channel_set="2"
module="module-simple-protocol-tcp"
# Temp files for tracking
STATUS_TRACK_FILE="/tmp/audio_stream_status"
PULSEAUDIO_TRACK_FILE="/tmp/pulseaudio_started_by_script" # Track if script started PulseAudio
MODULE_TRACK_FILE="/tmp/module_started_by_script" # Track if script started the streaming module
# Function to start PulseAudio if not already running
start_pulseaudio() {
if ! pgrep pulseaudio > /dev/null; then
pulseaudio --start > /dev/null 2>&1
touch "$PULSEAUDIO_TRACK_FILE" # Mark that script started PulseAudio
echo -e "${GREEN}PulseAudio started.${NC}"
echo -e "${GREEN}Audio quality enhanced on PulseAudio.${NC}"
elif [ -f "$PULSEAUDIO_TRACK_FILE" ]; then
echo -e "${YELLOW}PulseAudio is running already.${NC}"
fi
}
# Function to check if PulseAudio is active
is_pulseaudio_active() {
pactl info > /dev/null 2>&1
}
# Function to enhance audio quality on PulseAudio
set_sound_quality() {
pactl set-sink-volume @DEFAULT_SINK@ 100% # Sets volume to 100%
pactl set-sink-mute @DEFAULT_SINK@ 0 # Ensures sound is unmuted
}
# Function to load the streaming module with the configured parameters
start_stream() {
# Check if the streaming module is already loaded
if pactl list modules short | grep -q "$module"; then
if [ -f "$MODULE_TRACK_FILE" ]; then
return # Do nothing if module is already active, suppress output
fi
else
# Load the streaming module with configuration variables, exporting IP with listen parameter
pactl load-module "$module" rate="$audio_rate" format="$audio_format" channels="$channel_set" \
listen="$pulse_ip" source=auto_null.monitor record=true port="$pulse_port" || {
echo -e "${RED}Error: Failed to start streaming on $pulse_ip:$pulse_port.${NC}"
return 1
}
echo -e "${BLUE}Audio streaming started on $pulse_ip:$pulse_port.${NC}"
touch "$MODULE_TRACK_FILE"
fi
}
# Function to unload the TCP module and stop PulseAudio
stop_stream() {
if ! is_pulseaudio_active; then
echo -e "${YELLOW}No audio streaming module found.${NC}"
return
fi
local plist
plist=$(pactl list modules short | grep "$module" | awk '{print $1}')
if [ -n "$plist" ]; then
pactl unload-module "$plist" > /dev/null 2>&1 || {
echo -e "${RED}Error: Failed to unload the streaming module.${NC}"
return 1
}
pulseaudio --kill
rm -f "$STATUS_TRACK_FILE" "$PULSEAUDIO_TRACK_FILE" "$MODULE_TRACK_FILE" # Reset status tracking files on stop
echo -e "${GREEN}Audio streaming stopped.${NC}"
else
echo -e "${YELLOW}No audio streaming module found.${NC}"
fi
}
# Function to check if PulseAudio is installed
check_pulseaudio_installed() {
if ! command -v pulseaudio &> /dev/null; then
echo -e "${RED}Error: PulseAudio is not installed.${NC}"
exit 1
fi
}
# Function to check the stream status
check_stream_status() {
if ! is_pulseaudio_active; then
echo -e "${YELLOW}Audio streaming is not active.${NC}"
return
fi
if pactl list modules short | grep -q "$module"; then
if [ -f "$STATUS_TRACK_FILE" ]; then
echo -e "${YELLOW}Streaming module is already active.${NC}"
else
echo -e "${GREEN}Audio streaming is active.${NC}"
touch "$STATUS_TRACK_FILE"
fi
else
echo -e "${YELLOW}Audio streaming is not active.${NC}"
rm -f "$STATUS_TRACK_FILE" # Reset status if stream is inactive
fi
}
# Main script execution
check_pulseaudio_installed
case "$1" in
start)
start_pulseaudio
set_sound_quality
start_stream
;;
stop)
stop_stream
;;
status)
check_stream_status
;;
*)
echo -e "${YELLOW}Usage: $0 {start|stop|status}${NC}" >&2
exit 1
;;
esac