我似乎无法改变工具。我切换了仪器的值,但输出无异。无论我尝试什么值(value),我都只能让钢琴演奏。这是下面的简单代码。有没有人有什么建议?还是我缺少工具对象的基本知识?

import javax.sound.midi.*;
//import javax.sound.*;

public class Drum {
    static int instrument = 45;
    static int note = 100;
    static int timbre = 0;
    static int force = 100;
    public static void main(String[] args) {
        Synthesizer synth = null;
        try {
            synth = MidiSystem.getSynthesizer();
            synth.open();
        }
        catch (Exception e) {
            System.out.println(e);
        }
        Soundbank soundbank = synth.getDefaultSoundbank();
        Instrument[] instr = soundbank.getInstruments();
        synth.loadInstrument(instr[instrument]);    //Changing this int (instrument) does nothing
        MidiChannel[] mc = synth.getChannels();
        mc[4].noteOn(note, force);
        try { Thread.sleep(1000); }
        catch(InterruptedException e) {}
        System.out.println(instr[instrument].getName());

        synth.close();

    }
}

最佳答案

您需要告知 channel 使用仪器。我承认我从未在Java中使用过MIDI,但类似mc.programChange(instr.getPatch().getProgram())的声音听起来很有希望。

关于Java MIDI合成器-无法更改乐器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4881541/

10-09 19:43