Repository navigation
Expand file tree
/
Copy pathTestCase.java
More file actions
391 lines (321 loc) · 12.5 KB
/
Copy pathTestCase.java
File metadata and controls
391 lines (321 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright (c) 2026 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import javax.sound.midi.MetaEventListener;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import vavi.apps.mfiPlayer.MfiPlayer;
import vavi.sound.mfi.MfiChip;
import vavi.sound.mfi.MfiChip.Condition;
import vavi.sound.mfi.MfiChip.Detection;
import vavi.sound.mfi.vavi.VaviMfiSynthesizer.VaviMfiReceiver;
import vavi.sound.midi.fuetrek.FuetrekSynthesizer;
import vavi.sound.midi.ma7.Ma7Synthesizer;
import vavi.sound.midi.rohm.RohmSynthesizer;
import vavi.sound.midi.ymf262.YmF262MidiDeviceProvider;
import vavi.sound.smaf.vavi.VaviSmafSynthesizer.VaviSmafReceiver;
import vavi.util.Debug;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;
import vavi.util.properties.annotation.PropsEntity.Util;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import static vavi.sound.midi.MidiUtil.volume;
/**
* TestCase.
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (nsano)
* @version 0.00 2026-03-04 nsano initial version <br>
*/
@PropsEntity(url = "file:local.properties")
public class TestCase {
static boolean localPropertiesExists() {
return Files.exists(Paths.get("local.properties"));
}
static {
System.setProperty("javax.sound.midi.Sequencer", "#Real Time Sequencer");
}
static boolean onIde = System.getProperty("vavi.test", "").equals("ide");
static long time = onIde ? 1000 * 1000 : 10 * 1000;
@Property(name = "synthesizer")
String synthesizer = "#FMF262 MIDI Synthesizer";
@Property(name = "vavi.test.volume")
double volume = 0.2f;
@Property(name = "vavi.test.volume.midi")
float midiVolume = 0.2f;
@Property
String mmf = "src/test/resources/test.mid";
@Property
String mld = "src/test/resources/test.mid";
/**
* A soundbank for the synthesizer to play, an MA-3 preset voice library (".vm3") being
* one - which gives even a rom wave voice a timbre. Nothing named means the OPL3
* (YMF262) bank, see NukedSynthesizer#SOUNDBANK_KEY.
*/
@Property
String soundbank = "";
@Property
String pianoroll = "src/test/resources/test.mid";
@BeforeEach
void setup() throws Exception {
if (localPropertiesExists()) {
Util.bind(this);
}
System.setProperty("javax.sound.midi.Synthesizer", synthesizer);
System.setProperty("vavi.sound.mobile.AudioEngine.volume", String.valueOf(volume));
// a -D of the property wins, so that the OPL3 bank can be heard without editing this
if (!soundbank.isEmpty() && System.getProperty(YmF262MidiDeviceProvider.SOUNDBANK_KEY) == null) {
System.setProperty(YmF262MidiDeviceProvider.SOUNDBANK_KEY, soundbank);
}
Debug.println("midiVolume: " + midiVolume + ", synthesizer: " + System.getProperty("javax.sound.midi.Synthesizer"));
Debug.println("adpcm volume: " + System.getProperty("vavi.sound.mobile.AudioEngine.volume"));
Debug.println("soundbank: " + System.getProperty(YmF262MidiDeviceProvider.SOUNDBANK_KEY));
}
@Test
@DisplayName("gui")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test() throws Exception {
MfiPlayer.main(new String[0]);
CountDownLatch cdl = new CountDownLatch(1);
cdl.await();
}
@Test
@DisplayName("samf: use AudioEngine receiver")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test0() throws Exception {
Debug.println(mmf);
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Debug.println("synthesizer: " + synthesizer);
Sequencer sequencer = MidiSystem.getSequencer(false);
Receiver receiver = new VaviSmafReceiver(synthesizer);
sequencer.getTransmitter().setReceiver(receiver); // use AudioEngine adpcm driver
sequencer.open();
Debug.println("sequencer: " + sequencer + ", " + sequencer.getClass().getName());
Path path = Paths.get(mmf);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
CountDownLatch cdl = new CountDownLatch(1);
MetaEventListener mel = meta -> {
Debug.println("META: " + meta.getType());
if (meta.getType() == 47) cdl.countDown();
};
sequencer.setSequence(seq);
sequencer.addMetaEventListener(mel);
Debug.println("START");
sequencer.start();
volume(receiver, midiVolume);
if (!onIde) {
Thread.sleep(time);
sequencer.stop();
Debug.println("STOP");
} else {
cdl.await();
}
Debug.println("END");
sequencer.removeMetaEventListener(mel);
sequencer.close();
synthesizer.close();
}
@Test
@DisplayName("smaf: use the original receiver")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test01() throws Exception {
Debug.println(mmf);
System.setProperty("vavi.sound.mobile.AudioEngine.disabled", "true");
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Debug.println("synthesizer: " + synthesizer);
Sequencer sequencer = MidiSystem.getSequencer(false);
Receiver receiver = synthesizer.getReceiver();
sequencer.getTransmitter().setReceiver(receiver);
sequencer.open();
Debug.println("sequencer: " + sequencer + ", " + sequencer.getClass().getName());
Path path = Paths.get(mmf);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
CountDownLatch cdl = new CountDownLatch(1);
MetaEventListener mel = meta -> {
Debug.println("META: " + meta.getType());
if (meta.getType() == 47) cdl.countDown();
};
sequencer.setSequence(seq);
sequencer.addMetaEventListener(mel);
Debug.println("START");
sequencer.start();
volume(receiver, midiVolume);
if (!onIde) {
Thread.sleep(time);
sequencer.stop();
Debug.println("STOP");
} else {
cdl.await();
}
Debug.println("END");
sequencer.removeMetaEventListener(mel);
sequencer.close();
synthesizer.close();
System.clearProperty("vavi.sound.mobile.AudioEngine.disabled");
}
@Test
@DisplayName("mfi: use AudioEngine receiver")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test1() throws Exception {
Debug.println(mld);
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Debug.println("synthesizer: " + synthesizer);
Sequencer sequencer = MidiSystem.getSequencer(false);
Receiver receiver = new VaviMfiReceiver(synthesizer);
sequencer.getTransmitter().setReceiver(receiver); // use AudioEngine adpcm driver
sequencer.open();
Debug.println("sequencer: " + sequencer + ", " + sequencer.getClass().getName());
Path path = Paths.get(mld);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
CountDownLatch cdl = new CountDownLatch(1);
MetaEventListener mel = meta -> {
Debug.println("META: " + meta.getType());
if (meta.getType() == 47) cdl.countDown();
};
sequencer.setSequence(seq);
sequencer.addMetaEventListener(mel);
Debug.println("START");
sequencer.start();
volume(receiver, midiVolume);
if (!onIde) {
Thread.sleep(time);
sequencer.stop();
Debug.println("STOP");
} else {
cdl.await();
}
Debug.println("END");
sequencer.removeMetaEventListener(mel);
sequencer.close();
synthesizer.close();
}
@Test
@DisplayName("mfi: use the original receiver")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test11() throws Exception {
Debug.println(mld);
System.setProperty("vavi.sound.mobile.AudioEngine.disabled", "true");
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Debug.println("synthesizer: " + synthesizer);
Sequencer sequencer = MidiSystem.getSequencer(false);
Receiver receiver = synthesizer.getReceiver();
sequencer.getTransmitter().setReceiver(receiver);
sequencer.open();
Debug.println("sequencer: " + sequencer + ", " + sequencer.getClass().getName());
Path path = Paths.get(mld);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
CountDownLatch cdl = new CountDownLatch(1);
MetaEventListener mel = meta -> {
Debug.println("META: " + meta.getType());
if (meta.getType() == 47) cdl.countDown();
};
sequencer.setSequence(seq);
sequencer.addMetaEventListener(mel);
Debug.println("START");
sequencer.start();
volume(receiver, midiVolume);
if (!onIde) {
Thread.sleep(time);
sequencer.stop();
Debug.println("STOP");
} else {
cdl.await();
}
Debug.println("END");
sequencer.removeMetaEventListener(mel);
sequencer.close();
synthesizer.close();
System.clearProperty("vavi.sound.mobile.AudioEngine.disabled");
}
@Test
@DisplayName("mfi: auto detection")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test3() throws Exception {
Debug.println(mld);
System.setProperty("vavi.sound.mobile.AudioEngine.disabled", "true");
Path path = Paths.get(mld);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
Condition condition = Condition.create(seq, mld);
Detection detection = MfiChip.detect(condition);
MfiChip chip = detection.chip();
Debug.print(detection.reason() + " -> " + detection.chip());
Synthesizer synthesizer = switch (chip) {
case YAMAHA -> new Ma7Synthesizer();
case FUETREK -> new FuetrekSynthesizer();
case ROHM -> new RohmSynthesizer();
};
synthesizer.open();
Debug.println("synthesizer: " + synthesizer);
Receiver receiver = synthesizer.getReceiver();
Sequencer sequencer = MidiSystem.getSequencer(false);
sequencer.getTransmitter().setReceiver(receiver);
sequencer.open();
Debug.println("sequencer: " + sequencer + ", " + sequencer.getClass().getName());
CountDownLatch cdl = new CountDownLatch(1);
MetaEventListener mel = meta -> {
Debug.println("META: " + meta.getType());
if (meta.getType() == 47) cdl.countDown();
};
sequencer.setSequence(seq);
sequencer.addMetaEventListener(mel);
Debug.println("START");
sequencer.start();
volume(receiver, midiVolume);
if (!onIde) {
Thread.sleep(time);
sequencer.stop();
Debug.println("STOP");
} else {
cdl.await();
}
Debug.println("END");
sequencer.removeMetaEventListener(mel);
sequencer.close();
synthesizer.close();
System.clearProperty("vavi.sound.mobile.AudioEngine.disabled");
}
@Test
@DisplayName("PianoRoll")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test2() throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
JFrame frame = new JFrame();
frame.addWindowFocusListener(new WindowAdapter() {
@Override public void windowClosed(WindowEvent e) {
cdl.countDown();
}
});
Path path = Paths.get(pianoroll);
Sequence seq = MidiSystem.getSequence(new BufferedInputStream(Files.newInputStream(path)));
PianoRollPane pianoRoll = new PianoRollPane(seq);
pianoRoll.setPreferredSize(new Dimension(640, 400));
JScrollPane sp = new JScrollPane(pianoRoll);
frame.getContentPane().add(sp);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // TODO kills test and window listener doesn't work
cdl.await();
}
}