diff options
author | Darin Wright | 2008-01-28 15:04:24 +0000 |
---|---|---|
committer | Darin Wright | 2008-01-28 15:04:24 +0000 |
commit | df58149b7c876b39c609e095fb247a356fe402cc (patch) | |
tree | d9f91b962628b8e372e39cb7622d0819d64001a3 /org.eclipse.debug.examples.core/src | |
parent | b4150306417b8daa5a0847c09392316930014fd7 (diff) | |
download | eclipse.platform.debug-df58149b7c876b39c609e095fb247a356fe402cc.tar.gz eclipse.platform.debug-df58149b7c876b39c609e095fb247a356fe402cc.tar.xz eclipse.platform.debug-df58149b7c876b39c609e095fb247a356fe402cc.zip |
initial release of additional MIDI example
Diffstat (limited to 'org.eclipse.debug.examples.core/src')
6 files changed, 573 insertions, 1 deletions
diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunch.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunch.java new file mode 100644 index 000000000..b47b5a276 --- /dev/null +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunch.java @@ -0,0 +1,145 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.examples.core.midi.launcher; + +import javax.sound.midi.MidiFileFormat; +import javax.sound.midi.Sequencer; + +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.Launch; +import org.eclipse.debug.core.model.ISuspendResume; + +/** + * A launch containing a MIDI sequencer. + * + * @since 1.0 + */ +public class MidiLaunch extends Launch implements ISuspendResume { + + /** + * MIDI Sequencer + */ + private Sequencer fSequencer; + + /** + * MIDI file format + */ + private MidiFileFormat fFormat; + + /** + * Constructs a new MIDI launch. + * + * @param launchConfiguration configuration to play + * @param mode mode to play in + */ + public MidiLaunch(ILaunchConfiguration launchConfiguration, String mode) { + super(launchConfiguration, mode, null); + } + + /** + * Sets the sequencer used to play MIDI files. + * + * @param sequencer + */ + public void setSequencer(Sequencer sequencer) { + fSequencer = sequencer; + fireChanged(); + } + + /** + * Sets the format of the sequence + * @param format + */ + public void setFormat(MidiFileFormat format) { + fFormat = format; + } + + /** + * Returns the sequencer used to play MIDI files. + * + * @return the sequencer used to play MIDI files + */ + public Sequencer getSequencer() { + return fSequencer; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.Launch#canTerminate() + */ + public boolean canTerminate() { + return getSequencer().isOpen(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.Launch#isTerminated() + */ + public boolean isTerminated() { + if (fSequencer != null) { + return !fSequencer.isOpen(); + } + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.Launch#terminate() + */ + public void terminate() throws DebugException { + getSequencer().stop(); + getSequencer().close(); + fireTerminate(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISuspendResume#canResume() + */ + public boolean canResume() { + return isSuspended(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend() + */ + public boolean canSuspend() { + if (fSequencer != null) { + return fSequencer.isRunning(); + } + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended() + */ + public boolean isSuspended() { + if (fSequencer != null) { + return fSequencer.isOpen() & !fSequencer.isRunning(); + } + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISuspendResume#resume() + */ + public void resume() throws DebugException { + getSequencer().start(); + fireChanged(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISuspendResume#suspend() + */ + public void suspend() throws DebugException { + getSequencer().stop(); + fireChanged(); + } + + +} diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunchDelegate.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunchDelegate.java new file mode 100644 index 000000000..221fef395 --- /dev/null +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/MidiLaunchDelegate.java @@ -0,0 +1,117 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.examples.core.midi.launcher; + +import java.io.BufferedInputStream; +import java.io.IOException; + +import javax.sound.midi.InvalidMidiDataException; +import javax.sound.midi.MidiFileFormat; +import javax.sound.midi.MidiSystem; +import javax.sound.midi.MidiUnavailableException; +import javax.sound.midi.Sequencer; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.model.LaunchConfigurationDelegate; +import org.eclipse.debug.examples.core.pda.DebugCorePlugin; + +/** + * Creates and starts a MIDI sequencer. + * + * @since 1.0 + */ +public class MidiLaunchDelegate extends LaunchConfigurationDelegate { + + /** + * Identifier for the MIDI launch configuration type + * (value <code>midi.launchType</code>) + */ + public static final String ID_MIDI_LAUNCH_CONFIGURATION_TYPE = "midi.launchType"; + + /** + * Launch configuration attribute for the MIDI file to play + * (value <code>midi.file</code>) + */ + public static final String ATTR_MIDI_FILE = "midi.file"; + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor) + */ + public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { + String fileName = configuration.getAttribute(ATTR_MIDI_FILE, (String)null); + if (fileName == null) { + abort("MIDI file not specified.", null); + } + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + IFile file = root.getFile(new Path(fileName)); + if (!file.exists()) { + abort("MIDI file does not exist.", null); + } + Sequencer sequencer = null; + MidiFileFormat fileFormat = null; + try { + sequencer = MidiSystem.getSequencer(); + sequencer.open(); + IPath location = file.getLocation(); + if (location != null) { + fileFormat = MidiSystem.getMidiFileFormat(location.toFile()); + } + } catch (MidiUnavailableException e) { + abort("Cannot initialize sequencer.", e); + } catch (InvalidMidiDataException e) { + abort("Invalid MIDI file.", e); + } catch (IOException e) { + abort("Error reading MIDI file.", e); + } + BufferedInputStream stream = new BufferedInputStream(file.getContents()); + try { + sequencer.setSequence(stream); + } catch (IOException e) { + abort("Error reading MIDI file", e); + } catch (InvalidMidiDataException e) { + abort("Inavlid MIDI file.", e); + } + MidiLaunch midiLaunch = (MidiLaunch)launch; + midiLaunch.setSequencer(sequencer); + midiLaunch.setFormat(fileFormat); + sequencer.start(); + } + + /** + * Throws an exception with a new status containing the given + * message and optional exception. + * + * @param message error message + * @param e underlying exception + * @throws CoreException + */ + private void abort(String message, Throwable e) throws CoreException { + throw new CoreException(new Status(IStatus.ERROR, DebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier(), 0, message, e)); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate2#getLaunch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String) + */ + public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException { + return new MidiLaunch(configuration, mode); + } + +} diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/SequencerControl.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/SequencerControl.java new file mode 100644 index 000000000..945036875 --- /dev/null +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/SequencerControl.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.examples.core.midi.launcher; + +import javax.sound.midi.Sequencer; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.debug.core.DebugEvent; +import org.eclipse.debug.core.DebugPlugin; + +/** + * Controls some aspect of a MIDI sequencer. + * + * @since 1.0 + */ +public abstract class SequencerControl { + + /** + * The launch + */ + private MidiLaunch fLaunch; + + /** + * Control name + */ + private String fName; + + /** + * Constructs a control with the given name. + */ + public SequencerControl(String name, MidiLaunch launch) { + fName = name; + fLaunch = launch; + } + + /** + * Returns the launch this control is associated with. + * + * @return MIDI launch + */ + public MidiLaunch getLaunch() { + return fLaunch; + } + + /** + * Returns the sequencer associated with this control. + * + * @return associated sequencer + */ + public Sequencer getSequencer() { + return fLaunch.getSequencer(); + } + + /** + * Returns the name of this control. + * + * @return control name + */ + public String getName() { + return fName; + } + + /** + * Returns this controls current value. + * + * @return current value + */ + public abstract String getValue(); + + /** + * Whether this contol's value can be modified. + * + * @return Whether this contol's value can be modified + */ + public abstract boolean isEditable(); + + /** + * Returns a status indicating if the given value is + * a valid value for this control to accept. + * + * @param value new value + * @return whether the value is valid + */ + public abstract IStatus validateValue(String value); + + /** + * Sets the value of this control to the given value + * and returns a status indicating if the value was + * successfully set. + * + * @param newValue value + * @return whether successful + */ + public abstract IStatus setValue(String newValue); + + /** + * Fires a debug event. + * + * @param event debug event to fire + */ + public void fireEvent(DebugEvent event) { + DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event}); + } +} diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TempoControl.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TempoControl.java new file mode 100644 index 000000000..f6cc7f2ad --- /dev/null +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TempoControl.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.examples.core.midi.launcher; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.core.DebugEvent; +import org.eclipse.debug.examples.core.pda.DebugCorePlugin; + +/** + * Controls the tempo of a sequencer. + * + * @since 1.0 + */ +public class TempoControl extends SequencerControl { + + /** + * Constructs a tempo control for the given launch. + */ + public TempoControl(MidiLaunch launch) { + super("Tempo (BPM)", launch); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#getValue() + */ + public String getValue() { + float bpm = getSequencer().getTempoInBPM(); + return Float.toString(bpm); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#isEditable() + */ + public boolean isEditable() { + return getSequencer().isOpen(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#setValue(java.lang.String) + */ + public IStatus setValue(String newValue) { + try { + float value = getFloat(newValue); + getSequencer().setTempoInBPM(value); + fireEvent(new DebugEvent(this, DebugEvent.CHANGE)); + return Status.OK_STATUS; + } catch (CoreException e) { + return e.getStatus(); + } + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#validateValue(java.lang.String) + */ + public IStatus validateValue(String value) { + try { + getFloat(value); + return Status.OK_STATUS; + } catch (CoreException e) { + return e.getStatus(); + } + } + + /** + * Returns a float for the string. + * + * @param value string + * @return float + * @throws CoreException if not a valid value + */ + protected float getFloat(String value) throws CoreException { + try { + return Float.parseFloat(value); + } catch (NumberFormatException e) { + throw new CoreException(new Status(IStatus.ERROR, DebugCorePlugin.PLUGIN_ID, "Tempo must be a number", e)); + } + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object obj) { + if (obj instanceof TempoControl) { + return ((TempoControl)obj).getSequencer().equals(getSequencer()); + + } + return false; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + public int hashCode() { + return getSequencer().hashCode() + getClass().hashCode(); + } + +} diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TimeControl.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TimeControl.java new file mode 100644 index 000000000..6f0be95e4 --- /dev/null +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/midi/launcher/TimeControl.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.examples.core.midi.launcher; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.examples.core.pda.DebugCorePlugin; + +/** + * Controls the location of the sequencer in microseconds. + * + * @since 1.0 + */ +public class TimeControl extends SequencerControl { + + /** + * @param name + * @param launch + */ + public TimeControl(MidiLaunch launch) { + super("Time" , launch); + } + + /** + * Returns a long for the string. + * + * @param value string + * @return long + * @throws CoreException if not a valid value + */ + protected long getLong(String value) throws CoreException { + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + throw new CoreException(new Status(IStatus.ERROR, DebugCorePlugin.PLUGIN_ID, "Time must be a number", e)); + } + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#getValue() + */ + public String getValue() { + long position = getSequencer().getMicrosecondPosition(); + int milli = (int) (position & 0x3F); + int sec = (int) (position / 1000000); + StringBuffer clock = new StringBuffer(); + clock.append(sec); + clock.append(':'); + clock.append(milli); + return clock.toString(); + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#isEditable() + */ + public boolean isEditable() { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#setValue(java.lang.String) + */ + public IStatus setValue(String newValue) { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.examples.core.midi.launcher.SequencerControl#validateValue(java.lang.String) + */ + public IStatus validateValue(String value) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/pda/DebugCorePlugin.java b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/pda/DebugCorePlugin.java index f58fe8613..be74eb78a 100644 --- a/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/pda/DebugCorePlugin.java +++ b/org.eclipse.debug.examples.core/src/org/eclipse/debug/examples/core/pda/DebugCorePlugin.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2007 IBM Corporation and others. + * Copyright (c) 2005, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -55,6 +55,11 @@ public class DebugCorePlugin extends Plugin { public static final String ID_PDA_LAUNCH_CONFIGURATION_TYPE = "pda.launchType"; /** + * Plug-in identifier. + */ + public static final String PLUGIN_ID = "org.eclipse.debug.examples.core"; + + /** * The constructor. */ public DebugCorePlugin() { |