Skip to content

Commit ce999cb

Browse files
committed
Improve device selection handling (fixes #7), update README, build.xml and version bump to 2.0.1
1 parent ac61f10 commit ce999cb

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ The new Sound library for Processing 3 provides a simple way to work with audio.
66

77
The easiest way to install the Sound library is through Processing's Contribution Manager. The library comes with many example sketches, the full online reference can be found [here](https://www.processing.org/reference/libraries/sound/). Please report bugs [https://github.com/processing/processing-sound/issues](here).
88

9+
For detailed changelogs and to download older releases, have a look at the [Github releases page](https://github.com/processing/processing-sound/releases).
10+
911
### How to build
1012

1113
1. `git clone [email protected]:processing/processing-sound.git`
1214
2. into the `library/` folder copy (or soft-link) your Processsing's `core.jar` (and, optionally, also your Android SDK's `android.jar`, API level 26 or higher). Other dependencies (in particular Phil Burk's [JSyn](http://www.softsynth.com/jsyn/) engine on which this library is based) are downloaded automatically.
1315
3. `ant dist` (or, alternatively, run build.xml from within Eclipse)
1416

15-
The resulting `processing-sound.zip` can be extracted into your Processing installation's `libraries/` folder.
17+
The resulting `sound.zip` can be extracted into your Processing installation's `libraries/` folder.
1618

1719
### License
1820

build.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</target>
6161

6262
<target name="dist" depends="build,javadoc">
63-
<zip destfile="../processing-sound.zip">
63+
<zip destfile="../sound.zip">
6464
<zipfileset dir="." prefix="sound">
6565
<exclude name=".*" />
6666
<exclude name="build.xml" />
@@ -72,7 +72,6 @@
7272
<exclude name="src/**" />
7373
</zipfileset>
7474
</zip>
75-
<!--copy file="library.properties"
76-
toFile="../processing-sound.txt" /-->
75+
<copy file="library.properties" toFile="../sound.txt" />
7776
</target>
7877
</project>

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ authors=The Processing Foundation
44
url=https://processing.org/reference/libraries/sound/index.html
55
sentence=Provides a simple way to work with audio.
66
paragraph=
7-
version=9
8-
prettyVersion=2.0.0
7+
version=10
8+
prettyVersion=2.0.1
99
lastUpdated=0
1010
minRevision=228
1111
maxRevision=0

src/processing/sound/Engine.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,27 @@ private Engine(PApplet theParent) {
4848

4949
// create and start the synthesizer, and set this object as the singleton.
5050
this.synth = JSyn.createSynthesizer(Engine.getAudioManager());
51-
this.inputDevice = Engine.getAudioManager().getDefaultInputDeviceID();
52-
this.outputDevice = Engine.getAudioManager().getDefaultOutputDeviceID();
51+
52+
// select default devices
53+
for (int i = 0; i < Engine.getAudioManager().getDeviceCount(); i++) {
54+
if (Engine.checkDeviceHasOutputs(i)) {
55+
this.outputDevice = i;
56+
break;
57+
}
58+
if (i == Engine.getAudioManager().getDeviceCount()) {
59+
Engine.printError("library initalization failed: could not find any audio devices with a stereo output");
60+
return;
61+
}
62+
}
63+
for (int i = 0; i < Engine.getAudioManager().getDeviceCount(); i++) {
64+
if (Engine.checkDeviceHasInputs(i)) {
65+
this.inputDevice = i;
66+
break;
67+
}
68+
if (i == Engine.getAudioManager().getDeviceCount()) {
69+
Engine.printWarning("could not find any sound devices with input channels, you won't be able to use the AudioIn class");
70+
}
71+
}
5372

5473
this.lineOut = new LineOut(); // stereo lineout by default
5574
this.synth.add(lineOut);
@@ -79,7 +98,6 @@ protected void startSynth() {
7998
this.synth.stop();
8099
}
81100

82-
// TODO do some more user-friendly checks based on getMaxInput/OutputChannels
83101
this.synth.start(this.sampleRate,
84102
this.inputDevice, Engine.getAudioManager().getMaxInputChannels(this.inputDevice),
85103
// TODO limit number of output channels to 2?
@@ -103,14 +121,31 @@ protected void setSampleRate(int sampleRate) {
103121
Engine.singleton.startSynth();
104122
}
105123

124+
private static boolean checkDeviceHasInputs(int deviceId) {
125+
return Engine.getAudioManager().getMaxInputChannels(deviceId) > 0;
126+
}
127+
128+
private static boolean checkDeviceHasOutputs(int deviceId) {
129+
// require stereo output
130+
return Engine.getAudioManager().getMaxOutputChannels(deviceId) > 1;
131+
}
132+
106133
protected void selectInputDevice(int deviceId) {
107-
Engine.singleton.inputDevice = deviceId;
108-
Engine.singleton.startSynth();
134+
if (Engine.checkDeviceHasInputs(deviceId)) {
135+
Engine.singleton.inputDevice = deviceId;
136+
Engine.singleton.startSynth();
137+
} else {
138+
Engine.printError("audio device #" + deviceId + " has no input channels");
139+
}
109140
}
110141

111142
protected void selectOutputDevice(int deviceId) {
112-
Engine.singleton.outputDevice = deviceId;
113-
Engine.singleton.startSynth();
143+
if (Engine.checkDeviceHasOutputs(deviceId)) {
144+
Engine.singleton.outputDevice = deviceId;
145+
Engine.singleton.startSynth();
146+
} else {
147+
Engine.printError("audio device #" + deviceId + " has no stereo output channel");
148+
}
114149
}
115150

116151
protected void setVolume(double volume) {

src/processing/sound/Sound.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static String[] list() {
6868
int maxOutputs = audioManager.getMaxOutputChannels(i);
6969
boolean isDefaultInput = (i == audioManager.getDefaultInputDeviceID());
7070
boolean isDefaultOutput = (i == audioManager.getDefaultOutputDeviceID());
71-
System.out.println("deviceId" + i + ": " + deviceName);
71+
System.out.println("device id " + i + ": " + deviceName);
7272
System.out.println(" max inputs : " + maxInputs + (isDefaultInput ? " (default)" : ""));
7373
System.out.println(" max outputs: " + maxOutputs + (isDefaultOutput ? " (default)" : ""));
7474
}

0 commit comments

Comments
 (0)