Skip to content

Commit

Permalink
Updated FluidSynthJava for Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
jjazzboss committed Dec 22, 2023
1 parent 5231376 commit 8e024d1
Showing 1 changed file with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public final class FluidSynthJava

private static final String COMMAND_LINE_PROPERTY_FLUIDSYNTH_LIB = "fluidsynthlib.path";
private static final String DEFAULT_LIB_FILENAME_LINUX = "libfluidsynth.so.3";
private static final String DEFAULT_LIB_FILENAME_MAC = "libfluidsynth.dylib";
private static final String DEFAULT_LIB_FILENAME_MAC = "libfluidsynth.3.dylib";
private static final List<String> LIB_DIRS_LINUX = Arrays.asList("/usr/lib", "/usr/lib/x86_64-linux-gnu", "/usr/lib64", "/lib");
// Expect fluidsynth is the shared lib dir of the 3 package managers homebrew, fink (/opt/sw/lib), and MacPorts (opt/local/lib)
private static final List<String> LIB_DIRS_MAC = Arrays.asList("/usr/lib", "/opt/homebrew/lib", "/opt/sw/lib", "/opt/local/lib");
private static final String LIB_NAME_MAC = "fluidsynth";
private static final String PREF_LIB = "PrefLib";

Expand Down Expand Up @@ -114,8 +117,9 @@ public FluidSynthJava()
/**
* Create a JavaFluidSynth object from another one.
* <p>
* If jfs native resources are allocated, create the native resources initialized with the same values for: reverb, chorus,
* gain, soundfont file (if loaded), synth.device-id.
* If jfs native resources are allocated, create the native resources
* initialized with the same values for: reverb, chorus, gain, soundfont
* file (if loaded), synth.device-id.
*
* @param jfs Must be already open
* @param createAudioDriver If true create the associated audio driver
Expand Down Expand Up @@ -243,9 +247,11 @@ public boolean checkFluidSynthMinimumVersion(int major, int minor, int micro)
/**
* Set the synth device Id for XG System ON compatibility.
* <p>
* IMPORTANT: FluidSynth 2.3.0 (should be fixed in 2.3.1) expects a special XG System ON message (3rd byte is NOT 0001nnnn
* with n the deviceId), which differs from the standard one See https://github.com/FluidSynth/fluidsynth/issues/1092 Changing
* the deviceId to 16 is a trick to make it react to the standard XG System ON
* IMPORTANT: FluidSynth 2.3.0 (should be fixed in 2.3.1) expects a special
* XG System ON message (3rd byte is NOT 0001nnnn with n the deviceId),
* which differs from the standard one See
* https://github.com/FluidSynth/fluidsynth/issues/1092 Changing the
* deviceId to 16 is a trick to make it react to the standard XG System ON
*/
private void setDeviceIdForXGCompatibility()
{
Expand Down Expand Up @@ -367,7 +373,8 @@ public void sendShortMessage(ShortMessage sm)
/**
* Send a SysexMessage to the native FluidSynth instance.
* <p>
* For XG ON sysex message to works, synth.device-id must be 16 (up to FluidSynth 2.3.0) ! See
* For XG ON sysex message to works, synth.device-id must be 16 (up to
* FluidSynth 2.3.0) ! See
* https://github.com/FluidSynth/fluidsynth/issues/1092
*
* @param sm
Expand All @@ -385,9 +392,9 @@ public void sendSysexMessage(SysexMessage sm)
var fluidData_ma = allocator.allocateArray(CLinker.C_CHAR, fluidData);
var handled_seg = SegmentAllocator.ofScope(scope).allocate(CLinker.C_INT, 0);
fluid_synth_sysex(fluid_synth_ma,
fluidData_ma,
fluidData.length,
MemoryAddress.NULL, MemoryAddress.NULL, handled_seg, 0);
fluidData_ma,
fluidData.length,
MemoryAddress.NULL, MemoryAddress.NULL, handled_seg, 0);
int handled = handled_seg.toIntArray()[0];
}

Expand Down Expand Up @@ -623,7 +630,8 @@ public Chorus getChorus()
/**
* Generate a .wav file from midiFile.
* <p>
* From "Fast file renderer for non-realtime MIDI file rendering" https://www.fluidsynth.org/api/FileRenderer.html.
* From "Fast file renderer for non-realtime MIDI file rendering"
* https://www.fluidsynth.org/api/FileRenderer.html.
*
* @param midiFile The input Midi file
* @param wavFile The wav file to be created
Expand All @@ -646,7 +654,6 @@ public void generateWavFile(File midiFile, File wavFile) throws FluidSynthExcept
// Create a synth copy
FluidSynthJava synthCopy = new FluidSynthJava(this, false);


// specify the file to store the audio to
// make sure you compiled fluidsynth with libsndfile to get a real wave file
// otherwise this file will only contain raw s16 stereo PCM
Expand Down Expand Up @@ -712,8 +719,8 @@ public int loadSoundFont(File f) throws FluidSynthException

var sfont_path_native = CLinker.toCString(f.getAbsolutePath(), ResourceScope.newImplicitScope());
lastLoadedSoundFontFileId = fluid_synth_sfload(fluid_synth_ma,
sfont_path_native,
1); // 1: re-assign presets for all MIDI channels (equivalent to calling fluid_synth_program_reset())
sfont_path_native,
1); // 1: re-assign presets for all MIDI channels (equivalent to calling fluid_synth_program_reset())

if (lastLoadedSoundFontFileId == FLUID_FAILED())
{
Expand Down Expand Up @@ -789,7 +796,6 @@ static public boolean isPlatformSupported()
// =============================================================================================
// Private methods
// =============================================================================================

private void check(boolean b, String exceptionText) throws FluidSynthException
{
if (!b)
Expand Down Expand Up @@ -822,7 +828,7 @@ static private boolean loadNativeLibraries()
} else
{
LOGGER.log(Level.WARNING, "loadNativeLibraries() Platform not supported os.name={0}",
System.getProperty("os.name", "XX").toLowerCase(Locale.ENGLISH));
System.getProperty("os.name", "XX").toLowerCase(Locale.ENGLISH));
}

if (!error)
Expand All @@ -836,15 +842,16 @@ static private boolean loadNativeLibraries()
/**
* Load on Linux or Mac.
* <p>
* Try various strategies, first using the optional COMMAND_LINE_PROPERTY_FLUIDSYNTH_LIB if set, then standard fluidsynth lib
* Try various strategies, first using the optional
* COMMAND_LINE_PROPERTY_FLUIDSYNTH_LIB if set, then standard fluidsynth lib
* names in various standard directories.<p>
* ---
* <p>
* Can't manage to make System.loadLibrary("fluidsynth") to work, see
* https://stackoverflow.com/questions/74604651/system-loadlibrary-cant-find-a-shared-library-on-linux
* <p>
* But System.load(path_to_fluidsynth.so.xx) works fine on linux and mac, and no need to explicitly load dependencies like on
* windows.
* But System.load(path_to_fluidsynth.so.xx) works fine on linux and mac,
* and no need to explicitly load dependencies like on windows.
*
* @return true if success
*/
Expand All @@ -862,7 +869,6 @@ private static boolean loadNativeLibrariesLinuxMac()
LOGGER.warning("loadNativeLibrariesLinuxMac() could not load library " + lib);
}


