This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
alsamixer_webui.py
executable file
·349 lines (281 loc) · 10.7 KB
/
alsamixer_webui.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# File: alsamixer_webui.py
# Date: 24. 1. 2016
# Author: Jiri Skorpil <jiri.sko@gmail.com>
# Desc.: ALSA Mixer WebUI - main application
#
import sys
import re
import os
import errno
from subprocess import call, Popen, PIPE
import socket
import json
from flask import Flask, Response
import argparse
try:
# Python 2.x
import ConfigParser
except ImportError:
# Python 3.x
import configparser as ConfigParser
CONFIG_FILE = '/etc/amixer-webui.conf'
DEFAULT_HOST = '0.0.0.0'
DEFAULT_PORT = '8080'
class Handler(Flask):
card = None
equal = False
PULSE_AUDIO_DEVICE_NUMBER = 99999
def __init__(self, *args, **kwargs):
Flask.__init__(self, *args, **kwargs)
def __get_amixer_command__(self):
command = ["amixer"]
if self.card == self.PULSE_AUDIO_DEVICE_NUMBER:
command += ["-D", "pulse"]
elif self.card is not None:
command += ["-c", "%d" % self.card]
if self.equal is True:
command += ["-D", "equal"]
return command
@staticmethod
def __get_channel_name__(desc, name, i):
for control in desc:
lines = control.split("\n")
control_name = re.sub("',[0-9]+", "", lines[0][1:])
if control_name not in name:
continue
for line in lines[1:]:
if name.split(" ")[-2] in line:
names = line.split(": ")[1].split(" - ")
return names[i]
return None
def __get_cards__(self):
system_cards = []
try:
with open("/proc/asound/cards", 'rt') as f:
for l in f.readlines():
if ']:' in l:
system_cards.append(l.strip())
except IOError as e:
if e.errno != errno.ENOENT:
raise e
cards = {}
for i in system_cards:
card_number = i.split(" [")[0].strip()
card_detail = Popen(["amixer", "-c", card_number, "info"], stdout=PIPE).communicate()[0]
cards[card_number] = self.__decode_string(card_detail).split("\n")[1].split(":")[1].replace("'", "").strip()
pulse = Popen(["amixer", "-D", "pulse", "info"], stdout=PIPE)
pulse.communicate()
if pulse.wait() == 0:
cards[self.PULSE_AUDIO_DEVICE_NUMBER] = "PulseAudio"
return cards
def __get_controls__(self):
try:
amixer = Popen(self.__get_amixer_command__(), stdout=PIPE)
amixer_channels = Popen(["grep", "-e", "control", "-e", "channels"], stdin=amixer.stdout, stdout=PIPE)
amixer_chandesc = self.__decode_string(amixer_channels.communicate()[0]).split("Simple mixer control ")[1:]
amixer_contents = self.__decode_string(
Popen(self.__get_amixer_command__() + ["contents"], stdout=PIPE).communicate()[0])
except OSError:
return []
interfaces = []
for i in amixer_contents.split("numid=")[1:]:
lines = i.split("\n")
interface = {
"id": int(lines[0].split(",")[0]),
"iface": lines[0].split(",")[1].replace("iface=", ""),
"name": lines[0].split(",")[2].replace("name=", "").replace("'", ""),
"type": lines[1].split(",")[0].replace(" ; type=", ""),
"access": lines[1].split(",")[1].replace("access=", ""),
}
if interface["type"] == "ENUMERATED":
items = {}
for line in lines[2:-2]:
pcs = line.split(" '")
id = pcs[0].replace(" ; Item #", "")
name = pcs[1][:-1]
items[id] = name
interface["items"] = items
interface["values"] = []
for value in lines[-2].replace(" : values=", "").split(","):
interface["values"].append(int(value))
elif interface["type"] == "BOOLEAN":
interface["values"] = []
for value in lines[-2].replace(" : values=", "").split(","):
interface["values"].append(True if value == "on" else False)
elif interface["type"] == "INTEGER":
interface["min"] = int(lines[1].split(",")[3].replace("min=", ""))
interface["max"] = int(lines[1].split(",")[4].replace("max=", ""))
interface["step"] = int(lines[1].split(",")[5].replace("step=", ""))
line = ""
for j in reversed(lines):
if " : values=" in j:
line = j
break
interface["values"] = []
interface["channels"] = []
i = 0
for value in line.replace(" : values=", "").split(","):
interface["values"].append(value)
channel_desc = self.__get_channel_name__(amixer_chandesc, interface["name"], i)
if channel_desc is not None:
interface["channels"].append(channel_desc)
i += 1
if len(interface["channels"]) != len(interface["values"]):
interface.pop("channels", None)
interfaces.append(interface)
return interfaces
def __get_equalizer__(self):
self.equal = True
data = self.__get_controls__()
self.equal = False
return data
def __change_volume__(self, num_id, volumes_path):
volumes = []
for volume in volumes_path:
if volume != "" and is_digit(volume):
volumes.append(volume)
command = self.__get_amixer_command__() + ["cset", "numid=%s" % num_id, "--", ",".join(volumes)]
call(command)
@staticmethod
def __decode_string(string):
return string.decode("utf-8")
def is_digit(n):
try:
int(n)
return True
except ValueError:
return False
app = Handler(__name__, static_folder='htdocs', static_url_path='')
@app.route('/')
def index():
"""Sends HTML file (GET /)"""
return app.send_static_file('index.html')
@app.route('/hostname/')
def get_hostname():
"""Sends server's hostname [plain text:String]"""
data = json.dumps(socket.gethostname())
resp = Response(response=data, status=200, mimetype="application/json")
return resp
@app.route('/cards/')
def get_cards():
"""Sends list of sound cards [JSON object - <number:Number>:<name:String>]"""
data = json.dumps(app.__get_cards__())
resp = Response(response=data, status=200, mimetype="application/json")
return resp
@app.route('/card/')
def get_card():
"""Sends number of selected sound card [JSON - <Number|null>]"""
data = json.dumps(app.card)
resp = Response(response=data, status=200, mimetype="application/json")
return resp
@app.route('/controls/')
def get_controls():
"""Sends list of controls of selected sound card [JSON - list of objects: {
--- common keys ---
access: <String>
id: <Number>
iface: <String>
name: <String>
type: <ENUMERATED|BOOLEAN|INTEGER:String>
--- for type ENUMERATED ---
items: <Object {<number:Number>:<name:String>}>
values: [<Number> - selected item]
--- for type BOOLEAN ---
values: [true|false]
--- for type INTEGER ---
channels: <Array of String> - channel names
min: <Number>
max: <Number>
step: <Number>
values: <Array of Number> - channel values (order corresponds with order in `channels` key)
}]"""
data = json.dumps(app.__get_controls__())
resp = Response(response=data, status=200, mimetype="application/json")
return resp
@app.route('/equalizer/')
def get_equalizer():
"""Sends list of equalizer controls [same as /controls/ but contains only controls of INTEGER type]"""
data = json.dumps(app.__get_equalizer__())
resp = Response(response=data, status=200, mimetype="application/json")
return resp
@app.route('/control/<int:control_id>/<int:status>/', methods=['PUT'])
def put_control(control_id, status):
"""Turns BOOLEAN control on or off"""
if control_id <= 0:
return ''
if status != 0 and status != 1:
return ''
call(app.__get_amixer_command__() + ["cset", "numid=%s" % control_id, "--", 'on' if status == 1 else 'off'])
if os.geteuid() == 0:
call(["alsactl", "store"])
return ''
@app.route('/source/<int:control_id>/<int:item>/', methods=['PUT'])
def put_source(control_id, item):
"""Changes active ENUMERATED item"""
if control_id <= 0:
return ''
call(app.__get_amixer_command__() + ["cset", "numid=%s" % control_id, "--", str(item)])
if os.geteuid() == 0:
call(["alsactl", "store"])
return ''
@app.route('/volume/<int:control_id>/<path:volume_path>', methods=['PUT'])
def put_volume(control_id, volume_path):
"""Changes INTEGER channel volumes"""
app.__change_volume__(control_id, volume_path.split('/'))
if os.geteuid() == 0:
call(["alsactl", "store"])
return ''
@app.route('/equalizer/<int:control_id>/<path:level_path>', methods=['PUT'])
def put_equalizer(control_id, level_path):
"""Changes equalizer channel values"""
app.equal = True
card = app.card
app.card = None
app.__change_volume__(control_id, level_path.split('/'))
app.equal = False
app.card = card
if os.geteuid() == 0:
call(["alsactl", "store"])
return ''
@app.route('/card/<int:card_id>/', methods=['PUT'])
def put_card(card_id):
"""Changes selected sound card"""
app.card = card_id
return ''
@app.after_request
def set_server_header(response):
response.headers["Server"] = "ALSA Mixer webserver"
return response
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--host", type=str)
parser.add_argument("-p", "--port", type=int)
parser.add_argument("-d", "--debug", action="store_true")
args = parser.parse_args()
if os.path.isfile(CONFIG_FILE):
config = ConfigParser.RawConfigParser()
config.read(CONFIG_FILE)
if args.host is None:
args.host = config.get('amixer-webui', 'host')
if args.port is None:
port = config.get('amixer-webui', 'port')
if is_digit(port):
args.port = int(port)
if args.host == "":
args.host = DEFAULT_HOST
if args.port is None:
args.port = DEFAULT_PORT
try:
app.run(**vars(args))
except socket.error as e:
if e.errno == errno.EPIPE:
main()
else:
raise e
if __name__ == "__main__":
main()
sys.exit(0)
# end of alsamixer_webui.py