summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java')
-rw-r--r--Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java
new file mode 100644
index 0000000..c028c8b
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file