Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; // required for date / time function
- import javax.sound.midi.*; // required to play musical notes
- public class Chimes {
- /**
- * Program: Chimes.java
- * Purpose: Play musical chimes every quarter hour
- * Creator: Chris Clarke
- * Created: 28.06.2009
- */
- public static void playSequence(String s) {
- int duration;
- for (int i=0; i<4; i++) {
- if (i==3)
- duration = 12;
- else
- duration = 6;
- if (s.charAt(i)=='G')
- // parameters are voice (0-15),pitch,duration,volume
- np.playNote(VOICE,G,duration,VOLUME);
- if (s.charAt(i)=='C')
- np.playNote(VOICE,C,duration,VOLUME);
- if (s.charAt(i)=='D')
- np.playNote(VOICE,D,duration,VOLUME);
- if (s.charAt(i)=='E')
- np.playNote(VOICE,E,duration,VOLUME);
- }
- } // end playSequence
- public static void playAChime(int hour, int min) {
- if (min==15&&!played) {
- playSequence(SEQUENCE0);
- played = true;
- } else if (min==30&&!played) {
- playSequence(SEQUENCE1);
- playSequence(SEQUENCE2);
- played = true;
- } else if (min==45&&!played) {
- playSequence(SEQUENCE3);
- playSequence(SEQUENCE4);
- playSequence(SEQUENCE0);
- played = true;
- } else if (min==0&&!played) {
- playSequence(SEQUENCE1);
- playSequence(SEQUENCE2);
- playSequence(SEQUENCE3);
- playSequence(SEQUENCE4);
- for (int i=0; i<hour; i++)
- np.playNote(VOICE, G1, 12, VOLUME); // play hour
- played = true;
- }
- if (min==17 || min==32 || min==47 || min==2)
- played=false;
- } // end playAChime
- public static void main(String[] args) {
- np.loadInstr(); // load instruments
- GregorianCalendar now;
- do {
- now = new GregorianCalendar();
- playAChime(now.get(Calendar.HOUR), now.get(Calendar.MINUTE));
- } while (true);
- } // end main
- public static NotePlayer np = new NotePlayer();
- public static final int VOICE = 5;
- public static final int VOLUME = 500;
- public static final int C = 72;
- public static final int G = C-5;
- public static final int D = C+2;
- public static final int E = C+4;
- public static final int G1 = G-12;
- public static boolean played = false;
- public static final String SEQUENCE0 = "EDCG";
- public static final String SEQUENCE1 = "CEDG";
- public static final String SEQUENCE2 = "CDEC";
- public static final String SEQUENCE3 = "ECDG";
- public static final String SEQUENCE4 = "GDEC";
- }
- class NotePlayer {
- /**
- * Program: NotePlayer.java
- * Purpose: Simple musical note player, plays a single note.
- * Creator: Chris Clarke
- * Created: July 2007
- */
- MidiChannel[] mc;
- Synthesizer synth;
- Instrument[] instr;
- int tmpo = 500; // fixed tempo of 120 bpm
- public void loadInstr() {
- // only necessary to run this method once
- for (int voice=0; voice<16; voice++) {
- try {
- synth = MidiSystem.getSynthesizer();
- synth.open();
- mc = new MidiChannel[16];
- mc = synth.getChannels();
- instr = synth.getDefaultSoundbank().getInstruments();
- synth.loadInstrument(instr[voice]); // 0-15
- }
- catch (Exception e) {System.out.println("Caught exception");}
- }
- } // end loadInstr()
- public void playNote(int voice, int pitch, int duration, int vol) {
- // play note
- mc[voice].noteOn(pitch, vol);
- for (int i=0; i<duration; i++) {
- try {
- Thread.sleep(tmpo/4); // pause for one semiquaver
- } catch (InterruptedException e) {}
- }
- // stop playing it
- mc[voice].noteOff(pitch, vol);
- } // end playNote(int, int, int, int)
- public void playNote(int voice, int pitch, int vol) {
- // play note for natural duration of note or sound
- mc[voice].noteOn( pitch, vol);
- } // end playNote(int, int, int)
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement