From 9f79c7eedd2573a70baea493c95a2c097675707d Mon Sep 17 00:00:00 2001 From: kevinstadler Date: Tue, 12 Sep 2023 22:55:17 +0200 Subject: [PATCH] Tweak Sound.list() output format and handle device name trimming --- src/processing/sound/Engine.java | 14 +++++++++----- src/processing/sound/Sound.java | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/processing/sound/Engine.java b/src/processing/sound/Engine.java index 002cae9..f98df1e 100644 --- a/src/processing/sound/Engine.java +++ b/src/processing/sound/Engine.java @@ -254,7 +254,7 @@ protected int selectOutputDevice(int deviceId) { } catch (LineUnavailableException e) { // if this fails then we need to get the name of the old output device // and re-select it on the new device - String targetDeviceName = this.synth.getAudioDeviceManager().getDeviceName(deviceId); + String targetDeviceName = this.getDeviceName(deviceId); // this might replace this.synth if (!this.usePortAudio(true)) { // hopeless @@ -279,9 +279,13 @@ protected int selectOutputDevice(int deviceId) { return this.outputDevice; } + protected String getDeviceName(int deviceId) { + return this.synth.getAudioDeviceManager().getDeviceName(deviceId).trim(); + } + protected int getDeviceIdByName(String deviceName) { for (int i = 0; i < this.synth.getAudioDeviceManager().getDeviceCount(); i++) { - if (deviceName.equalsIgnoreCase(this.synth.getAudioDeviceManager().getDeviceName(i))) { + if (deviceName.equalsIgnoreCase(this.getDeviceName(i))) { return i; } } @@ -294,7 +298,7 @@ protected int getDeviceIdByName(String deviceName, boolean fuzzy) { } catch (RuntimeException e) { if (fuzzy) { for (int i = 0; i < this.synth.getAudioDeviceManager().getDeviceCount(); i++) { - if (this.synth.getAudioDeviceManager().getDeviceName(i).startsWith(deviceName)) { + if (this.getDeviceName(i).startsWith(deviceName)) { return i; } } @@ -318,11 +322,11 @@ protected int selectOutputChannel(int channel) { } protected String getSelectedInputDeviceName() { - return this.synth.getAudioDeviceManager().getDeviceName(this.inputDevice); + return this.getDeviceName(this.inputDevice); } protected String getSelectedOutputDeviceName() { - return this.synth.getAudioDeviceManager().getDeviceName(this.outputDevice); + return this.getDeviceName(this.outputDevice); } protected void setVolume(double volume) { diff --git a/src/processing/sound/Sound.java b/src/processing/sound/Sound.java index 386d4cc..91de5c7 100644 --- a/src/processing/sound/Sound.java +++ b/src/processing/sound/Sound.java @@ -78,22 +78,24 @@ public static AudioDeviceManager getAudioDeviceManager() { * number of input/output channels. */ public static String[] list(boolean printAll) { - AudioDeviceManager audioManager = Engine.getAudioDeviceManager(); + Engine e = Engine.getEngine(); + AudioDeviceManager audioManager = e.getAudioDeviceManager(); int numDevices = audioManager.getDeviceCount(); int longestLength = "Output device name".length(); String[] deviceNames = new String[numDevices]; for (int i = 0; i < numDevices; i++) { - deviceNames[i] = audioManager.getDeviceName(i); + deviceNames[i] = e.getDeviceName(i); longestLength = Math.max(longestLength, deviceNames[i].length()); } if (printAll) { - String lineFormat = "%1s %1s %3s | %-" + longestLength + "s | %6s | %4s%n"; + String lineFormat = "%-3s %3s | %-" + longestLength + "s | %6s | %4s%n"; - System.out.format(lineFormat, "", "", "id", "Device name", "inputs", "outputs"); + System.out.format(lineFormat, "", "id", "Device name", "inputs", "outputs"); System.out.println(" ----+" + "-".repeat(longestLength+2) + "+--------".repeat(2)); for (int i = 0; i < numDevices; i++) { - System.out.format(lineFormat, Engine.getEngine().inputDevice == i ? "I" : " ", - Engine.getEngine().outputDevice == i ? "O" : " ", i, deviceNames[i], + System.out.format(lineFormat, + Engine.getEngine().inputDevice == i ? (Engine.getEngine().outputDevice == i ? "I,O" : "I") : + (Engine.getEngine().outputDevice == i ? "O" : ""), i, deviceNames[i], audioManager.getMaxInputChannels(i), audioManager.getMaxOutputChannels(i)); } @@ -101,7 +103,7 @@ public static String[] list(boolean printAll) { } else { String lineFormat = "%1s %3s | %-" + longestLength + "s | %4s%n"; - System.out.format(lineFormat, " ", "id", "Input device name", "inputs"); + System.out.format(lineFormat, " ", "id", "Input device name", "inputs"); System.out.println(" ----+" + "-".repeat(longestLength+2) + "+--------"); for (int i = 0; i < numDevices; i++) { if (audioManager.getMaxInputChannels(i) > 0) { @@ -109,7 +111,7 @@ public static String[] list(boolean printAll) { } } System.out.println(); - System.out.format(lineFormat, " ", "id", "Output device name", "outputs"); + System.out.format(lineFormat, " ", "id", "Output device name", "outputs"); System.out.println(" ----+" + "-".repeat(longestLength+2) + "+--------"); for (int i = 0; i < numDevices; i++) { if (audioManager.getMaxOutputChannels(i) > 0) {