Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); Instrument[] orchestra = synthesizer.getAvailableInstruments();
/** * Loads a MIDI instrument by name. Throws exception if not found. * * @param synthesizer the synthesizer. * @param instrumentName the instrument name. * @return the instrument or {@code null} if the instrument couldn't be loaded. */ public static Instrument loadInstrument(Synthesizer synthesizer, String instrumentName) { Optional<Instrument> result = Arrays.stream(synthesizer.getAvailableInstruments()) .filter(instrument -> instrument.getName().equals(instrumentName)).findFirst(); if (!result.isPresent()) { throw new IllegalArgumentException("Instrument named " + instrumentName + " is not available"); } return synthesizer.loadInstrument(result.get()) ? result.get() : null; }
/** * Returns the MIDI instrument. * * @param synthesizer the synthesizer. * @param channel the channel number. * @return the MIDI instrument. */ public static Instrument getInstrument(Synthesizer synthesizer, int channel) { return synthesizer.getAvailableInstruments()[getChannel(synthesizer, channel, true).getProgram()]; }
/** * Sets the synthesizer instrument from the currently selected bank of instruments. * * @param synthesizer the synthesizer. * @param channel the channel. * @param instrumentName the instrument name. * @return the new instrument. */ public static Instrument setInstrument(Synthesizer synthesizer, int channel, String instrumentName) { Optional<Instrument> result = Arrays.stream(synthesizer.getAvailableInstruments()) .filter(instrument -> instrument.getName().equals(instrumentName)).findFirst(); if (!result.isPresent()) { throw new IllegalArgumentException("Instrument named " + instrumentName + " is not available"); } return setInstrument(synthesizer, channel, result.get().getPatch().getProgram()); }
/** * Logs available synthesizer instruments. */ public void logInstruments() { for (Instrument instrument : synthesizer.getAvailableInstruments()) { logger.info("Instrument {} ({}/{})", instrument.getName(), instrument.getPatch().getBank(), instrument.getPatch().getProgram()); } } }
Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); final Instrument[] orchestra = synthesizer.getAvailableInstruments(); SwingUtilities.invokeLater(new Runnable(){ public void run() {
Instrument[] availInstrms = dsynth.getAvailableInstruments(); getLogger().info("Instrument counts: loaded={}, avail={}", loadedInstrms.length, availInstrms.length); getLogger().info("Loaded Instruments: {}", loadedInstrms);
synth.open(); try { setInstrument(synth.getAvailableInstruments()[0]); } finally { synth.close();