diff --git a/data/cadence-pulse2jack b/data/cadence-pulse2jack old mode 100644 new mode 100755 index edbd270..361ce2a --- a/data/cadence-pulse2jack +++ b/data/cadence-pulse2jack @@ -1,42 +1,53 @@ #!/bin/bash -# Script to stop current pulseaudio and restart it in JACK mode +# Script to bridge/start pulseaudio into JACK mode INSTALL_PREFIX="X-PREFIX-X" -IsPulseAudioRunning() -{ - PROCESS=`ps -e | grep pulseaudio` - if [ "$PROCESS" == "" ]; then - false - else - true - fi -} +# ---------------------------------------------- if [ ! -d ~/.pulse ]; then - mkdir -p ~/.pulse + mkdir -p ~/.pulse fi if [ ! -f ~/.pulse/client.conf ]; then - echo "autospawn = no" > ~/.pulse/client.conf + echo "autospawn = no" > ~/.pulse/client.conf else - if (! cat ~/.pulse/client.conf | grep "autospawn = no" > /dev/null); then - sed -i '/autospawn =/d' ~/.pulse/client.conf - echo "autospawn = no" >> ~/.pulse/client.conf - fi + if (! cat ~/.pulse/client.conf | grep "autospawn = no" > /dev/null); then + sed -i '/autospawn =/d' ~/.pulse/client.conf + echo "autospawn = no" >> ~/.pulse/client.conf + fi fi if [ ! -f ~/.pulse/daemon.conf ]; then - echo "rlimit-rttime = -1" > ~/.pulse/daemon.conf -else - if (! cat ~/.pulse/daemon.conf | grep "rlimit-rttime = -1" > /dev/null); then - sed -i '/rlimit-rttime =/d' ~/.pulse/daemon.conf + echo "default-sample-format = float32le" > ~/.pulse/daemon.conf + echo "realtime-scheduling = yes" >> ~/.pulse/daemon.conf echo "rlimit-rttime = -1" >> ~/.pulse/daemon.conf - fi + echo "exit-idle-time = -1" >> ~/.pulse/daemon.conf +else + if (! cat ~/.pulse/daemon.conf | grep "default-sample-format = float32le" > /dev/null); then + sed -i '/default-sample-format = /d' ~/.pulse/daemon.conf + echo "default-sample-format = float32le" >> ~/.pulse/daemon.conf + fi + if (! cat ~/.pulse/daemon.conf | grep "realtime-scheduling = yes" > /dev/null); then + sed -i '/realtime-scheduling = /d' ~/.pulse/daemon.conf + echo "realtime-scheduling = yes" >> ~/.pulse/daemon.conf + fi + if (! cat ~/.pulse/daemon.conf | grep "rlimit-rttime = -1" > /dev/null); then + sed -i '/rlimit-rttime =/d' ~/.pulse/daemon.conf + echo "rlimit-rttime = -1" >> ~/.pulse/daemon.conf + fi + if (! cat ~/.pulse/daemon.conf | grep "exit-idle-time = -1" > /dev/null); then + sed -i '/exit-idle-time =/d' ~/.pulse/daemon.conf + echo "exit-idle-time = -1" >> ~/.pulse/daemon.conf + fi fi +# ---------------------------------------------- + +PLAY_ONLY="no" + case $1 in - -h|--h|--help) + -h|--h|--help) echo "usage: $0 [command] -p, --play Playback mode only @@ -49,37 +60,64 @@ NOTE: activate PulseAudio with both playback and record modes. " exit - ;; + ;; - -p|--p|--play) + -p|--p|--play) +PLAY_ONLY="yes" FILE=$INSTALL_PREFIX/share/cadence/pulse2jack/play.pa - ;; + ;; - *) + *) FILE=$INSTALL_PREFIX/share/cadence/pulse2jack/play+rec.pa - ;; + ;; esac -if (IsPulseAudioRunning); then +# ---------------------------------------------- + +IsPulseAudioRunning() { - echo "PulseAudio is running, stopping it..." - killall pulseaudio - env sleep 0.2 - - if (IsPulseAudioRunning); then - { - echo "Failed, force kill..." - pkill -KILL pulseaudio - env sleep 0.2 - } - else - echo "Success!" - fi + PROCESS=`ps -e | grep pulseaudio` + if [ "$PROCESS" == "" ]; then + false + else + true + fi } -fi -if (`pulseaudio --daemonize --high-priority --realtime --disallow-module-loading --exit-idle-time=-1 --file=$FILE -n`); then - echo "Initiated PulseAudio successfully!" +if (IsPulseAudioRunning); then +{ + if (`jack_lsp | grep "PulseAudio JACK Sink:" > /dev/null`); then + { + echo "PulseAudio is already running and bridged to JACK" + } + else + { + echo "PulseAudio is already running, bridge it..." + + if [ "$PLAY_ONLY" == "yes" ]; then + { + pactl load-module module-jack-sink > /dev/null + pacmd set-default-source jack_in > /dev/null + } + else + { + pactl load-module module-jack-sink > /dev/null + pactl load-module module-jack-source > /dev/null + pacmd set-default-sink jack_out > /dev/null + pacmd set-default-source jack_in > /dev/null + } + fi + + echo "Done" + } + fi +} else - echo "Failed to initialize PulseAudio!" +{ + if (`pulseaudio --daemonize --high-priority --realtime --disallow-module-loading --exit-idle-time=-1 --file=$FILE -n`); then + echo "Initiated PulseAudio successfully!" + else + echo "Failed to initialize PulseAudio!" + fi +} fi diff --git a/src/cadence_aloop_daemon.py b/src/cadence_aloop_daemon.py index 08d9ea4..51b7336 100755 --- a/src/cadence_aloop_daemon.py +++ b/src/cadence_aloop_daemon.py @@ -3,7 +3,7 @@ # Imports (Global) from ctypes import * -from os import system +from os import path, system from sys import version_info from signal import signal, SIGINT, SIGTERM from time import sleep @@ -52,7 +52,13 @@ def shutdown_callback(arg): # run alsa_in and alsa_out def run_alsa_bridge(): global sample_rate, buffer_size - system("killall alsa_in alsa_out zita-a2j zita-j2a pulseaudio") + killList = "alsa_in alsa_out zita-a2j zita-j2a" + + # On KXStudio, pulseaudio is nicely bridged & configured to work with JACK + if not path.exists("/usr/share/kxstudio/config/pulse/daemon.conf"): + killList += " pulseaudio" + + system("killall %s" % killList) #if (False): system("env JACK_SAMPLE_RATE=%i JACK_PERIOD_SIZE=%i alsa_in -j alsa2jack -d cloop -q 1 2>&1 1> /dev/null &" % (sampleRate, bufferSize))