Skip to content

Commit

Permalink
Fix portaudio dll loading on Windows and bundle Mac jnilib (see #85)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Sep 27, 2023
1 parent ed7e7ff commit d4a3a61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 4 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<target name="bundled-deps" description="Download JSyn and (J)PortAudio">
<get src="https://github.com/philburk/jsyn/releases/download/v17.1.0/jsyn-17.1.0.jar" dest="${lib}" skipexisting="true" />
<get src="https://www.softsynth.com/jsyn/developers/archives/jportaudio_pc_20120904.zip" dest="${lib}" skipexisting="true" />
<!--get src="https://www.softsynth.com/jsyn/developers/archives/jportaudio_mac_20120904.zip" dest="${lib}" skipexisting="true" /-->
<get src="https://www.softsynth.com/jsyn/developers/archives/jportaudio_mac_20120904.zip" dest="${lib}" skipexisting="true" />
<unzip src="${lib}/jportaudio_pc_20120904.zip" dest="${lib}">
<patternset>
<include name="**/*.jar" />
Expand All @@ -65,12 +65,13 @@
</patternset>
<mapper type="flatten"/>
</unzip>
<!--unzip src="${lib}/jportaudio_mac_20120904.zip" dest="${lib}/mac-x86_64/">
<!-- the mac libraries need to go into library/ rather than the sub-directory... -->
<unzip src="${lib}/jportaudio_mac_20120904.zip" dest="${lib}/">
<patternset>
<include name="**/*.jnilib" />
</patternset>
<mapper type="flatten"/>
</unzip-->
</unzip>
</target>

<target name="deps" description="Get library dependencies">
Expand Down
32 changes: 19 additions & 13 deletions src/processing/sound/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@
*/
class Engine {

// static {
// try {
// // TODO PRINT INSTRUCTIONS for going to System Preferences > Security &
// // Privacy > General to 'Allow' libjportaudio.jnilib
// // System.loadLibrary("libjportaudio");
// System.loadLibrary("portaudio_x64");
// } catch (UnsatisfiedLinkError e) {
// // System.loadLibrary("jportaudio_0_1_0");
// }
// }

private static AudioDeviceManager createDefaultAudioDeviceManager() {
try {
Class.forName("javax.sound.sampled.AudioSystem");
Expand All @@ -57,13 +46,28 @@ private static AudioDeviceManager createDefaultAudioDeviceManager() {
}

private static AudioDeviceManager createAudioDeviceManager(boolean portAudio) {
if (!portAudio) {
return Engine.createDefaultAudioDeviceManager();
}
// hide JPortAudio init messages from console
PrintStream originalStream = System.out;
System.setOut(new PrintStream(new OutputStream(){
public void write(int b) { }
}));
// JPortAudio takes care of loading all native libraries -- except the
// dependent portaudio dll on Windows for some reason. try loading it no
// matter what platform we're on and ignore any errors, if it's really not
// supported on this system then the JPortAudio device further down will
// blow up anyway
try {
System.loadLibrary("portaudio_x64");
} catch (UnsatisfiedLinkError e) {
}

try {
return portAudio ? new JPortAudioDevice() : Engine.createDefaultAudioDeviceManager();
return new JPortAudioDevice();
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
throw new RuntimeException("PortAudio is not supported on this operating system/architecture");
} finally {
System.setOut(originalStream);
Expand Down Expand Up @@ -321,11 +325,13 @@ protected int selectOutputDevice(int deviceId) {
// there is no point probing the channel on a JPortAudioDevice (which seems
// to throw IllegalArgumentException no matter what you probe it with), or
// the JSynAndroidAudioDeviceManager (which does not support the JavaSound
// clases used for probing)
// classes used for probing)
if (this.synth.getAudioDeviceManager() instanceof JavaSoundAudioDevice) {
// check for a working line first (since using PortAudio might change the
// number of available channels)
try {
// TODO does this also work as expected if the device is currently
// listed as having 0 output channels?
this.probeDeviceOutputLine(deviceId, this.sampleRate);
} catch (LineUnavailableException e) {
// try portaudio access to the same device -- need get the name of the
Expand Down

0 comments on commit d4a3a61

Please sign in to comment.