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
## 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
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
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():
25
if len(sys.argv) == 2:
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)
34
def show_mixer(name, idx=0):
26
35
# Demonstrates how mixer settings are queried.
29
mixer = alsaaudio.Mixer(name)
37
mixer = alsaaudio.Mixer(name, cardindex=idx)
30
38
except alsaaudio.ALSAAudioError:
39
sys.stderr.write("No such mixer\n")
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]))
41
50
mutes = mixer.getmute()
42
51
for i in range(len(mutes)):
43
if mutes[i]: print "Channel %i is muted"%i
53
sys.stdout.write("Channel %i is muted\n" % i)
44
54
except alsaaudio.ALSAAudioError:
45
55
# May not support muting
49
59
recs = mixer.getrec()
50
60
for i in range(len(recs)):
51
if recs[i]: print "Channel %i is recording"%i
62
sys.stdout.write("Channel %i is recording\n" % i)
52
63
except alsaaudio.ALSAAudioError:
53
64
# May not support recording
56
if (len(sys.argv)) == 3:
67
def set_mixer(name, args, idx=0):
57
68
# Demonstrates how to set mixer settings
60
mixer = alsaaudio.Mixer(name)
70
mixer = alsaaudio.Mixer(name, cardindex=idx)
61
71
except alsaaudio.ALSAAudioError:
72
sys.stderr.write("No such mixer")
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:])
80
channel = alsaaudio.MIXER_CHANNEL_ALL
82
if args in ['mute', 'unmute']:
67
83
# Mute/unmute the mixer
68
if args == 'mute': mixer.setmute(1)
69
else: mixer.setmute(0)
71
if args in ['rec','unrec']:
85
mixer.setmute(1, channel)
87
mixer.setmute(0, channel)
89
elif args in ['rec','unrec']:
72
90
# Enable/disable recording
73
if args == 'rec': mixer.setrec(1)
78
if args.find(',')!=-1:
79
channel,volume = map(int,args.split(','))
92
mixer.setrec(1, channel)
94
mixer.setrec(0, channel)
81
channel = alsaaudio.MIXER_CHANNEL_ALL
97
# Set volume for specified channel. MIXER_CHANNEL_ALL means set
98
# volume for all channels
83
# Set volume for specified channel. MIXER_CHANNEL_ALL means set
84
# volume for all channels
85
mixer.setvolume(volume,channel)
100
mixer.setvolume(volume, channel)
103
sys.stderr.write('usage: mixertest.py [-c <card>] ' \
104
'[ <control>[,<value>]]\n')
107
if __name__ == '__main__':
110
opts, args = getopt.getopt(sys.argv[1:], 'c:?h')
118
list_mixers(cardindex)
120
show_mixer(args[0], cardindex)
122
set_mixer(args[0], args[1], cardindex)