-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_devices.lua
210 lines (152 loc) · 3.68 KB
/
sound_devices.lua
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
function SoundDevices()
local mod
mod={}
mod.devices={}
mod.add=function(self, id, name)
local dev, toks
dev = {}
dev.id = id
toks=strutil.TOKENIZER(id, ":|,|.", "m")
dev.subtype=toks:next()
dev.devnum=toks:next()
dev.subnum=toks:next()
if strutil.strlen(name) == 0 then dev.name = id
else dev.name = name
end
table.insert(self.devices, dev)
return dev
end
mod.read_pcm=function(self, name, toks)
local dev, str, bracecount
dev = self:add(name)
while str ~= "{" do str=toks:next() end
bracecount=1
str=toks:next()
while str ~= nil
do
if str == "type" then self:readvalue(dev, "subtype", toks)
elseif str == "device" then self:readvalue(dev, "device", toks)
elseif str == "{" then bracecount=bracecount + 1
elseif str == "}" then bracecount=bracecount - 1
end
if bracecount < 1 then break end
str=toks:next()
end
end
mod.setvalue=function(self, dev, name, value)
if name == "subtype"
then
dev.subtype = value
if strutil.strlen(dev.device) > 0 then dev.name = dev.subtype .. "@" .. dev.device end
end
if name == "device"
then
dev.device = value
if strutil.strlen(dev.subtype) > 0 then dev.name = dev.subtype .. "@" .. dev.device end
end
end
mod.readvalue=function(self, dev, name, toks)
local str
str=toks:next()
while str==" " do str=toks:next()
self:setvalue(dev, name, str)
end
end
mod.load_asoundrc=function(self, path)
local S, str, toks, dev
S = stream.STREAM(path, "r")
if S ~= nil
then
toks = strutil.TOKENIZER(S:readdoc(), "\\S|{|}", "mQs")
S:close()
str = toks:next()
while str ~= nil
do
if string.sub(str, 1, 4) == "pcm." then self:read_pcm(string.sub(str, 5), toks) end
str = toks:next()
end
end
end
mod.parse_dev=function(self, id)
local devnum, subnum
local toks, tok
toks = strutil.TOKENIZER(id, "-")
devnum = tonumber(toks:next())
tok = toks:next()
if tok ~= nil then subnum = tonumber(tok) end
id = "hw:" .. tostring(devnum) .. "," .. tostring(subnum)
dev = self:add(id)
end
mod.load_pcms=function(self)
local S, id, tok
local playback_found = false
S = stream.STREAM("/proc/asound/pcm", "r")
if S ~= nil
then
str = S:readln()
while str ~= nil
do
dev = nil
toks = strutil.TOKENIZER(str, ":")
id = strutil.trim(toks:next())
tok = toks:next()
while tok ~= nil
do
tok = strutil.trim(tok)
if string.sub(tok, 1, 9) == "playback "
then
self:parse_dev(id)
playback_found = true
break
end
tok = toks:next()
end
str = S:readln()
end
S:close()
end
return playback_found
end
mod.update_devs=function(self, dev_id, name)
local id, dev
for id, dev in pairs(self.devices)
do
toks = strutil.TOKENIZER(dev.id, ",")
if toks:next() == dev_id then dev.name = strutil.trim(name) end
end
end
mod.load=function(self)
local S, devnum, tok
S = stream.STREAM("/proc/asound/cards", "r")
if S ~= nil
then
str = S:readln()
while str ~= nil
do
toks = strutil.TOKENIZER(str, "[|]:", "m")
devnum = tonumber(strutil.trim(toks:next()))
tok = toks:next() --throw away next token
mod:update_devs("hw:" .. devnum, toks:remaining())
str = S:readln() --throw away next line
str = S:readln()
end
S:close()
end
end
mod.find=function(self, devid)
local i, dev
for i,dev in ipairs(self.devices)
do
if dev.id==devid then return dev end
end
return nil
end
mod.init=function(self)
self:load_pcms(self.devices)
self:load()
self:load_asoundrc("/etc/asound.conf")
-- self:load_asoundrc(process.getenv("HOME") .. ".asoundrc")
end
mod:init()
return mod
end