1   /*
2    * SoundTest.java
3    *
4    * Created on August 19, 2003, 1:09 AM
5    */
6   
7   package simworldobjects;
8   
9   import java.awt.*;
10  import java.awt.event.*;
11  import javax.swing.*;
12  import javax.swing.border.*;
13  import javax.swing.table.*;
14  import javax.swing.event.*;
15  import javax.sound.midi.*;
16  import java.util.*;
17  import josx.platform.rcx.Sound.*;
18  import josx.util.*;
19  
20  
21  /**
22   *
23   * @author  Simon
24   */
25  public class Sound extends Thread {
26      
27      private static Synthesizer synthesizer;
28      private static Sequencer sequencer;
29      private static Sequence sequence;
30      private static Instrument instruments[];
31      private  static ChannelData cc; 
32      
33      
34     public static void playTune(int frequency, int duration){
35          
36          try {
37              
38              if (synthesizer == null) {
39                  if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
40                      System.out.println("getSynthesizer() failed!");
41                      return;
42                  }
43              }
44              
45              synthesizer.open();
46              sequencer = MidiSystem.getSequencer();
47              sequence = new Sequence(Sequence.PPQ, 2);
48              
49          } catch (Exception ex) { ex.printStackTrace(); return; }
50          
51          Soundbank sb = synthesizer.getDefaultSoundbank();
52      if (sb != null) {
53              instruments = synthesizer.getDefaultSoundbank().getInstruments();
54              synthesizer.loadInstrument(instruments[341]);
55              
56          }
57          
58          MidiChannel midiChannels[] = synthesizer.getChannels();
59          cc = new ChannelData(midiChannels[0], 0);
60          
61          cc.channel.programChange(341);
62          
63          cc.channel.allNotesOff();
64                        
65          cc.channel.noteOn(frequency, cc.velocity);
66          
67          try{Thread.sleep(duration);}
68          catch(Exception e){}
69          
70          cc.channel.allNotesOff();
71          
72           
73      }
74      
75      public static LinkedList getInstrumentList(){
76          LinkedList instrumentList = new LinkedList();
77          
78              for(int i=0; i<instruments.length;i++){
79                  instrumentList.add(instruments[i].getName());
80                  //System.out.println(i+" "+instruments[i].getName());
81              }
82          
83          return instrumentList;
84               
85      }
86      
87      public static void close(){
88          if (synthesizer != null) {
89              synthesizer.close();
90          }
91          if (sequencer != null) {
92              sequencer.close();
93          }
94          sequencer = null;
95          synthesizer = null;
96          instruments = null;
97          cc = null;
98      }
99      
100     static class ChannelData {
101 
102         MidiChannel channel;
103         boolean solo, mono, mute, sustain;
104         int velocity, pressure, bend, reverb;
105         int row, col, num;
106  
107         public ChannelData(MidiChannel channel, int num) {
108             this.channel = channel;
109             this.num = num;
110             velocity = pressure = bend = reverb = 100;
111             velocity = 127;
112         }
113     }
114     
115     /*
116      //@param args the command line arguments
117      
118     public static void main(String[] args) {
119         
120         
121         
122         Sound.playTune(1,500);
123         //Sound.playTune(62,500);
124         //Sound.playTune(63,500);
125         //Sound.playTune(64,500);
126         //Sound.playTune(65,500);
127         //Sound.playTune(66,500);
128         
129         //Sound.playTune(60,500);
130         
131         
132         
133         //test1.close();
134         
135     }
136     */
137     
138 }
139