javax.sound.midi.Synthesizer Java Examples

The following examples show how to use javax.sound.midi.Synthesizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dialogs.java    From Pixelitor with GNU General Public License v3.0 7 votes vote down vote up
public 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();
    }
}
 
Example #2
Source File: ExtraCharInSoundbank.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
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.");
}
 
Example #3
Source File: Midi2WavRenderer.java    From computoser with GNU Affero General Public License v3.0 7 votes vote down vote up
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;
}
 
Example #4
Source File: JDK13Services.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #5
Source File: ClosedReceiver.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: MidiOutGetMicrosecondPositionBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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!");
        }
    }
}
 
Example #7
Source File: MidiOutGetMicrosecondPositionBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: JDK13Services.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #9
Source File: JDK13Services.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #10
Source File: JDK13Services.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #11
Source File: ExtraCharInSoundbank.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #12
Source File: JDK13Services.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #13
Source File: JDK13Services.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #14
Source File: MidiPortSynthesizer.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
}
 
Example #15
Source File: JDK13Services.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #16
Source File: MidiPortSynthesizer.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
}
 
Example #17
Source File: MidiApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private void loadChannel() {
    try {
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();
        synth.loadInstrument(synth.getDefaultSoundbank().getInstruments()[5]);

        channel = synth.getChannels()[0];

    } catch (MidiUnavailableException e) {
        System.out.println("Cannot get synth");
        e.printStackTrace();
    }
}
 
Example #18
Source File: JDK13Services.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #19
Source File: OpenClose.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source File: JDK13Services.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #21
Source File: JDK13Services.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #22
Source File: LMidiSound.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
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();
	}
}
 
Example #23
Source File: JDK13Services.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** 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;
}
 
Example #24
Source File: JDK13Services.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #25
Source File: LMidiSound.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
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();
	}
}
 
Example #26
Source File: PassportMidiInterface.java    From jace with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void resume() {
    if (isRunning() && midiOut != null) {
        return;
    }
    try {
        MidiDevice selectedDevice = MidiSystem.getSynthesizer();
        MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
        if (devices.length == 0) {
            System.out.println("No MIDI devices found");
        } else {
            for (MidiDevice.Info dev : devices) {
                if (MidiSystem.getMidiDevice(dev).getMaxReceivers() == 0) {
                    continue;
                }
                System.out.println("MIDI Device found: " + dev);
                if ((preferredMidiDevice.getValue() == null && dev.getName().contains("Java Sound") && dev instanceof Synthesizer) ||
                        preferredMidiDevice.getValue().equalsIgnoreCase(dev.getName())
                    ) {
                    selectedDevice = MidiSystem.getMidiDevice(dev);
                    break;
                }
            }
        }
        if (selectedDevice != null) {
            System.out.println("Selected MIDI device: " + selectedDevice.getDeviceInfo().getName());
            selectedDevice.open();
            midiOut = selectedDevice.getReceiver();
            super.resume();
        }
    } catch (MidiUnavailableException ex) {
        System.out.println("Could not open MIDI synthesizer");
        Logger.getLogger(PassportMidiInterface.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #27
Source File: JDK13Services.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #28
Source File: JDK13Services.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/** 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;
}
 
Example #29
Source File: MidiSynthesizerManager.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Synthesizer getSynth() {
	try {
		if(!this.synth.isOpen()){
			this.synth.open();
		}
		this.synthesizerLoaded = this.synth.isOpen();
	} catch (Throwable throwable) {
		throwable.printStackTrace();
	}
	return this.synth;
}
 
Example #30
Source File: MidiPortProviderImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public List<MidiOutputPort> listPorts() throws MidiPlayerException{
	try {
		List<MidiOutputPort> ports = new ArrayList<MidiOutputPort>();
		MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
		for(int i = 0; i < infos.length; i++){
			try {
				Iterator<MidiOutputPort> it = ports.iterator();
				boolean exists = false;
				while(it.hasNext()){
					if( ((MidiOutputPort)it.next()).getKey().equals(infos[i].getName()) ){
						exists = true;
						break;
					}
				}
				if(!exists){
					MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
					if(device.getMaxReceivers() == 0 || device instanceof Sequencer){
						continue;
					}
					if(device instanceof Synthesizer){
						ports.add(new MidiPortSynthesizer((Synthesizer)device));
					}
					else{
						ports.add(new MidiPortOut(device));
					}
				}
			} catch (MidiUnavailableException e) {
				throw new MidiPlayerException("jsa.error.midi.unavailable", e);
			}
		}
		return ports;
	}catch (Throwable t) {
		throw new MidiPlayerException("jsa.error.unknown", t);
	}
}