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