Path: blob/master/test/jdk/javax/sound/midi/Sequencer/Looping.java
41155 views
/*1* Copyright (c) 2003, 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 javax.sound.midi.InvalidMidiDataException;24import javax.sound.midi.MetaEventListener;25import javax.sound.midi.MetaMessage;26import javax.sound.midi.MidiDevice;27import javax.sound.midi.MidiEvent;28import javax.sound.midi.MidiSystem;29import javax.sound.midi.Sequence;30import javax.sound.midi.Sequencer;31import javax.sound.midi.ShortMessage;32import javax.sound.midi.Track;3334/**35* @test36* @bug 420410537* @summary RFE: add loop() method(s) to Sequencer38* @key intermittent39*/40public class Looping {4142public static void main(String[] args) throws Exception {43out("4204105: RFE: add loop() method(s) to Sequencer");44boolean passed = testAll();45if (passed) {46out("Test PASSED.");47} else {48throw new Exception("Test FAILED.");49}50}5152/**53* Execute the test on all available Sequencers.54*55* @return true if the test passed for all Sequencers, false otherwise56*/57private static boolean testAll() throws Exception {58boolean result = true;59MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();60for (int i = 0; i < devices.length; i++) {61MidiDevice device = MidiSystem.getMidiDevice(devices[i]);62if (device instanceof Sequencer) {63result &= testSequencer((Sequencer) device);64}65}66return result;67}6869/**70* Execute the test on the passed Sequencer.71*72* @return true if the test is passed this Sequencer, false otherwise73*/74private static boolean testSequencer(Sequencer seq) throws Exception{75boolean result = true;76out("testing: " + seq);7778result &= testGetSet(seq);7980seq.setSequence(createSequence());8182result &= testGetSet(seq);8384result &= testPlay(seq);8586return result;87}8889private static boolean testGetSet(Sequencer seq) {90boolean result = true;91Sequence sequence = seq.getSequence();92boolean isSequenceLoaded = (sequence != null);9394out("TestGetSet");9596try {97if (seq.getLoopStartPoint() != 0) {98out("start point", isSequenceLoaded,99"isn't 0!");100result = false;101}102} catch (IllegalArgumentException iae) {103if (!isSequenceLoaded) {104out("Caught permissable IllegalArgumentException:");105} else {106out("Threw unacceptable IllegalArgumentException! FAILED");107result = false;108}109out(iae.toString());110}111112if (seq.getLoopEndPoint() != -1) {113out("end point", isSequenceLoaded,114"isn't -1!");115result = false;116}117118try {119seq.setLoopStartPoint(25);120if (seq.getLoopStartPoint() != 25) {121out("setLoopStartPoint()", isSequenceLoaded,122"doesn't set the start point correctly!");123result = false;124}125} catch (IllegalArgumentException iae) {126if (!isSequenceLoaded) {127out("Caught permissable IllegalArgumentException:");128} else {129out("Threw unacceptable IllegalArgumentException! FAILED");130result = false;131}132out(iae.toString());133}134135try {136seq.setLoopEndPoint(26);137if (seq.getLoopEndPoint() != 26) {138out("setLoopEndPoint()", isSequenceLoaded,139"doesn't set the end point correctly!");140result = false;141}142} catch (IllegalArgumentException iae) {143if (!isSequenceLoaded) {144out("Caught permissable IllegalArgumentException:");145} else {146out("Threw unacceptable IllegalArgumentException! FAILED");147result = false;148}149out(iae.toString());150}151152try {153seq.setLoopStartPoint(0);154if (seq.getLoopStartPoint() != 0) {155out("setLoopStartPoint()", isSequenceLoaded,156"doesn't set the start point correctly!");157result = false;158}159} catch (IllegalArgumentException iae) {160if (!isSequenceLoaded) {161out("Caught permissable IllegalArgumentException:");162} else {163out("Threw unacceptable IllegalArgumentException! FAILED");164result = false;165}166out(iae.toString());167}168169if (isSequenceLoaded) {170seq.setLoopEndPoint(sequence.getTickLength());171if (seq.getLoopEndPoint() != sequence.getTickLength()) {172out("setLoopEndPoint()", isSequenceLoaded,173"doesn't set the end point correctly!");174result = false;175}176} else {177// fails178seq.setLoopEndPoint(-1);179if (seq.getLoopEndPoint() != -1) {180out("setLoopEndPoint()", isSequenceLoaded,181"doesn't set the end point correctly!");182result = false;183}184}185186if (seq.getLoopCount() != 0) {187out("loop count", isSequenceLoaded,188"isn't 0!");189result = false;190}191192seq.setLoopCount(1001);193if (seq.getLoopCount() != 1001) {194out("setLoopCount()", isSequenceLoaded,195"doesn't set the loop count correctly!");196result = false;197}198199seq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);200if (seq.getLoopCount() != Sequencer.LOOP_CONTINUOUSLY) {201out("setLoopCount(Sequencer.LOOP_CONTINUOUSLY)", isSequenceLoaded,202"doesn't set the loop count correctly!");203result = false;204}205206try {207seq.setLoopCount(-55);208out("setLoopCount()", isSequenceLoaded,209"doesn't throw IllegalArgumentException on illegal value!");210result = false;211} catch (IllegalArgumentException e) {212// EXCEPTION IS EXPECTED213out("Caught permissable IAE");214}215216seq.setLoopCount(0);217if (seq.getLoopCount() != 0) {218out("setLoopCount()", isSequenceLoaded,219"doesn't set the loop count correctly!");220result = false;221}222223return result;224}225226private static boolean testPlay(Sequencer seq) {227boolean result = true;228long stopTime;229230out("TestPlay");231232TestMetaEventListener listener = new TestMetaEventListener();233seq.addMetaEventListener(listener);234long startTime = System.currentTimeMillis();235try {236seq.open();237out("Playing sequence, length="+(seq.getMicrosecondLength()/1000)+"millis");238seq.start();239while (true) {240stopTime = listener.getStopTime();241if (stopTime != 0) {242break;243}244try {245Thread.sleep(100);246} catch (InterruptedException e) {247}248}249long measuredDuration = stopTime - startTime;250out("play duration (us): " + measuredDuration);251} catch (Exception e) {252out("test not executed; exception:");253e.printStackTrace();254}255seq.close();256return result;257}258259/**260* Create a new Sequence for testing.261*262* @return a dummy Sequence, or null, if a problem occured while creating263* the Sequence264*/265private static Sequence createSequence() {266Sequence sequence = null;267int lengthInSeconds = 2;268long lengthInMicroseconds = lengthInSeconds * 1000000;269int resolution = 480;270long lengthInTicks = (lengthInMicroseconds * 120 * resolution) / 60000000l;271out("length in ticks: " + lengthInTicks);272try {273sequence = new Sequence(Sequence.PPQ, resolution, 1);274Track track = sequence.createTrack();275ShortMessage mm = new ShortMessage();276mm.setMessage(0xF6, 0, 0);277MidiEvent me = new MidiEvent(mm, lengthInTicks);278track.add(me);279} catch (InvalidMidiDataException e) {280// DO NOTHING281}282out("sequence length (ticks): " + sequence.getTickLength());283out("sequence length (us): " + sequence.getMicrosecondLength());284return sequence;285}286287288private static void out(String m1, boolean isSequenceLoaded, String m2) {289out(m1 + (isSequenceLoaded ? " with Sequence " : " without Sequence ") + m2);290}291292private static void out(String message) {293System.out.println(message);294}295296private static class TestMetaEventListener implements MetaEventListener {297private long stopTime;298299300public void meta(MetaMessage m) {301System.out.print(" Got MetaMessage: ");302if (m.getType() == 47) {303stopTime = System.currentTimeMillis();304System.out.println(" End Of Track -- OK");305} else {306System.out.println(" unknown. Ignored.");307}308}309310public long getStopTime() {311return stopTime;312}313}314}315316317