Path: blob/master/test/jdk/javax/sound/midi/Sequence/GetMicrosecondLength.java
41152 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.MetaMessage;25import javax.sound.midi.MidiEvent;26import javax.sound.midi.Sequence;27import javax.sound.midi.ShortMessage;28import javax.sound.midi.Track;2930/**31* @test32* @bug 492995533* @summary Sequence.getMicrosecondLength() returns wrong value34*/35public class GetMicrosecondLength {3637public static boolean failed = false;38//private static Sequencer seq = null;3940public static void main(String[] args) throws Exception {41/*42try {43seq = MidiSystem.getSequencer();44} catch (Exception e) {45e.printStackTrace();46}47*/48for (int sec = 1; sec < 10; sec += 4) {49for (int tempo=0; tempo < 1000; tempo+=120) {50for (int resolution=1; resolution < 480; ) {51testSequence(sec, tempo, resolution);52if (resolution == 1) {53resolution = 120;54} else {55resolution += 120;56}57}58}59}60if (failed) throw new Exception("Test FAILED!");61out("Test Passed.");62}6364/**65* Create a new Sequence for testing.66*/67private static void testSequence(int lengthInSeconds, int tempoInBPM, int resolution) {68Sequence sequence = null;69long lengthInMicroseconds = lengthInSeconds * 1000000;70boolean createTempoEvent = true;71if (tempoInBPM == 0) {72tempoInBPM = 120;73createTempoEvent = false;74System.out.print("Creating sequence: "+lengthInSeconds+"sec, "75+"resolution="+resolution+" ticks/beat...");76} else {77System.out.print("Creating sequence: "+lengthInSeconds+"sec, "78+tempoInBPM+" beats/min, "79+"resolution="+resolution+" ticks/beat...");80}81//long lengthInTicks = (lengthInMicroseconds * resolution) / tempoInBPM;82long lengthInTicks = (lengthInMicroseconds * tempoInBPM * resolution) / 60000000l;83//out("expected length in ticks: " + lengthInTicks);84try {85sequence = new Sequence(Sequence.PPQ, resolution);86Track track = sequence.createTrack();87if (createTempoEvent) {88int tempoInMPQ = (int) (60000000l / tempoInBPM);89MetaMessage tm = new MetaMessage();90byte[] msg = new byte[3];91msg[0] = (byte) (tempoInMPQ >> 16);92msg[1] = (byte) ((tempoInMPQ >> 8) & 0xFF);93msg[2] = (byte) (tempoInMPQ & 0xFF);9495tm.setMessage(0x51 /* Meta Tempo */, msg, msg.length);96track.add(new MidiEvent(tm, 0));97//out("regtest: tempoInMPQ="+tempoInMPQ);98//out("Added tempo event: new size="+track.size());99}100ShortMessage mm = new ShortMessage();101mm.setMessage(0xF6, 0, 0);102MidiEvent me = new MidiEvent(mm, lengthInTicks);103track.add(me);104//out("Added realtime event: new size="+track.size());105} catch (InvalidMidiDataException e) {106out(e);107}108boolean thisFailed = false;109long actualLengthInTicks = sequence.getTickLength();110// allow +/- 5%111if (Math.abs(actualLengthInTicks - lengthInTicks) > lengthInTicks / 20) {112out("FAILED:");113out(" expected length in ticks: " + lengthInTicks);114out(" actual length in ticks : " + actualLengthInTicks);115thisFailed = true;116}117long actualLengthInUs = sequence.getMicrosecondLength();118// allow +/- 5%119if (Math.abs(actualLengthInUs - lengthInMicroseconds) > lengthInMicroseconds / 20) {120if (!thisFailed) {121out("FAILED:");122}123out(" expected length in microsecs: " + lengthInMicroseconds);124out(" actual length in microsecs : " + actualLengthInUs);125thisFailed = true;126}127if (!thisFailed) {128out("OK");129}130/*if (seq != null) {131try {132seq.setSequence(sequence);133out("Sequencer tempo="+seq.getTempoInBPM());134} catch (Exception e) {135e.printStackTrace();136}137}138*/139failed |= thisFailed;140}141142143private static void out(Throwable t) {144t.printStackTrace(System.out);145}146147private static void out(String message) {148System.out.println(message);149}150}151152153