Skip to content

Commit

Permalink
Improve Custom Soundfont loading, also fix an AWTGUI bug with it
Browse files Browse the repository at this point in the history
Previously AWTGUI was checking against the "soundfont" field being
"on" or "off" instead of "Custom" or "Default". This is now fixed.

Also, the actual soundfont loading procedure is now more cross-platform
and less error-prone, falling back to the default JVM font if no
sf2 fonts are found. Anbu/SDL2 also has received code to use this
feature as well... still in dire need of a UI rework though.
  • Loading branch information
AShiningRay committed Sep 19, 2024
1 parent f455339 commit 0a79a62
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/org/recompile/freej2me/AWTGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ public void itemStateChanged(ItemEvent e)
{
public void itemStateChanged(ItemEvent e)
{
if(useCustomMidi.getState()){ config.updateSoundfont("on"); hasPendingChange = true; }
else{ config.updateSoundfont("off"); hasPendingChange = true; }
if(useCustomMidi.getState()){ config.updateSoundfont("Custom"); hasPendingChange = true; }
else{ config.updateSoundfont("Default"); hasPendingChange = true; }

restartRequiredDialog.setLocationRelativeTo(main);
restartRequiredDialog.setVisible(true);
Expand Down Expand Up @@ -833,7 +833,7 @@ public void updateOptions()
{
enableAudio.setState(config.settings.get("sound").equals("on"));
enableRotation.setState(config.settings.get("rotate").equals("on"));
useCustomMidi.setState(config.settings.get("soundfont").equals("on"));
useCustomMidi.setState(config.settings.get("soundfont").equals("Custom"));
fpsCapNone.setState(config.settings.get("fps").equals("0"));
fpsCap15.setState(config.settings.get("fps").equals("15"));
fpsCap30.setState(config.settings.get("fps").equals("30"));
Expand Down
20 changes: 20 additions & 0 deletions src/org/recompile/freej2me/Anbu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.recompile.mobile.*;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
Expand Down Expand Up @@ -71,6 +72,21 @@ public Anbu(String args[])

Mobile.setPlatform(new MobilePlatform(lcdWidth, lcdHeight));

/*
* If the directory for custom soundfonts doesn't exist, create it, no matter if the user
* is going to use it or not.
*/
try
{
if(!PlatformPlayer.soundfontDir.isDirectory())
{
PlatformPlayer.soundfontDir.mkdirs();
File dummyFile = new File(PlatformPlayer.soundfontDir.getPath() + File.separatorChar + "Put your sf2 bank here");
dummyFile.createNewFile();
}
}
catch(IOException e) { System.out.println("Failed to create custom midi info file:" + e.getMessage()); }

/* TODO: Anbu has no way of enabling "Dump Audio Streams", a UI rewrite might be in order */

config = new Config();
Expand Down Expand Up @@ -410,6 +426,10 @@ void settingsChanged()
if(rotate.equals("on")) { rotateDisplay = true; }
if(rotate.equals("off")) { rotateDisplay = false; }

String midiSoundfont = config.settings.get("soundfont");
if(midiSoundfont.equals("Custom")) { PlatformPlayer.customMidi = true; }
else if(midiSoundfont.equals("Default")) { PlatformPlayer.customMidi = false; }

if (Mobile.nokia) { System.setProperty("microedition.platform", "Nokia6233/05.10"); }
else if (Mobile.sonyEricsson)
{
Expand Down
6 changes: 3 additions & 3 deletions src/org/recompile/freej2me/FreeJ2ME.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public void windowClosing(WindowEvent e)
*/
try
{
if(!PlatformPlayer.soundfontDir.exists())
if(!PlatformPlayer.soundfontDir.isDirectory())
{
PlatformPlayer.soundfontDir.mkdirs();
File dummyFile = new File(PlatformPlayer.soundfontDir + "/Put your sf2 bank here");
File dummyFile = new File(PlatformPlayer.soundfontDir.getPath() + File.separatorChar + "Put your sf2 bank here");
dummyFile.createNewFile();
}
}
Expand Down Expand Up @@ -408,7 +408,7 @@ private void settingsChanged()

String midiSoundfont = config.settings.get("soundfont");
if(midiSoundfont.equals("Custom")) { PlatformPlayer.customMidi = true; }
if(midiSoundfont.equals("Default")) { PlatformPlayer.customMidi = false; }
else if(midiSoundfont.equals("Default")) { PlatformPlayer.customMidi = false; }

// Create a standard size LCD if not rotated, else invert window's width and height.
if(!rotateDisplay)
Expand Down
6 changes: 3 additions & 3 deletions src/org/recompile/freej2me/Libretro.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public Libretro(String args[])
*/
try
{
if(!PlatformPlayer.soundfontDir.exists())
if(!PlatformPlayer.soundfontDir.isDirectory())
{
PlatformPlayer.soundfontDir.mkdirs();
File dummyFile = new File(PlatformPlayer.soundfontDir + "/Put your sf2 bank here");
File dummyFile = new File(PlatformPlayer.soundfontDir.getPath() + File.separatorChar + "Put your sf2 bank here");
dummyFile.createNewFile();
}
}
Expand Down Expand Up @@ -481,7 +481,7 @@ private void settingsChanged()

String midiSoundfont = config.settings.get("soundfont");
if(midiSoundfont.equals("Custom")) { PlatformPlayer.customMidi = true; }
if(midiSoundfont.equals("Default")) { PlatformPlayer.customMidi = false; }
else if(midiSoundfont.equals("Default")) { PlatformPlayer.customMidi = false; }

if(lcdWidth != w || lcdHeight != h)
{
Expand Down
33 changes: 20 additions & 13 deletions src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class PlatformPlayer implements Player

public static boolean customMidi = false;

public static File soundfontDir = new File("freej2me_system/customMIDI/");
public static File soundfontDir = new File("freej2me_system" + File.separatorChar + "customMIDI" + File.separatorChar);

public PlatformPlayer(InputStream stream, String type)
{
Expand Down Expand Up @@ -269,15 +269,15 @@ public midiPlayer(InputStream stream)
* Check if the user wants to run a custom MIDI soundfont. Also, there's no harm
* in checking if the directory exists again.
*/
if(customMidi && soundfontDir.exists())
if(customMidi)
{
/* Get the first sf2 soundfont in the directory */
String[] fontfile = soundfontDir.list(new FilenameFilter()
{
@Override
public boolean accept(File f, String soundfont )
{
return soundfont.endsWith(".sf2");
return soundfont.toLowerCase().endsWith(".sf2");
}
});

Expand All @@ -287,22 +287,29 @@ public boolean accept(File f, String soundfont )
*/
if(fontfile.length > 0)
{
soundfont = MidiSystem.getSoundbank(new File(soundfontDir + "/" + fontfile[0]));
midi = MidiSystem.getSequencer(false);
synth = MidiSystem.getSynthesizer();
synth.open();
synth.loadAllInstruments(soundfont);
midi.getTransmitter().setReceiver(synth.getReceiver());
}
}

for(int i = 0; i < fontfile.length; i++)
{
// Load the first .sf2 font, if there's none that's valid, don't set any and use JVM's default
if(fontfile[i].toLowerCase().endsWith(".sf2"))
{
soundfont = MidiSystem.getSoundbank(new File(soundfontDir.getPath() + File.separatorChar + fontfile[i]));
midi = MidiSystem.getSequencer(false);
synth = MidiSystem.getSynthesizer();
synth.open();
synth.loadAllInstruments(soundfont);
midi.getTransmitter().setReceiver(synth.getReceiver());
break;
}
}
} else { System.out.println("PlatformPlayer: Custom MIDI enabled but there's no soundfont in" + (soundfontDir.getPath() + File.separatorChar)); }
}
midi.open();
midi.setSequence(stream);
state = Player.PREFETCHED;
}
catch (Exception e)
{
System.out.println("Couldn't load midi file::" + e.getMessage());
System.out.println("Couldn't load midi file:" + e.getMessage());
if(customMidi) { synth.close(); }
midi.close();
}
Expand Down

0 comments on commit 0a79a62

Please sign in to comment.