aboutsummaryrefslogtreecommitdiffstats
path: root/jackscanall
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xjackscanall326
1 files changed, 326 insertions, 0 deletions
diff --git a/jackscanall b/jackscanall
new file mode 100755
index 0000000..24d87df
--- /dev/null
+++ b/jackscanall
@@ -0,0 +1,326 @@
1#!/usr/bin/env luajit
2
3--[[
4This is part of the JackOnAllDevices project, JOAD for short.
5
6The purpose is to scan for all ALSA / asound audio devices, and hook them
7all up to JACK. Then it starts up JACK, and hooks up any joysticks it
8finds as MIDI controllers.
9
10
11Alas ~/.asoundrc doesn't understand ~ or $HOME, or even "try the current
12directory" it seems. So you have to hard cade the path. Make sure your
13~/.asoundrc or /etc/asoundrc includes something like this -
14
15</var/lib/JOAD/asoundrc>
16
17
18Run this once as root to create that file, and each time you need to
19change your devices.
20
21
22TODO - Leave it running, and hotplug ALSA / asound audio devices.
23 a2jmidid takes care of hotplugging MIDI devices.
24 Though I think I still need to deal with hotplugged joysticks.
25
26NOTE - Seems both ALSA and JACK are per user.
27
28]]
29
30
31-- CHANGE these to suit.
32local asoundrcPath = '/var/lib/JOAD'
33local asoundrc = 'asoundrc'
34
35
36
37-- This APT stuff was copied from apt-panopticon.
38local APT = {}
39
40APT.readCmd = function(cmd)
41 local result = {}
42 local output = io.popen(cmd)
43 if nil ~= output then
44 for l in output:lines() do
45 table.insert(result, l)
46 end
47 end
48 return result
49end
50
51
52APT.exe = function(c)
53 local exe = {status = 0, result = '', lines = {}, log = true, cmd = c .. ' ', command = c}
54
55 function exe:log()
56 self.log = true
57 return self
58 end
59 function exe:Nice(c)
60 if nil == c then
61 self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd
62 else
63 self.cmd = self.cmd .. 'ionice -c3 nice -n 19 ' .. c .. ' '
64 end
65 return self
66 end
67 function exe:timeout(c)
68 -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well.
69 -- --kill-after means "send KILL after TERM fails.
70 if nil == c then
71 self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd
72 else
73 self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd
74 end
75 return self
76 end
77 function exe:also(c)
78 if nil == c then c = '' else c = ' ' .. c end
79 self.cmd = self.cmd .. ';' .. c .. ' '
80 return self
81 end
82 function exe:And(c)
83 if nil == c then c = '' else c = ' ' .. c end
84 self.cmd = self.cmd .. '&&' .. c .. ' '
85 return self
86 end
87 function exe:Or(c)
88 if nil == c then c = '' end
89 self.cmd = self.cmd .. '|| ' .. c .. ' '
90 return self
91 end
92 function exe:noErr()
93 self.cmd = self.cmd .. '2>/dev/null '
94 return self
95 end
96 function exe:wait(w)
97 self.cmd = self.cmd .. '&& touch ' .. w .. ' '
98 return self
99 end
100 function exe:Do()
101 --[[ "The condition expression of a control structure can return any
102 value. Both false and nil are considered false. All values different
103 from nil and false are considered true (in particular, the number 0
104 and the empty string are also true)."
105 says the docs, I beg to differ.]]
106 if true == self.log then D(" executing - &nbsp; <code>" .. self.cmd .. "</code>") end
107 --[[ Damn os.execute()
108 Lua 5.1 says it returns "a status code, which is system-dependent"
109 Lua 5.2 says it returns true/nil, "exit"/"signal", the status code.
110 I'm getting 7168 or 0. No idea what the fuck that is.
111 local ok, rslt, status = os.execute(s)
112 ]]
113 local f = APT.readCmd(self.cmd, 'r')
114 -- The last line will be the command's returned status, collect everything else in result.
115 self.status = '' -- Otherwise the result starts with 0.
116 self.result = '\n'
117 self.lines = f
118 for i,l in ipairs(f) do
119 self.result = self.result .. l .. "\n"
120 end
121 f = APT.readCmd('echo "$?"', 'r')
122 for i,l in ipairs(f) do
123 self.status = tonumber(l)
124 if (137 == self.status) or (124 == self.status) then
125 print("timeout killed " .. self.status .. ' ' .. self.command)
126 E("timeout killed " .. self.status .. ' ' .. self.command)
127 elseif (0 ~= self.status) then
128 print("status |" .. self.status .. '| ' .. self.command)
129 E("status |" .. self.status .. '| ' .. self.command)
130 end
131 end
132 return self
133 end
134 function exe:fork(host)
135 if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end
136 self.cmd = '{ ' .. self.cmd .. '; } &'
137 if true == self.log then D(" forking - &nbsp; <code>" .. self.cmd .. "</code>") end
138 os.execute(self.cmd)
139 return self
140 end
141 return exe
142end
143
144
145
146local Cards = {}
147
148print('Scanning for audio devices.')
149local cards = APT.exe('ls -d1 /proc/asound/card[0-9]*'):noErr():Do()
150for i,l in ipairs(cards.lines) do
151 local f, e = io.open(l .. '/id', "r")
152 if nil == f then print("Could not open " .. l .. '/id') else
153 Cards[l] = {path = l, name = f:read("*a"):sub(1, -2), devs = {}, captureDevs = {}, playbackDevs = {}}
154 if "Loopback" ~= Cards[l]['name'] then
155 Cards[l]['capture'] = APT.exe('ls -d1 ' .. l .. '/pcm[0-9]*c*'):noErr():Do()
156 for j,c in ipairs(Cards[l]['capture'].lines) do
157 local n = c:match(".*pcm(%d+).*")
158 Cards[l]['captureDevs'][j] = n
159 Cards[l]['devs'][n] = n
160 print("\tFound capture device: " .. Cards[l]['name'] .. "\tDEVICE: " .. Cards[l]['captureDevs'][j] .. ' ' .. n)
161 end
162 Cards[l]['playback'] = APT.exe('ls -d1 ' .. l .. '/pcm[0-9]*p*'):noErr():Do()
163 for j,p in ipairs(Cards[l]['playback'].lines) do
164 local n = p:match(".*pcm(%d+).*")
165 Cards[l]['playbackDevs'][j] = n
166 Cards[l]['devs'][n] = n
167 print("\tFound playback device: " .. Cards[l]['name'] .. "\tDEVICE: " .. Cards[l]['playbackDevs'][j] .. ' ' .. n)
168 end
169 end
170 end
171end
172
173APT.exe('mkdir -p ' .. asoundrcPath):Do()
174local a, e = io.open(asoundrcPath .. '/' .. asoundrc, "w")
175if nil == a then print("Could not open " .. asoundrcPath .. '/' .. asoundrc) else
176 for i,C in pairs(Cards) do
177 for j,c in pairs(C['devs']) do
178 a:write("pcm." .. C['name'] .. j .. " {\n")
179 a:write(" type hw\n")
180 a:write(" card " .. C['name'] .. "\n")
181 a:write(" device " .. C['devs'][j] .. "\n")
182 a:write("}\n")
183 a:write("ctl." .. C['name'] .. j .. " {\n")
184 a:write(" type hw\n")
185 a:write(" card " .. C['name'] .. "\n")
186 a:write(" device " .. C['devs'][j] .. "\n")
187 a:write("}\n\n")
188 end
189 end
190 a:write([[
191#################################################################################################################################
192
193# The complex way - https://alsa.opensrc.org/Jack_and_Loopback_device_as_Alsa-to-Jack_bridge
194
195# More custom version, but it didn't work for me.
196# hardware 0,0 : used for ALSA playback
197#pcm.loophw00 {
198# type hw
199# card Loopback
200# device 0
201# subdevice 0
202# format S32_LE
203# rate 48000
204#}
205
206# playback PCM device: using loopback subdevice 0,0
207# Don't use a buffer size that is too small. Some apps
208# won't like it and it will sound crappy
209
210#pcm.amix {
211# type dmix
212# ipc_key 219345
213# slave {
214# pcm loophw00
215## period_size 4096
216## periods 2
217# }
218#}
219
220# software volume
221#pcm.asoftvol {
222# type softvol
223# slave.pcm "amix"
224
225# control { name PCM }
226
227# min_dB -51.0
228# max_dB 0.0
229#}
230
231
232# for jack alsa_in: looped-back signal at other ends
233#pcm.cloop {
234# type hw
235# card Loopback
236# device 1
237# subdevice 0
238# format S32_LE
239# rate 48000
240#}
241
242# hardware 0,1 : used for ALSA capture
243#pcm.loophw01 {
244# type hw
245# card Loopback
246# device 0
247# subdevice 1
248# format S32_LE
249# rate 48000
250#}
251
252# for jack alsa_out: looped-back signal at other end
253#pcm.ploop {
254# type hw
255# card Loopback
256# device 1
257# subdevice 1
258# format S32_LE
259# rate 48000
260#}
261
262# duplex device combining our PCM devices defined above
263#pcm.aduplex {
264# type asym
265# playback.pcm "asoftvol"
266# capture.pcm "loophw01"
267#}
268
269# default device
270#pcm.!default {
271# type plug
272# slave.pcm aduplex
273
274# hint {
275# show on
276# description "Duplex Loopback"
277# }
278#}
279
280
281
282# Generic method seems to work better.
283# playback PCM device: using loopback subdevice 0,0
284pcm.amix {
285 type dmix
286 ipc_key 219345
287 slave.pcm "hw:Loopback,0,0"
288}
289
290# capture PCM device: using loopback subdevice 0,1
291pcm.asnoop {
292 type dsnoop
293 ipc_key 219346
294 slave.pcm "hw:Loopback,0,1"
295}
296
297# duplex device combining our PCM devices defined above
298pcm.aduplex {
299 type asym
300 playback.pcm "amix"
301 capture.pcm "asnoop"
302}
303
304# ------------------------------------------------------
305# for jack alsa_in and alsa_out: looped-back signal at other ends
306pcm.ploop {
307 type plug
308 slave.pcm "hw:Loopback,1,1"
309}
310
311pcm.cloop {
312 type dsnoop
313 ipc_key 219348
314 slave.pcm "hw:Loopback,1,0"
315}
316
317# ------------------------------------------------------
318# default device
319
320pcm.!default {
321 type plug
322 slave.pcm "aduplex"
323}
324 ]])
325a:close()
326end