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

1    package sound; 
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 sound.soundDemo.CapturePlaybackPanel; 
12    
13   import javax.sound.midi.MidiChannel; 
14   import javax.sound.midi.MidiSystem; 
15   import javax.sound.midi.MidiUnavailableException; 
16   import javax.sound.midi.Synthesizer; 
17   import javax.sound.sampled.AudioFormat; 
18   import java.util.Vector; 
19   import java.awt.Container; 
20   import java.awt.BorderLayout; 
21    
22   import gui.ClosableJFrame; 
23    
24   public class Utils { 
25       private static MidiChannel channels[] = null; 
26       private static Synthesizer synth = null; 
27    
28       public static void play(int ia[], int vel, int dur) { 
29           for (int i = 0; i < ia.length; i++) 
30               play(ia[i], vel, dur); 
31       } 
32    
33       public static void playThread(final int ia[], 
34                                     final int vel, 
35                                     final int dur) { 
36           Runnable r = new Runnable() { 
37               public void run() { 
38                   for (int i = 0; i < ia.length; i++) 
39                       play(ia[i], vel, dur); 
40               } 
41           }; 
42           Thread t = new Thread(r); 
43           t.start(); 
44       } 
45    
46       public static void play(int ia[], int dur) { 
47           for (int i = 0; i < ia.length; i++) 
48               play(ia[i], 127, dur); 
49       } 
50    
51       public static int[] transpose(int ia[], int bias) { 
52           int ans[] = new int[ia.length]; 
53           for (int i = 0; i < ans.length; i++) 
54               ans[i] = ia[i] + bias; 
55           return ans; 
56       } 
57    
58       public static void print(int ia[]) { 
59           for (int i = 0; i < ia.length; i++) 
60               System.out.println(ia[i]); 
61       } 
62    
63       public static void play(int nn) { 
64           play(nn, 127, 20); 
65       } 
66    
67       public static void play(int nn, int dur) { 
68           play(nn, 127, dur); 
69       } 
70    
71       public static void play(int nn, int vel, int dur) { 
72           play(getSynthesizer(), nn, vel, dur); 
73       } 
74    
75       public static void play(Synthesizer synth, int nNoteNumber, 
76                               int nVelocity, int nDuration) { 
77           noteOn(nNoteNumber, nVelocity); 
78           if (nDuration < 0) return; 
79           sleep(nDuration); 
80           noteOff(nNoteNumber); 
81           System.out.println("synthesizer=" + synth); 
82       } 
83    
84       public static void noteOff(int nNoteNumber) { 
85           channels[0].noteOff(nNoteNumber); 
86       } 
87    
88       public static void noteOn(int nNoteNumber, int nVelocity) { 
89           channels[0].noteOn(nNoteNumber, nVelocity); 
90       } 
91    
92       private static void sleep(int nDuration) { 
93           try { 
94               Thread.sleep(nDuration); 
95           } catch (InterruptedException e) { 
96           } 
97       } 
98    
99       public static Synthesizer getSynthesizer() { 
100          Synthesizer synth = initSynth(); 
101          openSynth(synth); 
102          channels = initChannel(synth); 
103          return synth; 
104      } 
105   
106      private static Synthesizer initSynth() { 
107          if (synth != null) return synth; 
108          try { 
109              synth = MidiSystem.getSynthesizer(); 
110          } catch (MidiUnavailableException e) { 
111          } 
112          return synth; 
113      } 
114   
115      private static void openSynth(Synthesizer synth) { 
116          try { 
117              synth.open(); 
118          } catch (MidiUnavailableException e) { 
119              e.printStackTrace(); 
120              System.exit(1); 
121          } 
122      } 
123      public static void main(String[] args) { 
124          ClosableJFrame cf = new ClosableJFrame(); 
125          Container c = cf.getContentPane(); 
126          c.setLayout(new BorderLayout()); 
127          CapturePlaybackPanel cpp = new CapturePlaybackPanel(); 
128          c.add(cpp,BorderLayout.CENTER); 
129          cf.setSize(600,400); 
130          cf.show(); 
131          //Vector graphicLineData = cpp.getGraphicLineData(); 
132      } 
133   
134      private static MidiChannel[] initChannel(Synthesizer synth) { 
135          MidiChannel[] channels = synth.getChannels(); 
136          return channels; 
137      } 
138   
139      public static double[] getDoubleData(AudioFormat format, 
140                                           byte[] audioBytes) { 
141          int data16[] = get16BitAudioData(format, audioBytes); 
142          double d[] = new double[data16.length]; 
143          for (int i = 0; i < d.length; i++) 
144              d[i] = data16[i] / 32768.0; 
145          return d; 
146      } 
147   
148      public static int[] get16BitAudioData(AudioFormat format, 
149                                            byte[] audioBytes) { 
150          int[] audioData = new int[audioBytes.length]; 
151          if (format.getSampleSizeInBits() == 16) { 
152              int nlengthInSamples = audioBytes.length / 2; 
153              audioData = new int[nlengthInSamples]; 
154              if (format.isBigEndian()) { 
155                  for (int i = 0; i < nlengthInSamples; i++) { 
156                      /* First byte is MSB (high order) */ 
157                      int MSB = (int) audioBytes[2 * i]; 
158                      /* Second byte is LSB (low order) */ 
159                      int LSB = (int) audioBytes[2 * i + 1]; 
160                      audioData[i] = MSB << 8 | (255 & LSB); 
161                  } 
162              } else { 
163                  for (int i = 0; i < nlengthInSamples; i++) { 
164                      /* First byte is LSB (low order) */ 
165                      int LSB = (int) audioBytes[2 * i]; 
166                      /* Second byte is MSB (high order) */ 
167                      int MSB = (int) audioBytes[2 * i + 1]; 
168                      audioData[i] = MSB << 8 | (255 & LSB); 
169                  } 
170              } 
171          } else if (format.getSampleSizeInBits() == 8) { 
172              int nlengthInSamples = audioBytes.length; 
173              audioData = new int[nlengthInSamples]; 
174              if (format.getEncoding().toString().startsWith("PCM_SIGN")) { 
175                  for (int i = 0; i < audioBytes.length; i++) { 
176                      audioData[i] = audioBytes[i]; 
177                  } 
178              } else { 
179                  for (int i = 0; i < audioBytes.length; i++) { 
180                      audioData[i] = audioBytes[i] - 128; 
181                  } 
182              } 
183          } 
184          return audioData; 
185      } 
186   
187      public static void echo(double[] linearSamples) { 
188          int ECHO_NUMBER = 4; 
189          double DECAY = 0.5; 
190          echo(ECHO_NUMBER, DECAY, linearSamples); 
191      } 
192   
193      public static void echo(int numberOfEchos, double decay, double[] linearSamples) { 
194          int n = linearSamples.length; 
195          int numTimes = numberOfEchos + 1; 
196          double currDecay = 1.0; 
197          short sample, newSample; 
198          double[] newSamples = new double[n * numTimes]; 
199          for (int j = 0; j < numTimes; j++) { 
200              for (int i = 0; i < n; i++) // copy the sound's bytes 
201                  newSamples[i + (n * j)] = 
202                          linearSamples[i] * currDecay; 
203              currDecay *= decay; 
204          } 
205          linearSamples = newSamples; 
206          UlawCodec ulc = new UlawCodec(linearSamples); 
207          ulc.play(); 
208      } 
209   
210      public static AudioFormat get8khzMono8Format() { 
211          float sampleRate = 8000.0F; 
212  //8000,11025,16000,22050,44100 
213          int sampleSizeInBits = 8; 
214  //8,16 
215          int channels = 1; 
216  //1,2 
217          // use signed if 16 bit, otherwise use 
218          // unsigned 
219          boolean signed = false; 
220  //true,false 
221          boolean bigEndian = false; 
222  //true,false 
223          return new AudioFormat(sampleRate, 
224                  sampleSizeInBits, 
225                  channels, 
226                  signed, 
227                  bigEndian); 
228      }//end get8khzMono8Format 
229  } 
230