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).

!7

srecord AudioSink(S description, S volume, S baseVolume, bool mute, int index) {}

cmodule ListAudioSinks > DynObjectTable<AudioSink> {
  //transient SimpleLiveValue<Float> lvVolume = floatLiveValue(); // for selected sink
  transient JTextField tfVolume;
  
  start {
    set fieldsInOrder;
    dontPersist();
    doEveryAndNow(10.0, r updateList);
  }
  
 
  void updateList enter {
    LS lines = lines(backtick("pactl list sinks"));
    new Matches m;
    new L<AudioSink> list;
    for i over lines:
      if (swic(lines.get(i), "Sink #")) {
        int idx = parseFirstInt(lines.get(i));
        AudioSink as = nu(AudioSink, index := idx, description := lines.get(i));
        for (int j = i+1; j < l(lines); j++) {
          S line = lines.get(j);
          if (!startsWithSpace(line)) break;
          line = trim(line);
          if (swic_trim(line, "Description:", m))
            as.description = m.rest();
          else if (swic_trim(line, "Base Volume:", m))
            as.baseVolume = m.rest();
          else if (swic_trim(line, "Volume:", m))
            as.volume = iround(parseFirstInt(m.rest())*100.0/65536) + "%";
          else if (swic_trim(line, "Mute:", m))
            as.mute = isYes(m.rest());
        }
        list.add(as);
      }
    setData(list);
  }
  
  visualize {
    JComponent c = super.visualize();
    onTableSelectionChanged(table, r {
      setText(tfVolume, selected() == null ? "" : firstIntAsString(selected().volume))
    });
    tfVolume = jTextField();
    onEnter(tfVolume, r {
      if (selected() == null) ret;
      backtickToConsole("pactl set-sink-volume " + selected().index + " " + iround(parseFirstInt(gtt(tfVolume))/100.0*65536));
      updateList();
    });
    
    ret centerAndEastWithMargins(c, vstackWithSpacing(
      tableDependentButton_extraCondition(table, "Mute",
        rThread {
 muteSink(selected())
 }, func -> bool { !selected().mute }),
      tableDependentButton_extraCondition(table, "Unmute",
        rThread {
 unmuteSink(selected())
 }, func -> bool { selected().mute }),
      jlabel("Volume:"),
      tfVolume
,
      jbutton("Export as text", r { showText("Audio sink settings", exportAsText()) })
    ));
  }
  
  // API
  
  S getSinkName(int index) {
    ret getString(objectWhere(data, +index), 'description);
  }
  
  AudioSink findSinkByName(S name) {
    ret objectWhereIC(data, description := name);
  }
  
  void muteSink(AudioSink sink) {
    if (sink == null) ret;
    backtickToConsole("pactl set-sink-mute " + sink.index + " 1");
    updateList();
  }

  void unmuteSink(AudioSink sink) {
    if (sink == null) ret;
    backtickToConsole("pactl set-sink-mute " + sink.index + " 0");
    updateList();
  }
  
  bool isSinkMuted(AudioSink sink) {
    ret sink != null && sink.mute;
  }

  S exportAsText() {
    ret linesMap(data(), func(AudioSink sink) -> S {
      (sink.mute ? "Muted audio sink" : "Audio sink")
        + " " + quote(sink.description) + " has volume " + parseFirstInt(sink.volume) + "."
    });
  }
}

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: 303 / 12037
Version history: 3 change(s)
Referenced in: [show references]