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
|
/* Listing4902.java */
import javax.sound.midi.*;
public class Listing4902
{
private static void playAlleMeineEntchen()
throws Exception
{
//Partitur {{Tonhoehe, DauerInViertelNoten, AnzahlWdh},...}
final int DATA[][] = {
{60, 1, 1}, //C
{62, 1, 1}, //D
{64, 1, 1}, //E
{65, 1, 1}, //F
{67, 2, 2}, //G,G
{69, 1, 4}, //A,A,A,A
{67, 4, 1}, //G
{69, 1, 4}, //A,A,A,A
{67, 4, 1}, //G
{65, 1, 4}, //F,F,F,F
{64, 2, 2}, //E,E
{62, 1, 4}, //D,D,D,D
{60, 4, 1} //C
};
//Synthesizer �ffnen und Receiver holen
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
Receiver rcvr = synth.getReceiver();
//Melodie spielen
ShortMessage msg = new ShortMessage();
for (int i = 0; i < DATA.length; ++i) {
for (int j = 0; j < DATA[i][2]; ++j) { //Anzahl Wdh. je Note
//Note an
msg.setMessage(ShortMessage.NOTE_ON, 0, DATA[i][0], 64);
rcvr.send(msg, -1);
//Pause
try {
Thread.sleep(DATA[i][1] * 400);
} catch (Exception e) {
//nothing
}
//Note aus
msg.setMessage(ShortMessage.NOTE_OFF, 0, DATA[i][0], 0);
rcvr.send(msg, -1);
}
}
//Synthesizer schlie�en
synth.close();
}
public static void main(String[] args)
{
try {
playAlleMeineEntchen();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
}
|