我们可以创建一个使用SoundFont音色和JFugue我们自己的成分?(Can we create

2019-10-18 17:10发布

使用SoundFont音色我们可以创建自己的成分? 我使用的塔布拉鼓的音色库与JFugue一起创造新的组成和发挥它。 但是,从音库加载乐器后程序仍然扮演着Piano.How的默认声音,以使仪器从音库启用。 请检查下面的代码:

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);
        }
    }

在命令提示符输出:

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

现在装上合成:

-----------------------------------------------------------------------
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
文章来源: Can we create our own composition using Soundfont and JFugue?