// If it worked before, we saved the path as a preference
String prefLib = prefs.get(PREF_LIB, null);
if (prefLib != null)
Expand All @@ -877,7 +883,6 @@ private static boolean loadNativeLibrariesLinuxMac()
}
}


// Check various dirs
var libDirs = getLinuxOrMacLibDirs();
String libFilename = getLinuxOrMacLibFileName();
Expand All @@ -902,9 +907,7 @@ private static boolean loadNativeLibrariesLinuxMac()
*/
private static List<String> getLinuxOrMacLibDirs()
{
var dirs = Utilities.isLinux()
? Arrays.asList("/usr/lib", "/usr/lib/x86_64-linux-gnu", "/usr/lib64", "/lib") // Should be ok for the main distros
: Arrays.asList("/usr/lib"); // TO BE CHECKED FOR MAC
var dirs = Utilities.isLinux() ? LIB_DIRS_LINUX : LIB_DIRS_MAC;
return dirs;
}

Expand Down Expand Up @@ -978,7 +981,8 @@ private static boolean loadNativeLibrariesWin()
}

/**
* Get the relative path for the required FluidSynth libs in *reverse* dependency order.
* Get the relative path for the required FluidSynth libs in *reverse*
* dependency order.
* <p>
*
* @return Empty if no relevant libraries found.
Expand All @@ -1003,7 +1007,6 @@ private static String getLinuxOrMacLibFileName()
return Utilities.isMac() ? DEFAULT_LIB_FILENAME_MAC : DEFAULT_LIB_FILENAME_LINUX;
}


static private boolean quietLoadOrLoadLibrary(String lib)
{
if (lib.startsWith("/"))
Expand Down

0 comments on commit 8e024d1

Please sign in to comment.