Path: blob/master/test/jdk/javax/sound/midi/Sequencer/SetTickPosition.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.MidiEvent;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.Sequence;26import javax.sound.midi.Sequencer;27import javax.sound.midi.ShortMessage;28import javax.sound.midi.Track;2930/**31* @test32* @bug 449377533* @summary Sequncer method, setTickPosition(long) doesnot set the Tick position34*/35public class SetTickPosition {36private static boolean testPassed = true;3738public void runTest()39{40Sequencer theSequencer = null;41try42{43System.out.print("Getting Sequencer...");44theSequencer = MidiSystem.getSequencer();45System.out.println("got "+theSequencer);4647if(!(theSequencer.isOpen()))48{49System.out.println("Opening Sequencer...");50theSequencer.open();5152if(!(theSequencer.isOpen()))53{54System.out.println("Unable to open the Sequencer. Test NOT FAILED.");55return;56}57}5859System.out.println("theSequencer is open!\n");6061System.out.println("Creating New Sequence...");62Sequence theSequence = new Sequence(Sequence.PPQ, 120);6364System.out.println("Adding Track To Sequence...");65Track theTrack = theSequence.createTrack();6667int theChannel = 0;6869int theNote = 60;70int theVelocity = 100;71ShortMessage theShortMessage = new ShortMessage();7273for (int tick=0; tick<2000; tick+=120) {74//System.out.println("Adding NOTE_ON To Track At Tick: " + tick + "...\n");75theShortMessage.setMessage(ShortMessage.NOTE_ON, theChannel, theNote, theVelocity);76MidiEvent theMidiEvent = new MidiEvent(theShortMessage, tick);77theTrack.add(theMidiEvent);7879//System.out.println("Adding NOTE_OFF To Track At Tick: " + (tick+60) + "...\n");80theShortMessage.setMessage(ShortMessage.NOTE_OFF, theChannel, theNote, theVelocity);81theMidiEvent = new MidiEvent(theShortMessage, tick+60);82theTrack.add(theMidiEvent);83}84theSequencer.setSequence(theSequence);8586float theTempoInBPM = 120;87theSequencer.setTempoInBPM(theTempoInBPM);88long theTickLengthOfSequence = theSequencer.getTickLength();89System.out.println("Length Of Sequence In Ticks: " + theTickLengthOfSequence);90System.out.println("Sequence resolution: " + theSequencer.getSequence().getResolution());9192theSequencer.start();93for(long theTickPosition = 0; theTickPosition < theTickLengthOfSequence; theTickPosition += (theTickLengthOfSequence / 10))94{95System.out.println("Now Setting Tick Position To: " + theTickPosition);96theSequencer.setTickPosition(theTickPosition);9798long theCurrentTickPosition = theSequencer.getTickPosition();99long theCurrentMsPosition = (long) (theSequencer.getMicrosecondPosition()/1000);100System.out.println("IsRunning()=" + theSequencer.isRunning());101System.out.println("Now Current Tick Position Is: " + theCurrentTickPosition);102//System.out.println("Now Current micro Position Is: " + theCurrentMsPosition);103System.out.println("");104105try {106Thread.sleep(800);107} catch (InterruptedException ie) {}108109// last time, set tick pos to 0110if (theTickPosition>0 && theTickPosition<(theTickLengthOfSequence / 10)) {111theTickPosition=(theTickLengthOfSequence / 10);112}113114// 30 = 1/4 * 120, the resolution of the sequence115if(Math.abs(theCurrentTickPosition - theTickPosition) > 30) {116System.out.println("theCurrentTickPosition != theTickPosition!");117testPassed = false;118}119}120121}122catch (Exception ex) { ex.printStackTrace(); }123if (theSequencer != null) {124theSequencer.close();125}126if (testPassed) {127System.out.println("Test Passed.");128}129}130131public static void main(String[] args) throws Exception {132SetTickPosition thisTest = new SetTickPosition();133thisTest.runTest();134if (!testPassed) {135throw new Exception("Test FAILED");136}137}138}139140141