LoginSignup
0
0

More than 1 year has passed since last update.

【Linux】もっと多くの音を鳴らす方法【Ubuntu】【Ubuntu MATE】

Last updated at Posted at 2022-05-01

概要

Linuxでは、Windowsとはまた違ったサウンドの設定があります。

Linuxのサウンドの作り方は、過去の記事で紹介しました。

登録しても、使われない音もあります。
なので今回は、起動音、終了音などより多くの音を鳴らす方法を紹介します。

ダウンロード

今回のセットのリンクを貼ります。

お使いのデスクトップ環境のscripts.tar.gzをダウンロードして下さい。

(2022/9/1追記)
セットを更新して、形式をtar.gzにしました。

MATE

GNOME、Budgie、GNOMEベースのデスクトップ環境

Xfce

LXQt

注意

Cinnamon、KDE、Trinity(Q4OS)は、サウンドのテーマの構造が異なっているので、
紹介するとしたら別記事で紹介します。

(2022/9/1追記)
記事ができました。

登録方法

例として、Ubuntu MATEでMATE用のセットを使って登録してみます。
なお、ここに載せてあるシェルスクリプトは、全てMATE用です。

scripts.tar.gzをダウンロードします。(画像では7zになっていますが、tar.gzだと思ってください)

a.png

scripts.tar.gzをホームに展開します。

コマンドで展開する場合は、scripts.tar.gzをホームに移動し、ホームから端末を開いて
tar zxvf scripts.tar.gz
を実行します。

b.png

scriptsフォルダーから端末を使って1-prepare-sound.shを実行します。

./1-prepare-sound.sh

c.png

1-prepare-sound.sh
#!/bin/bash

gsettings set org.mate.sound event-sounds true
gsettings set org.mate.sound input-feedback-sounds true

directory=$(cd $(dirname $0); pwd)

XDG_DESKTOP_DIR=`xdg-user-dir DESKTOP`
cd $XDG_DESKTOP_DIR
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=window-close\nIcon[ja]=window-close\nName[ja]=ゴミ箱を空にする\nExec=$directory/recycle.sh\nName=ゴミ箱を空にする" > ゴミ箱を空にする.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-log-out\nIcon[ja]=system-log-out\nName[ja]=ログアウト\nExec=$directory/logout.sh\nName=ログアウト" > ログアウト.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-lock-screen\nIcon[ja]=system-lock-screen\nName[ja]=ロック\nExec=$directory/lock.sh\nName=ロック" > ロック.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-hibernate\nIcon[ja]=system-hibernate\nName[ja]=ハイバネート\nExec=systemctl hibernate\nName=ハイバネート" > ハイバネート.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-suspend\nIcon[ja]=system-suspend\nName[ja]=サスペンド\nExec=systemctl suspend\nName=サスペンド" > サスペンド.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-reboot\nIcon[ja]=system-reboot\nName[ja]=再起動\nExec=$directory/reboot.sh\nName=再起動" > 再起動.desktop
echo -e "#!/usr/bin/env xdg-open\n[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nIcon=system-shutdown\nIcon[ja]=system-shutdown\nName[ja]=シャットダウン\nExec=$directory/shutdown.sh\nName=シャットダウン" > シャットダウン.desktop
chmod 755 *.desktop

mkdir ~/.config/autostart
cd ~/.config/autostart
echo -e "[Desktop Entry]\nType=Application\nExec=canberra-gtk-play -i \"desktop-login\"\nHidden=false\nName[ja]=起動音\nName=起動音\nComment[ja]=\nComment=\nX-MATE-Autostart-Delay=0" > canberra-gtk-play.desktop

if [ -d "/sys/class/power_supply" ]; then
	sudo apt update
	sudo apt install acpi

	echo -e "[Desktop Entry]\nType=Application\nExec=$directory/battery-alarm.sh\nHidden=false\nName[ja]=バッテリーアラーム\nName=バッテリーアラーム\nComment[ja]=\nComment=\nX-MATE-Autostart-Delay=0" > battery-alarm.sh.desktop

	($directory/battery-alarm.sh &)
fi

途中で指示が入った場合は、指示に従って操作します。

ランチャー

1-prepare-sound.shが終了すると、デスクトップに7つのランチャーが作られます。

d.png

ゴミ箱を空にするは、ゴミ箱を空にしてから、trash-emptyを鳴らします。

recycle.sh
#!/bin/bash
rm -r ~/.local/share/Trash/files/*
rm -r ~/.local/share/Trash/info/*
rm -r /media/*/*/.Trash-1000
canberra-gtk-play -i "trash-empty"

ログアウト、再起動、シャットダウンは、
desktop-logoutdesktop-logoffsystem-shutdownのいずれかを鳴らしてから、
ログアウト、再起動、シャットダウンします。

logout.sh
#!/bin/bash

canberra-gtk-play -i "desktop-logout" || canberra-gtk-play -i "desktop-logoff" || canberra-gtk-play -i "system-shutdown"
killall mate-session
reboot.sh
#!/bin/bash

canberra-gtk-play -i "desktop-logout" || canberra-gtk-play -i "desktop-logoff" || canberra-gtk-play -i "system-shutdown"
reboot
shutdown.sh
#!/bin/bash

canberra-gtk-play -i "desktop-logout" || canberra-gtk-play -i "desktop-logoff" || canberra-gtk-play -i "system-shutdown"
poweroff

ロックは、service-logoutを鳴らしてから画面ロックします。
その後、zenityコマンドでメッセージボックスを表示して待機し、
メッセージボックスが閉じられたらservice-loginを鳴らします。

e.png

lock.sh
#!/bin/bash

mate-screensaver-command -l
canberra-gtk-play -i "service-logout"

