1 /*
   2  * Copyright (c) 2002, 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.ByteArrayInputStream;
  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 
  28 import javax.sound.midi.MidiDevice;
  29 import javax.sound.midi.MidiSystem;
  30 import javax.sound.midi.MidiUnavailableException;
  31 import javax.sound.midi.Sequence;
  32 import javax.sound.midi.Sequencer;
  33 
  34 /**
  35  * @test
  36  * @bug 4713900
  37  * @summary default Sequencer allows to set Mute for invalid track
  38  */
  39 public class SequencerSetMuteSolo {
  40 
  41     public static void main(String args[]) throws Exception {
  42         if (!hasSequencer()) {
  43             return;
  44         }
  45 
  46         //printMidiFile(args[0]);
  47 
  48         boolean failed = false;
  49         Sequencer seq = null;
  50         Sequence midiData = getSequence();
  51         int numTracks = midiData.getTracks().length;
  52         int totalNumberOfSequencers = 0;
  53         int totalNumberOfTestedSequencers = 0;
  54 
  55         MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  56         for (int device=0; device<infos.length; device++) {
  57             //seq = MidiSystem.getSequencer();
  58             MidiDevice dev = MidiSystem.getMidiDevice(infos[device]);
  59             if (dev instanceof Sequencer) {
  60                 seq = (Sequencer) dev;
  61                 totalNumberOfSequencers++;
  62                 System.out.println("Opening sequencer "+infos[device]);
  63                 try {
  64                     seq.open();
  65                     try {
  66                         seq.setSequence(midiData);
  67                         System.err.println("Number of tracks: " + numTracks);
  68                         System.err.println("TrackMute["+numTracks+"] state was: " + seq.getTrackMute(numTracks));
  69                         System.err.println("  setting to muted.");
  70                         seq.setTrackMute(numTracks, true);
  71                         System.err.println("  TrackMute["+numTracks+"] is now: " + seq.getTrackMute(numTracks));
  72                         if (seq.getTrackMute(numTracks)) {
  73                             failed = true;
  74                         }
  75                         System.err.println("TrackSolo["+numTracks+"] state was: " + seq.getTrackSolo(numTracks));
  76                         System.err.println("  setting to solo.");
  77                         seq.setTrackSolo(numTracks, true);
  78                         System.err.println("  TrackSolo["+numTracks+"] is now: " + seq.getTrackSolo(numTracks));
  79                         if (seq.getTrackSolo(numTracks)) {
  80                             failed = true;
  81                         }
  82                         totalNumberOfTestedSequencers++;
  83                     } finally {
  84                         if (seq != null) {
  85                             seq.close();
  86                             seq = null;
  87                         }
  88                     }
  89                 } catch (MidiUnavailableException mue) {
  90                     System.err.println("MidiUnavailableException was thrown: " + mue);
  91                     System.err.println("could not test this sequencer.");
  92                     return;
  93                 }
  94             }
  95         }
  96         if (totalNumberOfSequencers == 0) {
  97             System.out.println("No sequencers installed!");
  98             failed = true;
  99         }
 100         if (totalNumberOfTestedSequencers == 0) {
 101             System.out.println("Could not test any sequencers!");
 102             failed = true;
 103         }
 104         if( failed ) {
 105             throw new Exception("FAILED");
 106         } else {
 107             System.out.println("test OK");
 108         }
 109     }
 110 
 111     public static String getString(byte b) {
 112         //String res = Integer.toHexString(b & 0xFF).toUpperCase();
 113         //while (res.length()<2) res="0"+res;
 114         //return res;
 115         return String.valueOf(b);
 116     }
 117 
 118 
 119     public static void printMidiFile(String filename) throws Exception {
 120         File file = new File(filename);
 121         FileInputStream fis = new FileInputStream(file);
 122         byte[] data = new byte[(int) file.length()];
 123         fis.read(data);
 124         String s = "";
 125         for (int i=0; i<data.length; i++) {
 126                 s+=getString(data[i])+", ";
 127                 if (s.length()>72) {
 128                         System.out.println(s);
 129                         s="";
 130                 }
 131         }
 132         System.out.println(s);
 133     }
 134 
 135     public static Sequence getSequence() throws Exception {
 136         ByteArrayInputStream bais = new ByteArrayInputStream(pitchbend);
 137         Sequence seq = MidiSystem.getSequence(bais);
 138         return seq;
 139     }
 140 
 141     public static byte[] pitchbend = {
 142         77, 84, 104, 100, 0, 0, 0, 6, 0, 1, 0, 2, 0, 120, 77, 84, 114, 107, 0, 0,
 143         0, 27, 0, -1, 3, 19, 77, 73, 68, 73, 32, 116, 101, 115, 116, 32, 45, 32,
 144         116, 114, 97, 99, 107, 32, 48, 0, -1, 47, 0, 77, 84, 114, 107, 0, 0, 0, -44,
 145         0, -1, 3, 19, 77, 73, 68, 73, 32, 116, 101, 115, 116, 32, 45, 32, 116, 114,
 146         97, 99, 107, 32, 49, 0, -64, 30, 0, -112, 68, 126, 0, -32, 6, 67, 0, 14,
 147         71, 0, 20, 74, 0, 26, 77, 0, 32, 80, 0, 42, 85, 6, 50, 89, 6, 56, 92, 5,
 148         66, 97, 6, 74, 101, 6, 80, 104, 11, 84, 106, 20, 76, 102, 6, 70, 99, 5, 60,
 149         94, 6, 52, 90, 5, 44, 86, 4, 34, 81, 5, 26, 77, 5, 20, 74, 6, 10, 69, 5,
 150         2, 65, 7, 0, 64, 42, -112, 66, 123, 11, 68, 0, 72, 63, 126, 4, 66, 0, 43,
 151         -32, 0, 63, 6, 0, 60, 7, 0, 56, 6, 0, 53, 5, 0, 49, 5, 0, 43, 4, 0, 37, 3,
 152         0, 30, 3, 0, 25, 3, 0, 19, 3, 0, 13, 4, 0, 8, 4, 0, 2, 4, 0, 0, 70, 0, 3,
 153         5, 0, 9, 3, 0, 14, 7, 0, 16, 25, 0, 21, 5, 0, 25, 7, 0, 28, 5, 0, 32, 5,
 154         0, 36, 5, 0, 41, 6, 0, 46, 5, 0, 50, 5, 0, 53, 4, 0, 58, 7, 0, 61, 7, 0,
 155         64, 117, -112, 63, 0, 0, -1, 47, 0
 156     };
 157 
 158     static boolean hasSequencer() {
 159         try {
 160             Sequencer seq = MidiSystem.getSequencer();
 161             if (seq != null) {
 162                 seq.open();
 163                 seq.close();
 164                 return true;
 165             }
 166         } catch (Exception e) {}
 167         System.out.println("No sequencer available! Cannot execute test.");
 168         return false;
 169     }
 170 }