Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

102
LINES

< > BotCompany Repo | #1027072 // System Audio Sinks [Linux, Pulse Audio, dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (9901L/55K).

1  
!7
2  
3  
srecord AudioSink(S description, S volume, S baseVolume, bool mute, int index) {}
4  
5  
cmodule ListAudioSinks > DynObjectTable<AudioSink> {
6  
  //transient SimpleLiveValue<Float> lvVolume = floatLiveValue(); // for selected sink
7  
  transient JTextField tfVolume;
8  
  
9  
  start {
10  
    set fieldsInOrder;
11  
    dontPersist();
12  
    doEveryAndNow(10.0, r updateList);
13  
  }
14  
  
15  
 
16  
  void updateList enter {
17  
    LS lines = lines(backtick("pactl list sinks"));
18  
    new Matches m;
19  
    new L<AudioSink> list;
20  
    for i over lines:
21  
      if (swic(lines.get(i), "Sink #")) {
22  
        int idx = parseFirstInt(lines.get(i));
23  
        AudioSink as = nu(AudioSink, index := idx, description := lines.get(i));
24  
        for (int j = i+1; j < l(lines); j++) {
25  
          S line = lines.get(j);
26  
          if (!startsWithSpace(line)) break;
27  
          line = trim(line);
28  
          if (swic_trim(line, "Description:", m))
29  
            as.description = m.rest();
30  
          else if (swic_trim(line, "Base Volume:", m))
31  
            as.baseVolume = m.rest();
32  
          else if (swic_trim(line, "Volume:", m))
33  
            as.volume = iround(parseFirstInt(m.rest())*100.0/65536) + "%";
34  
          else if (swic_trim(line, "Mute:", m))
35  
            as.mute = isYes(m.rest());
36  
        }
37  
        list.add(as);
38  
      }
39  
    setData(list);
40  
  }
41  
  
42  
  visualize {
43  
    JComponent c = super.visualize();
44  
    onTableSelectionChanged(table, r {
45  
      setText(tfVolume, selected() == null ? "" : firstIntAsString(selected().volume))
46  
    });
47  
    tfVolume = jTextField();
48  
    onEnter(tfVolume, r {
49  
      if (selected() == null) ret;
50  
      backtickToConsole("pactl set-sink-volume " + selected().index + " " + iround(parseFirstInt(gtt(tfVolume))/100.0*65536));
51  
      updateList();
52  
    });
53  
    
54  
    ret centerAndEastWithMargins(c, vstackWithSpacing(
55  
      tableDependentButton_extraCondition(table, "Mute",
56  
        rThread {
57  
 muteSink(selected())
58  
 }, func -> bool { !selected().mute }),
59  
      tableDependentButton_extraCondition(table, "Unmute",
60  
        rThread {
61  
 unmuteSink(selected())
62  
 }, func -> bool { selected().mute }),
63  
      jlabel("Volume:"),
64  
      tfVolume
65  
,
66  
      jbutton("Export as text", r { showText("Audio sink settings", exportAsText()) })
67  
    ));
68  
  }
69  
  
70  
  // API
71  
  
72  
  S getSinkName(int index) {
73  
    ret getString(objectWhere(data, +index), 'description);
74  
  }
75  
  
76  
  AudioSink findSinkByName(S name) {
77  
    ret objectWhereIC(data, description := name);
78  
  }
79  
  
80  
  void muteSink(AudioSink sink) {
81  
    if (sink == null) ret;
82  
    backtickToConsole("pactl set-sink-mute " + sink.index + " 1");
83  
    updateList();
84  
  }
85  
86  
  void unmuteSink(AudioSink sink) {
87  
    if (sink == null) ret;
88  
    backtickToConsole("pactl set-sink-mute " + sink.index + " 0");
89  
    updateList();
90  
  }
91  
  
92  
  bool isSinkMuted(AudioSink sink) {
93  
    ret sink != null && sink.mute;
94  
  }
95  
96  
  S exportAsText() {
97  
    ret linesMap(data(), func(AudioSink sink) -> S {
98  
      (sink.mute ? "Muted audio sink" : "Audio sink")
99  
        + " " + quote(sink.description) + " has volume " + parseFirstInt(sink.volume) + "."
100  
    });
101  
  }
102  
}

Author comment

Began life as a copy of #1020375

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027072
Snippet name: System Audio Sinks [Linux, Pulse Audio, dev.]
Eternal ID of this version: #1027072/4
Text MD5: d7dae2ba163acaec66117530135c8dfe
Transpilation MD5: 2018f404d59b8a7efe63c36f9baccf03
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-17 13:27:23
Source code size: 3124 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 305 / 12041
Version history: 3 change(s)
Referenced in: [show references]