TL; DR
Per riprodurre un suono al termine del comando:
long-running-command; sn3
(dove sn3
è il suono numero 3) metti questo in .bashrc
:
sound() {
# plays sounds in sequence and waits for them to finish
for s in [email protected]; do
paplay $s
done
}
sn1() {
sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg
}
sn2() {
sound /usr/share/sounds/freedesktop/stereo/complete.oga
}
sn3() {
sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}
Oppure leggi sotto per ulteriori opzioni:
Suono diverso in caso di errore e successo
Ecco quello che uso esattamente per quello che chiedi - con una differenza: non solo riproduce un suono quando il comando finisce ma riproduce un suono diverso in caso di successo e in caso di errore. (Ma puoi cambiarlo se non ti piace.)
Ho una funzione di Bash chiamata oks
che uso alla fine dei comandi long running:
make && make test && make install; oks
Riproduce un suono e visualizza OK o ERRORE (con codice di errore) al termine del comando precedente.
Codice sorgente
Ecco la funzione con due helper:
sound() {
# plays sounds in sequence and waits for them to finish
for s in [email protected]; do
paplay $s
done
}
soundbg() {
# plays all sounds at the same time in the background
for s in [email protected]; do
# you may need to change 0 to 1 or something else:
pacmd play-file $s 0 >/dev/null
done
}
oks() {
# like ok but with sounds
s=$?
sound_ok=/usr/share/sounds/ubuntu/stereo/dialog-information.ogg
sound_error=/usr/share/sounds/ubuntu/stereo/dialog-warning.ogg
if [[ $s = 0 ]]; then
echo OK
soundbg $sound_ok
else
echo ERROR: $s
soundbg $sound_error
fi
}
Installazione
Puoi metterlo nella tua ~ / .bashrc direttamente o metterlo in qualche altro file e poi mettere questa linea in ~ / .bashrc:
. ~/path/to/file/with/function
Configurazione
Cambia sound_ok
e sound_error
con altri suoni.
Puoi sperimentare con sound
rispetto a soundbg
e modificare sound_ok
e sound_error
per utilizzare sequenze di molti suoni che ti piacciono per ottenere il risultato che desideri.
Suoni buoni
Per trovare alcuni suoni buoni sul tuo sistema puoi provare:
for i in /usr/share/sounds/*/stereo/*; do echo $i; paplay $i; sleep 1; done
Ecco alcuni suoni che uso spesso che sono disponibili su Ubuntu di default che sono buoni per le notifiche - sn1 è forte e bello, sn2 è molto rumoroso e comunque molto carino, sn3 è estremamente rumoroso e non così bello:
sn1() {
sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg
}
sn2() {
sound /usr/share/sounds/freedesktop/stereo/complete.oga
}
sn3() {
sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}
Ancora, puoi cambiare sound
in soundbg
se vuoi giocare in background senza aspettare che il suono finisca (ad esempio per non rallentare gli script quando suoni un sacco di suoni).
Versione silenziosa
E nel caso in cui - qui c'è la stessa funzione di oks
ma senza suoni:
ok() {
# prints OK or ERROR and exit status of previous command
s=$?
if [[ $s = 0 ]]; then
echo OK
else
echo ERROR: $s
fi
}
Uso
Ecco come lo usi:
Esempio con successo:
ls / && ls /bin && ls /usr; oks
esempio con errore:
ls / && ls /bim && ls /usr; oks
Ovviamente nella pratica i comandi sono più simili a:
make && make test && make install; oks
ma ho usato ls
in modo da poter vedere rapidamente come funziona.
Puoi utilizzare ok
anziché oks
per una versione invisibile.
Oppure puoi utilizzare ad esempio:
ls / && ls /bim && ls /usr; ok; sn1
per stampare OK / ERRORE ma riprodurre sempre lo stesso suono, ecc.
Aggiornamento
Metto queste funzioni su GitHub, vedi:
La fonte può essere scaricata da:
Aggiornamento 2
Ho aggiunto una funzione soundloop
al repository precedente. Riproduce un suono e può essere interrotto da Ctrl + C (a differenza di un semplice while true; do paplay file.ogg; done
che ci si aspetterebbe di funzionare ma non lo fa) come richiesto da shadi nei commenti. È implementato come:
soundloop() {
set +m
a='date +%s'
{ paplay & } 2>/dev/null
wait
b='date +%s'
d=$(($b-$a))
[ $d -eq 0 ] && d=1
while :; do
pacmd play-file 0 >/dev/null
sleep $d
done
}
Se ritieni che sia complicato, indirizza i tuoi reclami agli sviluppatori PulseAudio.