Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ You can dump raw MIDI messages received and Pipewire commands sent by enabling
the debug mode using the `-d` parameter:

```sh
$ java -jar midi-mixer-1.0-SNAPSHOT.jar -d # Run the program in debug mode
$ java -jar midi-mixer-<VERSION>.jar -d # Run the program in debug mode
```

### Changing cooldown time for Pipewire ID refreshes

If a Pipewire node is not found (because for example it was not active while
pw-mixer was started), the program will try to refresh the Pipewire ID every
10000ms.

That cooldown time can be changed through the `-c` parameter:'

```sh
$ java -jar midi-mixer-<VERSION>.jar -c 1000 # Change the cooldown to 1000ms
```

### Future and additions
Expand Down
55 changes: 47 additions & 8 deletions src/main/java/io/github/katacc/AudioController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class AudioController {
private final AtomicBoolean volumeWorkerRunning = new AtomicBoolean(false);
// Protect ID/Config Updates
private final Object configLock = new Object();
private long lastConfigRefresh = 0;
private long configRefreshCooldownMs = 10000; // default 10 seconds cooldown

private AudioController() {

Expand Down Expand Up @@ -120,6 +122,14 @@ public void setDebug(boolean debug) {
this.debug = debug;
}

public long getConfigRefreshCooldownMs() {
return configRefreshCooldownMs;
}

public void setConfigRefreshCooldownMs(long configRefreshCooldownMs) {
this.configRefreshCooldownMs = configRefreshCooldownMs;
}

public void changeVolume(MidiMessage msg) {

byte[] message = msg.getMessage();
Expand Down Expand Up @@ -231,13 +241,17 @@ private void applyVolumeToControl(int control, float scaled_volume) {
}

if (ids.isEmpty()) {
if (debug) {
System.out.println("id" + control + " is empty... refreshing config...");
}
getConfigUnsafe();
ids = getIdsForVolumeControl(control);
if (ids == null) {
return;
if (System.currentTimeMillis() - lastConfigRefresh > configRefreshCooldownMs) {
if (debug) {
System.out.println("id" + control + " is empty... refreshing config...");
}
getConfigUnsafe();
ids = getIdsForVolumeControl(control);
if (ids == null) {
return;
}
} else if (debug) {
System.out.println("id" + control + " is empty, but cooldown in effect. Skipping refresh.");
}
}

Expand Down Expand Up @@ -312,7 +326,10 @@ private void runPlayerctl(String action) {
* getId to get id of a application from pw-dump
* */
public List<Integer> getId(String name) {

long startTime = System.currentTimeMillis();
if (debug) {
System.out.println("Looking up ID for: " + name);
}

String targetName = name;
List<Integer> appId = new ArrayList<>();
Expand Down Expand Up @@ -349,6 +366,11 @@ public List<Integer> getId(String name) {
System.out.println("Error: " + e.getMessage());
}

if (debug) {
long duration = System.currentTimeMillis() - startTime;
System.out.println("Found IDs for " + name + ": " + appId + " (took " + duration + "ms)");
}

return appId;
}

Expand All @@ -365,6 +387,12 @@ public void getConfig() {
* Internal implementation; caller must hold configLock.
*/
private void getConfigUnsafe() {
lastConfigRefresh = System.currentTimeMillis();
if (debug) {
System.out.println("Refreshing config...");
}
long startTime = System.currentTimeMillis();

// Path to config file
String userHome = System.getProperty("user.home");
String configPath = userHome + "/.config/midi-mixer/config.ini";
Expand Down Expand Up @@ -436,6 +464,11 @@ private void getConfigUnsafe() {
System.out.println("Error reading configs from file: " + IOE.getMessage());
}

if (debug) {
long duration = System.currentTimeMillis() - startTime;
System.out.println("Config refresh finished (took " + duration + "ms)");
}

}

/**
Expand All @@ -458,6 +491,9 @@ private void constructConfigUnsafe(int fader, Vector<String> applications) {
this.id0App = applications;

for (String app : id0App) {
if (debug) {
System.out.println("Processing application: " + app + " for fader " + fader);
}
List<Integer> temp_id = AudioController.getInstance().getId(app);
if (!temp_id.isEmpty()) {
this.id0.addAll(temp_id);
Expand All @@ -469,6 +505,9 @@ private void constructConfigUnsafe(int fader, Vector<String> applications) {
this.id1App = applications;

for (String app : id1App) {
if (debug) {
System.out.println("Processing application: " + app + " for fader " + fader);
}
List<Integer> temp_id = AudioController.getInstance().getId(app);
if (!temp_id.isEmpty()) {
this.id1.addAll(temp_id);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/github/katacc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ public class Main {
public static void main(String[] args) throws InterruptedException {
AudioController controller = AudioController.getInstance();

for (String arg : args) {
if (arg.equals("-d") || arg.equals("--debug")) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-d") || args[i].equals("--debug")) {
controller.setDebug(true);
System.out.println("Debug mode enabled");
} else if ((args[i].equals("-c") || args[i].equals("--cooldown")) && i + 1 < args.length) {
try {
long cooldown = Long.parseLong(args[i + 1]);
controller.setConfigRefreshCooldownMs(cooldown);
System.out.println("Cooldown set to " + cooldown + "ms");
i++; // skip next arg
} catch (NumberFormatException e) {
System.err.println("Invalid cooldown value: " + args[i + 1]);
}
}
}

Expand Down