Advertisement
brilliant_moves

Chimes.java

Oct 17th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.62 KB | None | 0 0
  1. import java.util.*;     // required for date / time function
  2. import javax.sound.midi.*;  // required to play musical notes
  3.  
  4. public class Chimes {
  5.  
  6.     /**
  7.     *   Program:    Chimes.java
  8.     *   Purpose:    Play musical chimes every quarter hour
  9.     *   Creator:    Chris Clarke
  10.     *   Created:    28.06.2009
  11.     */
  12.  
  13.     public static void playSequence(String s) {
  14.         int duration;
  15.         for (int i=0; i<4; i++) {
  16.             if (i==3)
  17.                 duration = 12;
  18.             else
  19.                 duration = 6;
  20.             if (s.charAt(i)=='G')
  21.                 // parameters are voice (0-15),pitch,duration,volume
  22.                 np.playNote(VOICE,G,duration,VOLUME);
  23.             if (s.charAt(i)=='C')
  24.                 np.playNote(VOICE,C,duration,VOLUME);
  25.             if (s.charAt(i)=='D')
  26.                 np.playNote(VOICE,D,duration,VOLUME);
  27.             if (s.charAt(i)=='E')
  28.                 np.playNote(VOICE,E,duration,VOLUME);
  29.         }
  30.     } // end playSequence
  31.  
  32.     public static void playAChime(int hour, int min) {
  33.         if (min==15&&!played) {
  34.             playSequence(SEQUENCE0);
  35.             played = true;
  36.         } else if (min==30&&!played) {
  37.             playSequence(SEQUENCE1);
  38.             playSequence(SEQUENCE2);
  39.             played = true;
  40.         } else if (min==45&&!played) {
  41.             playSequence(SEQUENCE3);
  42.             playSequence(SEQUENCE4);
  43.             playSequence(SEQUENCE0);
  44.             played = true;
  45.         } else if (min==0&&!played) {
  46.             playSequence(SEQUENCE1);
  47.             playSequence(SEQUENCE2);
  48.             playSequence(SEQUENCE3);
  49.             playSequence(SEQUENCE4);
  50.             for (int i=0; i<hour; i++)
  51.                 np.playNote(VOICE, G1, 12, VOLUME); // play hour
  52.             played = true;
  53.         }
  54.         if (min==17 || min==32 || min==47 || min==2)
  55.             played=false;
  56.     } // end playAChime
  57.  
  58.     public static void main(String[] args) {
  59.         np.loadInstr(); // load instruments
  60.         GregorianCalendar now;
  61.         do {
  62.             now = new GregorianCalendar();
  63.             playAChime(now.get(Calendar.HOUR), now.get(Calendar.MINUTE));
  64.         } while (true);
  65.     } // end main
  66.  
  67.     public static NotePlayer np = new NotePlayer();
  68.     public static final int VOICE = 5;
  69.     public static final int VOLUME = 500;
  70.  
  71.     public static final int C = 72;
  72.     public static final int G = C-5;
  73.     public static final int D = C+2;
  74.     public static final int E = C+4;
  75.     public static final int G1 = G-12;
  76.  
  77.     public static boolean played = false;
  78.  
  79.     public static final String SEQUENCE0 = "EDCG";
  80.     public static final String SEQUENCE1 = "CEDG";
  81.     public static final String SEQUENCE2 = "CDEC";
  82.     public static final String SEQUENCE3 = "ECDG";
  83.     public static final String SEQUENCE4 = "GDEC";
  84. }
  85.  
  86. class NotePlayer {
  87.  
  88.     /**
  89.     *   Program: NotePlayer.java
  90.     *   Purpose: Simple musical note player, plays a single note.
  91.     *   Creator: Chris Clarke
  92.     *   Created: July 2007
  93.     */
  94.  
  95.     MidiChannel[]   mc;
  96.     Synthesizer     synth;
  97.     Instrument[]    instr;
  98.     int     tmpo = 500; // fixed tempo of 120 bpm
  99.  
  100.     public void loadInstr() {
  101.         // only necessary to run this method once
  102.         for (int voice=0; voice<16; voice++) {
  103.             try {
  104.                 synth = MidiSystem.getSynthesizer();
  105.                 synth.open();
  106.        
  107.                 mc = new MidiChannel[16];
  108.                 mc = synth.getChannels();
  109.                 instr = synth.getDefaultSoundbank().getInstruments();  
  110.                 synth.loadInstrument(instr[voice]); // 0-15
  111.             }
  112.             catch (Exception e) {System.out.println("Caught exception");}
  113.         }
  114.     } // end loadInstr()
  115.  
  116.     public void playNote(int voice, int pitch, int duration, int vol) {
  117.         // play note
  118.         mc[voice].noteOn(pitch, vol);
  119.         for (int i=0; i<duration; i++) {
  120.             try {
  121.                     Thread.sleep(tmpo/4); // pause for one semiquaver
  122.             } catch (InterruptedException e) {}
  123.         }
  124.         // stop playing it
  125.         mc[voice].noteOff(pitch, vol);
  126.     } // end playNote(int, int, int, int)
  127.  
  128.     public void playNote(int voice, int pitch, int vol) {
  129.         // play note for natural duration of note or sound
  130.         mc[voice].noteOn( pitch, vol);
  131.     } // end playNote(int, int, int)
  132. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement