import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.PitchControl;
import javax.microedition.media.control.TempoControl;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDlet;
public class ControllableMIDIMIDlet extends MIDlet implements ItemStateListener {
Player midiPlayer = null;
VolumeControl volControl = null;
PitchControl pitchControl = null;
TempoControl tempoControl = null;
Form form = new Form("MIDI Player", null);
Gauge volGauge = new Gauge("Volume: 50", true, 100, 50);
Gauge pitchGauge = new Gauge("Pitch: 0", true, 10, 5);
Gauge tempoGauge = new Gauge("Tempo: 120", true, 30, 12);
public ControllableMIDIMIDlet() {
try {
midiPlayer = Manager.createPlayer(getClass().getResourceAsStream("/a.mid"), "audio/midi");
midiPlayer.prefetch();
volControl = (VolumeControl) midiPlayer.getControl("javax.microedition.media.control.VolumeControl");
pitchControl = (PitchControl) midiPlayer.getControl("javax.microedition.media.control.PitchControl");
tempoControl = (TempoControl) midiPlayer.getControl("javax.microedition.media.control.TempoControl");
form.append(volGauge);
form.append(tempoGauge);
form.append(pitchGauge);
form.setItemStateListener(this);
Display.getDisplay(this).setCurrent(form);
} catch (Exception e) {
System.err.println(e);
}
}
public void itemStateChanged(Item item) {
if (!(item instanceof Gauge))
return;
Gauge gauge = (Gauge) item;
int val = gauge.getValue();
if (item == volGauge) {
volControl.setLevel(val);
volGauge.setLabel("Volume: " + val);
}
if (item == tempoGauge && tempoControl != null) {
tempoControl.setTempo((val) * 10 * 1000);
tempoGauge.setLabel("Tempo: " + (val * 10));
}
if (item == pitchGauge && pitchControl != null) {
pitchControl.setPitch((val - 5) * 12 * 1000);
pitchGauge.setLabel("Pitch: " + (val - 5));
}
}
public void startApp() {
try {
if (midiPlayer != null) {
midiPlayer.start();
}
} catch (Exception e) {
System.err.println(e);
}
}
public void pauseApp() {
try {
if (midiPlayer != null) {
midiPlayer.stop();
}
} catch (Exception e) {
System.err.println(e);
}
}
public void destroyApp(boolean unconditional) {
try {
if (midiPlayer != null) {
midiPlayer.close();
}
} catch (Exception e) {
System.err.println(e);
}
}
}
|