-
Notifications
You must be signed in to change notification settings - Fork 11
/
pajackconnect
executable file
·192 lines (184 loc) · 6.33 KB
/
pajackconnect
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
#+
# This script is intended to be invoked via QjackCtl to start up and
# shut down JACK on a system running PulseAudio. It handles the
# necessary setup to make the two work together, so PulseAudio clients
# get transparently routed through JACK while the latter is running, or
# if pulseaudio is suspend by pasuspender, do nothing
#
# Usage: in QjackCtl’s Settings window, in the “Options” tab, enter
# the command
#
# pajackconnect start &
#
# in the field labelled “Execute script after Startup”, and put
#
# pajackconnect stop &
#
# in the field labelled “Execute script on Shutdown”.
#
# pajackconnect reset &
#
# in the field labelled “Execute script after Shutdown”.
#
# for use jack without pulseaudio, add in Qjackctl setting window
# in the serverpath field 'pasuspender -- ' before 'jackd', save settings
# as "No Pulse" for example. Remove 'pasuspender -- ',
# and save settings as 'Pulse'. Now you can select from the
# Qjackctl setting window, if you would start jack with or without pulse.
#
# References:
# PulseAudio: <http://www.freedesktop.org/wiki/Software/PulseAudio/>
# JACK: <http://jackaudio.org/>
#
# Copyright 2014 by Lawrence D'Oliveiro <[email protected]>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#
# overall script formating by Hermann Meyer
# added possibility to run jack with suspend pulse by Hermann Meyer
# added save and restore previous used pulseaudio ports by Hermann Meyer
# added reload pulse jack modules after resume from suspend
# (needs to be called by a systemd.service file) by Hermann Meyer
#-
# variable to fetch pasuspender status
p=`ps ax | grep '[p]asuspender'`
if [ "${XDG_RUNTIME_DIR-}" != "" ]
then
pajack_dir="$XDG_RUNTIME_DIR/pulse"
else
pajack_dir="/tmp/pajackconnect-${LOGNAME-${USER-}}"
fi
mkdir -m 700 -p "$pajack_dir"
function restart() {
# reload pulse jack module after resume from suspend.
# give the system time to reload pulse deamon before load the modules.
sleep 5
# if command-line option restart is given
# check if pasuspender waiting for jack, if so, exit here. Nothing more
# to do.
# echo "restart"
if [[ "$p" =~ "jackd" ]]; then
#echo "pasuspender is active"
exit 0
else
# check if pulse jack modules are loaded
jc=`pacmd list | grep jack-sink`
# check if jack is running
ja=`pidof jackd | wc -w`
# check if pulseaudio is running
pa=`pidof pulseaudio | wc -w`
# write results to log file
date +%H:%M:%S >>"$pajack_dir/pajack.log"
echo 'jack-sink =' $jc >>"$pajack_dir/pajack.log"
echo 'jack =' $ja >>"$pajack_dir/pajack.log"
echo 'pulse =' $pa >>"$pajack_dir/pajack.log"
if [[ ! "$jc" =~ "jack" ]] && [[ "$ja" == "1" ]]; then
# if pulseaudio didn't run, let's try start it
if [[ ! "$pa" == "1" ]]; then
sleep 5
pulseaudio -D
echo reload pulseaudio >>"$pajack_dir/pajack.log"
sleep 10
fi
# if jack is running and pulse jack modules aren't loaded,
# let's load them and set them as default ports
[[ "$1" == "--no-auto-connect" ]] && no_auto_connect="connect=0" || no_auto_connect=""
pacmd load-module module-jack-sink channels=2 ${no_auto_connect}
pacmd load-module module-jack-source channels=2 ${no_auto_connect}
pacmd unload-module module-suspend-on-idle
pacmd set-default-sink jack_out
# move around source port, seems to make pulseaudio work with jack
# otherwise it seems that pulseaudio is connected, but it didn't work.
pacmd set-default-source jack_in && sleep 1
source=`cat "$pajack_dir"/pasourcej`
pacmd set-default-source $source && sleep 1
pacmd set-default-source jack_in
# echo "pa source switched"
echo reloaded jack modules >>"$pajack_dir/pajack.log"
else
echo already run >>"$pajack_dir/pajack.log"
fi
fi
}
function start() {
# if command-line option start is given
# check if pasuspender waiting for jack, if so, exit here. Nothing more
# to do.
if [[ "$p" =~ "jackd" ]]; then
#echo "pasuspender is active"
exit 0
else
# if pasuspender isn't waiting for jack, let's fetch the current
# ports, pulseaudio use, save them to "$pajack_dir", for later use.
#echo "pasuspender is NOT active"
sink=`pacmd list short sinks | sed -n "/Default sink name/p"| sed "s/Default sink name://"`
echo $sink >"$pajack_dir"/pasinkj
source=`pacmd list short sinks | sed -n "/Default source name/p"| sed "s/Default source name://"`
echo $source >"$pajack_dir"/pasourcej
# load the pulseaudio jack module and set them as default ports
[[ "$1" == "--no-auto-connect" ]] && no_auto_connect="connect=0" || no_auto_connect=""
pacmd load-module module-jack-sink channels=2 ${no_auto_connect}
pacmd load-module module-jack-source channels=2 ${no_auto_connect}
pacmd unload-module module-suspend-on-idle
pacmd set-default-sink jack_out
# move around source port, seems to make pulseaudio work with jack
# otherwise it seems that pulseaudio is connected, but it didn't work.
pacmd set-default-source jack_in && sleep 1
pacmd set-default-source $source && sleep 1
pacmd set-default-source jack_in
#echo "pa source switched"
fi
}
function stop() {
# if command-line option stop is given
# again, check if pasuspender waiting for jack, if not, unload the pulse
# jack module and reload the module-suspend-on-idle.
if [[ ! "$p" =~ "jackd" ]]; then
#echo "pasuspender is NOT active"
pacmd unload-module module-jack-sink
pacmd unload-module module-jack-source
pacmd load-module module-suspend-on-idle
else
# if pasuspender waiting for jack, add a timeout to pasuspender
# to allow a smooth reset action afterward.
#echo "pasuspender is active"
pasuspender -- sleep 5 &
fi
}
function reset() {
# if command-line option reset is given
# check if we have add a timeout to pasuspender, if so, exit here.
if [[ "$p" =~ "sleep" ]]; then
#echo "pasuspender is active"
exit 0
else
# fetch the previous saved pulse audio ports and set them back
# as default ports for pulseaudio. Done.
sink=`cat "$pajack_dir"/pasinkj`
source=`cat "$pajack_dir"/pasourcej`
pacmd set-default-sink $sink
pacmd set-default-source $source
#echo "pasuspender is NOT active"
fi
}
# check for given command-line option
case "$1" in
restart)
restart $2
;;
start)
start $2
;;
stop)
stop
;;
reset)
reset
;;
*)
printf "Usage:\n\t $0 {restart|start|stop|reset} [--no-auto-connect]\n"
exit 1
esac
exit 0