1 /*
   2  * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.PrintStream;
  25 
  26 import javax.sound.midi.Instrument;
  27 import javax.sound.midi.MidiChannel;
  28 import javax.sound.midi.MidiSystem;
  29 import javax.sound.midi.MidiUnavailableException;
  30 import javax.sound.midi.Soundbank;
  31 import javax.sound.midi.Synthesizer;
  32 
  33 /**
  34  * @test
  35  * @bug 4987585
  36  * @summary Some MidiChannel methods are asynchronous
  37  */
  38 public class AsynchronousMidiChannel {
  39     static PrintStream log = System.err;
  40     static PrintStream ref = System.out;
  41 
  42     public static void main(String args[]) {
  43         doIt(args);
  44     }
  45 
  46     public static void doIt(String args[]) {
  47         Synthesizer synth = null;
  48         MidiChannel mChanArr[];
  49         MidiChannel chan = null;
  50         boolean failed = false;
  51         int i = 0;
  52         int chanNum = 0;
  53 
  54         int val = 1;
  55         int contr = 0;
  56         Soundbank sBank;
  57         Instrument[] insArr;
  58         Instrument instr = null;
  59         Object ev = new Object();
  60 
  61         try {
  62             synth = MidiSystem.getSynthesizer();
  63             System.out.println("Got synth: "+synth);
  64             synth.open();
  65 
  66             int latency = (int) synth.getLatency();
  67             System.out.println("  -> latency: "
  68                                +latency
  69                                +" microseconds");
  70 
  71             mChanArr = synth.getChannels();
  72             while ((i < mChanArr.length) && (chan == null)) {
  73                 chanNum = i;
  74                 chan = mChanArr[i++];
  75             }
  76             if (chan == null) {
  77                 System.out.println("No channels in "
  78                                    +"this synthesizer!");
  79                 return;
  80             }
  81             System.out.println("Got MidiChannel: "+chan);
  82 
  83 
  84             sBank = synth.getDefaultSoundbank();
  85             if (sBank == null) {
  86                 System.out.println("No default sound bank!");
  87                 return;
  88             }
  89 
  90 
  91             insArr = sBank.getInstruments();
  92             for (int j = 0; j < insArr.length; j++) {
  93                 if (insArr[j].getPatch().getBank() == val) {
  94                     instr = insArr[j];
  95                     synth.loadInstrument(instr);
  96                 }
  97             }
  98             if (instr == null) {
  99                 System.out.println("No instr. with this bank!");
 100                 return;
 101             }
 102 
 103             chan.controlChange(contr, val);
 104 
 105             // need to respect the synthesizer's latency
 106             if (latency > 0) {
 107                 try {
 108                     Thread.sleep(latency/1000);
 109                 } catch (InterruptedException inEx) {
 110                 }
 111             }
 112 
 113             if (chan.getController(contr) != val) {
 114                 failed = true;
 115                 System.err.println("getController() does not "
 116                                    +"return proper value: "
 117                 + chan.getController(contr));
 118             }  else {
 119                 System.out.println("getController("
 120                 + contr + ") returns proper value: "
 121                 + chan.getController(contr));
 122             }
 123 
 124         } catch (MidiUnavailableException mue) {
 125             System.err.println("MidiUnavailableException was "
 126                                +"thrown: " + mue);
 127             System.out.println("could not test.");
 128             return;
 129         } catch(SecurityException se) {
 130             se.printStackTrace();
 131             System.err.println("Sound access is not denied but "
 132             + "SecurityException was thrown!");
 133             return;
 134 
 135         } finally {
 136             if (synth != null) synth.close();
 137         }
 138 
 139 
 140         if (failed == true) {
 141             System.out.println("test failed");
 142         } else {
 143             System.out.println("OKAY");
 144         }
 145         return;
 146     }
 147 }