package core;
imports java.awt.* , java.util.* , java.util.prefs.* , javax.sound.midi.* , javax.sound.midi.MidiDevice.* , javax.swing.*;

Device class defines some information for your synthesizer, such as
a name of manufacturer, the model name, author(s) of drivers, etc.
It also manages a list of Driver classes which provides actualfunctions.

Subclass must define two constructors, with no argument
and with one argument: Preferences.

Here is an example;
import java.util.prefs.Preferences;
...
//Constructor for DeviceListWriter.
public KawaiK4Device() {
super("Kawai", "K4/K4R", "F07E..0602400000040000000000f7",
INFO_TEXT, "Brian Klock & Gerrit Gehnen");
}
//Constructor for for actual work.
public KawaiK4Device(Preferences prefs) {
this();
this.prefs = prefs;
 
addDriver(new KawaiK4BulkConverter());
...
}
Compatibility Note: The following fields are now
private. Use setter/getter method to access them.
manufacturerName, modelName, inquiryID, infoText, authors,
synthName, channel, inPort, port

see IDriver

Declared Fileds:

public abstract class Device implements Serializable, Storable {

protected Preferences prefs = null;
Preferences node for storing configuration options.
private final String manufacturerName;
The company which made the Synthesizer.
private final String modelName;
The fixed name of the model supported by this driver, as stated
on the type plate of the engine. eg TG33/SY22
private final String inquiryID;
The response to the Universal Inquiry Message. It is a
regular expression. It can be up to 16 bytes.
Ex. [["F07E..0602413F01000000020000f7"]]
private final String infoText;
Information about Device.
@see DeviceDetailsDialog
private final String authors;
Authors of the device driver.
Not important for writing a driver:
(maybe we should rmove these from the wiki?)
private boolean initPort = false;
set to true when initialization of MIDI output is done.
private Receiver rcvr;
MIDI output Receiver
private boolean initInPort = false;
set to true when initialization of MIDI input is done.
private int midiOutBufSize = 0;
MIDI message size. If zero, whole MIDI message is passed to lower MIDI
driver.
private int midiOutDelay = 0;
delay (msec) after every MIDI message transfer.
private ArrayList driverList = new ArrayList ();
The List for all available drivers of this device.

Constructor and methods

public Device (String manufacturerName, String modelName,
String inquiryID, String infoText, String authors)
{
this.manufacturerName = manufacturerName;
this.modelName = modelName;
this.inquiryID = (inquiryID == null) ? "NONE" : inquiryID;
this.infoText = (infoText == null)
? "There is no information about this Device." : infoText;
this.authors = authors;
this.synthName = modelName; }
Creates a new Device instance.
@param manufacturerName The company which made the Synthesizer.
@param modelName The fixed name of the model supported by
this driver, as stated on the type plate of the engine. eg
TG33/SY22
@param inquiryID The response to the Universal Inquiry Message.
It can have wildcards (*). It can be up to 16 bytes.
Ex. [["F07E0602413F01000000020000f7"]]
@param
infoText Information about Device.
@param
authors Authors of the device driver.
protected void setup() {

setInPort(prefs**.getInt("inPort", AppConfig.getInitPortIn()));
set default MIDI in/out port
setPort(prefs.getInt("port", AppConfig.getInitPortOut())); }
Called after Device(Preferences prefs) is called.

public final Preferences getPreferences() {
return prefs; }
Returns the user's preferences, can be useful for the configuration panel.

protected JPanel config() {
JPanel panel = new JPanel();
panel.add(new JLabel("This Device has no configuration options."));
return panel; }
Create a configuration panel. Override this if your device
supports a configuration panel.

Getters:

public final String getManufacturerName () {
return manufacturerName; }
Getter for property getManufacturerName.
@return Value of property getManufacturerName.
public final String getModelName () {
return modelName; }
Getter for property modelName.
@return Value of property modelName.
public final String getInquiryID() {
return inquiryID; }
Getter for property inquiryID.
@return Value of property inquiryID.
public final String getInfoText() {
return infoText; }
Getter for property infoText.
@return Value of property infoText.
public final String getAuthors() {
return authors; }
Getter for property authors.
@return Value of property authors.
public String getSynthName () {
return prefs.get("synthName", modelName); }
Getter for property synthName.
@return Value of property synthName.
(overridden by XMLDevice)
protected final void setSynthName (String synthName) {
prefs.put("synthName", synthName); }
Setter for property synthName. The synthName is your personal
naming of the device. A user can change it in the first column
of the Synth-Configuration dialog. modelName is used as
default value. A synth driver should not use this.
@param synthName New value of property synthName.
public final int getChannel () {
return prefs.getInt("channel", 1); }
Getter for property channel.
@return Value of property channel.
public final int getDeviceID() {
int deviceID = prefs.getInt("deviceID", -1);
return deviceID == -1 ? getChannel() : deviceID; }
Getter for property deviceID.
@return Value of property deviceID. (For backward compatibility if this has the initial value (-1), The value of channel is used as deviceID.

Setters:

protected final void setChannel (int channel) {
prefs.putInt("channel", channel); }
Setter for property channel which is used for playPatch, etc.
The value must be 1 or greater than 1, and 16 or less than 16.
A synth driver may use this method to set default value.
Some old drivers use this for device ID. Use setDeviceID
method to set device ID.
@param channel The value must be 1 or greater than 1, and 16 or
less than 16.
protected final void setDeviceID(int deviceID) {
prefs.putInt("deviceID", deviceID); }
Setter for property deviceID. The value must be 1 or greater
than 1, and 256 or less than 256. A synth driver may use this
to set default device ID.
For backward compatibility if this has the initial value (-1),
The value of channel is used as device ID.
@param deviceID The value must be 1 or greater than 1, and 256
or less than 256.


public final int getPort () {
return prefs.getInt("port", AppConfig.getInitPortOut()); }
Getter for property port (MIDI output port).
@return Value of property port.


protected final void setPort (int port) {
if (!MidiUtil.isOutputAvailable())
return;

if (!initPort
getPort() != port) {
don't close
if (rcvr != null) rcvr.close();
try {
rcvr = MidiUtil.getReceiver(port);
} catch (MidiUnavailableException e) {
ErrorMsg.reportStatus(e);
}
}
prefs.putInt("port", port);
initPort = true; }
Setter for property port, the MIDI output port number, where
the cable to the device is connected. A synth driver
should not use this.
@param port New value of property port.

protected final void setMidiBufSize(int bufSize, int delay) {
midiOutBufSize = bufSize;
midiOutDelay = delay; }
If the target MIDI device cannot handle a whole Sysex message and
requires to divide the Sysex Message into several small messages, use
this method.

@param bufSize
MIDI message size. If zero, whole MIDI message is passed to
lower MIDI driver.
@param delay
delay (msec) after every MIDI message transfer.

public final void send(MidiMessage message) {
if (rcvr == null)
return;
try {
if (midiOutBufSize == 0 && AppConfig.getMidiOutBufSize() == 0)
MidiUtil.send(rcvr, message);
else
MidiUtil.send(rcvr, message,
Math.min(midiOutBufSize, AppConfig.getMidiOutBufSize()),
Math.max(midiOutDelay, AppConfig.getMidiOutDelay()));
} catch (MidiUnavailableException e) {
ErrorMsg.reportStatus(e);
} catch (InvalidMidiDataException e) {
ErrorMsg.reportStatus(e);
}
}
send MidiMessage to MIDI output. Called by Driver.send().

public final int getInPort () {
return prefs.getInt("inPort", AppConfig.getInitPortIn()); }
Getter for property inPort.
@return Value of property inPort.


protected final void setInPort (int inPort) {
if (!MidiUtil.isInputAvailable())
return;

if (!initInPort
getInPort() != inPort)
MidiUtil.setSysexInputQueue(inPort);
prefs.putInt("inPort", inPort);
initInPort = true; }
Setter for property inPort, the MIDI input port number, where
the cable <B>to</B> the device is connected. A synth driver
should not use this.
@param inPort New value of property inPort.

Getters/Setters, etc for Drivers:

public final void addDriver(IDriver driver) {
driver.setDevice(this);
driverList.add(driver); }
Add Driver. Usually a constructor of a subclass of
Device calls this.
Bulk converters must be added
before simple drivers!

@param driver IDriver to be added.
@see IConverter

final int driverCount() {
return this.driverList.size(); }
Size query for driverList.

protected final IDriver getDriver(int i) {
return (IDriver) this.driverList.get(i); }
Indexed getter for driverList elements.

final int getDriverNum(IDriver drv) {
return driverList.indexOf(drv); }
Returns the index of a Driver

final IDriver removeDriver(int i) {
return (IDriver) this.driverList.remove(i); }
Remover for driverList elements.

final int getDeviceNum() {
return AppConfig.getDeviceIndex(this); }
getter for device number.

public String getDeviceName() {
String di = "";
try {
di = MidiUtil.getOutputName(getPort()); } catch (Exception ex) {}
return getManufacturerName() + " " + getModelName()
+ " <" + getSynthName() + "> - MIDI Out Port: "
+ ((di == "") ? "None" : di)
+ " - MIDI Channel: " + getChannel(); }
Getter for DeviceName.
@return String of Device Name with inPort and Channel.

public String toString() {
return getDeviceName(); }
Same as getDeviceName().
See #getDeviceName

public void showDetails(Frame owner) {
DeviceDetailsDialog ddd = new DeviceDetailsDialog(owner, this);
ddd.setVisible(true); }
}
Show a dialog for the details of the device.