问题
Can we create our own composition using Soundfont? I am using Tabla Soundfont along with JFugue to create the new composition and play it. But after loading the instruments from the Soundbank the program still plays the default sound of Piano.How to make the Instrument enable from the sound bank. Check the following Code:
import org.jfugue.*;
import java.io.File;
import java.io.FileInputStream;
import javax.sound.midi.Instrument;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Soundbank;
import javax.sound.midi.Synthesizer;
import com.sun.media.sound.SF2Soundbank;
public class SF2Demo {
static Synthesizer synthesizer;
static Sequencer sequencer;
static MidiDevice dev;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
synthesizer=MidiSystem.getSynthesizer();
synthesizer.open();
}
catch (Exception e) {e.printStackTrace();}
loadSF2();
Player player = new Player();
Pattern pattern = new Pattern("C#3 D#3 D#3 C#3 C#3 D#3 D#3 C#3");//"Dha Dhin Dhin Dha Dha Dhin Dhin Dha " Tabla Pattern
player.play(pattern);
}
private static void loadSF2() {
String filename;
boolean duringInitialLoad=false;
try{
filename="C:/Javapro/tabla-sf2/tabla.sf2";
File f= new File(filename);
if(f.exists()){
Soundbank soundbank=new SF2Soundbank(
new FileInputStream(f));
synthesizer.loadAllInstruments(soundbank);
synthesizer.open();
p("\n now loaded on synthesizer: ");
printInstruments(soundbank,synthesizer.getLoadedInstruments());
}
else {
p(filename + " not found");
System.exit(0);
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void printInstruments(Soundbank soundbank,Instrument[] instruments){
p("");
p("----------------------------------------------------------------------------");
p("Soundbank name: " + soundbank.getName());
p("Soundbank version: " + soundbank.getVersion());
p("Description: " + soundbank.getDescription());
p("Author: " + soundbank.getVendor() + ".");
p("Number of instruments: " + soundbank.getInstruments().length);
for (Instrument i : instruments)
{
p( "Bank=" + i.getPatch().getBank() +
" Patch=" + i.getPatch().getProgram() +
" Instr.=" + i);
}
}
public static void p(Object o){
System.out.println(o);
}
}
At Command Prompt Output:
C:\Javapro>javac -classpath gervill.jar;jfugue-4.0.3.jar SF2Test2.java
C:\Javapro>java -classpath gervill.jar;jfugue-4.0.3.jar;. SF2Test2
now loaded on synthesizer:
-----------------------------------------------------------------------
Soundbank name: Tabla from Indian Orchestral Soundfont CD
Soundbank version: 2.1
Description: To learn more go to www.soundfont.com
Author: Dr. S.K. Chew.
Number of instruments: 3
Bank=0 Patch=0 Instr.=Instrument: TablaSP bank #0 preset #0
Bank=0 Patch=1 Instr.=Instrument: TablaSPMLFO bank #0 preset #1
Bank=0 Patch=2 Instr.=Instrument: TablaSPVLFO bank #0 preset #2
来源:https://stackoverflow.com/questions/19161634/can-we-create-our-own-composition-using-soundfont-and-jfugue