Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Sequencer/MetaCallback.java
38853 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.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* @run main/othervm MetaCallback39*/40public class MetaCallback implements MetaEventListener {4142static ShortMessage MidiMsg3(int a, int b, int c) {43try {44ShortMessage msg = new ShortMessage();45msg.setMessage((byte)a,(byte)b,(byte)c);46return msg;47} catch(InvalidMidiDataException ex) {48throw new RuntimeException();49}50}5152//Synthesizer synth;53Instrument[] instruments;54Sequencer sequencer;55Sequence sequence;56Track track;5758public static int TOTAL_COUNT = 100;5960int metaCount = 0;61boolean finished = false;6263MetaCallback() throws Exception {6465sequencer=MidiSystem.getSequencer();66sequence=new Sequence(Sequence.PPQ,240);67track=sequence.createTrack();68sequencer.addMetaEventListener(this);6970byte[] data = new byte[1];7172track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));73track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));74int c;75for(c=0; c < TOTAL_COUNT; c++) {76data[0]=(byte)(c+1);77MetaMessage meta = new MetaMessage();78meta.setMessage(1, data, 1); // type, data, length79track.add(new MidiEvent(meta,c*20));80}81track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),c*20));82track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),c*20 + 10));8384sequencer.setSlaveSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);85sequencer.setMasterSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);86sequencer.open();87sequencer.setSequence(sequence);88sequencer.setTempoInBPM(100);89System.out.println("Starting playback...");90this.start();91while (!finished && sequencer.getTickPosition() < sequencer.getTickLength()) {92System.out.println("Tick "+sequencer.getTickPosition()+"...");93Thread.sleep(1000);94}95System.out.println("Stopping playback...");96this.stop();97if (metaCount != TOTAL_COUNT) {98throw new Exception("Expected "+TOTAL_COUNT+" callbacks, but got "+metaCount+"!");99}100}101void start() {sequencer.start();}102void stop() {sequencer.stop();}103104public void meta(MetaMessage msg) {105System.out.println(""+metaCount+": got "+msg);106if (msg.getType() == 0x2F) {107finished = true;108} else if (msg.getData().length > 0 && msg.getType() == 1) {109metaCount++;110}111}112113public static void main(String[] argv) throws Exception {114if (hasSequencer()) {115new MetaCallback();116System.out.println("Test passed");117}118}119120static boolean hasSequencer() {121try {122Sequencer seq = MidiSystem.getSequencer();123if (seq != null) {124seq.open();125seq.close();126return true;127}128} catch (Exception e) {}129System.out.println("No sequencer available! Cannot execute test.");130return false;131}132}133134135