From 04dffa36f6040b661bf1af8f98e2ce1978a7df33 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Mon, 28 Oct 2024 23:32:26 +1000 Subject: Let the user pick the best sound device. --- aataaj.lua | 77 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/aataaj.lua b/aataaj.lua index e044ed1..9acea95 100755 --- a/aataaj.lua +++ b/aataaj.lua @@ -141,45 +141,44 @@ if _.runnable('espeak-ng') then speaker = 'espeak-ng' end local Cards = {} +local cnt = 0 print('Scanning for audio devices.') local cards = __'ls -d1 /proc/asound/card[0-9]*':noErr():Do() for i,l in ipairs(cards.lines) do local f, e = io.open(l .. '/id', 'r') - if nil == f then print('Could not open ' .. l .. '/id') else - Cards[l] = {path = l, name = f:read('*a'):sub(1, -2), devs = {}, captureDevs = {}, playbackDevs = {}, card=i} - if 'Loopback' ~= Cards[l]['name'] then - Cards[l]['capture'] = __('ls -d1 ' .. l .. '/pcm[0-9]*c*'):noErr():Do() - for j,c in ipairs(Cards[l]['capture'].lines) do - local n = c:match('.*pcm(%d+).*') - Cards[l]['captureDevs'][j] = n - Cards[l]['devs'][n] = n - print('\tFound capture device: ' .. Cards[l]['name'] .. '\tDEVICE: ' .. Cards[l]['captureDevs'][j] .. ' ' .. n) + if nil == f then print('Could not open ' .. l .. '/id - ' .. e) else + local n = f:read('*a'):sub(1, -2) + Cards[n] = {path = l, name = n, devs = {}, captureDevs = {}, playbackDevs = {}, card=i} + cnt = cnt + 1 + if 'Loopback' ~= Cards[n]['name'] then + Cards[n]['capture'] = __('ls -d1 ' .. l .. '/pcm[0-9]*c*'):noErr():Do() + for j,c in ipairs(Cards[n]['capture'].lines) do + local m = c:match('.*pcm(%d+).*') + Cards[n]['captureDevs'][j] = m + Cards[n]['devs'][m] = m + print('\tFound capture device: ' .. Cards[n]['name'] .. '\tDEVICE: ' .. Cards[n]['captureDevs'][j] .. ' ' .. m) io.flush() end - Cards[l]['playback'] = __('ls -d1 ' .. l .. '/pcm[0-9]*p*'):noErr():Do() - for j,p in ipairs(Cards[l]['playback'].lines) do - local n = p:match('.*pcm(%d+).*') - Cards[l]['playbackDevs'][j] = n - Cards[l]['devs'][n] = n - print('\tFound playback device ' .. Cards[l].card - 1 .. ' : ' .. Cards[l]['name'] .. '\tDEVICE: ' .. Cards[l]['playbackDevs'][j] .. ' ' .. n) + Cards[n]['playback'] = __('ls -d1 ' .. l .. '/pcm[0-9]*p*'):noErr():Do() + for j,p in ipairs(Cards[n]['playback'].lines) do + local m = p:match('.*pcm(%d+).*') + Cards[n]['playbackDevs'][j] = m + Cards[n]['devs'][m] = m + print('\tFound playback device ' .. Cards[n].card - 1 .. ' : ' .. Cards[n]['name'] .. '\tDEVICE: ' .. Cards[n]['playbackDevs'][j] .. ' ' .. m) io.flush() if 'JACK' ~= arg[1] then - print('\t\tALSA_CARD=' .. Cards[l].card - 1 .. ' ' .. speaker .. ' "Found playback device ' .. Cards[l].card - 1 .. ' : ' .. Cards[l]['name'] .. ' DEVICE: ' .. Cards[l]['playbackDevs'][j] .. ' ' .. n .. '"') + print('\t\tALSA_CARD=' .. Cards[n].card - 1 .. ' ' .. speaker .. ' "Found playback device ' .. Cards[n].card - 1 .. ' : ' .. Cards[n]['name'] .. ' DEVICE: ' .. Cards[n]['playbackDevs'][j] .. ' ' .. m .. '"') io.flush() end end end end end +table.sort(Cards, function(a, b) return a.card < b.card end) print('') io.flush() --- #Cards kept telling me 0, no number indexes. -local cnt = 0 -for i,C in pairs(Cards) do - cnt = cnt + 1 -end print('Your ' .. cnt .. ' audio devices are ') for k,C in pairs(Cards) do @@ -193,13 +192,28 @@ io.flush() for k,C in pairs(Cards) do print('' .. C.card - 1 .. ' : ' .. C.name) __('echo "' .. C.card - 1 .. ' : ' .. C.name .. '"') - __('ALSA_CARD=' .. C.card - 1 .. ' ' .. speaker .. ' "Device ' .. C.card - 1 .. ' ' .. C.name .. '"'):log():noErr():noOut():Do() + __('ALSA_CARD=' .. C.card - 1 .. ' ' .. speaker .. ' "Device number ' .. C.card - 1 .. ' ' .. C.name .. '"'):log():noErr():noOut():Do() io.flush() __'sleep 1':Do() end print('') io.flush() +for k,C in pairs(Cards) do + print('' .. C.card - 1 .. ' : ' .. C.name) + __('ALSA_CARD=' .. C.card - 1 .. ' ' .. speaker .. ' "Please type the device number you heard best."'):log():noErr():noOut():fork() +end +io.write('Please type the device number you heard best - ') +local choice = tonumber(io.read()) -- Lua has no way of just checking IF there is ANY input, so can't do "check if there was a keypress, continue if not". +local ourCard = '' + +for k,C in pairs(Cards) do + if (C.card - 1) == choice then + print('You choose ' .. choice .. ' ' .. C.name) + ourCard = C.name + end +end + if 'start' == arg[1] then __('mkdir -p ' .. options.asoundrcPath.value):Do() @@ -218,10 +232,8 @@ if 'start' == arg[1] then ]]) a:close() end - local a, e = io.open(options.asoundrcPath.value .. '/' .. options.asoundrc.value, 'w') - if nil == a then print('Could not open ' .. options.asoundrcPath.value .. '/' .. options.asoundrc.value) else - for i,C in pairs(Cards) do - for j,c in pairs(C['devs']) do + + local outCard = function(a, C, j) a:write('pcm.' .. C['name'] .. j .. ' {\n') a:write(' type hw\n') a:write(' card ' .. C['name'] .. '\n') @@ -232,6 +244,19 @@ if 'start' == arg[1] then a:write(' card ' .. C['name'] .. '\n') a:write(' device ' .. C['devs'][j] .. '\n') a:write('}\n\n') + end + + local a, e = io.open(options.asoundrcPath.value .. '/' .. options.asoundrc.value, 'w') + if nil == a then print('Could not open ' .. options.asoundrcPath.value .. '/' .. options.asoundrc.value .. ' - ' .. e) else + print('Writing suggested ALSA configuration file to ' .. options.asoundrcPath.value .. '/' .. options.asoundrc.value) + if '' ~= ourCard then + outCard(a, Cards[ourCard], '0') -- How the fuck is that a string? + end + for i,C in pairs(Cards) do + for j,c in pairs(C['devs']) do + if C.name ~= ourCard then + outCard(a, C, j) + end end end a:write([[ -- cgit v1.1