5

I'd like to only allow audio from a certain application. That is, I'd like to have audio turned off by default for new programs.

I'm aware that in PulseAudio one can mute programs, but the problem is that (for me) new programs always begin unmuted.

pjvandehaar
  • 155
  • 4
  • Do you simply want your applications to start muted or do you wish to prevent access to your sound hardware? – darnir Mar 26 '13 at 08:08
  • Either option sounds good. – pjvandehaar Apr 06 '13 at 16:24
  • 1
    One option is to add your user to the audio group, thus allowing applications to grab the hardware (any following application will report "hardware busy"). Theoretically :D – Vorac Aug 12 '13 at 14:59

1 Answers1

3

It's a shame that this option is not available... by the way, before starting your application, you can run this command:

for i in $(pactl list sink-inputs short | awk '{print $1}'); do
    pactl set-sink-input-mute $i 1
done

It simply mutes all the active streams. However, new streams will still open unmuted. For this, you can add another loop to detect new streams and mute them as soon they start:

old_list=$(pactl list sink-inputs short)
while true; do
    new_list=$(pactl list sink-inputs short)
    if [[ "$old_list" != "$new_list" ]]; then
        for i in $(pactl list sink-inputs short | awk '{print $1}'); do
            pactl set-sink-input-mute $i 1
        done
    fi
done

The problem now is that it mutes even our application. So imagine naming the script like pa_solo_mode and use it like pa_solo_mode mycommand -with --any --option, e.g.

pa_solo_mode vlc -v

We now detect the process name and mute it only if the command is different.

function get_stream_pid_list {
  pactl list sink-inputs | awk '
    /application.process.id/ {
      gsub("\"", "", $3)
      print $3
    }'
}

function get_stream_index_from_pid {
  pid=$1
  pactl list sink-inputs | awk '
    /^Sink Input #[0-9]+$/ {
      sub("#", "", $3)
      x=$3
    } 
    /application.process.id = "'$pid'"/ {
      print x
    }'
}

function daemon_mute_except {
  except_pid=$1
  echo "Muting all streams except $except_pid!"
  while true; do

    for pid in $(get_stream_pid_list); do
      if [[ "$except_pid" != "$pid" ]]; then
        index=$(get_stream_index_from_pid $pid)
        pactl set-sink-input-mute $index 1
      fi
    done
    sleep 0.25
  done
}

# run our command and take its PID
$@ &
except_pid=$!
# start the daemon: continuously mute streams
daemon_mute_except $except_pid

fortea
  • 159
  • 1
  • 6