Path: blob/master/test/jdk/javax/sound/midi/Sequencer/MetaCallback.java
41155 views
/*1* Copyright (c) 2003, 2017, 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.Instrument;24import javax.sound.midi.InvalidMidiDataException;25import javax.sound.midi.MetaEventListener;26import javax.sound.midi.MetaMessage;27import javax.sound.midi.MidiEvent;28import javax.sound.midi.MidiSystem;29import javax.sound.midi.Sequence;30import javax.sound.midi.Sequencer;31import javax.sound.midi.ShortMessage;32import javax.sound.midi.Track;3334/**35* @test36* @bug 434713537* @summary MIDI MetaMessage callback inconsistent38* @key intermittent39* @run main/othervm MetaCallback40*/41public class MetaCallback implements MetaEventListener {4243static ShortMessage MidiMsg3(int a, int b, int c) {44try {45ShortMessage msg = new ShortMessage();46msg.setMessage((byte)a,(byte)b,(byte)c);47return msg;48} catch(InvalidMidiDataException ex) {49throw new RuntimeException();50}51}5253//Synthesizer synth;54Instrument[] instruments;55Sequencer sequencer;56Sequence sequence;57Track track;5859public static int TOTAL_COUNT = 100;6061int metaCount = 0;62boolean finished = false;6364MetaCallback() throws Exception {6566sequencer=MidiSystem.getSequencer();67sequence=new Sequence(Sequence.PPQ,240);68track=sequence.createTrack();69sequencer.addMetaEventListener(this);7071byte[] data = new byte[1];7273track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));74track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));75int c;76for(c=0; c < TOTAL_COUNT; c++) {77data[0]=(byte)(c+1);78MetaMessage meta = new MetaMessage();79meta.setMessage(1, data, 1); // type, data, length80track.add(new MidiEvent(meta,c*20));81}82track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),c*20));83track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),c*20 + 10));8485sequencer.setSlaveSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);86sequencer.setMasterSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);87sequencer.open();88sequencer.setSequence(sequence);89sequencer.setTempoInBPM(100);90System.out.println("Starting playback...");91this.start();92while (!finished && sequencer.getTickPosition() < sequencer.getTickLength()) {93System.out.println("Tick "+sequencer.getTickPosition()+"...");94Thread.sleep(1000);95}96System.out.println("Stopping playback...");97this.stop();98if (metaCount != TOTAL_COUNT) {99throw new Exception("Expected "+TOTAL_COUNT+" callbacks, but got "+metaCount+"!");100}101}102void start() {sequencer.start();}103void stop() {sequencer.stop();}104105public void meta(MetaMessage msg) {106System.out.println(""+metaCount+": got "+msg);107if (msg.getType() == 0x2F) {108finished = true;109} else if (msg.getData().length > 0 && msg.getType() == 1) {110metaCount++;111}112}113114public static void main(String[] argv) throws Exception {115if (hasSequencer()) {116new MetaCallback();117System.out.println("Test passed");118}119}120121static boolean hasSequencer() {122try {123Sequencer seq = MidiSystem.getSequencer();124if (seq != null) {125seq.open();126seq.close();127return true;128}129} catch (Exception e) {}130System.out.println("No sequencer available! Cannot execute test.");131return false;132}133}134135136