/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class<?> typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ public static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } return result; }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ private static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = !(device instanceof Sequencer) && !(device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } return result; }
private static void doAll() throws Exception { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i=0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if ((! (device instanceof Sequencer)) && (! (device instanceof Synthesizer)) && (device.getMaxReceivers() > 0 || device.getMaxReceivers() == -1)) { System.out.println("--------------"); System.out.println("Testing MIDI device: " + infos[i]); testDevice(device); } if (infos.length==0) { System.out.println("No MIDI devices available!"); } } }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ public static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } if (!result) { System.err.println("Soundcard does not exist or sound drivers not installed!"); System.err.println("This test requires sound drivers for execution."); } return result; }
public static boolean checkInstrumentNames(Synthesizer theSynthesizer) { boolean containsControlCharacters = false; Instrument[] theLoadedInstruments = theSynthesizer.getLoadedInstruments(); System.out.println("Checking soundbank..."); for(int theInstrumentIndex = 0; theInstrumentIndex < theLoadedInstruments.length; theInstrumentIndex++) { String name = theLoadedInstruments[theInstrumentIndex].getName(); if (containsControlChar(name)) { containsControlCharacters = true; System.out.print("Instrument[" + theInstrumentIndex + "] contains unexpected control characters: "); printName(name); } } return !containsControlCharacters; }
public static void main(String[] args) throws Exception { // the internal synthesizer needs a soundcard to work properly if (!isSoundcardInstalled()) { return; } Synthesizer theSynth = MidiSystem.getSynthesizer(); System.out.println("Got synth: "+theSynth); theSynth.open(); try { Soundbank theSoundbank = theSynth.getDefaultSoundbank(); System.out.println("Got soundbank: "+theSoundbank); theSynth.loadAllInstruments(theSoundbank); try { if (!checkInstrumentNames(theSynth)) { throw new Exception("Test failed"); } } finally { theSynth.unloadAllInstruments(theSoundbank); } } finally { theSynth.close(); } System.out.println("Test passed."); }
public static void openInput() throws Exception { setup(); if (bUseDefaultSynthesizer) { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); r = synth.getReceiver(); try { Transmitter t = launchpad_s_plus.Launchpad.getInputDevice() .getTransmitter(); t.setReceiver(r); } catch (MidiUnavailableException e) { out("wasn't able to connect the device's Transmitter to the default Synthesizer:"); out(e); launchpad_s_plus.Launchpad.getInputDevice().close(); System.exit(1); } } out("\nNow taking input."); }
private static void playWarningSound() { // if (2 > 1) { // return; // } try { // int velocity = 127; // max volume int velocity = 90; // max volume 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, velocity); Thread.sleep(100); channel.noteOff(sound + i); } } catch (MidiUnavailableException | InterruptedException e1) { e1.printStackTrace(); } }
public Synthesizer getSynth() { try { if(!this.synth.isOpen()){ this.synth.open(); if(!isSoundbankLoaded( false )){ String path = MidiConfigUtils.getSoundbankPath(this.context); if( path != null ){ this.loadSoundbank(new File(TGExpressionResolver.getInstance(this.context).resolve(path))); } if(!isSoundbankLoaded( true )){ this.loadSoundbank(this.synth.getDefaultSoundbank()); } if(!isSoundbankLoaded( true )){ new SBAssistant(this.context, this).process(); } } } this.synthesizerLoaded = this.synth.isOpen(); } catch (Throwable throwable) { throwable.printStackTrace(); } return this.synth; }
public Synthesizer getSynth() { try { if(!this.synth.isOpen()){ this.synth.open(); if(!isSoundbankLoaded( false )){ if(!isSoundbankLoaded( true )){ this.loadSoundbank(this.synth.getDefaultSoundbank()); } } } this.synthesizerLoaded = this.synth.isOpen(); } catch (Throwable throwable) { throwable.printStackTrace(); } return this.synth; }
private Synthesizer findAudioSynthesizer() throws MidiUnavailableException, ClassNotFoundException { Class<?> audioSynth = Class.forName("com.sun.media.sound.AudioSynthesizer"); // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (audioSynth.isAssignableFrom(synth.getClass())) { return synth; } // If default synthesizer is not AudioSynthesizer, check others. MidiDevice.Info[] midiDeviceInfo = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < midiDeviceInfo.length; i++) { MidiDevice dev = MidiSystem.getMidiDevice(midiDeviceInfo[i]); if (audioSynth.isAssignableFrom(dev.getClass())) { return (Synthesizer)dev; } } return null; }
@Override protected void installImp(ProjectContainer project) { super.installImp(project); if(soundBankFileName != null) try { Soundbank soundbank; if(midiDevice instanceof SynthWrapper) soundbank = ((SynthWrapper)midiDevice).getSoundbank(new File(soundBankFileName)); else soundbank = MidiSystem.getSoundbank(new File(soundBankFileName)); ((Synthesizer)midiDevice).loadAllInstruments(soundbank); System.out.println("Soundbank loaded"); } catch(Exception e){ e.printStackTrace(); } }
/** * Return a list of voices for a device. if device does not provide a list * return null. * * @param dev * @param channel * @return */ public PatchNameMap getVoiceList(MidiDevice dev, int channel) { if (! (dev instanceof SynthWrapper) ) return null; assert (dev instanceof SynthWrapper); if (channel < 0 ) return null; // Make channel -1 someone elses problem since we will get an array exception // use first part of the device string. // TODO think ??? Still thinking PJL ?? :) /PJS // Still think this is all a bit ugly PJL if(((SynthWrapper)dev).getRealDevice() instanceof Synthesizer) { return new PatchNameMap((Synthesizer)dev, channel); } else if (((SynthWrapper)dev).getRealDevice() instanceof DrumMapper ){ return null; } else { String lookup = dev.getDeviceInfo().toString().split("\\s")[0]; PatchNameMap vl = voiceTreeMap.get(lookup); if (vl == null) { System.out.println(" Could not find voice map for \"" + lookup + "\""); return voiceTreeMap.get("default"); } return vl; } }
/** * Find available AudioSynthesizer */ public static AudioSynthesizer findAudioSynthesizer() throws MidiUnavailableException { // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (synth instanceof AudioSynthesizer) return (AudioSynthesizer) synth; // If default synhtesizer is not AudioSynthesizer, check others. for (Info info : MidiSystem.getMidiDeviceInfo()) { MidiDevice dev = MidiSystem.getMidiDevice(info); if (dev instanceof AudioSynthesizer) return (AudioSynthesizer) dev; } // No AudioSynthesizer was found, return null. return null; }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String value; String propertyName = typeClass.getName(); value = JSSecurityManager.getProperty(propertyName); if (value == null) { value = getProperties().getProperty(propertyName); } if ("".equals(value)) { value = null; } return value; }
public static AudioSynthesizer findAudioSynthesizer() throws MidiUnavailableException, IOException, InvalidMidiDataException { // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (synth instanceof AudioSynthesizer) { return (AudioSynthesizer) synth; } // If default synhtesizer is not AudioSynthesizer, check others. Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice dev = MidiSystem.getMidiDevice(infos[i]); if (dev instanceof AudioSynthesizer) { return (AudioSynthesizer) dev; } } // No AudioSynthesizer was found, return null. return null; }
/** * Stops all notes from playing on all MIDI channels. */ public static void allNotesOff(Synthesizer synth) { try { if (!synth.isOpen()) { synth.open(); } MidiChannel[] channels = synth.getChannels(); for (int i=0; i < channels.length; i++) { channels[i].allNotesOff(); } } catch (MidiUnavailableException e) { throw new JFugueException(JFugueException.GENERAL_ERROR); } }
public LMidiSound() { if (rendererStatus == UNINITIALIZED) { rendererStatus = INITIALIZING; Thread thread = new Thread() { public final void run() { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); volumeSupported = (sequencer instanceof Synthesizer); sequencer.close(); available = true; } catch (Throwable e) { available = false; } rendererStatus = INITIALIZED; } }; thread.setDaemon(true); thread.start(); } }
public LMidiSound(String fileName) throws IOException { bytes = new ArrayByte(UIRes.getStream(fileName), ArrayByte.BIG_ENDIAN); if (rendererStatus == UNINITIALIZED) { rendererStatus = INITIALIZING; Thread thread = new Thread() { public final void run() { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); volumeSupported = (sequencer instanceof Synthesizer); sequencer.close(); available = true; } catch (Throwable e) { available = false; } rendererStatus = INITIALIZED; } }; thread.setDaemon(true); thread.start(); } }
public Synthesizer getSynthesizer() { try { if(!this.synthesizer.isOpen()){ this.synthesizer.open(); if(!isSoundbankLoaded( false )){ String path = MidiConfigUtils.getSoundbankPath(); if(path != null){ this.loadSoundbank(new File(path)); } if(!isSoundbankLoaded( true )){ this.loadSoundbank( this.synthesizer.getDefaultSoundbank() ); } if(!isSoundbankLoaded( true )){ new SBAssistant(this).process(); } } } this.synthesizerLoaded = this.synthesizer.isOpen(); } catch (Throwable throwable) { throwable.printStackTrace(); } return this.synthesizer; }
public static AudioSynthesizer findAudioSynthesizer() throws MidiUnavailableException { // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (synth instanceof AudioSynthesizer) return (AudioSynthesizer) synth; // If default synhtesizer is not AudioSynthesizer, check others. Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice dev = MidiSystem.getMidiDevice(infos[i]); if (dev instanceof AudioSynthesizer) return (AudioSynthesizer) dev; } // No AudioSynthesizer was found, return null. return null; }
public static void main(String[] args) throws Exception { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); synth.unloadAllInstruments(synth.getDefaultSoundbank()); synth.loadAllInstruments(new MyOscillator()); Sequence seq = MidiSystem.getSequence(FMTest1.class.getResource("/FMTest1.mid")); Sequencer seqr = MidiSystem.getSequencer(false); seqr.open(); seqr.getTransmitter().setReceiver(synth.getReceiver()); seqr.setSequence(seq); seqr.start(); System.out.println(); System.out.println("Is active, press enter to stop"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); System.out.println("Stop..."); seqr.stop(); seqr.close(); synth.close(); System.exit(0); }
/** * Sets the synthesizer. If there has already been an another synthesizer set then it will be closed. * * @param synthesizer the synthesizer. */ public void setSynthesizer(Synthesizer synthesizer) { if (synthesizer != this.synthesizer) { MidiUtils.close(this.synthesizer); } this.synthesizer = synthesizer; }
/** * 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 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()); }