Collection of tools useful for audio production
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
10KB

  1. #!/bin/bash
  2. # Script to bridge/start pulseaudio into JACK mode
  3. INSTALL_PREFIX="X-PREFIX-X"
  4. # ----------------------------------------------
  5. if [ ! -d ~/.pulse ]; then
  6. mkdir -p ~/.pulse
  7. fi
  8. if [ ! -f ~/.pulse/client.conf ]; then
  9. echo "autospawn = no" > ~/.pulse/client.conf
  10. else
  11. if (! cat ~/.pulse/client.conf | grep "autospawn = no" > /dev/null); then
  12. sed -i '/autospawn =/d' ~/.pulse/client.conf
  13. echo "autospawn = no" >> ~/.pulse/client.conf
  14. fi
  15. fi
  16. if [ ! -f ~/.pulse/daemon.conf ]; then
  17. echo "default-sample-format = float32le" > ~/.pulse/daemon.conf
  18. echo "realtime-scheduling = yes" >> ~/.pulse/daemon.conf
  19. echo "rlimit-rttime = -1" >> ~/.pulse/daemon.conf
  20. echo "exit-idle-time = -1" >> ~/.pulse/daemon.conf
  21. else
  22. if (! cat ~/.pulse/daemon.conf | grep "default-sample-format = float32le" > /dev/null); then
  23. sed -i '/default-sample-format = /d' ~/.pulse/daemon.conf
  24. echo "default-sample-format = float32le" >> ~/.pulse/daemon.conf
  25. fi
  26. if (! cat ~/.pulse/daemon.conf | grep "realtime-scheduling = yes" > /dev/null); then
  27. sed -i '/realtime-scheduling = /d' ~/.pulse/daemon.conf
  28. echo "realtime-scheduling = yes" >> ~/.pulse/daemon.conf
  29. fi
  30. if (! cat ~/.pulse/daemon.conf | grep "rlimit-rttime = -1" > /dev/null); then
  31. sed -i '/rlimit-rttime =/d' ~/.pulse/daemon.conf
  32. echo "rlimit-rttime = -1" >> ~/.pulse/daemon.conf
  33. fi
  34. if (! cat ~/.pulse/daemon.conf | grep "exit-idle-time = -1" > /dev/null); then
  35. sed -i '/exit-idle-time =/d' ~/.pulse/daemon.conf
  36. echo "exit-idle-time = -1" >> ~/.pulse/daemon.conf
  37. fi
  38. fi
  39. # ----------------------------------------------
  40. wanted_capture_ports=0 # -1 means default
  41. wanted_playback_ports=0 # -1 means default
  42. arg_is_for=""
  43. for arg in "$@";do
  44. case "$arg" in
  45. -h|--h|--help)
  46. echo "usage: $0 [command]
  47. -p Playback only with default number of channels
  48. -p <NUMBER> Number of playback channels
  49. -c Capture only with default number of channels
  50. -c <NUMBER> Number of capture channels
  51. -h, --help Show this help menu
  52. --dummy Don't do anything, just create the needed files
  53. NOTE:
  54. When runned with no arguments, $(basename "$0") will
  55. activate PulseAudio with both playback and record modes with default number of channels.
  56. "
  57. exit
  58. ;;
  59. --dummy)
  60. exit
  61. ;;
  62. -c|--capture)
  63. arg_is_for="capture"
  64. wanted_capture_ports=-1 # -1 means default, if no (correct) argument is given.
  65. ;;
  66. -p|--play|--playback)
  67. arg_is_for="playback"
  68. wanted_playback_ports=-1
  69. ;;
  70. * )
  71. case "$arg_is_for" in
  72. "capture")
  73. [ "$arg" -ge 0 ] 2>/dev/null && wanted_capture_ports="$arg"
  74. ;;
  75. "playback")
  76. [ "$arg" -ge 0 ] 2>/dev/null && wanted_playback_ports="$arg"
  77. ;;
  78. esac
  79. ;;
  80. esac
  81. done
  82. if [ $wanted_capture_ports == 0 ] && [ $wanted_playback_ports == 0 ];then
  83. #no sense to want to start/bridge pulseaudio without ports, set as default
  84. capture_ports=-1 # -1 means default
  85. playback_ports=-1 # -1 means default
  86. fi
  87. str_capture="channels=$wanted_capture_ports" #used for pulseaudio commands
  88. str_playback="channels=$wanted_playback_ports" ##
  89. [ $wanted_capture_ports == -1 ] && str_capture="" # -1 means default, no command channels=n
  90. [ $wanted_playback_ports == -1 ] && str_playback="" ##
  91. # ----------------------------------------------
  92. IsPulseAudioRunning()
  93. {
  94. PROCESS=`ps -u $USER | grep pulseaudio`
  95. if [ "$PROCESS" == "" ]; then
  96. false
  97. else
  98. true
  99. fi
  100. }
  101. StartBridged()
  102. {
  103. #write pulseaudio config file in a tmp file
  104. pa_file=$(mktemp --suffix .pa)
  105. echo .fail > $pa_file
  106. ### Automatically restore the volume of streams and devices
  107. echo load-module module-device-restore >> $pa_file
  108. echo load-module module-stream-restore >> $pa_file
  109. echo load-module module-card-restore >> $pa_file
  110. ### Load Jack modules
  111. [ $wanted_capture_ports != 0 ] && echo load-module module-jack-source $str_capture >> $pa_file
  112. [ $wanted_playback_ports != 0 ] && echo load-module module-jack-sink $str_playback >> $pa_file
  113. ### Load unix protocol
  114. echo load-module module-native-protocol-unix >> $pa_file
  115. ### Automatically restore the default sink/source when changed by the user
  116. ### during runtime
  117. ### NOTE: This should be loaded as early as possible so that subsequent modules
  118. ### that look up the default sink/source get the right value
  119. echo load-module module-default-device-restore >> $pa_file
  120. ### Automatically move streams to the default sink if the sink they are
  121. ### connected to dies, similar for sources
  122. echo load-module module-rescue-streams >> $pa_file
  123. ### Make sure we always have a sink around, even if it is a null sink.
  124. echo load-module module-always-sink >> $pa_file
  125. ### Make Jack default
  126. [ $wanted_capture_ports != 0 ] && echo set-default-source jack_in >> $pa_file
  127. [ $wanted_playback_ports != 0 ] && echo set-default-sink jack_out >> $pa_file
  128. if (`pulseaudio --daemonize --high-priority --realtime --exit-idle-time=-1 --file=$pa_file -n`); then
  129. echo "Initiated PulseAudio successfully!"
  130. else
  131. echo "Failed to initialize PulseAudio!"
  132. fi
  133. }
  134. killReStart()
  135. {
  136. pulseaudio -k && StartBridged
  137. exit
  138. }
  139. JackNotRunning()
  140. {
  141. echo "JACK seems not running, start JACK before bridge PulseAudio"
  142. exit 1
  143. }
  144. if (IsPulseAudioRunning); then
  145. {
  146. #count all Jack Audio Physical ports
  147. all_jack_lsp=$(jack_lsp -p -t) || JackNotRunning
  148. output_physical_lines=$(echo "$all_jack_lsp"|grep -n "output,physical,"|cut -d':' -f1)
  149. input_physical_lines=$( echo "$all_jack_lsp"|grep -n "input,physical," |cut -d':' -f1)
  150. audio_lines=$(echo "$all_jack_lsp" |grep -n " audio"|cut -d':' -f1)
  151. capture_physical_ports=0
  152. playback_physical_ports=0
  153. for out_phy_line in $output_physical_lines;do
  154. if echo "$audio_lines"|grep -q $(($out_phy_line + 1));then
  155. ((capture_physical_ports++))
  156. fi
  157. done
  158. for in_phy_line in $input_physical_lines;do
  159. if echo "$audio_lines"|grep -q $(($in_phy_line + 1));then
  160. ((playback_physical_ports++))
  161. fi
  162. done
  163. #count PulseAudio jack ports
  164. current_playback_ports=$(echo "$all_jack_lsp"|grep ^"PulseAudio JACK Sink:" |wc -l)
  165. current_capture_ports=$( echo "$all_jack_lsp"|grep ^"PulseAudio JACK Source:"|wc -l)
  166. #if number of pulseaudio ports equal to physical ports, consider pulseaudio module is running the default mode (no channels=n)
  167. [ $current_capture_ports == $capture_physical_ports ] && current_capture_ports=-1
  168. [ $current_playback_ports == $playback_physical_ports ] && current_playback_ports=-1
  169. [ $wanted_capture_ports == $capture_physical_ports ] && wanted_capture_ports=-1
  170. [ $wanted_playback_ports == $playback_physical_ports ] && wanted_playback_ports=-1
  171. if [ $wanted_capture_ports == $current_capture_ports ] && [ $wanted_playback_ports == $current_playback_ports ];then
  172. echo "PulseAudio is already started and bridged to Jack with $current_capture_ports inputs and $current_playback_ports outputs, nothing to do !"
  173. exit
  174. fi
  175. if [ $current_capture_ports != $wanted_capture_ports ];then
  176. if [ $current_capture_ports != 0 ];then
  177. echo "unload PulseAudio JACK Source"
  178. pactl unload-module module-jack-source > /dev/null || killReStart
  179. fi
  180. if [ $wanted_capture_ports != 0 ];then
  181. echo "load PulseAudio JACK Source $str_capture"
  182. pactl load-module module-jack-source $str_capture > /dev/null
  183. pacmd set-default-source jack_in > /dev/null
  184. fi
  185. fi
  186. if [ $current_playback_ports != $wanted_playback_ports ];then
  187. if [ $current_playback_ports != 0 ];then
  188. echo "unload PulseAudio JACK Sink"
  189. pactl unload-module module-jack-sink > /dev/null || killReStart
  190. fi
  191. if [ $wanted_playback_ports != 0 ];then
  192. echo "load PulseAudio JACK Sink $str_playback"
  193. pactl load-module module-jack-sink $str_playback > /dev/null
  194. pactl set-default-sink jack_out > /dev/null
  195. fi
  196. fi
  197. }
  198. else
  199. {
  200. StartBridged
  201. # #write pulseaudio config file in a tmp file
  202. # pa_file=$(mktemp --suffix .pa)
  203. # echo .fail > $pa_file
  204. #
  205. # ### Automatically restore the volume of streams and devices
  206. # echo load-module module-device-restore >> $pa_file
  207. # echo load-module module-stream-restore >> $pa_file
  208. # echo load-module module-card-restore >> $pa_file
  209. #
  210. # ### Load Jack modules
  211. # [ $wanted_capture_ports != 0 ] && echo load-module module-jack-source $str_capture >> $pa_file
  212. # [ $wanted_playback_ports != 0 ] && echo load-module module-jack-sink $str_playback >> $pa_file
  213. #
  214. # ### Load unix protocol
  215. # echo load-module module-native-protocol-unix >> $pa_file
  216. #
  217. # ### Automatically restore the default sink/source when changed by the user
  218. # ### during runtime
  219. # ### NOTE: This should be loaded as early as possible so that subsequent modules
  220. # ### that look up the default sink/source get the right value
  221. # echo load-module module-default-device-restore >> $pa_file
  222. #
  223. # ### Automatically move streams to the default sink if the sink they are
  224. # ### connected to dies, similar for sources
  225. # echo load-module module-rescue-streams >> $pa_file
  226. #
  227. # ### Make sure we always have a sink around, even if it is a null sink.
  228. # echo load-module module-always-sink >> $pa_file
  229. #
  230. # ### Make Jack default
  231. # [ $wanted_capture_ports != 0 ] && echo set-default-source jack_in >> $pa_file
  232. # [ $wanted_playback_ports != 0 ] && echo set-default-sink jack_out >> $pa_file
  233. #
  234. # if (`pulseaudio --daemonize --high-priority --realtime --exit-idle-time=-1 --file=$pa_file -n`); then
  235. # echo "Initiated PulseAudio successfully!"
  236. # else
  237. # echo "Failed to initialize PulseAudio!"
  238. # fi
  239. }
  240. fi