I got a sample soundfont from:
http://www.hammersound.com/cgi-bin/soundlink.pl?action=view_category&category=Sound+Effects&ListStart=0&ListLength=15
And tested the concept with some random code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Soundbank;
import javax.sound.midi.Synthesizer;
import javax.swing.JButton;
import javax.swing.JFrame;
public class LoadSoundbank extends JFrame {
private static final boolean DEBUG = true;
public static JButton playButton = new JButton("play");
public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, IOException {
Synthesizer synth = null;
synth = MidiSystem.getSynthesizer();
if (DEBUG) out("Synthesizer: " + synth);
Soundbank soundbank = null;
File file = new File("SFX_StarWars_weapons.SF2");
System.out.println(file.exists());
soundbank = MidiSystem.getSoundbank(file);
if (DEBUG) out("Soundbank: " + soundbank);
synth.open();
if (DEBUG) out("Defaut soundbank: " + synth.getDefaultSoundbank());
if (soundbank != null)
{
out("soundbank supported: " + synth.isSoundbankSupported(soundbank));
boolean bInstrumentsLoaded = synth.loadAllInstruments(soundbank);
if (DEBUG) out("Instruments loaded: " + bInstrumentsLoaded);
}
playButton.addActionListener(new PlayListener(synth));
new LoadSoundbank();
}
private static void printUsageAndExit()
{
out("LoadSoundbank: usage:");
out("java LoadSoundbank [<soundbankfilename>]");
System.exit(1);
}
private static void out(String strMessage)
{
System.out.println(strMessage);
}
public LoadSoundbank (){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(playButton, BorderLayout.CENTER);
pack();
show();
}
private static class PlayListener implements ActionListener {
int nNoteNumber = (int)(80*Math.random()); // MIDI key number
int nVelocity = 100; // MIDI note on velocity
int nDuration = 50;
int channelNumber = 10;
Synthesizer synth;
public PlayListener(Synthesizer synth){
this.synth = synth;
channelNumber = 0;
}
@Override
public void actionPerformed(ActionEvent arg0) {
for (int i = 0;i < 50; i++){
MidiChannel[] channels = synth.getChannels();
nNoteNumber = (int)(80*Math.random());
// nNoteNumber = 45;
MidiChannel channel = channels[9];
// channels[9].noteOn(nNoteNumber, nVelocity);
int PAN_CONTROLLER = 10;
// Pan to Center:
// channel.controlChange(PAN_CONTROLLER, 64);
if (i<25){
// Pan hard left:
channel.controlChange(PAN_CONTROLLER, 0);
}
else {
// Pan hard right:
channel.controlChange(PAN_CONTROLLER, 127);
}
// "Active stereo", Jimi-Hendrix-style
// sweep from almost-full left to almost-full right:
// for (int position = 8; position < 127; position += 8) {
// channel.controlChange(PAN_CONTROLLER, position);
// try {
// Thread.sleep(5);
// } catch (InterruptedException e) {
// }
// }
channels[channelNumber].setMono(false);
channels[channelNumber].noteOn(nNoteNumber, nVelocity);
/*
* Wait for the specified amount of time
* (the duration of the note).
*/
try
{
Thread.sleep(nDuration);
}
catch (InterruptedException e)
{
}
// channels[channelNumber].noteOff(nNoteNumber);
}
/*
* Turn the note off.
*/
}
}
}
Problem now is how to get MY soundeffects into a sf2 file…