/Users/lyon/j4p/src/sound/musica/Utils.java

1    package sound.musica; 
2     
3    /* 
4     * Open Source Software by http://www.Docjava.com 
5     * programmer: D. Lyon 
6     * e-mail: lyon@docjava.com 
7     * Date: Apr 29, 2002 
8     * Time: 12:35:09 PM 
9     */ 
10    
11   import javax.sound.midi.*; 
12    
13   public class Utils { 
14       private static MidiChannel channels[] = null; 
15       private static Synthesizer synth = null; 
16       public static final String[] instruments = 
17               {"Acoustic bass drum", "Bass drum 1", "Side stick", "Acoustic snare", 
18                "Hand clap", "Electric snare", "Low floor tom", "Closed hi-hat", 
19                "High floor tom", "Pedal hi-hat", "Low tom", "Open hi-hat", 
20                "Low-mid tom", "Hi-mid tom", "Crash cymbal 1", "High tom", 
21                "Ride cymbal 1", "Chinese cymbal", "Ride bell", "Tambourine", 
22                "Splash cymbal", "Cowbell", "Crash cymbal 2", "Vibraslap", 
23                "Ride cymbal 2", "Hi bongo", "Low bongo", "Mute hi conga", 
24                "Open hi conga", "Low conga", "High timbale", "Low timbale", 
25                "High agogo", "Low agogo", "Cabasa", "Maracas", 
26                "Short whistle", "Long whistle", "Short guiro", "Long guiro", 
27                "Claves", "Hi wood block", "Low wood block", "Mute cuica", 
28                "Open cuica", "Mute triangle", "Open triangle"}; 
29    
30       public static void play(int ia[], int vel, int dur) { 
31           for (int i = 0; i < ia.length; i++) 
32               play(ia[i], vel, dur); 
33       } 
34    
35       public static void playThread( 
36               final int ia[], 
37               final int vel, 
38               final int dur) { 
39           Runnable r = new Runnable() { 
40               public void run() { 
41                   for (int i = 0; i < ia.length; i++) 
42                       play(ia[i], vel, dur); 
43    
44               } 
45           }; 
46           Thread t = new Thread(r); 
47           t.start(); 
48       } 
49    
50       public static void play(int ia[], int dur) { 
51           for (int i = 0; i < ia.length; i++) 
52               play(ia[i], 127, dur); 
53       } 
54    
55       public static int[] transpose(int ia[], int bias) { 
56           int ans[] = new int[ia.length]; 
57           for (int i = 0; i < ans.length; i++) 
58               ans[i] = ia[i] + bias; 
59           return ans; 
60       } 
61    
62       public static void print(int ia[]) { 
63           for (int i = 0; i < ia.length; i++) 
64               System.out.println(ia[i]); 
65       } 
66    
67       public static void play(int nn) { 
68           play(nn, 127, 20); 
69       } 
70    
71       public static void play(int nn, int dur) { 
72           play(nn, 127, dur); 
73       } 
74    
75       public static void play(int nn, int vel, int dur) { 
76           play(getSynthesizer(), nn, vel, dur); 
77       } 
78    
79       //protected static void play(Synthesizer synth, int nNoteNumber, 
80       //                        int nVelocity, int nDuration) { 
81       // Carl Weiman replacement below, 07-OCT-03 
82       public static void play(Synthesizer synth, int nNoteNumber, 
83                                  int nVelocity, int nDuration) { 
84           noteOn(nNoteNumber, nVelocity); 
85           if (nDuration < 0) return; 
86           sleep(nDuration); 
87           noteOff(nNoteNumber); 
88           System.out.println("synthesizer=" + synth); 
89       } 
90    
91       private static void noteOff(int nNoteNumber) { 
92           channels[0].noteOff(nNoteNumber); 
93       } 
94    
95       private static void noteOn(int nNoteNumber, int nVelocity) { 
96           channels[0].noteOn(nNoteNumber, nVelocity); 
97       } 
98    
99       private static void sleep(int nDuration) { 
100          try { 
101              Thread.sleep(nDuration); 
102          } catch (InterruptedException e) { 
103          } 
104      } 
105   
106      //protected static Synthesizer getSynthesizer() { 
107      // Carl Weiman replacement below, 07-OCT-03 
108      public static Synthesizer getSynthesizer() { 
109          Synthesizer synth = initSynth(); 
110          openSynth(synth); 
111          channels = initChannel(synth); 
112          return synth; 
113      } 
114   
115      private static Synthesizer initSynth() { 
116          if (synth != null) return synth; 
117          try { 
118              synth = MidiSystem.getSynthesizer(); 
119          } catch (MidiUnavailableException e) { 
120          } 
121          return synth; 
122      } 
123   
124      private static void openSynth(Synthesizer synth) { 
125          try { 
126              synth.open(); 
127          } catch (MidiUnavailableException e) { 
128              e.printStackTrace(); 
129              synth.close(); 
130              //System.exit(1); 
131          } 
132      } 
133   
134      private static MidiChannel[] initChannel(Synthesizer synth) { 
135          MidiChannel[] channels = synth.getChannels(); 
136          return channels; 
137      } 
138   
139      public static Sequencer getAndOpenSequencer() 
140              throws MidiUnavailableException { 
141          Sequencer sequencer = MidiSystem.getSequencer(); 
142          sequencer.open(); 
143          return sequencer; 
144      } 
145   
146  } 
147