aboutsummaryrefslogtreecommitdiffstats
path: root/_bin/volume.sh
blob: f8558d926d9b85d22ca34813d67df68ecc467e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
#
# Volume control wrapper to support different systems.
#
# Aaron LI
# Created: 2016-01-07
# Updated: 2016-01-07
#

# ALSA device/driver (Linux)
ALSA_DEV="Master"
# OSS device (FreeBSD/DragonFly)
OSS_DEV="vol"


if [ $# -ne 1 ]; then
    echo "Usage:"
    echo "    `basename $0` < +% | -% | toggle | mute | unmute >"
    exit 1
fi


# amixer: Linux ALSA
vol_amixer() {
    case "$1" in
        toggle)
            amixer set ${ALSA_DEV} toggle
            ;;
        mute)
            amixer set ${ALSA_DEV} mute
            ;;
        unmute)
            amixer set ${ALSA_DEV} unmute
            ;;
        +[1-9]*|-[1-9]*)
            sign=`echo "$1" | cut -c1`
            value="${1#?}"
            amixer set ${ALSA_DEV} "${value}%${sign}" unmute
            ;;
        *)
            echo "ERROR: amixer: unknown control command"
            exit 11
            ;;
    esac
}


# mixer: FreeBSD/DragonFly OSS
vol_mixer() {
    # FIXME: how to toggle/mute/unmute ???
    case "$1" in
        +[1-9]*|-[1-9]*)
            sign=`echo "$1" | cut -c1`
            value="${1#?}"
            mixer ${OSS_DEV} "${sign}${value}:${sign}${value}"
            ;;
        *)
            echo "ERROR: mixer: unknown control command"
            exit 21
            ;;
    esac
}


OS=`uname -s`
case "${OS}" in
    Linux)
        vol_amixer "$1"
        ;;
    FreeBSD|DragonFly)
        vol_mixer "$1"
        ;;
    *)
        echo "ERROR: currently unsupport operating system"
        exit 2
        ;;
esac