From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/Listing4902.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing4902.java') 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 -- cgit v1.2.3