Skip to content

Commit

Permalink
Tweak Sound.list() output format and handle device name trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Sep 12, 2023
1 parent 82d1557 commit 9f79c7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/processing/sound/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions src/processing/sound/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,40 @@ 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));
}

} 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) {
System.out.format(lineFormat, Engine.getEngine().inputDevice == i ? "I" : " ", i, deviceNames[i], audioManager.getMaxInputChannels(i));
}
}
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) {
Expand Down

0 comments on commit 9f79c7e

Please sign in to comment.