SMPTEDuration.java revision 16009:5445b9413d9d
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
24import javax.sound.midi.InvalidMidiDataException;
25import javax.sound.midi.MidiEvent;
26import javax.sound.midi.Sequence;
27import javax.sound.midi.ShortMessage;
28import javax.sound.midi.Track;
29
30/**
31 * @test
32 * @bug 4702328
33 * @summary Wrong time in sequence for SMPTE based types
34 */
35public class SMPTEDuration {
36
37    public static void main(String args[]) throws Exception {
38         int[][] dataMes =  { {ShortMessage.NOTE_ON, 10, 0x24, 0x50} ,
39                 { ShortMessage.NOTE_OFF, 10, 0x24, 0x44 },
40                 { ShortMessage.NOTE_ON, 10, 0x24, 0x50 },
41                 { ShortMessage.NOTE_ON, 10, 0x26, 0x50 },
42                 { ShortMessage.NOTE_OFF, 10, 0x26, 0x53 } };
43         long[] ticks = { 0, 68, 240, 240, 286};
44         int res = 240;
45         ShortMessage msg;
46         Sequence midiData = null;
47         Track track;
48         boolean failed = false;
49
50
51         try {
52             midiData = new Sequence(Sequence.SMPTE_24 , res);
53         } catch (InvalidMidiDataException invMidiEx) {
54             invMidiEx.printStackTrace(System.out);
55             System.out.println("Unexpected InvalidMidiDataException: "
56                     + invMidiEx.getMessage());
57             failed = true;
58         }
59         track = midiData.createTrack();
60         for (int i = 0; i < dataMes.length; i++) {
61             msg = new ShortMessage();
62             try {
63                 msg.setMessage(dataMes[i][0], dataMes[i][1], dataMes[i][2],
64                         dataMes[i][3]);
65             } catch (InvalidMidiDataException invMidiEx) {
66                 invMidiEx.printStackTrace(System.out);
67                 System.out.println("Unexpected InvalidMidiDataException: "
68                         + invMidiEx.getMessage());
69                 failed = true;
70             }
71             track.add(new MidiEvent(msg, ticks[i]));
72         }
73         //   lengthInMs = (tickLength*1000000)/(divType*Res)
74         long micros = (long) ((midiData.getTickLength() * 1000000) / (res * Sequence.SMPTE_24));
75         if (midiData.getMicrosecondLength() != micros) {
76             failed = true;
77             System.out.println("getMicrosecondLength() returns wrong length: "
78                     + midiData.getMicrosecondLength());
79             System.out.println("getMicrosecondLength() must return length: "
80                     + micros);
81         }
82         if (midiData.getTickLength() != 286) {
83             failed = true;
84             System.out.println("getTickLength() returns wrong length: "
85                     + midiData.getTickLength());
86         }
87
88         if( failed == true ) {
89             throw new Exception("test failed");
90         } else {
91             System.out.println("Passed.");
92         }
93    }
94}
95