Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/midi/Sequencer/MetaCallback.java
41155 views
1
/*
2
* Copyright (c) 2003, 2017, 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
24
import javax.sound.midi.Instrument;
25
import javax.sound.midi.InvalidMidiDataException;
26
import javax.sound.midi.MetaEventListener;
27
import javax.sound.midi.MetaMessage;
28
import javax.sound.midi.MidiEvent;
29
import javax.sound.midi.MidiSystem;
30
import javax.sound.midi.Sequence;
31
import javax.sound.midi.Sequencer;
32
import javax.sound.midi.ShortMessage;
33
import javax.sound.midi.Track;
34
35
/**
36
* @test
37
* @bug 4347135
38
* @summary MIDI MetaMessage callback inconsistent
39
* @key intermittent
40
* @run main/othervm MetaCallback
41
*/
42
public class MetaCallback implements MetaEventListener {
43
44
static ShortMessage MidiMsg3(int a, int b, int c) {
45
try {
46
ShortMessage msg = new ShortMessage();
47
msg.setMessage((byte)a,(byte)b,(byte)c);
48
return msg;
49
} catch(InvalidMidiDataException ex) {
50
throw new RuntimeException();
51
}
52
}
53
54
//Synthesizer synth;
55
Instrument[] instruments;
56
Sequencer sequencer;
57
Sequence sequence;
58
Track track;
59
60
public static int TOTAL_COUNT = 100;
61
62
int metaCount = 0;
63
boolean finished = false;
64
65
MetaCallback() throws Exception {
66
67
sequencer=MidiSystem.getSequencer();
68
sequence=new Sequence(Sequence.PPQ,240);
69
track=sequence.createTrack();
70
sequencer.addMetaEventListener(this);
71
72
byte[] data = new byte[1];
73
74
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));
75
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));
76
int c;
77
for(c=0; c < TOTAL_COUNT; c++) {
78
data[0]=(byte)(c+1);
79
MetaMessage meta = new MetaMessage();
80
meta.setMessage(1, data, 1); // type, data, length
81
track.add(new MidiEvent(meta,c*20));
82
}
83
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),c*20));
84
track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),c*20 + 10));
85
86
sequencer.setSlaveSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
87
sequencer.setMasterSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
88
sequencer.open();
89
sequencer.setSequence(sequence);
90
sequencer.setTempoInBPM(100);
91
System.out.println("Starting playback...");
92
this.start();
93
while (!finished && sequencer.getTickPosition() < sequencer.getTickLength()) {
94
System.out.println("Tick "+sequencer.getTickPosition()+"...");
95
Thread.sleep(1000);
96
}
97
System.out.println("Stopping playback...");
98
this.stop();
99
if (metaCount != TOTAL_COUNT) {
100
throw new Exception("Expected "+TOTAL_COUNT+" callbacks, but got "+metaCount+"!");
101
}
102
}
103
void start() {sequencer.start();}
104
void stop() {sequencer.stop();}
105
106
public void meta(MetaMessage msg) {
107
System.out.println(""+metaCount+": got "+msg);
108
if (msg.getType() == 0x2F) {
109
finished = true;
110
} else if (msg.getData().length > 0 && msg.getType() == 1) {
111
metaCount++;
112
}
113
}
114
115
public static void main(String[] argv) throws Exception {
116
if (hasSequencer()) {
117
new MetaCallback();
118
System.out.println("Test passed");
119
}
120
}
121
122
static boolean hasSequencer() {
123
try {
124
Sequencer seq = MidiSystem.getSequencer();
125
if (seq != null) {
126
seq.open();
127
seq.close();
128
return true;
129
}
130
} catch (Exception e) {}
131
System.out.println("No sequencer available! Cannot execute test.");
132
return false;
133
}
134
}
135
136