如何使用Java Sound API设置从midi文件中提取的音乐的速度?

时间:2017-05-17 11:29:44

标签: java audio midi javasound

我能够使用.getTempoInBPM()从midi文件中提取速度但不知何故我使用的函数.setTempoFactor()似乎改变了音乐的速度软合成器正在产生。我知道问题肯定在于函数Thread.sleep()我在midi消息的每次迭代中使用.noteOn(key,velocity)函数之后。 但我找不到可能的替代方案。

我附上了我的代码。 非常感谢任何见解。提前谢谢。

    import java.io.*; 
    import java.util.*;
    import javax.sound.midi.*;

    public class Test1 {

    public static final int NOTE_ON = 0x90;
    public static final int NOTE_OFF = 0x80;

     public static void main(String[] args) throws Exception {

int instrmt = 0;
    int tempo = 120;
    String filename = null;
    Sequencer sequencer;
sequencer = MidiSystem.getSequencer(); 

    Sequence sequence = MidiSystem.getSequence(new File("STE-003-chord.mid")); 
    sequencer.setSequence(sequence);
    int a = 0; 

Scanner input = new Scanner(System.in);
float fBPM;
System.out.println("Enter tempo");
fBPM=input.nextFloat();

    float tempoBPM = sequencer.getTempoInBPM();
    System.out.println(tempoBPM);
    float fFactor= fBPM / tempoBPM;
     sequencer.setTempoFactor((float)(fFactor));

    String inst[] = new String[131];
     try
    {
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();
        MidiChannel channels[] = synth.getChannels();
        Soundbank bank = synth.getDefaultSoundbank();
        synth.loadAllInstruments(bank);
        Instrument instrs[] = synth.getLoadedInstruments();
    for (int i=0; i < instrs.length; i++)
    {
            System.out.println((i+1)+". "+instrs     [i].getName());
            inst[i+1] = instrs[i].getName();
    }
         String str;
        Instrument instrument = null;
        System.out.println("Enter an instrument number.");
        int s = input.nextInt();
        str=inst[s];
        for (int i=0; i < instrs.length; i++)
        {
            if (instrs[i].getName().equals(str))
            {
                instrument = instrs[i];
                break;
            }
        }

        if (instrument == null)
        {
            System.out.println("Instrument not compatible.");
            System.exit(0);
        }

        Patch instrumentPatch = instrument.getPatch();
        channels[0].programChange(instrumentPatch.getBank(),
            instrumentPatch.getProgram());
    int trackNumber = 0;
    for (Track track :  sequence.getTracks()) {
        trackNumber++;
        System.out.println();
        for (int i=0; i < track.size(); i++) { 
            MidiEvent event = track.get(i);
            //System.out.print("@" + event.getTick() + " ");
            MidiMessage message = event.getMessage();
            if (message instanceof ShortMessage) {
                ShortMessage sm = (ShortMessage) message;

                if (sm.getCommand() == NOTE_ON) {
                    int key = sm.getData1();
                    int octave = (key / 12)-1;
                    int note = key % 12;
                   // String noteName = NOTE_NAMES[note];
                    int velocity = sm.getData2();

                   channels[0].noteOn(key,velocity);
                Thread.sleep(1000); //This might create the problem
                } else if (sm.getCommand() == NOTE_OFF) {
                    int key = sm.getData1();
                    int octave = (key / 12)-1;
                    int note = key % 12;
                   // String noteName = NOTE_NAMES[note];
                    int velocity = sm.getData2();

                   channels[0].noteOff(key);
                }
        }

        System.out.println();
    }
}
catch (Exception exc)
    {
        exc.printStackTrace();
    }
}
   }

0 个答案:

没有答案