Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 221fef395fb5531507cc51f747f4e879469762f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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);
	}

}

Back to the top