1/*
2 * Copyright (c) 2003, 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
24import javax.sound.midi.MidiEvent;
25import javax.sound.midi.MidiSystem;
26import javax.sound.midi.Sequence;
27import javax.sound.midi.Sequencer;
28import javax.sound.midi.ShortMessage;
29import javax.sound.midi.Track;
30
31/**
32 * @test
33 * @bug 4493775
34 * @summary Sequncer method, setTickPosition(long) doesnot set the Tick position
35 */
36public class SetTickPosition {
37        private static boolean testPassed = true;
38
39        public void runTest()
40        {
41            Sequencer theSequencer = null;
42            try
43            {
44                System.out.print("Getting Sequencer...");
45                theSequencer = MidiSystem.getSequencer();
46                System.out.println("got "+theSequencer);
47
48                if(!(theSequencer.isOpen()))
49                {
50                    System.out.println("Opening Sequencer...");
51                    theSequencer.open();
52
53                    if(!(theSequencer.isOpen()))
54                    {
55                        System.out.println("Unable to open the Sequencer. Test NOT FAILED.");
56                        return;
57                    }
58                }
59
60                System.out.println("theSequencer is open!\n");
61
62                System.out.println("Creating New Sequence...");
63                Sequence theSequence = new Sequence(Sequence.PPQ, 120);
64
65                System.out.println("Adding Track To Sequence...");
66                Track theTrack = theSequence.createTrack();
67
68                int theChannel = 0;
69
70                int theNote = 60;
71                int theVelocity = 100;
72                ShortMessage theShortMessage = new ShortMessage();
73
74                for (int tick=0; tick<2000; tick+=120) {
75                    //System.out.println("Adding NOTE_ON To Track At Tick: " + tick + "...\n");
76                    theShortMessage.setMessage(ShortMessage.NOTE_ON, theChannel, theNote, theVelocity);
77                    MidiEvent theMidiEvent = new MidiEvent(theShortMessage, tick);
78                    theTrack.add(theMidiEvent);
79
80                    //System.out.println("Adding NOTE_OFF To Track At Tick: " + (tick+60) + "...\n");
81                    theShortMessage.setMessage(ShortMessage.NOTE_OFF, theChannel, theNote, theVelocity);
82                    theMidiEvent = new MidiEvent(theShortMessage, tick+60);
83                    theTrack.add(theMidiEvent);
84                }
85                theSequencer.setSequence(theSequence);
86
87                float theTempoInBPM = 120;
88                theSequencer.setTempoInBPM(theTempoInBPM);
89                long theTickLengthOfSequence = theSequencer.getTickLength();
90                System.out.println("Length Of Sequence In Ticks: " + theTickLengthOfSequence);
91                System.out.println("Sequence resolution: " + theSequencer.getSequence().getResolution());
92
93                theSequencer.start();
94                for(long theTickPosition = 0; theTickPosition < theTickLengthOfSequence; theTickPosition += (theTickLengthOfSequence / 10))
95                {
96                    System.out.println("Now Setting Tick Position To: " + theTickPosition);
97                    theSequencer.setTickPosition(theTickPosition);
98
99                    long theCurrentTickPosition = theSequencer.getTickPosition();
100                    long theCurrentMsPosition = (long) (theSequencer.getMicrosecondPosition()/1000);
101                    System.out.println("IsRunning()=" + theSequencer.isRunning());
102                    System.out.println("Now Current Tick Position Is: " + theCurrentTickPosition);
103                    //System.out.println("Now Current micro Position Is: " + theCurrentMsPosition);
104                    System.out.println("");
105
106                    try {
107                        Thread.sleep(800);
108                    } catch (InterruptedException ie) {}
109
110                    // last time, set tick pos to 0
111                    if (theTickPosition>0 && theTickPosition<(theTickLengthOfSequence / 10)) {
112                        theTickPosition=(theTickLengthOfSequence / 10);
113                    }
114
115                    // 30 = 1/4 * 120, the resolution of the sequence
116                    if(Math.abs(theCurrentTickPosition - theTickPosition) > 30) {
117                        System.out.println("theCurrentTickPosition != theTickPosition!");
118                        testPassed = false;
119                    }
120                }
121
122            }
123            catch (Exception ex) { ex.printStackTrace(); }
124            if (theSequencer != null) {
125                theSequencer.close();
126            }
127            if (testPassed) {
128                System.out.println("Test Passed.");
129            }
130        }
131
132    public static void main(String[] args) throws Exception {
133        SetTickPosition thisTest = new SetTickPosition();
134        thisTest.runTest();
135        if (!testPassed) {
136            throw new Exception("Test FAILED");
137        }
138    }
139}
140