Advertisement
campellodavide

Untitled

Jul 17th, 2013
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.91 KB | None | 0 0
  1.  
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.GraphicsConfiguration;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Insets;
  9. import java.awt.Point;
  10. import java.awt.Rectangle;
  11. import java.awt.Toolkit;
  12. import java.awt.Window;
  13. import java.awt.datatransfer.DataFlavor;
  14. import java.awt.datatransfer.Transferable;
  15. import java.awt.datatransfer.UnsupportedFlavorException;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.WindowAdapter;
  19. import java.awt.event.WindowEvent;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Properties;
  28.  
  29. import javax.sound.midi.InvalidMidiDataException;
  30. import javax.sound.midi.MidiChannel;
  31. import javax.sound.midi.MidiDevice;
  32. import javax.sound.midi.MidiSystem;
  33. import javax.sound.midi.MidiUnavailableException;
  34. import javax.sound.midi.Sequence;
  35. import javax.sound.midi.Sequencer;
  36. import javax.sound.midi.Soundbank;
  37. import javax.sound.midi.Synthesizer;
  38. import javax.sound.midi.SysexMessage;
  39. import javax.sound.midi.MidiDevice.Info;
  40. import javax.sound.sampled.AudioFormat;
  41. import javax.sound.sampled.AudioSystem;
  42. import javax.sound.sampled.DataLine;
  43. import javax.sound.sampled.Line;
  44. import javax.sound.sampled.Mixer;
  45. import javax.sound.sampled.SourceDataLine;
  46. import javax.swing.Icon;
  47. import javax.swing.ImageIcon;
  48. import javax.swing.JButton;
  49. import javax.swing.JComponent;
  50. import javax.swing.JFileChooser;
  51. import javax.swing.JFrame;
  52. import javax.swing.JLabel;
  53. import javax.swing.JMenuItem;
  54. import javax.swing.JPanel;
  55. import javax.swing.JPopupMenu;
  56. import javax.swing.SwingUtilities;
  57. import javax.swing.TransferHandler;
  58. import javax.swing.filechooser.FileFilter;
  59.  
  60. import com.sun.media.sound.AudioSynthesizer;
  61. import com.sun.media.sound.EmergencySoundbank;
  62.  
  63. public class SimpleMidiPlayer extends JFrame {
  64.  
  65.     public class ImagePanel extends JPanel {
  66.  
  67.         private static final long serialVersionUID = 1L;
  68.  
  69.         Icon icon;
  70.  
  71.         public ImagePanel(Icon icon) {
  72.             super();
  73.             this.icon = icon;
  74.         }
  75.  
  76.         protected void paintComponent(Graphics g) {
  77.             super.paintComponent(g);
  78.             icon.paintIcon(this, g, 0, 0);
  79.         }
  80.  
  81.     }
  82.  
  83.     private static final long serialVersionUID = 1L;
  84.  
  85.     public static void main(String[] args) {
  86.         if (!configExists()) {
  87.             ConfigDialog cd = new ConfigDialog(null);
  88.             cd.setVisible(true);
  89.             if (!cd.isOK())
  90.                 return;
  91.         }
  92.         new SimpleMidiPlayer().setVisible(true);
  93.     }
  94.     public JButton makeButton(String caption) {
  95.         JButton butt = new JButton(caption);
  96.         butt.setMargin(new Insets(2, 2, 2, 2));
  97.         butt.setFocusable(false);
  98.         butt.setFont(butt.getFont().deriveFont(Font.PLAIN));
  99.         return butt;
  100.     }
  101.  
  102.     JPopupMenu loadmenu;
  103.  
  104.     boolean synth_loaded = false;
  105.  
  106.     Sequencer seqr = null;
  107.  
  108.     Sequence seq = null;
  109.  
  110.     String seq_errmsg = null;
  111.  
  112.     File seqfile = null;
  113.  
  114.     Soundbank sbk = null;
  115.  
  116.     String sbk_errmsg = null;
  117.  
  118.     File sbkfile = null;
  119.  
  120.     Synthesizer softsynth = null;
  121.  
  122.     Mixer synthmixer = null;
  123.     SourceDataLine line = null;
  124.  
  125.     InfoFrame infoframe;
  126.    
  127.     AudioFormat format;
  128.    
  129.     /*
  130.      * Find available AudioSynthesizer.
  131.      */
  132.     public static AudioSynthesizer findAudioSynthesizer()
  133.             throws MidiUnavailableException {
  134.         // First check if default synthesizer is AudioSynthesizer.
  135.         Synthesizer synth = MidiSystem.getSynthesizer();
  136.         if (synth instanceof AudioSynthesizer)
  137.             return (AudioSynthesizer) synth;
  138.  
  139.         // If default synhtesizer is not AudioSynthesizer, check others.
  140.         Info[] infos = MidiSystem.getMidiDeviceInfo();
  141.         for (int i = 0; i < infos.length; i++) {
  142.             MidiDevice dev = MidiSystem.getMidiDevice(infos[i]);
  143.             if (dev instanceof AudioSynthesizer)
  144.                 return (AudioSynthesizer) dev;
  145.         }
  146.  
  147.         // No AudioSynthesizer was found, return null.
  148.         return null;
  149.     }  
  150.  
  151.     public void initMIDI() {
  152.         try {
  153.            
  154.             final AudioSynthesizer synth = findAudioSynthesizer();
  155.  
  156.             Properties p = getConfig();
  157.             Map<String, Object> ainfo = new HashMap<String, Object>();
  158.  
  159.             try {
  160.  
  161.                 format = new AudioFormat(Float.parseFloat(p
  162.                         .getProperty("samplerate", "44100")), Integer
  163.                         .parseInt(p.getProperty("bits", "16")), Integer
  164.                         .parseInt(p.getProperty("channels", "2")), true, false);
  165.                
  166.                 int latency = Integer.parseInt(p.getProperty("latency", "200")) * 1000;
  167.                
  168.                 String devname = p.getProperty("devicename");
  169.                 if (devname != null) {
  170.                     Mixer.Info selinfo = null;
  171.                     for (Mixer.Info info : AudioSystem.getMixerInfo()) {
  172.                         Mixer mixer = AudioSystem.getMixer(info);
  173.                         boolean hassrcline = false;
  174.                         for (Line.Info linfo : mixer.getSourceLineInfo())
  175.                             if (linfo instanceof javax.sound.sampled.DataLine.Info)
  176.                                 hassrcline = true;
  177.                         if (hassrcline) {
  178.                             if (info.getName().equals(devname)) {
  179.                                 selinfo = info;
  180.                                 break;
  181.                             }
  182.                         }
  183.                     }
  184.                     if (selinfo != null) {
  185.                         synthmixer = AudioSystem.getMixer(selinfo);
  186.                         try {
  187.                             synthmixer.open();
  188.                            
  189.                             int bufferSize = (int)                         
  190.                                 (format.getFrameSize() * format.getFrameRate()
  191.                                 * latency / 1000000f);
  192.                             if(bufferSize < 500) bufferSize = 500;
  193.                                
  194.                             DataLine.Info dataLineInfo = new DataLine.Info(
  195.                                     SourceDataLine.class, format, bufferSize);
  196.                             if (synthmixer.isLineSupported(dataLineInfo))
  197.                                 line = (SourceDataLine) synthmixer
  198.                                         .getLine(dataLineInfo);
  199.                                                        
  200.                             line.open(format, bufferSize);
  201.                             line.start();
  202.                                                                                    
  203.                         } catch (Throwable t) {
  204.                             t.printStackTrace();
  205.                             synthmixer = null;
  206.                         }
  207.                     }
  208.                 }
  209.                
  210.                 //ainfo.put("multi threading", true);
  211.                 ainfo.put("format", format);
  212.                 ainfo.put("max polyphony", Integer.parseInt(p.getProperty(
  213.                         "polyphony", "64")));              
  214.                 ainfo.put("latency", Long.parseLong(p.getProperty("latency",
  215.                         "200")) * 1000L);
  216.  
  217.                 ainfo.put("interpolation", p.getProperty("interpolation"));                
  218.                 String largemode = p.getProperty("largemode");
  219.                 if(largemode == null) largemode = "false";
  220.                 ainfo.put("large mode", largemode.equalsIgnoreCase("true"));
  221.  
  222.             } catch (Throwable t) {
  223.                 t.printStackTrace();
  224.             }
  225.            
  226.             synth.open(line, ainfo);
  227.  
  228.             Runnable r = new Runnable() {
  229.                 public void run() {
  230.                     softsynth = synth;
  231.                     if (sbk == null)
  232.                         sbk = synth.getDefaultSoundbank();
  233.                     try {
  234.                         if (seqr == null) {
  235.                             try {
  236.                                 seqr = MidiSystem.getSequencer(false);
  237.                             } catch (MidiUnavailableException e2) {
  238.                                 e2.printStackTrace();
  239.                             }
  240.                         }
  241.                         if (seqr.isOpen())
  242.                             seqr.close();
  243.                         seqr.getTransmitter().setReceiver(
  244.                                 softsynth.getReceiver());
  245.                         seqr.open();
  246.                     } catch (MidiUnavailableException e) {
  247.                         e.printStackTrace();
  248.                     }
  249.                     synth_loaded = true;
  250.                 }
  251.             };
  252.  
  253.             if (SwingUtilities.isEventDispatchThread())
  254.                 r.run();
  255.             else
  256.                 SwingUtilities.invokeLater(r);
  257.         } catch (Exception e) {
  258.             e.printStackTrace();
  259.         }
  260.     }
  261.  
  262.     public void initMIDI_inThread() {
  263.         synth_loaded = false;
  264.         new Thread() {
  265.             public void run() {
  266.                 initMIDI();
  267.             }
  268.         }.start();
  269.  
  270.     }
  271.  
  272.     public void closeMIDI() {
  273.         if (synth_loaded) {
  274.             seqr.close();
  275.             softsynth.close();
  276.             if (line != null) {
  277.                 line.close();
  278.                 line = null;
  279.             }
  280.             if (synthmixer != null) {
  281.                 synthmixer.close();
  282.                 synthmixer = null;
  283.             }
  284.         }
  285.     }
  286.  
  287.     JLabel displayLab = new JLabel();
  288.  
  289.     public void updateDisplay() {
  290.  
  291.         if (!synth_loaded) {
  292.             displayLab.setText("<html><body>Initializing . . .");
  293.         } else {
  294.             MidiDevice.Info info = softsynth.getDeviceInfo();
  295.            
  296.             String fmts = (int) format.getSampleRate() + "Hz "
  297.                     + format.getSampleSizeInBits() + "bit "
  298.                     + format.getChannels() + "ch";
  299.             String line1 = "<b>" + info.getName() + " " + info.getVersion()
  300.                     + "</b> &nbsp;" + fmts;
  301.             String line2 = "";
  302.  
  303.             if (sbk == null) {
  304.                 line2 = "No SoundBank Loaded!";
  305.             } else {
  306.                 if (sbk_errmsg != null)
  307.                     line2 = sbk_errmsg;
  308.                 else if (sbkfile == null)
  309.                     line2 = "Default SoundBank";
  310.                 else
  311.                     line2 = sbkfile.getName();
  312.                 if (line2.length() > 31)
  313.                     line2 = line2.substring(0, 31);
  314.             }
  315.  
  316.             String line3 = "";
  317.             if (seq == null) {
  318.                 line3 = "No Sequence";
  319.             } else {
  320.                 if (seqr.isRunning() || seqr.getTickPosition() != 0) {
  321.  
  322.                     long a = seqr.getTickPosition() / seq.getResolution();
  323.                     long b = seqr.getTickLength() / seq.getResolution();
  324.                     if (seqr.isRunning())
  325.                         line3 = "PLAY " + a + " of " + b;
  326.                     else
  327.                         line3 = "STOP " + a + " of " + b;
  328.  
  329.                 } else {
  330.                     if (seq_errmsg != null)
  331.                         line3 = seq_errmsg;
  332.                     else
  333.                         line3 = seqfile.getName();
  334.                     if (line3.length() > 31)
  335.                         line3 = line3.substring(0, 31);
  336.                 }
  337.             }
  338.             displayLab.setText("<html><body>" + line1 + "<br>" + line2 + "<br>"
  339.                     + line3);
  340.         }
  341.  
  342.     }
  343.  
  344.     JFileChooser loadseq;
  345.  
  346.     JFileChooser loadsndbk;
  347.  
  348.     Thread actdisplay;
  349.  
  350.     boolean player_running = true;
  351.  
  352.     private static String CONFIG_FILE_NAME = "SimpleMidiPlayer.xml";
  353.  
  354.     private static File userDir = new File(System.getProperty("user.home"),
  355.             ".gervill");
  356.  
  357.     private static File configFile = new File(userDir, CONFIG_FILE_NAME);
  358.  
  359.     private static Properties configp = null;
  360.  
  361.     public static void centerWindow(Window w) {
  362.         Rectangle windowSize;
  363.         // Insets windowInsets;
  364.  
  365.         Toolkit toolkit = Toolkit.getDefaultToolkit();
  366.         GraphicsEnvironment ge = java.awt.GraphicsEnvironment
  367.                 .getLocalGraphicsEnvironment();
  368.         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  369.                 .getDefaultConfiguration();
  370.         if (gc == null)
  371.             gc = w.getGraphicsConfiguration();
  372.  
  373.         if (gc != null) {
  374.             windowSize = gc.getBounds();
  375.         } else {
  376.             windowSize = new java.awt.Rectangle(toolkit.getScreenSize());
  377.         }
  378.  
  379.         Dimension size = w.getSize();
  380.         Point parent_loc = w.getLocation();
  381.         w.setLocation(parent_loc.x + windowSize.width / 2 - (size.width / 2),
  382.                 parent_loc.y + windowSize.height / 2 - (size.height / 2));
  383.  
  384.     }
  385.  
  386.     public static boolean configExists() {
  387.         synchronized (configFile) {
  388.             return configFile.exists();
  389.         }
  390.     }
  391.  
  392.     public static Properties getConfig() {
  393.         synchronized (configFile) {
  394.  
  395.             if (configp != null) {
  396.                 Properties p = new Properties();
  397.                 p.putAll(configp);
  398.                 return p;
  399.             }
  400.             Properties p = new Properties();
  401.             if (configFile.exists()) {
  402.                 FileInputStream fis;
  403.                 try {
  404.                     fis = new FileInputStream(configFile);
  405.                     try {
  406.                         p.loadFromXML(fis);
  407.                     } finally {
  408.                         fis.close();
  409.                     }
  410.                 } catch (Exception e) {
  411.                     e.printStackTrace();
  412.                 }
  413.             }
  414.             return p;
  415.  
  416.         }
  417.     }
  418.  
  419.     public static void storeConfig(Properties p) {
  420.         synchronized (configFile) {
  421.  
  422.             try {
  423.                 configp = new Properties();
  424.                 configp.putAll(p);
  425.  
  426.                 if (!userDir.exists())
  427.                     userDir.mkdirs();
  428.                 FileOutputStream fos = new FileOutputStream(configFile);
  429.                 try {
  430.                     p.storeToXML(fos, "GervillMidiPlayer");
  431.                 } finally {
  432.                     fos.close();
  433.                 }
  434.             } catch (Exception e) {
  435.                 e.printStackTrace();
  436.             }
  437.  
  438.         }
  439.     }
  440.  
  441.     public void loadMidiSeq(File newseqfile) {
  442.         try {
  443.             seq_errmsg = null;
  444.             Sequence newseq = MidiSystem.getSequence(newseqfile);
  445.  
  446.             seq = newseq;
  447.             seqfile = newseqfile;
  448.             // boolean running = seqr.isRunning();
  449.             seqr.stop();
  450.            
  451.             // Reset All Channels
  452.             for(MidiChannel c : softsynth.getChannels())
  453.                 c.resetAllControllers();
  454.            
  455.             seqr.setSequence(seq);
  456.             seqr.setTickPosition(0);
  457.             seqr.start();
  458.         } catch (Throwable e1) {
  459.             seq_errmsg = e1.toString();
  460.         }
  461.  
  462.     }
  463.  
  464.     public void loadSoundbank(File newsbkfile) {
  465.         try {
  466.             sbk_errmsg = null;
  467.             Soundbank newsbk = MidiSystem.getSoundbank(newsbkfile);
  468.             if (sbk != null)
  469.                 softsynth.unloadAllInstruments(sbk);
  470.             sbkfile = newsbkfile;
  471.             sbk = newsbk;
  472.             softsynth.loadAllInstruments(sbk);
  473.         } catch (Throwable e1) {
  474.             sbk_errmsg = e1.toString();
  475.         }
  476.  
  477.     }
  478.  
  479.     public SimpleMidiPlayer() {
  480.  
  481.         loadseq = new JFileChooser();
  482.         loadseq.setDialogTitle("Load MIDI Sequence");
  483.         loadseq.setFileFilter(new FileFilter() {
  484.             public boolean accept(File f) {
  485.                 if (!f.isFile())
  486.                     return true;
  487.                 return f.getName().toLowerCase().endsWith(".mid");
  488.             }
  489.  
  490.             public String getDescription() {
  491.                 return "MIDI Sequence";
  492.             }
  493.         });
  494.         loadsndbk = new JFileChooser();
  495.         loadsndbk.setFileFilter(new FileFilter() {
  496.             public boolean accept(File f) {
  497.                 if (!f.isFile())
  498.                     return true;
  499.                 String name = f.getName().toLowerCase();
  500.                 if (name.endsWith(".sf2"))
  501.                     return true;
  502.                 if (name.endsWith(".dls"))
  503.                     return true;
  504.                 if (name.endsWith(".pat"))
  505.                     return true;
  506.                 if (name.endsWith(".cfg"))
  507.                     return true;
  508.                 if (name.endsWith(".wav"))
  509.                     return true;
  510.                 if (name.endsWith(".au"))
  511.                     return true;
  512.                 if (name.endsWith(".aif"))
  513.                     return true;
  514.                 return false;
  515.             }
  516.  
  517.             public String getDescription() {
  518.                 return "SoundBank (*.sf2,*.dls,*.pat,*.cfg,*.wav,*.au,*.aif)";
  519.             }
  520.         });
  521.  
  522.         // setLocationByPlatform(true);
  523.         setResizable(false);
  524.         setTitle("Gervill - MIDI Player");
  525.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  526.  
  527.         addWindowListener(new WindowAdapter() {
  528.             public void windowClosing(WindowEvent e) {
  529.                 closeMIDI();
  530.                 player_running = false;
  531.                 try {
  532.                     actdisplay.join(1000);
  533.                 } catch (InterruptedException e1) {
  534.                     e1.printStackTrace();
  535.                 }
  536.             }
  537.         });
  538.  
  539.         infoframe = new InfoFrame(this);
  540.  
  541.         actdisplay = new Thread() {
  542.             public void run() {
  543.                 boolean ok = true;
  544.                 while (ok) {
  545.                     synchronized (SimpleMidiPlayer.this) {
  546.                         ok = player_running;
  547.                     }
  548.                     SwingUtilities.invokeLater(new Runnable() {
  549.                         public void run() {
  550.                             updateDisplay();
  551.                             infoframe.updateDisplay();
  552.                         }
  553.                     });
  554.                     try {
  555.                         Thread.sleep(100);
  556.                     } catch (InterruptedException e) {
  557.                         e.printStackTrace();
  558.                         return;
  559.                     }
  560.                 }
  561.             }
  562.         };
  563.         actdisplay.start();
  564.  
  565.         initMIDI_inThread();
  566.  
  567.         ImageIcon backgr = new javax.swing.ImageIcon(getClass().getResource(
  568.                 "/simplemidiplayer/backgr.png"));
  569.         ImageIcon swan = new javax.swing.ImageIcon(getClass().getResource(
  570.                 "/simplemidiplayer/swan.png"));
  571.         setIconImage(swan.getImage());
  572.  
  573.         JPanel panel = new ImagePanel(backgr);
  574.         Dimension size = new Dimension(443, 125);
  575.         panel.setPreferredSize(size);
  576.         panel.setMinimumSize(size);
  577.         panel.setLayout(null);
  578.  
  579.         TransferHandler thandler = new TransferHandler() {
  580.             private static final long serialVersionUID = 1L;
  581.  
  582.             public boolean canImport(JComponent comp,
  583.                     DataFlavor[] transferFlavors) {
  584.  
  585.                 for (int i = 0; i < transferFlavors.length; i++) {
  586.                     if (transferFlavors[i]
  587.                             .equals(DataFlavor.javaFileListFlavor)) {
  588.                         return true;
  589.                     }
  590.                 }
  591.                 return false;
  592.             }
  593.  
  594.             public boolean importData(JComponent comp, Transferable t) {
  595.  
  596.                 List files = null;
  597.                 try {
  598.                     files = (List) t
  599.                             .getTransferData(DataFlavor.javaFileListFlavor);
  600.                 } catch (UnsupportedFlavorException e) {
  601.                     e.printStackTrace();
  602.                 } catch (IOException e) {
  603.                     e.printStackTrace();
  604.                 }
  605.  
  606.                 if (files == null)
  607.                     return false;
  608.  
  609.                 for (Object o : files) {
  610.                     File file = (File) o;
  611.                     if (file.isFile()) {
  612.                         if (file.getName().toLowerCase().endsWith(".mid"))
  613.                             loadMidiSeq(file);
  614.                         else
  615.                             loadSoundbank(file);
  616.                     }
  617.                 }
  618.  
  619.                 return true;
  620.             }
  621.         };
  622.  
  623.         panel.setTransferHandler(thandler);
  624.  
  625.         setContentPane(panel);
  626.  
  627.         displayLab.setSize(225, 67);
  628.         displayLab.setLocation(206, 20);
  629.         displayLab.setFont(new Font("Monospaced", Font.PLAIN, 12));
  630.         displayLab.setVerticalAlignment(JLabel.TOP);
  631.         displayLab.setVerticalTextPosition(JLabel.TOP);
  632.         displayLab.setTransferHandler(thandler);
  633.         panel.add(displayLab);
  634.  
  635.         JPanel toolBar = new JPanel();
  636.         toolBar.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 5));
  637.         toolBar.setSize(429, 80);
  638.         toolBar.setLocation(0, 82);
  639.         toolBar.setOpaque(false);
  640.  
  641.         final JButton config = makeButton("CONFIG");
  642.         final JButton info = makeButton("INFO");
  643.         final JButton load = makeButton("LOAD");
  644.         final JButton play = makeButton("PLAY");
  645.         final JButton stop = makeButton("STOP");
  646.  
  647.         JMenuItem loadseq_menuitem = new JMenuItem("MIDI Sequence...");
  648.  
  649.         loadseq_menuitem.addActionListener(new ActionListener() {
  650.             public void actionPerformed(ActionEvent e) {
  651.                 if (!synth_loaded)
  652.                     return;
  653.                 if (loadseq.showOpenDialog(SimpleMidiPlayer.this) == JFileChooser.APPROVE_OPTION) {
  654.                     loadMidiSeq(loadseq.getSelectedFile());
  655.                 }
  656.             }
  657.         });
  658.  
  659.         JMenuItem loadsndbk_menuitem = new JMenuItem("Soundbank...");
  660.  
  661.         loadsndbk_menuitem.addActionListener(new ActionListener() {
  662.             public void actionPerformed(ActionEvent e) {
  663.                 if (!synth_loaded)
  664.                     return;
  665.                 if (loadsndbk.showOpenDialog(SimpleMidiPlayer.this) == JFileChooser.APPROVE_OPTION) {
  666.                     loadSoundbank(loadsndbk.getSelectedFile());
  667.                 }
  668.             }
  669.         });
  670.  
  671.         JMenuItem default_loadsndbk_menuitem = new JMenuItem(
  672.                 "Default Soundbank");
  673.  
  674.         default_loadsndbk_menuitem.addActionListener(new ActionListener() {
  675.             public void actionPerformed(ActionEvent e) {
  676.                 if (softsynth.getDefaultSoundbank() != null) {
  677.                     if (sbk != null)
  678.                         softsynth.unloadAllInstruments(sbk);
  679.                     sbk = softsynth.getDefaultSoundbank();
  680.                     sbkfile = null;
  681.                     softsynth.loadAllInstruments(sbk);
  682.                 }
  683.             }
  684.         });
  685.  
  686.         JMenuItem emerg_loadsndbk_menuitem = new JMenuItem(
  687.         "Emergency Soundbank");
  688.  
  689.         emerg_loadsndbk_menuitem.addActionListener(new ActionListener() {
  690.         public void actionPerformed(ActionEvent e) {
  691.            
  692.             Soundbank emsbk;
  693.             try {
  694.                 emsbk = EmergencySoundbank.createSoundbank();
  695.             } catch (Exception e1) {
  696.                 e1.printStackTrace();
  697.                 return;
  698.             }
  699.             if (sbk != null)
  700.                 softsynth.unloadAllInstruments(sbk);
  701.             sbk = emsbk;
  702.             sbkfile = null;
  703.             softsynth.loadAllInstruments(sbk);
  704.         }
  705.         });
  706.        
  707.         loadmenu = new JPopupMenu();
  708.         loadmenu.add(loadseq_menuitem);
  709.         loadmenu.addSeparator();
  710.         loadmenu.add(loadsndbk_menuitem);
  711.         loadmenu.add(default_loadsndbk_menuitem);
  712.         loadmenu.add(emerg_loadsndbk_menuitem);
  713.  
  714.         config.addActionListener(new ActionListener() {
  715.             public void actionPerformed(ActionEvent e) {
  716.                 if (!synth_loaded)
  717.                     return;
  718.  
  719.                 ConfigDialog cd = new ConfigDialog(SimpleMidiPlayer.this);
  720.                 cd.setVisible(true);
  721.                 if (cd.isOK()) {
  722.                     Sequence pseq = seqr.getSequence();
  723.                     long ptick = seqr.getTickPosition();
  724.                     boolean prunning = seqr.isRunning();
  725.                     seqr.stop();
  726.                     softsynth.close();
  727.                     if (synthmixer != null) {
  728.                         synthmixer.close();
  729.                         synthmixer = null;
  730.                     }
  731.                     initMIDI();
  732.                     if (pseq != null) {
  733.                         try {
  734.                             seqr.setSequence(pseq);
  735.                         } catch (InvalidMidiDataException e1) {
  736.                             e1.printStackTrace();
  737.                         }
  738.                     }
  739.                     seqr.setTickPosition(ptick);
  740.                     if (prunning) {
  741.                         seqr.start();
  742.                     }
  743.  
  744.                 }
  745.             }
  746.         });
  747.  
  748.         load.addActionListener(new ActionListener() {
  749.             public void actionPerformed(ActionEvent e) {
  750.                 if (!synth_loaded)
  751.                     return;
  752.                 loadmenu.show(load, 0, 0);
  753.             }
  754.         });
  755.  
  756.         info.addActionListener(new ActionListener() {
  757.             public void actionPerformed(ActionEvent e) {
  758.                 infoframe.setVisible(!infoframe.isVisible());
  759.             }
  760.         });
  761.  
  762.         play.addActionListener(new ActionListener() {
  763.             public void actionPerformed(ActionEvent e) {
  764.                 if (!synth_loaded)
  765.                     return;
  766.                 if (seq == null)
  767.                     return;
  768.                 seqr.start();
  769.             }
  770.         });
  771.  
  772.         stop.addActionListener(new ActionListener() {
  773.             public void actionPerformed(ActionEvent e) {
  774.  
  775.                 if (!synth_loaded)
  776.                     return;
  777.                 if (seq == null)
  778.                     return;
  779.                 if (seqr.isRunning())
  780.                     seqr.stop();
  781.                 else
  782.                     seqr.setTickPosition(0);
  783.             }
  784.         });
  785.  
  786.         toolBar.add(config);
  787.         toolBar.add(load);
  788.         toolBar.add(info);
  789.         toolBar.add(play);
  790.         toolBar.add(stop);
  791.         panel.add(toolBar);
  792.  
  793.         pack();
  794.  
  795.         centerWindow(this);
  796.  
  797.     }
  798.  
  799. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement