Gervill and SF2 sound incorrectly on some midi files

1.9k views Asked by At

I would appreciate your ideas on the attached source code / problem.
The code below works fine for most midi files. However, some sound incorrectly.
They produce a sort of Wha-Wha sound. I was able to circumvent the problem for pure Piano pieces like the one used below by modifying the Java StandardMidiFileReader to drop 176/1/77/0 messages (vibrato depth)
Would anyone have an idea how to really fix this problem; it´s not just this one piece of music.

I believe that there might be a bug with Gervill, as with Gervill on the classpath (or Java 7 where Gervill seem to be part of the distribution (and as I just found out current java 6 versions)) certain other files cannot be played correctly at all. An example is http://mb-s2.sakura.ne.jp/tirol/junk/midi/wagner_walkurenritt.mid
(from about 20 seconds the same funny Wha-Wha background noise starts when Gervill is beeing used)
By replacing the standard synthesizer with Keishi Suenaga's extended version of Timidity++ and using the same SF2 there all pieces sound fine so it´s not corrupt midi files.

Code:For Java SF2 support Gervill needs to be on the classpath
Soundfont: I have tried out JCLIVE.SF2 and a a Steinway SF2;
soundfonts are available for instance on hammersound.net or synthfont.com
sfarc unpacker can be downloaded from melodymachine.com or if site is down 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);
    }
}

I also tried out to load single instruments from SF2 soundfiles onto the synthesizer, where I don´t rely on the com.sun.media.sound.SF2Soundbank class, same result:

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

There are 0 answers