From bead40c7d0d8782f84749e610d2422b802101782 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 25 Apr 2015 15:09:25 +0200 Subject: add example audio-volume-control MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/audio-volume-control/README | 33 +++++++++++++++++++++++++++++++ doc/examples/audio-volume-control/amixer | 8 ++++++++ doc/examples/audio-volume-control/cleanup | 3 +++ doc/examples/audio-volume-control/init | 12 +++++++++++ doc/examples/audio-volume-control/monitor | 15 ++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 doc/examples/audio-volume-control/README create mode 100755 doc/examples/audio-volume-control/amixer create mode 100755 doc/examples/audio-volume-control/cleanup create mode 100755 doc/examples/audio-volume-control/init create mode 100755 doc/examples/audio-volume-control/monitor (limited to 'doc/examples/audio-volume-control') diff --git a/doc/examples/audio-volume-control/README b/doc/examples/audio-volume-control/README new file mode 100644 index 0000000..f3864b7 --- /dev/null +++ b/doc/examples/audio-volume-control/README @@ -0,0 +1,33 @@ +Use-case example. + +Assume you have program that display the audio volume. +This program checks every second third if the volume +have changed. + +Also assume that you use amixer to change the volume, +most often by using keybindings via xbindkeys. + +To reduce the delay, you want to send a signal to the +monitor program that the volume have changed. For this +more primitive IPC is sufficient, but lets assume there +are other programs interested in this information too. + +To accomplish this, you create a wrapper for amixer +than broadcasts updates on a bus. This wrapper is +installed as ~/.local/bin/amixer, and ~/.local/bin/ +is included in $PATH before /usr/bin. + + + +Before starting run ~/.init, this code is +should be run from your profile file if you +want to implement this on your system. + +After running ~/.init, you can start one +or more listeners by running ~/.alsa-monitor. + +To change the volume run +`./amixer -c 0 -- set Master 5%+` or similar. + +When you are done run ~/.cleanup + diff --git a/doc/examples/audio-volume-control/amixer b/doc/examples/audio-volume-control/amixer new file mode 100755 index 0000000..c46d4e5 --- /dev/null +++ b/doc/examples/audio-volume-control/amixer @@ -0,0 +1,8 @@ +#!/bin/sh +/usr/bin/amixer "$@" +for arg in "$@"; do + if [ "${arg}" = "set" ] || [ "${arg}" = "sset" ] || [ "${arg}" = "cset" ]; then + exec bus broadcast "/tmp/example-bus" '0 volume-changed *' + fi +done + diff --git a/doc/examples/audio-volume-control/cleanup b/doc/examples/audio-volume-control/cleanup new file mode 100755 index 0000000..a711e4c --- /dev/null +++ b/doc/examples/audio-volume-control/cleanup @@ -0,0 +1,3 @@ +#!/bin/sh +exec bus remove "/tmp/example-bus" + diff --git a/doc/examples/audio-volume-control/init b/doc/examples/audio-volume-control/init new file mode 100755 index 0000000..a429174 --- /dev/null +++ b/doc/examples/audio-volume-control/init @@ -0,0 +1,12 @@ +#!/bin/sh +bus create "/tmp/example-bus" + +# The following code is more suitable in the real world, +# if used, the other files should use "${BUS_AUDIO}" +# instead of "/tmp/example-bus". +# +# export BUS_AUDIO="${XDG_RUNTIME_DIR}/bus/audio" +# if [ ! -f "${BUS_AUDIO}" ]; then +# bus create "${BUS_AUDIO}" +# fi + diff --git a/doc/examples/audio-volume-control/monitor b/doc/examples/audio-volume-control/monitor new file mode 100755 index 0000000..82fe7bc --- /dev/null +++ b/doc/examples/audio-volume-control/monitor @@ -0,0 +1,15 @@ +#!/bin/sh +if [ $# = 1 ]; then + if [ "$(echo "${1}" | cut -d ' ' -f 2)" = "volume-changed" ]; then + printf '\e[H\e[2J' + amixer get Master + fi + exit 0 +fi + +exec 2>/dev/null + +printf '\e[?1049h\e[H\e[2J' +trap -- "printf '\e[?1049l'" SIGINT +bus listen "/tmp/example-bus" \'"${0/\'/\'\\\'\'}"\'' "${arg}"' + -- cgit v1.2.3-70-g09d2