1 /* Demo.java -- And example of MIDI support
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
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)
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.
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
21 package gnu.classpath.examples.midi;
24 import java.awt.event.*;
26 import javax.sound.midi.*;
29 * An example how javax.sound.midi facilities work.
31 public class Demo extends Frame implements ItemListener
33 Choice midiInChoice = new Choice();
34 Choice midiOutChoice = new Choice();
36 MidiDevice inDevice = null;
37 MidiDevice outDevice = null;
39 ArrayList inDevices = new ArrayList();
40 ArrayList outDevices = new ArrayList();
42 public Demo () throws Exception
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()
49 public void actionPerformed(ActionEvent e)
57 setTitle("synthcity: the GNU Classpath MIDI Demo");
58 setLayout(new FlowLayout());
60 MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
62 for (int i = 0; i < infos.length; i++)
64 MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
65 if (device.getMaxReceivers() > 0)
67 midiOutChoice.addItem(infos[i].getDescription());
68 outDevices.add(device);
70 if (device.getMaxTransmitters() > 0)
72 midiInChoice.addItem(infos[i].getDescription());
73 inDevices.add(device);
78 add(new Label("MIDI IN: "));
80 add(new Label(" MIDI OUT: "));
83 midiInChoice.addItemListener(this);
84 midiOutChoice.addItemListener(this);
90 public void itemStateChanged (ItemEvent e)
94 if (e.getItemSelectable() == midiInChoice)
98 inDevice = (MidiDevice)
99 inDevices.get(midiInChoice.getSelectedIndex());
102 if (e.getItemSelectable() == midiOutChoice)
104 if (outDevice != null)
106 outDevice = (MidiDevice)
107 outDevices.get(midiOutChoice.getSelectedIndex());
110 if (inDevice != null && outDevice != null)
112 if (! inDevice.isOpen())
114 if (! outDevice.isOpen())
116 Transmitter t = inDevice.getTransmitter();
118 System.err.println (inDevice + ".getTransmitter() == null");
119 Receiver r = outDevice.getReceiver();
121 System.err.println (outDevice + ".getReceiver() == null");
123 if (t != null && r != null)
129 ex.printStackTrace();
133 public static void main (String args[]) throws Exception