~ubuntu-branches/ubuntu/trusty/pyalsaaudio/trusty

« back to all changes in this revision

Viewing changes to mixertest.py

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-01-29 19:52:21 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100129195221-l6y4xmauznm77dl3
Tags: 0.5+svn36-1ubuntu1
* Merge from debian testing (LP: #514453, #331171), remaining changes:
  - Call setup.py install with --root= --install-layout=deb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
1
3
## mixertest.py
2
4
##
3
5
## This is an example of using the ALSA mixer API
10
12
## python mixertest.py Master        # show Master mixer settings
11
13
## python mixertest.py Master 80     # set the master volume to 80%
12
14
## python mixertest.py Master 1,90   # set channel 1 volume to 90%
13
 
## python mixertest.py Master mute   # mute the master mixer
14
 
## python mixertest.py Master unmute # unmute the master mixer
15
 
 
 
15
## python mixertest.py Master [un]mute   # [un]mute the master mixer
 
16
## python mixertest.py Capture [un]rec   # [dis/en]able capture
 
17
## python mixertest.py Master 0,[un]mute   # [un]mute channel 0
 
18
## python mixertest.py Capture 0,[un]rec   # [dis/en]able capture on channel 0
 
19
 
 
20
 
 
21
# Footnote: I'd normally use print instead of sys.std(out|err).write,
 
22
# but we're in the middle of the conversion between python 2 and 3
 
23
# and this code runs on both versions without conversion
 
24
 
 
25
import sys
 
26
import getopt
16
27
import alsaaudio
17
 
import sys
18
 
 
19
 
if len(sys.argv) == 1:
20
 
    # Demonstrates how to read the available mixers
21
 
    print "Available mixer controls:"
22
 
    for m in alsaaudio.mixers():
23
 
        print "  '%s'" % m
24
 
 
25
 
if len(sys.argv) == 2:
 
28
 
 
29
def list_mixers(idx=0):
 
30
    sys.stdout.write("Available mixer controls:\n")
 
31
    for m in alsaaudio.mixers(idx):
 
32
        sys.stdout.write("  '%s'\n" % m)
 
33
    
 
34
def show_mixer(name, idx=0):
26
35
    # Demonstrates how mixer settings are queried.
27
 
    name = sys.argv[1]
28
36
    try:
29
 
        mixer = alsaaudio.Mixer(name)
 
37
        mixer = alsaaudio.Mixer(name, cardindex=idx)
30
38
    except alsaaudio.ALSAAudioError:
31
 
        print "No such mixer"
 
39
        sys.stderr.write("No such mixer\n")
32
40
        sys.exit(1)
33
41
 
34
 
    print "Mixer name: '%s'"%mixer.mixer()
35
 
    print "Capabilities",mixer.volumecap()+mixer.switchcap()
 
42
    sys.stdout.write("Mixer name: '%s'\n" % mixer.mixer())
 
43
    sys.stdout.write("Capabilities: %s %s\n" % (' '.join(mixer.volumecap()),
 
44
                                                ' '.join(mixer.switchcap())))
36
45
    volumes = mixer.getvolume()
37
46
    for i in range(len(volumes)):
38
 
        print "Channel %i volume: %i%%"%(i,volumes[i])
 
47
        sys.stdout.write("Channel %i volume: %i%%\n" % (i,volumes[i]))
39
48
        
40
49
    try:
41
50
        mutes = mixer.getmute()
42
51
        for i in range(len(mutes)):
43
 
            if mutes[i]: print "Channel %i is muted"%i
 
52
            if mutes[i]:
 
53
                sys.stdout.write("Channel %i is muted\n" % i)
44
54
    except alsaaudio.ALSAAudioError:
45
55
        # May not support muting
46
56
        pass
48
58
    try:
49
59
        recs = mixer.getrec()
50
60
        for i in range(len(recs)):
51
 
            if recs[i]: print "Channel %i is recording"%i
 
61
            if recs[i]:
 
62
                sys.stdout.write("Channel %i is recording\n" % i)
52
63
    except alsaaudio.ALSAAudioError:
53
64
        # May not support recording
54
65
        pass
55
66
 
56
 
if (len(sys.argv)) == 3:
 
67
def set_mixer(name, args, idx=0):
57
68
    # Demonstrates how to set mixer settings
58
 
    name = sys.argv[1]
59
69
    try:
60
 
        mixer = alsaaudio.Mixer(name)
 
70
        mixer = alsaaudio.Mixer(name, cardindex=idx)
61
71
    except alsaaudio.ALSAAudioError:
62
 
        print "No such mixer"
 
72
        sys.stderr.write("No such mixer")
63
73
        sys.exit(1)
64
74
 
65
 
    args = sys.argv[2]
66
 
    if args in ['mute','unmute']:
 
75
    if args.find(',') != -1:
 
76
        args_array = args.split(',')
 
77
        channel = int(args_array[0])
 
78
        args = ','.join(args_array[1:])
 
79
    else:
 
80
        channel = alsaaudio.MIXER_CHANNEL_ALL
 
81
 
 
82
    if args in ['mute', 'unmute']:
67
83
        # Mute/unmute the mixer
68
 
        if args == 'mute': mixer.setmute(1)
69
 
        else: mixer.setmute(0)
70
 
        sys.exit(0)
71
 
    if args in ['rec','unrec']:
 
84
        if args == 'mute':
 
85
            mixer.setmute(1, channel)
 
86
        else:
 
87
            mixer.setmute(0, channel)
 
88
        
 
89
    elif args in ['rec','unrec']:
72
90
        # Enable/disable recording
73
 
        if args == 'rec': mixer.setrec(1)
74
 
        else: mixer.setrec(0)
75
 
        sys.exit(0)
76
 
        
77
 
            
78
 
    if args.find(',')!=-1:
79
 
        channel,volume = map(int,args.split(','))
 
91
        if args == 'rec':
 
92
            mixer.setrec(1, channel)
 
93
        else:
 
94
            mixer.setrec(0, channel)
 
95
 
80
96
    else:
81
 
        channel = alsaaudio.MIXER_CHANNEL_ALL
 
97
        # Set volume for specified channel. MIXER_CHANNEL_ALL means set
 
98
        # volume for all channels
82
99
        volume = int(args)
83
 
    # Set volume for specified channel. MIXER_CHANNEL_ALL means set
84
 
    # volume for all channels
85
 
    mixer.setvolume(volume,channel)
86
 
 
87
 
 
88
 
 
 
100
        mixer.setvolume(volume, channel)
 
101
 
 
102
def usage():
 
103
    sys.stderr.write('usage: mixertest.py [-c <card>] ' \
 
104
                     '[ <control>[,<value>]]\n')
 
105
    sys.exit(2)
 
106
 
 
107
if __name__ == '__main__':
 
108
 
 
109
    cardindex = 0
 
110
    opts, args = getopt.getopt(sys.argv[1:], 'c:?h')
 
111
    for o, a in opts:
 
112
        if o == '-c':
 
113
            cardindex = int(a)
 
114
        else:
 
115
            usage()
 
116
 
 
117
    if not len(args):
 
118
        list_mixers(cardindex)
 
119
    elif len(args) == 1:
 
120
        show_mixer(args[0], cardindex)
 
121
    else:
 
122
        set_mixer(args[0], args[1], cardindex)