/Users/lyon/j4p/src/sound/soundDemo/JavaSound.java

1    package sound.soundDemo; 
2    // sound.soundDemo.JavaSound 
3     
4    /* 
5     * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 
6     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
7     */ 
8     
9     
10   import javax.sound.midi.MidiSystem; 
11   import javax.sound.sampled.AudioSystem; 
12   import javax.swing.*; 
13   import javax.swing.border.BevelBorder; 
14   import javax.swing.border.CompoundBorder; 
15   import javax.swing.border.EmptyBorder; 
16   import javax.swing.event.ChangeEvent; 
17   import javax.swing.event.ChangeListener; 
18   import java.awt.*; 
19   import java.awt.event.ActionEvent; 
20   import java.awt.event.ActionListener; 
21   import java.awt.event.WindowAdapter; 
22   import java.awt.event.WindowEvent; 
23   import java.io.File; 
24   import java.util.Vector; 
25    
26    
27   /** 
28    * The Java Sound Samples : MidiSynth, Juke, CapturePlaybackPanel, Groove. 
29    * 
30    * @version @(#)JavaSound.java  1.17 02/02/06 
31    * @author Brian Lichtenwalter 
32    */ 
33   public class JavaSound implements ChangeListener, Runnable { 
34    
35       Vector demoVector = new Vector(4); 
36       JTabbedPane tabPane = new JTabbedPane(); 
37       int width = 760, height = 500; 
38       int index; 
39       private final MyJPanel jPanel = new MyJPanel(); 
40    
41       public MyJPanel getPanel() { 
42           return jPanel; 
43       } 
44    
45    
46       public JavaSound(String audioDirectory) { 
47    
48           jPanel.setLayout(new BorderLayout()); 
49    
50           JMenuBar menuBar = new JMenuBar(); 
51    
52           if (JavaSoundApplet.applet == null) { 
53               JMenu fileMenu = (JMenu) menuBar.add(new JMenu("File")); 
54               JMenuItem item = (JMenuItem) fileMenu.add(new JMenuItem("Exit")); 
55               item.addActionListener(new ActionListener() { 
56                   public void actionPerformed(ActionEvent e) { 
57                       System.exit(0); 
58                   } 
59               }); 
60           } 
61           JMenu options = (JMenu) menuBar.add(new JMenu("Options")); 
62           JMenuItem item = (JMenuItem) options.add(new JMenuItem("Applet Info")); 
63           item.addActionListener(new ActionListener() { 
64               public void actionPerformed(ActionEvent e) { 
65                   showInfoDialog(); 
66               } 
67           }); 
68           jPanel.add(menuBar, BorderLayout.NORTH); 
69    
70           tabPane.addChangeListener(this); 
71    
72           EmptyBorder eb = new EmptyBorder(5, 5, 5, 5); 
73           BevelBorder bb = new BevelBorder(BevelBorder.LOWERED); 
74           CompoundBorder cb = new CompoundBorder(eb, bb); 
75           JPanel p = new JPanel(new BorderLayout()); 
76           p.setBorder(new CompoundBorder(cb, new EmptyBorder(0, 0, 90, 0))); 
77           final Juke juke = new Juke(audioDirectory); 
78           p.add(juke); 
79           demoVector.add(juke); 
80           tabPane.addTab("Juke Box", p); 
81    
82           new Thread(this).start(); 
83    
84           jPanel.add(tabPane, BorderLayout.CENTER); 
85       } 
86    
87    
88       public void stateChanged(ChangeEvent e) { 
89           close(); 
90           System.gc(); 
91           index = tabPane.getSelectedIndex(); 
92           open(); 
93       } 
94    
95    
96       public void close() { 
97           ((ControlContext) demoVector.get(index)).close(); 
98       } 
99    
100   
101      public void open() { 
102          ((ControlContext) demoVector.get(index)).open(); 
103      } 
104   
105   
106      public static void showInfoDialog() { 
107          final String msg = 
108                  "When running the Java Sound demo as an applet these permissions\n" + 
109                  "are necessary in order to load/save files and record audio :  \n\n" + 
110                  "grant { \n" + 
111                  "  permission java.io.FilePermission \"<<ALL FILES>>\", \"read, write\";\n" + 
112                  "  permission javax.sound.sampled.AudioPermission \"record\"; \n" + 
113                  "  permission java.util.PropertyPermission \"user.dir\", \"read\";\n" + 
114                  "}; \n\n" + 
115                  "The permissions need to be added to the .java.policy file."; 
116          new Thread(new Runnable() { 
117              public void run() { 
118                  JOptionPane.showMessageDialog(null, msg, "Applet Info", JOptionPane.INFORMATION_MESSAGE); 
119              } 
120          }).start(); 
121      } 
122   
123   
124      /** 
125       * Lazy load the tabbed pane with CapturePlaybackPanel, MidiSynth and Groove. 
126       */ 
127      public void run() { 
128          EmptyBorder eb = new EmptyBorder(5, 5, 5, 5); 
129          BevelBorder bb = new BevelBorder(BevelBorder.LOWERED); 
130          CompoundBorder cb = new CompoundBorder(eb, bb); 
131          addCapturePlaybackPanel(); 
132          JPanel p; 
133          MidiSynth midiSynth = new MidiSynth(); 
134          demoVector.add(midiSynth); 
135          tabPane.addTab("Midi Synthesizer", midiSynth); 
136   
137          p = new JPanel(new BorderLayout()); 
138          p.setBorder(new CompoundBorder(cb, new EmptyBorder(0, 0, 5, 20))); 
139          Groove groove = new Groove(); 
140          demoVector.add(groove); 
141          p.add(groove); 
142          tabPane.addTab("Groove Box", p); 
143      } 
144   
145      private void addCapturePlaybackPanel() { 
146          JPanel p = getCapturePlaybackPanel(); 
147          tabPane.addTab("Capture/Playback", p); 
148      } 
149   
150      public  JPanel getCapturePlaybackPanel() { 
151          JPanel p = new JPanel(new BorderLayout()); 
152   
153          CapturePlaybackPanel capturePlaybackPanel = new CapturePlaybackPanel(); 
154          demoVector.add(capturePlaybackPanel); 
155          p.add(capturePlaybackPanel); 
156          return p; 
157      } 
158   
159      public static void main(String[] args) { 
160   
161          checkAudio(); 
162   
163          String media = "media"; 
164          if (args.length > 0) { 
165              File file = new File(args[0]); 
166              if (file == null && !file.isDirectory()) { 
167                  System.out.println("usage: java JavaSound audioDirectory"); 
168              } else { 
169                  media = args[0]; 
170              } 
171          } 
172   
173          final JavaSound demo = new JavaSound(media); 
174          JFrame f = new JFrame("Java Sound Demo"); 
175          f.addWindowListener(new WindowAdapter() { 
176              public void windowClosing(WindowEvent e) { 
177                  System.exit(0); 
178              } 
179   
180              public void windowDeiconified(WindowEvent e) { 
181                  demo.open(); 
182              } 
183   
184              public void windowIconified(WindowEvent e) { 
185                  demo.close(); 
186              } 
187          }); 
188          f.getContentPane().add("Center", demo.jPanel); 
189          f.pack(); 
190          Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
191          f.setLocation(d.width / 2 - demo.width / 2, d.height / 2 - demo.height / 2); 
192          f.setSize(new Dimension(demo.width, demo.height)); 
193          f.setVisible(true); 
194      } 
195   
196      private static void checkAudio() { 
197          try { 
198              if (MidiSystem.getSequencer() == null) { 
199                  System.err.println("MidiSystem Sequencer Unavailable, exiting!"); 
200                  System.exit(1); 
201              } else if (AudioSystem.getMixer(null) == null) { 
202                  System.err.println("AudioSystem Unavailable, exiting!"); 
203                  System.exit(1); 
204              } 
205          } catch (Exception ex) { 
206              ex.printStackTrace(); 
207              System.exit(1); 
208          } 
209      } 
210   
211      private class MyJPanel extends JPanel { 
212          public Dimension getPreferredSize() { 
213              return new Dimension(JavaSound.this.width, JavaSound.this.height); 
214          } 
215      } 
216  } 
217