Module: ALSA::Aconnect::Parser

Defined in:
lib/alsa/aconnect/parser.rb

Class Method Summary collapse

Class Method Details

.parse_client(output) ⇒ Object



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
# File 'lib/alsa/aconnect/parser.rb', line 15

def parse_client(output)
  client_line, *port_lines = output.split("\n")

  ports = []
  port_lines.reject(&:empty?).each do |line|
    line = line.strip
    if line =~ /\A\d+/
      ports.push(line)
    else
      ports[-1] = "#{ports[-1]} #{line}"
    end
  end

  matched = client_line.match(/\Aclient (\d+): '(.+)' \[(.+)\]\z/)
  id = matched[1].to_i
  name = matched[2].strip
  tags = matched[3].split(',').map { |l| l.split('=') }.to_h
  {
    id: id,
    name: name,
    type: tags['type'],
    card: tags['card'],
    pid: tags['pid'],
    ports: ports
  }
end

.parse_clients(output) ⇒ Object



8
9
10
11
12
13
# File 'lib/alsa/aconnect/parser.rb', line 8

def parse_clients(output)
  output
    .split('client ')
    .reject(&:empty?)
    .map { |l| "client #{l.strip}" }
end

.parse_port(output) ⇒ Object



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
# File 'lib/alsa/aconnect/parser.rb', line 42

def parse_port(output)
  matched = output.match(/\A(\d) '(.+)'/)
  id = matched[1].to_i
  name = matched[2].strip
  connected_to_type = nil
  connected_to_client_id = nil
  connected_to_port_id = nil

  matched_from = output.match(/Connected From: (\d):(\d)\z/)
  if matched_from
    connected_to_type = :input
    connected_to_client_id = matched_from[1]
    connected_to_port_id = matched_from[2]
  end

  matched_from = output.match(/Connecting To: (\d):(\d)\z/)
  if matched_from
    connected_to_type = :output
    connected_to_client_id = matched_from[1]
    connected_to_port_id = matched_from[2]
  end

  {
    id: id,
    name: name,
    connected_to_type: connected_to_type,
    connected_to_client_id: connected_to_client_id,
    connected_to_port_id: connected_to_port_id
  }
end