OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / midi / Demo.java
1 /* Demo.java -- And example of MIDI support
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21 package gnu.classpath.examples.midi;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.util.*;
26 import javax.sound.midi.*;
27
28 /**
29  * An example how javax.sound.midi facilities work.
30  */
31 public class Demo extends Frame implements ItemListener
32 {
33   Choice midiInChoice = new Choice();
34   Choice midiOutChoice = new Choice();
35
36   MidiDevice inDevice = null;
37   MidiDevice outDevice = null;
38   
39   ArrayList inDevices = new ArrayList();
40   ArrayList outDevices = new ArrayList();
41
42   public Demo () throws Exception
43   {
44     MenuBar mb = new MenuBar ();
45     Menu menu = new Menu ("File");
46     MenuItem quit = new MenuItem("Quit", new MenuShortcut('Q'));
47     quit.addActionListener(new ActionListener()
48       {
49         public void actionPerformed(ActionEvent e)
50         {
51           System.exit(0);
52         }
53       });
54     menu.add (quit);
55     mb.add(menu);
56     
57     setTitle("synthcity: the GNU Classpath MIDI Demo");
58     setLayout(new FlowLayout());
59     
60     MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
61
62     for (int i = 0; i < infos.length; i++)
63       {
64         MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
65         if (device.getMaxReceivers() > 0)
66           {
67             midiOutChoice.addItem(infos[i].getDescription());
68             outDevices.add(device);
69           }
70         if (device.getMaxTransmitters() > 0)
71           {
72             midiInChoice.addItem(infos[i].getDescription());
73             inDevices.add(device);
74           }
75       }
76
77     setMenuBar (mb);
78     add(new Label("MIDI IN: "));
79     add(midiInChoice);
80     add(new Label("   MIDI OUT: "));
81     add(midiOutChoice);
82
83     midiInChoice.addItemListener(this);
84     midiOutChoice.addItemListener(this);
85
86     pack();
87     show();
88   }
89   
90   public void itemStateChanged (ItemEvent e)
91   {
92     try
93       {
94         if (e.getItemSelectable() == midiInChoice)
95           {
96             if (inDevice != null)
97               inDevice.close();
98             inDevice =  (MidiDevice) 
99               inDevices.get(midiInChoice.getSelectedIndex());
100           }
101         
102         if (e.getItemSelectable() == midiOutChoice)
103           {
104             if (outDevice != null)
105               outDevice.close();
106             outDevice = (MidiDevice)
107               outDevices.get(midiOutChoice.getSelectedIndex());
108           }
109         
110         if (inDevice != null && outDevice != null)
111           {
112             if (! inDevice.isOpen())
113               inDevice.open();
114             if (! outDevice.isOpen())
115               outDevice.open();
116             Transmitter t = inDevice.getTransmitter();
117             if (t == null)
118               System.err.println (inDevice + ".getTransmitter() == null");
119             Receiver r = outDevice.getReceiver();
120             if (r == null)
121               System.err.println (outDevice + ".getReceiver() == null");
122             
123             if (t != null && r != null)
124               t.setReceiver (r);
125           }
126       }
127     catch (Exception ex)
128       {
129         ex.printStackTrace();
130       }
131   }
132
133   public static void main (String args[]) throws Exception
134     {
135       new Demo();
136     }
137 }