MIDI乐器清单?

9

最近我根据《Head First Java》的代码实现了一个MIDI节拍盒,并且非常希望能够更多地利用Java的MIDI功能。我想从添加更多的非打击乐器开始,但是我似乎找不到可用乐器及其int键的简单列表。

这样的列表是否存在于JDK附带的Soundbank中?

2个回答

18

你喜欢这样吗?

import javax.sound.midi.*;
import javax.swing.*;

class Instruments {

    public static void main(String[] args) throws MidiUnavailableException {
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        Instrument[] orchestra = synthesizer.getAvailableInstruments();

        final StringBuilder sb = new StringBuilder();
        String eol = System.getProperty("line.separator");
        sb.append("The orchestra has ");
        sb.append(orchestra.length);
        sb.append(" instruments.");
        sb.append(eol);
        for (Instrument instrument : orchestra) {
            sb.append(instrument.toString());
            sb.append(eol);
        }
        synthesizer.close();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(null,
                        new JScrollPane(new JTextArea(sb.toString(), 20, 30)));
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

输出

The orchestra has 411 instruments.
Instrument Piano (bank 0 program 0)
Instrument Bright Piano (bank 0 program 1)
Instrument Electric Grand (bank 0 program 2)
Instrument Honky Tonk Piano (bank 0 program 3)
Instrument Electric Piano 1 (bank 0 program 4)
Instrument Electric Piano 2 (bank 0 program 5)
Instrument Harpsichord (bank 0 program 6)
Instrument Clavinet (bank 0 program 7)
Instrument Celesta (bank 0 program 8)
Instrument Glockenspiel (bank 0 program 9)
...

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接