Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jmms/MSCycle.java


1   /*
2   <This Java Class is part of the jMusic API version 1.4, February 2003.>
3   
4   Copyright (C) 2000 Andrew Sorensen & Andrew Brown
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or any
9   later version.
10  This program is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14  You should have received a copy of the GNU General Public Licens
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18  
19  package jmms;
20  
21  import jm.music.data.*;
22  import java.awt.*;
23  import java.awt.event.ActionEvent;
24  
25  /**
26   * MSCycle.java
27   *  This class is for making it easy to play looped music via MIDI in realtime.
28   *  You just give it a score and it keeps on looping.
29   *  If you want it to change the looped music, then you give it a different score.
30   *  The tempo can be changed independently of the score.
31   * Created on 11 March 2003, 14:17
32   * @author RenŽ Wooller and Andrew Brown
33   */
34  public class MSCycle extends Thread{
35      
36      private double tempo = 120.0;
37      private Score score;
38      // MIDIshare sequencer object
39      private Sequencer seq = new Sequencer();
40      // Looping flag
41      private boolean playing = true;
42      
43      /**
44       *  to create an MSCycle with the default score of a one beat long note
45       */
46      public MSCycle() {
47          this(new Score(new Part(new Phrase(new Note(60, 1.0, 120)))));
48      }
49      
50      /** Creates a new instance of MSCycle, with a specific score for it to 
51       *  play.
52       * @param scoreToSet The jMusic Score to be looped.
53       */
54      public MSCycle(Score scoreToSet) {
55         this(scoreToSet, true);
56      }
57      
58      /** Creates a new instance of MSCycle, with a specific score for it to 
59       *  play.
60       * @param scoreToSet The jMusic Score to be looped.
61       * @param playing Wheather or not the score should start immediatly.
62       */
63      public MSCycle(Score scoreToSet, boolean playing) {
64          this.playing = playing;
65          setScore(scoreToSet);
66          tempo = score.getTempo();
67          this.setPriority(Thread.MAX_PRIORITY - 1);
68          this.start();
69      }
70  
71      /** To run the thread. */
72      public void run() {
73          while (playing) {    
74              seq.play(score);       
75              try { 
76                  Thread.sleep((int)(1000 * score.getEndTime() * 60.0/tempo)); 
77              } catch( InterruptedException e) {}                             
78          }
79      }
80      
81      /** Use this to set the new score. */
82      public void setScore(Score toSet) {
83          score = toSet.copy();
84      }
85      
86      /** 
87      * Use this to set the tempo. 
88      * @param newTempo The tempo for subsequent repeats.
89      */
90      public void setTempo(double newTempo) {
91          tempo = newTempo;
92          seq.setTempo(tempo);
93      }
94      
95      /**
96      * Halt the score looping.
97      */
98      public void stopPlayback() {
99          playing = false;
100     }
101     
102     /**
103     * Start or Restart the score looping.
104     */
105     public void startPlayback() {
106         playing = true;
107     }   
108 }
109