Java Code Examples for javax.sound.midi.Synthesizer#getChannels()

The following examples show how to use javax.sound.midi.Synthesizer#getChannels() . 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: 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 3
Source File: AsynchronousMidiChannel.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void doIt(String args[]) {
    Synthesizer synth = null;
    MidiChannel mChanArr[];
    MidiChannel chan = null;
    boolean failed = false;
    int i = 0;
    int chanNum = 0;

    int val = 1;
    int contr = 0;
    Soundbank sBank;
    Instrument[] insArr;
    Instrument instr = null;
    Object ev = new Object();

    try {
        synth = MidiSystem.getSynthesizer();
        System.out.println("Got synth: "+synth);
        synth.open();

        int latency = (int) synth.getLatency();
        System.out.println("  -> latency: "
                           +latency
                           +" microseconds");

        mChanArr = synth.getChannels();
        while ((i < mChanArr.length) && (chan == null)) {
            chanNum = i;
            chan = mChanArr[i++];
        }
        if (chan == null) {
            System.out.println("No channels in "
                               +"this synthesizer!");
            return;
        }
        System.out.println("Got MidiChannel: "+chan);


        sBank = synth.getDefaultSoundbank();
        if (sBank == null) {
            System.out.println("No default sound bank!");
            return;
        }


        insArr = sBank.getInstruments();
        for (int j = 0; j < insArr.length; j++) {
            if (insArr[j].getPatch().getBank() == val) {
                instr = insArr[j];
                synth.loadInstrument(instr);
            }
        }
        if (instr == null) {
            System.out.println("No instr. with this bank!");
            return;
        }

        chan.controlChange(contr, val);

        // need to respect the synthesizer's latency
        if (latency > 0) {
            try {
                Thread.sleep(latency/1000);
            } catch (InterruptedException inEx) {
            }
        }

        if (chan.getController(contr) != val) {
            failed = true;
            System.err.println("getController() does not "
                               +"return proper value: "
            + chan.getController(contr));
        }  else {
            System.out.println("getController("
            + contr + ") returns proper value: "
            + chan.getController(contr));
        }

    } catch (MidiUnavailableException mue) {
        System.err.println("MidiUnavailableException was "
                           +"thrown: " + mue);
        System.out.println("could not test.");
        return;
    } catch(SecurityException se) {
        se.printStackTrace();
        System.err.println("Sound access is not denied but "
        + "SecurityException was thrown!");
        return;

    } finally {
        if (synth != null) synth.close();
    }


    if (failed == true) {
        System.out.println("test failed");
    } else {
        System.out.println("OKAY");
    }
    return;
}