Path: blob/master/test/jdk/javax/sound/midi/Synthesizer/AsynchronousMidiChannel.java
41154 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.PrintStream;2425import javax.sound.midi.Instrument;26import javax.sound.midi.MidiChannel;27import javax.sound.midi.MidiSystem;28import javax.sound.midi.MidiUnavailableException;29import javax.sound.midi.Soundbank;30import javax.sound.midi.Synthesizer;3132/**33* @test34* @bug 498758535* @summary Some MidiChannel methods are asynchronous36*/37public class AsynchronousMidiChannel {38static PrintStream log = System.err;39static PrintStream ref = System.out;4041public static void main(String args[]) {42doIt(args);43}4445public static void doIt(String args[]) {46Synthesizer synth = null;47MidiChannel mChanArr[];48MidiChannel chan = null;49boolean failed = false;50int i = 0;51int chanNum = 0;5253int val = 1;54int contr = 0;55Soundbank sBank;56Instrument[] insArr;57Instrument instr = null;58Object ev = new Object();5960try {61synth = MidiSystem.getSynthesizer();62System.out.println("Got synth: "+synth);63synth.open();6465int latency = (int) synth.getLatency();66System.out.println(" -> latency: "67+latency68+" microseconds");6970mChanArr = synth.getChannels();71while ((i < mChanArr.length) && (chan == null)) {72chanNum = i;73chan = mChanArr[i++];74}75if (chan == null) {76System.out.println("No channels in "77+"this synthesizer!");78return;79}80System.out.println("Got MidiChannel: "+chan);818283sBank = synth.getDefaultSoundbank();84if (sBank == null) {85System.out.println("No default sound bank!");86return;87}888990insArr = sBank.getInstruments();91for (int j = 0; j < insArr.length; j++) {92if (insArr[j].getPatch().getBank() == val) {93instr = insArr[j];94synth.loadInstrument(instr);95}96}97if (instr == null) {98System.out.println("No instr. with this bank!");99return;100}101102chan.controlChange(contr, val);103104// need to respect the synthesizer's latency105if (latency > 0) {106try {107Thread.sleep(latency/1000);108} catch (InterruptedException inEx) {109}110}111112if (chan.getController(contr) != val) {113failed = true;114System.err.println("getController() does not "115+"return proper value: "116+ chan.getController(contr));117} else {118System.out.println("getController("119+ contr + ") returns proper value: "120+ chan.getController(contr));121}122123} catch (MidiUnavailableException mue) {124System.err.println("MidiUnavailableException was "125+"thrown: " + mue);126System.out.println("could not test.");127return;128} catch(SecurityException se) {129se.printStackTrace();130System.err.println("Sound access is not denied but "131+ "SecurityException was thrown!");132return;133134} finally {135if (synth != null) synth.close();136}137138139if (failed == true) {140System.out.println("test failed");141} else {142System.out.println("OKAY");143}144return;145}146}147148149