Gervill和SF2在某些midi文件上声音不正确

时间:2012-11-09 20:22:21

标签: java gervill

感谢您对所附源代码/问题的想法 下面的代码适用于大多数midi文件。但是,有些声音不正确 它们产生一种Wha-Wha声音。通过修改Java StandardMidiFileReader以丢弃176/1/77/0消息(颤音深度),我能够绕过下面使用的纯钢琴片的问题。 有人会知道如何真正解决这个问题;它不仅仅是这一首音乐。

我相信Gervill可能存在一个错误,就像Gervill在类路径上一样(或者Java 7,其中Gervill似乎是发行版的一部分(而且我刚发现当前的java 6版本))某些其他文件不能完全正确地玩。一个例子是http://mb-s2.sakura.ne.jp/tirol/junk/midi/wagner_walkurenritt.mid
(大约20秒时,当使用Gervill时,同样有趣的Wha-Wha背景噪音开始)
通过将标准合成器替换为Keishi Suenaga的扩展版Timidity ++并使用相同的SF2,所有部件听起来都很好,所以它不会损坏midi文件。

代码:对于Java SF2支持,Gervill需要在类路径上 Soundfont:我尝试过JCLIVE.SF2和Steinway SF2;
声音字体可在例如hammersound.net或synthfont.com上获得 sfarc unpacker可以从melodymachine.com下载,如果网站已经关闭,请联系sourceforge.net/projects/miditool/files/third%20party/

package test;

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 SF2Test2 {
    static Synthesizer synthesizer;
    static Sequencer sequencer;
    static MidiDevice dev;


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            synthesizer=MidiSystem.getSynthesizer();
            synthesizer.open();
        //  soundbank =synthesizer.getDefaultSoundbank();
        //  p("Soundbank: "  + soundbank.getName() + " / "  + soundbank.getDescription()+ " / " + soundbank.getVendor());
            } 
        catch (Exception e) {e.printStackTrace();}

        loadSF2();

        File f = new File("D:\\SF2\\rach.mid");  //http://mb-s2.sakura.ne.jp/tirol/junk/midi/rachmaninoff_op3_2.mid

        play(f);
    }





    private static void loadSF2() {
        String filename;
        boolean duringInitialLoad=false;
        try{
            filename="D:/SF2/JC.SF2";   
            File f= new File(filename);
            if(f.exists()){
                Soundbank soundbank=new SF2Soundbank(
                        new FileInputStream(f));
                synthesizer.loadAllInstruments(soundbank);
                //synthesizer.open(); 
                //moving open command to here: synthesizer will still run on default (Gervill) soundbank
                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 play(File file){

        try {
            sequencer=MidiSystem.getSequencer();
            Sequence sequence= MidiSystem.getSequence(file);
            sequencer.setSequence(sequence);
            sequencer.open();
            sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
            p("starting Sequence...");
            sequencer.start();
            Thread.sleep(15000);
            sequencer.stop();
            sequencer.close();

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

我还尝试将单个乐器从SF2声音文件加载到合成器上,我不依赖于com.sun.media.sound.SF2Soundbank类,结果相同:

try{
    filename="D:/SF2/JCLIVE.SF2";
    File f= new File(filename);
    if(f.exists()){
        Soundbank soundbank=MidiSystem.getSoundbank(f);
        Instrument [] sf2Instruments = soundbank.getInstruments();
        Instrument [] newInstruments = new Instrument[128];
        int replaced=0;
        //load pianos only
        for(int i=0;i<3;i++){
            try{
                int num=sf2Instruments[i].getPatch().getProgram();
                newInstruments[i]=sf2Instruments[num];
                synthesizer.loadInstrument(sf2Instruments[num]);
                replaced++;
            }catch(Exception e){}
        }
        //synthesizer.loadAllInstruments(soundbank);
        p("\nreplaced: "+ replaced);
        p("\n now loaded on synthesizer: ");
        printInstruments(soundbank,synthesizer.getLoadedInstruments());
    }
}catch(Exception e){
    e.printStackTrace();
}

0 个答案:

没有答案