/** * Returns the synthesizer channels. * * @return the synthesizer channels. */ public List<MidiChannel> getChannels() { return Arrays.asList(synthesizer.getChannels()); }
Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); Instrument[] orchestra = synthesizer.getAvailableInstruments();
/** * Loads all instruments to the synthesizer from the default soundbank. * * @param synthesizer the synthesizer. * @return {@code true} on success. */ public static boolean loadAllInstruments(Synthesizer synthesizer) { if (synthesizer.getDefaultSoundbank() != null) { return loadAllInstruments(synthesizer, synthesizer.getDefaultSoundbank()); } else { return false; } }
/** * 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; }
private void playSoundbankDemo(Synthesizer dsynth) throws Throwable { getLogger().info("Synth latency={}, polyphony={}", dsynth.getLatency(), dsynth.getMaxPolyphony()); Soundbank dsdbk = dsynth.getDefaultSoundbank(); getLogger().info("Default synth has default soundbank {}, of class {}", dsdbk, (dsdbk != null) ? dsdbk.getClass() : "NULL"); dsynth.open(); Instrument[] loadedInstrms = dsynth.getLoadedInstruments(); Instrument[] availInstrms = dsynth.getAvailableInstruments(); getLogger().info("Instrument counts: loaded={}, avail={}", loadedInstrms.length, availInstrms.length); getLogger().info("Loaded Instruments: {}", loadedInstrms); VoiceStatus[] synthVoiceStatusArr = dsynth.getVoiceStatus(); getLogger().info("VoiceStatus array size={}, firstEntry={}", synthVoiceStatusArr.length, synthVoiceStatusArr[0]); MidiChannel[] midChans = dsynth.getChannels(); for (int cidx = 0; cidx < midChans.length; cidx++) { MidiChannel chan = midChans[cidx]; Receiver synthRcvr = dsynth.getReceiver(); dsynth.close();
Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments(); MidiChannel[] mChannels = midiSynth.getChannels(); midiSynth.loadInstrument(instr[0]);//load an instrument
Synthesizer synth = MidiSystem.getSynthesizer (); synth.open (); synth.loadAllInstruments (synth.getDefaultSoundbank ()); Instrument [] insts = synth.getLoadedInstruments (); MidiChannel channels[] = synth.getChannels (); for (int i = 0; i < channels.length; i++) { if (channels [i] != null) {
Soundbank soundbank = synth.getDefaultSoundbank(); if(soundbank == null) synth.loadAllInstruments(soundbank); receiver = synth.getReceiver(); usingHardwareSoundbank = false; MidiChannel[] channels = synth.getChannels();
synthesizer.open(); if( synthesizer.getDefaultSoundbank() == null ) synthesizer.getReceiver() );
private static void playWarningSound() { try { int maxVolume = 90; int sound = 65; Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); MidiChannel channel = synthesizer.getChannels()[9]; // drums channel. for (int i = 0; i < 10; i++) { Thread.sleep(100); channel.noteOn(sound + i, maxVolume); Thread.sleep(100); channel.noteOff(sound + i); } } catch (MidiUnavailableException | InterruptedException e1) { e1.printStackTrace(); } }
if (instrument==null) { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); try { setInstrument(synth.getAvailableInstruments()[0]); } finally { synth.close();
synth.open(); sm_openedMidiDeviceList.add(synth); Receiver synthReceiver = synth.getReceiver(); Transmitter seqTransmitter = seqr.getTransmitter(); seqTransmitter.setReceiver(synthReceiver);
/** * Returns the synthesizer loaded instruments. * * @param synthesizer the synthesizer. * @return the instruments. */ public static List<Instrument> getInstruments(Synthesizer synthesizer) { return Arrays.stream(synthesizer.getLoadedInstruments()).collect(Collectors.toList()); }
/** Sets the instrument to be used for sequence playback. This implicitly loads the * given instrument. * @param instr The instrument to be used for sequence playback. */ public void setInstrument(Instrument instr) throws MidiUnavailableException { MidiSystem.getSynthesizer().loadInstrument(instr); instrument = instr; }
/** * Updates the synthesizer state. This method should be invoked after setting the synthesizer in the plugin. */ public void updateSynthesizer() { lock.lock(); try { MidiUtils.open(synthesizer); // Close existing and create a new synthesizer receiver to generate sounds. MidiUtils.close(synthesizerReceiver); if (synthesizer != null) { synthesizerReceiver = synthesizer.getReceiver(); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } finally { lock.unlock(); } }
/** * Loads all instruments to the synthesizer from the soundbank. * * @param synthesizer the synthesizer. * @param soundbank the soundbank. * @return {@code true} on success. */ public static boolean loadAllInstruments(Synthesizer synthesizer, Soundbank soundbank) { return synthesizer.loadAllInstruments(soundbank); }
syn = MidiSystem.getSynthesizer(); syn.open(); midChannel = syn.getChannels(); instrument = syn.getDefaultSoundbank().getInstruments(); syn.loadInstrument(instrument[90]);
/** * Returns the MIDI channel. * * @param synthesizer the synthesizer. * @param channel the channel number. * @param required if {@code true} then throws exception when not found or not available. Otherwise returns {@code null}. * @return the MIDI channel. */ public static MidiChannel getChannel(Synthesizer synthesizer, int channel, boolean required) { MidiChannel[] channels = synthesizer.getChannels(); if (channel >= channels.length || channels[channel] == null) { if (required) { throw new IllegalArgumentException("Channel " + channel + " is not available"); } else { return null; } } return channels[channel]; }
/** * 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()); }
import javax.sound.midi.*; public class MidiSynthesizerSample { public static void main(String[] args) { int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60}; try { Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); MidiChannel channel = synthesizer.getChannels()[0]; for (int note : notes) { channel.noteOn(note, 50); try { Thread.sleep(200); } catch (InterruptedException e) { break; } finally { channel.noteOff(note); } } } catch (MidiUnavailableException e) { e.printStackTrace(); } } }