You are not logged in.

#1 2021-03-20 12:24:07

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

[SOLVED] Event sounds - playing a sound reliably from terminal command

Hi all,

My goal is simple, yet I have been having trouble achieving it reliably; I want to play a simple audio clip of a camera shutter sound when I take a screenshot.

I am using i3-gaps WM and I have bound my PrintScreen key to a command that initiates a screenshot with scrot as below:

bindcode 107 $exe scrot -m 'scrot-%Y-%m-%d_%H-%M-%S.png' -e 'mv $f ~/screenshots/'

The screenshot works as intended, however, I am looking for a way to also make it play the sound file after.

I have been playing around with paplay and mpv in the terminal with a .wav file that I downloaded, however, both paplay and mpv seem to only actually play the sound file on the 3rd or 4th attempt, ie. the first and second time I execute the commands below, I hear no sound. Yet if I execute the command 3 or 4 times on the terminal, it will eventually be audible.

The commands:

paplay camera-shutter.wav

mpv camera-shutter.wav

Obviously, once I find a reliable solution, I will tail the command on to the end of my line in my i3 config, but as it isn't playing sound on the first go, there is no point just yet.
I suspect it has something to do with loading the appropriate application in to memory in time to play the file or having the sound server ready, but I'm unsure how to diagnose.
TIA.

Last edited by coxe87b (2021-05-09 09:31:14)


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

#2 2021-03-20 12:51:31

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

My preferred command line way of playing sound effects is "play" from the https://archlinux.org/packages/community/x86_64/sox/ package. Always seems to work reliably in scripts...

Offline

#3 2021-03-20 12:56:01

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

What's your audio setup? Are things going through external devices before it reaches your speakers? Maybe something is going into standby and needs a second to wake up.

For example here for me I have a DAC and if I use it through SPDIF TOSLINK I need to make sure that PulseAudio is always keeping the sink awake and never letting it go into idle. If it goes into idle then the SPDIF connection and DAC will need a second to get back up and running. I also need to make sure that PulseAudio is always using the same sample rate. By default PulseAudio can change between two values 44.1kHz and 48kHz but a change will cause the SPDIF connection and DAC to need a second to get running.

Offline

#4 2021-03-20 13:02:31

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

Morn wrote:

My preferred command line way of playing sound effects is "play" from the https://archlinux.org/packages/community/x86_64/sox/ package. Always seems to work reliably in scripts...

Thank you. It definitely helps to have some extra options, and I've installed sox, looks good so far.


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

#5 2021-03-20 13:06:46

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

Ropid wrote:

What's your audio setup? Are things going through external devices before it reaches your speakers? Maybe something is going into standby and needs a second to wake up.

For example here for me I have a DAC and if I use it through SPDIF TOSLINK I need to make sure that PulseAudio is always keeping the sink awake and never letting it go into idle. If it goes into idle then the SPDIF connection and DAC will need a second to get back up and running. I also need to make sure that PulseAudio is always using the same sample rate. By default PulseAudio can change between two values 44.1kHz and 48kHz but a change will cause the SPDIF connection and DAC to need a second to get running.

I think you have nailed the problem.
My setup involves my speakers connected via 3.5mm jack to my monitor, which then connects via DisplayPort to my GPU. I have it set up this way so that when I switch my monitor over to my Xbox, the speakers function for the Xbox as well as the PC without changing plugs. So currently, my default audio in pavucontrol is set to HDMI/DisplayPort 3 device, rather than Built-In Audio.

How would I make PulseAudio keep the sink always awake?


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

#6 2021-03-20 13:14:42

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

@Ropid - I can confirm your theory is correct. I plugged the speakers direct in to my 3.5mm Line out and the sound works every time without delay now.


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

#7 2021-03-20 13:55:31

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

coxe87b wrote:

[...] How would I make PulseAudio keep the sink always awake?

I did the following changes here to make my problematic DAC + SPDIF setup work well with PulseAudio:

There's two changes needed in these two files here:

(1) /etc/pulse/daemon.conf   <-- set sample rate to a fixed 48kHz
(2) /etc/pulse/default.pa    <-- disable "module-suspend-on-idle"

The concrete changes are:

(1) daemon.conf: use a fixed 48k sample rate instead of the default two changing 44.1k and 48k rates:

## /etc/pulse/daemon.conf

default-sample-rate = 48000
alternate-sample-rate = 48000

resample-method = speex-float-5

(2) default.pa: disable the suspension of sinks when they are idle (there's two different ways to do it):

## /etc/pulse/default.pa

## either (a) comment out the line where the module is loaded:
#load-module module-suspend-on-idle

## or (b) add an unload command at the very end of the file:
unload-module module-suspend-on-idle

EDIT:

I just remembered I also have power saving features of the snd-hda-intel kernel module disabled. That might be important as well:

## /etc/modprobe.d/snd-hda-intel.conf
options snd_hda_intel power_save=0 power_save_controller=N

Last edited by Ropid (2021-03-20 14:00:07)

Offline

#8 2021-03-20 14:31:07

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

You could also try to enable the "silent stream" option for the hdmi codec:
https://patchwork.kernel.org/project/al … intel.com/

options snd-hda-codec-hdmi enable_silent_stream=1

Last edited by progandy (2021-03-20 14:31:22)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#9 2021-03-22 21:34:32

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

Thank you both for your replies. I will try it when I get home an post an update here with findings.


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

#10 2021-05-09 09:22:03

coxe87b
Member
From: Canberra
Registered: 2019-12-08
Posts: 67

Re: [SOLVED] Event sounds - playing a sound reliably from terminal command

Bit of a late reply, but I ended up just plugging my speakers directly in to my motherboard 3.5mm jack and it seems to reliably play sound each time I run that script now, so I no longer have the issue anymore. More of a work-around than a solution, but it is still handy to know why it was happening.
Cheers


Desktop: Arch Linux  |  i3-gaps WM  |  Intel Core i5-9600K  |  16GB RAM  |  AMD Radeon RX 6700XT  |  Dual monitors @ 1440p + 1080p
Laptop: Garuda Linux  |  Sway WM  |  Dell Latitude E7270  |  Intel Core i5-6300U  |  16GB RAM
~ Do or do not, there is no try ~

Offline

Board footer

Powered by FluxBB