zenity --info --text="再開します。" --no-wrap
canberra-gtk-play -i "service-login"

ハイバネート、サスペンドは、そのままハイバネート、サスペンドします。(シェルスクリプトを使っていません)

起動音

起動音も登録されます。

startup-sound.sh
#!/bin/bash

directory=$(cd $(dirname $0); pwd)

canberra-gtk-play -i "desktop-login" || canberra-gtk-play -i "system-ready"

バッテリーアラーム

さらに、ノートパソコンの場合は、バッテリーアラームをつけます。
80%以上になったら、バッテリ充電完了アラームbattery-full

f.png

30%以下になったらバッテリ低下アラームbattery-low

g.png

20%以下になったらバッテリ切れアラームbattery-cautionを鳴らします。

h.png

なお、アラームが鳴るのは範囲に入ったら一度だけです(連続では鳴りません)。

battery-alarm.sh
#!/bin/bash

if [ $$ != `pgrep -fo $0` ]; then
	exit 1
fi

full_battery_alarm=0
low_battery_alarm=0
critical_battery_alarm=0

while true
	do
		export DISPLAY=:0.0
		battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
		if on_ac_power; then					#check if AC is plugged in
			if [ $battery_level -ge 80 ]; then		#check if the battery level is over 80%
				if [ $full_battery_alarm -eq 0 ]; then
					notify-send -u critical "バッテリ充電完了アラーム" "バッテリーの充電が完了しました。充電器を取り外してください。" -i battery-full-charged
					canberra-gtk-play -i "battery-full"
					full_battery_alarm=1
				fi
			fi
			low_battery_alarm=0
			critical_battery_alarm=0
		else
			if [ $battery_level -le 20 ]; then		#check if the battery level is under 20%
				if [ $critical_battery_alarm -eq 0 ]; then
					notify-send -u critical "バッテリ切れアラーム" "バッテリーが切れそうになりました。充電してください。" -i battery-caution
					canberra-gtk-play -i "battery-caution"
					critical_battery_alarm=1
				fi
			elif [ $battery_level -le 30 ]; then		#check if the battery level is under 30%
				if [ $low_battery_alarm -eq 0 ]; then
					notify-send -u normal "バッテリ低下アラーム" "バッテリーが低下しました。充電する準備をしてください。" -i battery-low
					canberra-gtk-play -i "battery-low"
					low_battery_alarm=1
				fi
			fi
			full_battery_alarm=0
		fi
		sleep 60						#wait for 60 seconds before checking again
	done

このシェルスクリプトは、こちらを参考にしました。

サウンドのテーマの変更

Ubuntu MATEではサウンドの設定で簡単にサウンドのテーマを変更できますが、そうでないディストリビューションもたくさんあります。
そのために、change-sound.shを用意しました。

change-sound.sh
#!/bin/bash

if [ $# -eq 0 ]; then
	ls /usr/share/sounds

	read theme
else
	theme=$1
fi

if [ -e "/usr/share/sounds/$theme/index.theme" ]; then
	gsettings set org.mate.sound theme-name $theme

	theme_name=`sed -n 's/Name=//p' "/usr/share/sounds/$theme/index.theme"`
	notify-send -i dialog-information "サウンドのテーマ変更" "サウンドのテーマを$theme_nameに変更しました。"
	canberra-gtk-play -i "theme-demo"
else
	notify-send -i dialog-error "サウンドのテーマ変更" "サウンドのテーマ$themeは見つかりませんでした。"
	canberra-gtk-play -i "dialog-error"
fi

このシェルスクリプトを実行すると、サウンドのテーマのフォルダー名が一覧表示されます。
フォルダー名を入力すると、サウンドのテーマが変更され、theme-demoが鳴ります。

ただし、index.themeがないフォルダーの場合、フォルダー名を入力してもサウンドのテーマは変更されません。

サウンドのテスト

現在のサウンドのテーマをテストするシェルスクリプトを用意しました。
Linuxのすべての音をテストします。

sound-test.shは、サウンドのテーマにない音はデフォルトの音を鳴らします。

sound-test.sh(一部)
#!/bin/bash

play_sound() {
	clear
	echo "$1"
	canberra-gtk-play -i "$1"
}

play_sound "network-connectivity-lost"
play_sound "network-connectivity-error"
play_sound "dialog-error"
play_sound "battery-low"
play_sound "suspend-error"
play_sound "software-update-urgent"
play_sound "power-unplug-battery-low"
play_sound "message"
play_sound "message-new-instant"
play_sound "message-new-email"

sound-test-debug.shは、サウンドのテーマにない音は鳴らしません。
そして、音のフルパスを表示します。

sound-test-debug.sh(一部)
#!/bin/bash

play_sound_debug() {
	echo "$1"
	theme=`gsettings get org.mate.sound theme-name | sed "s/'//g"`
	sound="/usr/share/sounds/$theme/stereo/$1"
	if [ -e "$sound.oga" ]; then
		echo "$sound.oga"
		paplay "$sound.oga"
	elif [ -e "$sound.ogg" ]; then
		echo "$sound.ogg"
		paplay "$sound.ogg"
	elif [ -e "$sound.wav" ]; then
		echo "$sound.wav"
		paplay "$sound.wav"
	else
		echo "サウンドがありません。"
	fi
}

play_sound_debug "network-connectivity-lost"
play_sound_debug "network-connectivity-error"
play_sound_debug "dialog-error"
play_sound_debug "battery-low"
play_sound_debug "suspend-error"
play_sound_debug "software-update-urgent"
play_sound_debug "power-unplug-battery-low"
play_sound_debug "message"
play_sound_debug "message-new-instant"
play_sound_debug "message-new-email"

最後に

わからない、うまく動かない、改善案などがあれば、コメントでお知らせください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0