1 | package org.apollo;
|
---|
2 |
|
---|
3 | import java.awt.Image;
|
---|
4 | import java.awt.Toolkit;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.awt.event.KeyListener;
|
---|
7 | import java.awt.event.WindowEvent;
|
---|
8 | import java.awt.event.WindowListener;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.apollo.actions.ApolloActions;
|
---|
17 | import org.apollo.agents.MelodySearch;
|
---|
18 | import org.apollo.audio.ApolloPlaybackMixer;
|
---|
19 | import org.apollo.audio.RecordManager;
|
---|
20 | import org.apollo.audio.SampledAudioManager;
|
---|
21 | import org.apollo.audio.util.FrameLayoutDaemon;
|
---|
22 | import org.apollo.audio.util.MultiTrackPlaybackController;
|
---|
23 | import org.apollo.audio.util.PlaybackClock;
|
---|
24 | import org.apollo.audio.util.SoundDesk;
|
---|
25 | import org.apollo.gui.FramePlaybackBarRenderer;
|
---|
26 | import org.apollo.gui.FrameRenderPasses;
|
---|
27 | import org.apollo.io.SampledAudioFileImporter;
|
---|
28 | import org.apollo.util.AudioSystemLog;
|
---|
29 | import org.expeditee.actions.Actions;
|
---|
30 | import org.expeditee.gui.Browser;
|
---|
31 | import org.expeditee.gui.FrameGraphics;
|
---|
32 | import org.expeditee.importer.FrameDNDTransferHandler;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Provides initialization and shutdown services for the Apollo system.
|
---|
36 | *
|
---|
37 | * @author Brook Novak
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public final class ApolloSystem {
|
---|
41 |
|
---|
42 | // TODO: Create actual frames
|
---|
43 | public static final String SYSTEM_FRAMESET_NAME = "apollosystem";
|
---|
44 | public static final String HELP_TOP_FRAMENAME = SYSTEM_FRAMESET_NAME + 2;
|
---|
45 |
|
---|
46 | // TODO: How to get good results: collection (moteef) and querry
|
---|
47 | // TODO: How to omit indexing on tracks
|
---|
48 | public static final String HELP_MELODYSEARCH_FRAMENAME = SYSTEM_FRAMESET_NAME + 3;
|
---|
49 |
|
---|
50 | public static boolean useQualityGraphics = true;
|
---|
51 |
|
---|
52 | /** Would be nice to seperate outside of this class.. */
|
---|
53 | private static boolean isControlDown = false;
|
---|
54 |
|
---|
55 | private ApolloSystem() {
|
---|
56 | }
|
---|
57 |
|
---|
58 | private static boolean hasInitialized = false;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Initializes Apollo mod for expeditee - prepares all subsystems.
|
---|
62 | */
|
---|
63 | public static void initialize() {
|
---|
64 |
|
---|
65 | if (hasInitialized) return;
|
---|
66 |
|
---|
67 | AudioSystemLog.println("Initializing...");
|
---|
68 |
|
---|
69 | SwingUtilities.invokeLater(new Runnable() {
|
---|
70 | public void run() {
|
---|
71 |
|
---|
72 | try {
|
---|
73 |
|
---|
74 |
|
---|
75 | URL url = ClassLoader.getSystemResource("org/apollo/icons/mainicon.png");
|
---|
76 |
|
---|
77 | if (url != null) {
|
---|
78 | Image img = Toolkit.getDefaultToolkit().getImage(url);
|
---|
79 | Browser._theBrowser.setIconImage(img);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 |
|
---|
84 | } catch (Exception e) {
|
---|
85 | e.printStackTrace();
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Ensure that resources are released before the application is closed.
|
---|
89 | Browser._theBrowser.addWindowListener(new WindowListener() {
|
---|
90 |
|
---|
91 | public void windowActivated(WindowEvent e) {
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void windowClosed(WindowEvent e) {
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void windowClosing(WindowEvent e) {
|
---|
98 | ApolloSystem.shutdown();
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void windowDeactivated(WindowEvent e) {
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void windowDeiconified(WindowEvent e) {
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void windowIconified(WindowEvent e) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void windowOpened(WindowEvent e) {
|
---|
111 | }
|
---|
112 |
|
---|
113 | });
|
---|
114 |
|
---|
115 | // Refresh frame for graphical helpers that display / hide when the
|
---|
116 | // user presses various keys
|
---|
117 | Browser._theBrowser.getContentPane().addKeyListener(new KeyListener() {
|
---|
118 |
|
---|
119 | public void keyPressed(KeyEvent e) {
|
---|
120 | isControlDown = e.isControlDown();
|
---|
121 | if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
|
---|
122 | FrameGraphics.refresh(false);
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void keyReleased(KeyEvent e) {
|
---|
128 | isControlDown = e.isControlDown();
|
---|
129 | if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
|
---|
130 | FrameGraphics.refresh(false);
|
---|
131 | } else if (e.getKeyCode() == KeyEvent.VK_G && e.isControlDown()) {
|
---|
132 | useQualityGraphics = !useQualityGraphics;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void keyTyped(KeyEvent e) {
|
---|
137 | }
|
---|
138 |
|
---|
139 | });
|
---|
140 | }
|
---|
141 | });
|
---|
142 |
|
---|
143 | // TODO: Have sample rates configurable (remmebr to delete all metadata upen new sample rates).
|
---|
144 | // TODO: Fix conversion for me
|
---|
145 | if (SampledAudioManager.PLAYBACK_SAMPLE_RATE != 22050.0f) {
|
---|
146 | AudioSystemLog.print("***BETA-WARNING**: Sample-rate will not give melody searches");
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Set title and icon
|
---|
150 | //Browser._theBrowser.etTitle("Apollo");
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | System.err.println("WARNING: MISSING MAIN ICON"); // TODO
|
---|
155 |
|
---|
156 | AudioSystemLog.println(" Preparing sub-systems...");
|
---|
157 |
|
---|
158 | SampledAudioManager.getInstance();
|
---|
159 |
|
---|
160 | RecordManager.getInstance();
|
---|
161 |
|
---|
162 | ApolloPlaybackMixer.getInstance();
|
---|
163 |
|
---|
164 | FrameLayoutDaemon.getInstance();
|
---|
165 |
|
---|
166 | FrameRenderPasses.getInstance();
|
---|
167 |
|
---|
168 | PlaybackClock.getInstance();
|
---|
169 |
|
---|
170 | FramePlaybackBarRenderer.getInstance();
|
---|
171 |
|
---|
172 | // Setup for importing audio
|
---|
173 | FrameDNDTransferHandler.getInstance().addCustomFileImporter(
|
---|
174 | new SampledAudioFileImporter());
|
---|
175 |
|
---|
176 | AudioSystemLog.println(" Loading actions and agents...");
|
---|
177 |
|
---|
178 | // Add apollo actions
|
---|
179 | Actions.LoadMethods(ApolloActions.class);
|
---|
180 |
|
---|
181 | Set<String> agents = new HashSet<String>();
|
---|
182 | agents.add(MelodySearch.class.getName());
|
---|
183 |
|
---|
184 | Collection<String> omitted = Actions.addAgents(agents);
|
---|
185 |
|
---|
186 | for (String agent : omitted) {
|
---|
187 |
|
---|
188 | if (agent == null || agent.length() == 0) continue;
|
---|
189 |
|
---|
190 | String name = agent;
|
---|
191 |
|
---|
192 | int index = agent.lastIndexOf('.');
|
---|
193 | if (index > 0 && agent.length() > (index - 1)) {
|
---|
194 | name = agent.substring(index);
|
---|
195 | }
|
---|
196 | AudioSystemLog.println(" WARNING: Failed to add agent \"" + name + "\"");
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 | AudioSystemLog.println(" Loading banks...");
|
---|
201 | SoundDesk.getInstance(); // loads upon creation
|
---|
202 |
|
---|
203 | AudioSystemLog.println("Initialized");
|
---|
204 |
|
---|
205 | hasInitialized = true;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 |
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Releases all resources currently used by the SampledAudioManager.
|
---|
213 | */
|
---|
214 | private static void shutdown() {
|
---|
215 |
|
---|
216 | AudioSystemLog.println("Saving banks...");
|
---|
217 | SoundDesk.getInstance().saveMasterMix();
|
---|
218 | SoundDesk.getInstance().saveMixes();
|
---|
219 |
|
---|
220 | AudioSystemLog.println("Releasing resources...");
|
---|
221 |
|
---|
222 | ApolloPlaybackMixer.getInstance().releaseResources();
|
---|
223 | RecordManager.getInstance().releaseResources(); // blocking
|
---|
224 | MultiTrackPlaybackController.getInstance().releaseResources();
|
---|
225 |
|
---|
226 | AudioSystemLog.println("Audio subsystems shutdown");
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * @return
|
---|
231 | * True if has initialized.
|
---|
232 | */
|
---|
233 | public static boolean isInitialized() {
|
---|
234 | return hasInitialized;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * @return True if control is down.
|
---|
240 | */
|
---|
241 | public static boolean isControlDown() {
|
---|
242 | return isControlDown;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | public static void setControlDown(boolean isControlDown) {
|
---|
249 | ApolloSystem.isControlDown = isControlDown;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * The apollo main just ensures that apollo is initialized during startup.
|
---|
257 | *
|
---|
258 | * This may eventually become redundant once Expeditee implements a plugin system
|
---|
259 | * that allows a plugin / mod to initialize itself at started...
|
---|
260 | *
|
---|
261 | * @param args
|
---|
262 | */
|
---|
263 | public static void main(String[] args) {
|
---|
264 |
|
---|
265 | // Run expeditee
|
---|
266 | Browser.main(args);
|
---|
267 |
|
---|
268 | // Initialize apollo
|
---|
269 | SwingUtilities.invokeLater(new Runnable() {
|
---|
270 | public void run() {
|
---|
271 | ApolloSystem.initialize();
|
---|
272 | }
|
---|
273 | });
|
---|
274 | }
|
---|
275 |
|
---|
276 | }
|
---|