Path: blob/master/test/jdk/javax/sound/midi/Sequencer/TickLength.java
41155 views
/*1* Copyright (c) 2002, 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.MidiEvent;27import javax.sound.midi.MidiSystem;28import javax.sound.midi.Sequence;29import javax.sound.midi.Sequencer;30import javax.sound.midi.ShortMessage;31import javax.sound.midi.Track;3233/**34* @test35* @bug 442789036* @run main/othervm TickLength37* @summary Sequencer.getTickLength() and Sequence.getTickLength() report the38* wrong length39*/40public class TickLength implements MetaEventListener {41private Sequence theSequence;42private Sequencer theSequencer;4344public TickLength() {45this.initMidiCompoments();46System.out.println("Got Sequencer "+theSequencer);47theSequence = this.generateSequence();48try {49theSequencer.setSequence(theSequence);50}51catch(Exception e) {52System.out.println(this.getClass()+"\tCannot set sequence to sequencer ("+e+")");53return;54}55}5657public void start() {58theSequencer.start();59}6061/*62instantiate the necessary midi components63*/64private boolean initMidiCompoments() {656667try {68theSequencer = MidiSystem.getSequencer();69}70catch(Exception e) {71System.out.println(this.getClass()+"\tSequencer Device not supported"+e+")");72return false;73}7475try {76theSequencer.open();77}78catch(Exception e) {79System.out.println(this.getClass()+"Cannot open Sequencer Device");80return false;81}82if(!theSequencer.addMetaEventListener(this)) {83System.out.println(this.getClass()+"\tCould not register MetaEventListener - there will be problems with scrolling! ");84return false;85}86return true;87}8889static int lastTick = 0;9091private Sequence generateSequence() {92MidiEvent dummyMidiEvent;93ShortMessage dummyShortMessage;94Sequence dummySequence = null;95Track[] allTracks ;96Track theTrack;9798try {99dummySequence = new Sequence(Sequence.PPQ,1500);100}101catch(InvalidMidiDataException e) {102System.out.println("O o "+e);103}104105dummySequence.createTrack();106allTracks = dummySequence.getTracks();107theTrack = allTracks[0];108lastTick = 0;109for(int i=0;i<20; i++) {110theTrack.add(this.createShortMidiEvent(ShortMessage.NOTE_ON, 2, 30+i, 100,100+1000*i));111theTrack.add(this.createMetaMidiEvent(1,"start",100+1000*i));112lastTick = (1000*i)+600;113theTrack.add(this.createShortMidiEvent(ShortMessage.NOTE_OFF, 2, 30+i, 100, lastTick));114theTrack.add(this.createMetaMidiEvent(1,"end",lastTick));115}116117return dummySequence;118}119120/*121A method to create a short midi event (sound)122*/123124public MidiEvent createShortMidiEvent(int theCommand, int theChannel, int theData1, int theData2, long theTime) {125ShortMessage dummyShortMessage;126MidiEvent dummyMidiEvent;127128try {129dummyShortMessage = new ShortMessage();130dummyShortMessage.setMessage(theCommand, theChannel, theData1, theData2);131dummyMidiEvent = new MidiEvent(dummyShortMessage,theTime);132}133catch (Exception e) {134System.out.println(this.getClass()+"\t"+e);135return null;136}137138return dummyMidiEvent;139}140141/*142A method to create a meta midi event (used in meta() method)143*/144public MidiEvent createMetaMidiEvent(int theType, String theData1, long theTime) {145MetaMessage dummyMetaMessage;146MidiEvent dummyMidiEvent;147148try {149dummyMetaMessage = new MetaMessage();150dummyMetaMessage.setMessage(theType, theData1.getBytes(), theData1.length());151dummyMidiEvent = new MidiEvent(dummyMetaMessage,theTime);152}153catch (Exception e) {154System.out.println(e);155return null;156}157158return dummyMidiEvent;159}160161/*162the method is activated by each meta midi event163it puts out the actual tick position, as well as the WRONG total tick length and the RIGHT164tick length using the work around by dividing the total length by 64165*/166public void meta(MetaMessage p1) {167if(p1.getType() ==47) {168return;169}170System.out.println("getTickPosition:\t"+theSequencer.getTickPosition()171+"\t Sequencer.getTickLength:\t"+theSequencer.getTickLength()172+"\tReal Length:\t"+lastTick173+"\t Sequence.getTickLength:\t"+theSequence.getTickLength()174//(theSequencer.getTickLength()/64));175);176}177178public void checkLengths() throws Exception {179System.out.println("Sequencer.getTickLength() = "+theSequencer.getTickLength());180System.out.println("Sequence.getTickLength() = "+theSequence.getTickLength());181long diff = theSequencer.getTickLength() - theSequence.getTickLength();182if (diff > 100 || diff < -100) {183throw new Exception("Difference too large! Failed.");184}185System.out.println("Passed");186}187188public static void main(String[] args) throws Exception {189if (!hasSequencer()) {190return;191}192TickLength tlt = new TickLength();193//tlt.start();194tlt.checkLengths();195}196197static boolean hasSequencer() {198try {199Sequencer seq = MidiSystem.getSequencer();200if (seq != null) {201seq.open();202seq.close();203return true;204}205} catch (Exception e) {}206System.out.println("No sequencer available! Cannot execute test.");207return false;208}209210}211212213