Is there a way to control the JACK Output volume via terminal or get a PulseAudio Input?

Solution 1:

Update July 21, 2019

From a professional music website for Linux:

  • Jack Master Volume?

Sorry for posting non-ardour question but I need your guidance. When Jack takes control over audio, many apps, including flashplayer, act very loud i.e in VLC I have volume set to 5%. I would like to control the master volume like in alsamixer instead of setting levels for each app individually. How can I do that?

There are two answers posted and this one is the most helpful:

there is no such concept. JACK is designed for pro-audio and music creation workflows. Its not a desktop sound server, even though some of use it in that way. If you need such a thing, you will need to route all your JACK clients via a mixing client which could be something as simply as JackMix or as complex as Ardour. JACK itself does not provide this facility, and its totally out of the scope of its design.

The simplest solution then is to install JackMix:

"Ever struggled with a number of jack applications on your desktop everyone using its own master volume-fader but not one common place for all the volumes directly accessible?"

"The solution to your problem is JackMix, a mixer app for jack that looks exactly like the mixer you would use if you had to connect your analog equipment."

There are other applications for Jack which you can find listed here.


Original answer

I must confess to be musically-challenged but I think this script is where you are heading:

You can get a list of all sinks with pacmd list-sinks, and set the volume withpacmd set-sink-volume`, so you need to do something like

VOLUME='+5%'
for SINK in `pacmd list-sinks | grep 'index:' | cut -b12-`
do
  pacmd set-sink-volume $SINK $VOLUME
done

where $VOLUME can be absolute (150%) or relative (+5%, -5%), and possibly other formats, too.

Most window managers can be configured to launch scripts or programs, complete with arguments, when you press keys. That's the best method, but if your WM doesn't, there are tools like xbindkeys. So you can customize in any way you want.

Note that Pulseaudio will start using hardware mixers if the sink volume goes over 100%, and that can distort the sound.

Also note that Pulseaudio allows to set the volume for each application ("audio stream") with pamcd set-sink-input-volume. You can list them with pacmd list-sink-inputs and set them in a similar way.

That allows you have the sink volumes at a fixed level so they are about equal, without using hardware mixers, and when you switch sinks, it will automatically have the "right" volume. That's the setup I prefer.

Solution 2:

Based on this example we can solve the problem.

If your sound card can't control the volume on the hardware side or the driver doesn't support this feature of your sound card, a possible workaround is to define a new virtual pcm device in the ~/.asoundrc file, which controls the volume on the software side.

First we need to know the name of our soundcard, thus

 aplay -L

is the helpful command here. The actual card name will be displayed after CARD= and the device name (number) after DEV=.

We can test the device with

speaker-test -D <card name> -c <channel count> -twav

Now we create a new softvol device by adding

pcm.softvol {
    type            softvol
    slave {
    pcm         "<card name>,<device name>"
    }
    control {
        name        "<control name>"
        card        "<card name>"
    }
}

to ~/.asoundrc. (If the file doesn't exist, we have to create the file)

In this case should be Master, please see additional information on control names in the link above.

Now we test the new device with

speaker-test -D softvol -c <channel count> -twav

Open alsamixer, you should see the new control Master now and should be able to change the volume using alsamixer.

It may be necessary to additionally set the device as default in /etc/asound.conf with:

pcm.!default {
    type   hw
    card   <card name>
}
ctl.!default {
    type   hw
    card   <card name>
}

Note that this is different from the suggestion in the link above, but that's what OP reported to work.

Now we need to set Jack interface device to softvol and can use e.g.

amixer -q sset Master 5%+
amixer -q sset Master 5%-
amixer -q sset Master 50%

to increase or decrease the output volume by 5% respectively or set to 50%, fixed.

Amixer needs to "open" the device the first time before the Master volume commands are accessible. Start a sound-test with speaker-test -D softvol -c <channel count> -twav while jackd is not running and then use sudo alsactl store to save the Master-volume state. Otherwise, a sound must be played through softvol after each reboot for the volume control to work.