我们可以使用Soundfont和JFugue创建自己的作品吗?

时间:2013-10-03 14:16:08

标签: java jfugue soundfont gervill

我们可以使用Soundfont创建自己的作品吗?我正在使用Tabla Soundfont和JFugue创建新的合成并播放它。但是从Soundbank加载乐器后,程序仍然会播放Piano的默认声音。如何从声音库中启用乐器。 请检查以下代码:

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

0 个答案:

没有答案