File: Player.java

package info (click to toggle)
freetts 1.2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,244 kB
  • sloc: java: 21,305; xml: 1,340; sh: 969; lisp: 587; ansic: 241; makefile: 25; awk: 11
file content (524 lines) | stat: -rw-r--r-- 13,889 bytes parent folder | download | duplicates (4)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
 * Copyright 2001 Sun Microsystems, Inc.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.File;

import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Player extends JFrame {

    private PlayerModel playerModel;
    private PlayerPanel playerPanel;
    private PlayerMenuBar playerMenuBar;

    private Font globalFont;
    private String globalFontFace = "Arial";
    
            
    /**
     * Constructs a Player with the given title.
     *
     * @param title the title of the Player
     */
    public Player(String title) {
	super(title);

	setDefaultLookAndFeelDecorated(true);
	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    playerModel.close();
		    System.exit(0);
		}
	    });

	playerModel = new PlayerModelImpl();
	playerPanel = new PlayerPanel(playerModel);
	setSize(playerPanel.getSize());
	getContentPane().add(playerPanel, BorderLayout.CENTER);
    }


    /**
     * Returns the view model of this Player application.
     *
     * @return the view model
     */
    public PlayerPanel getView() {
	return playerPanel;
    }
    

    /**
     * Returns the data model of this Player application.
     *
     * @return the data model
     */
    public PlayerModel getModel() {
	return playerModel;
    }
    

    /**
     * Sets the menubar for this Player.
     *
     * @param menubar the menubar
     */
    public void setMenuBar(PlayerMenuBar menubar) {
	playerMenuBar = menubar;
	setJMenuBar(playerMenuBar);
    }


    /**
     * Show/hide the current monitor from the applcation frame.
     *
     * @param show true to show, false to hide
     */
    public void showMonitor(boolean show) {
	Monitor monitor = playerModel.getMonitor();
	int newHeight = getSize().height;
	int monitorHeight = monitor.getSize().height;

	if (show) {
	    playerPanel.add(monitor, BorderLayout.SOUTH);
	    newHeight += monitorHeight;
	} else {
	    playerPanel.remove(monitor);
	    newHeight -= monitorHeight;
	}

	Dimension newSize = new Dimension(getSize().width, newHeight);
	playerPanel.setSize(newSize);
	setSize(newSize);

	playerModel.setMonitorVisible(show);
        repaint();
    }


    /**
     * Changes the font size of all components in this Player.
     * 
     * @param change the change in font size
     */
    public void setGlobalFontSize(int fontSize) {
	if (globalFont == null) {
	    globalFont = getFont();
	}
	globalFont = new Font(globalFontFace, Font.BOLD, fontSize);
		
	UIManager.put("Button.font", globalFont);
	UIManager.put("ComboBox.font", globalFont);
	UIManager.put("Label.font", globalFont);
	UIManager.put("List.font", globalFont);
	UIManager.put("Menu.font", globalFont);
	UIManager.put("MenuItem.font", globalFont);
	UIManager.put("TextArea.font", globalFont);
	UIManager.put("ToggleButton.font", globalFont);
	UIManager.put("ToolTip.font", globalFont);

	setFont(globalFont);
	
	SwingUtilities.updateComponentTreeUI(this);
	repaint();
    }


    /**
     * A convenience method for setting the Look & Feel.
     *
     * @param lookAndFeel the Look & Feel specification
     */
    public void setLookAndFeel(Object lookAndFeel) {
	try {
	    if (lookAndFeel instanceof String) {
		UIManager.setLookAndFeel((String) lookAndFeel);
	    } else if (lookAndFeel instanceof LookAndFeel) {
		UIManager.setLookAndFeel((LookAndFeel) lookAndFeel);
	    }

	    SwingUtilities.updateComponentTreeUI(this);
	    repaint();
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
    }

    /**
     * Sets the voice for the Player.  The main purpose of this
     * method is to set the Player to a nice voice on startup.
     */
    public void setVoice(String voiceName) {
        PlayerModel model = getModel();
        ListModel descList = model.getSynthesizerList();
        for (int i = 0; i < descList.getSize(); i++) {
            SynthesizerModeDesc desc = (SynthesizerModeDesc)
                descList.getElementAt(i);
            Voice[] voices = desc.getVoices();
            for (int j = 0; j < voices.length; j++) {
                if (voices[j].getName().equals(voiceName)) {
                    model.setSynthesizer(i);
                    model.setVoice(j);
                    break;
                }
            }
        }
    }
    

    /**
     * The main() method of the Player.
     */
    public static void main(String[] args) throws Exception {
	boolean showMonitor = false;
        String firstVoice = "kevin16";
        
	Player player = new Player("FreeTTS Player");

	for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-voice")) {
                if (++i < args.length) {
		    firstVoice = args[i];
                }
            } else if (args[i].equals("-fontsize")) {
		if (++i < args.length) {
		    player.setGlobalFontSize(Integer.parseInt(args[i]));
		}
	    } else if (args[i].equals("-monitor")) {
		showMonitor = true;
	    } else if (args[i].equals("-input")) {

		PlayerModel model = player.getModel();

		for (i++; i < args.length; i++) {
		    int start = args[i].indexOf(':');
		    if (start > -1) {
			String content = args[i].substring(start+1);
			System.out.println(content);
			if (args[i].startsWith("plaintext:")) {
			    model.addPlayable
				(Playable.createTextPlayable(content));
			} else if (args[i].startsWith("textfile:")) {
			    model.addPlayable
				(Playable.createTextFilePlayable
				 (new File(content)));
			} else if (args[i].startsWith("jsmltext:")) {
			    model.addPlayable
				(Playable.createJSMLPlayable(content));
			} else if (args[i].startsWith("jsmlfile:")) {
			    model.addPlayable
				(Playable.createJSMLFilePlayable
				 (new File(content)));
			}
		    }
		}
	    }
        }

	player.setMenuBar(new PlayerMenuBar(player));
	player.getModel().createSynthesizers();
	player.setVoice(firstVoice);
        
	player.show();

	if (showMonitor) {
	    player.showMonitor(showMonitor);
	}
    }
}

/**
 * Implements the menubar of the Player application.
 */
class PlayerMenuBar extends JMenuBar {
    private Player player;
    private PlayerModel playerModel;

    private JFileChooser fileChooser;
    
    private JMenuItem fileSpeakJSMLMenuItem;
    private JMenuItem fileSpeakTextMenuItem;
    private JMenuItem fileSpeakURLMenuItem;
    private JMenuItem fileExitMenuItem;

    private JMenuItem styleLFCrossPlatformMenuItem;
    private JMenuItem styleLFSystemMenuItem;
    private JMenuItem styleFontSizeLargerMenuItem;
    private JMenuItem styleFontSizeSmallerMenuItem;

    private JMenuItem monitorHideMenuItem;
    private JMenuItem monitorShowMenuItem;
                
    private static char crossPlatformMnemonic = 'C';
    private static char exitMnemonic = 'X';
    private static char fileMnemonic = 'F';
    private static char hideMonitorMnemonic = 'H';
    private static char jsmlMnemonic = 'J';
    private static char monitorMnemonic = 'M';
    private static char showMonitorMnemonic = 'S';
    private static char speakMnemonic = 'S';
    private static char styleMnemonic = 'E';
    private static char styleLFMnemonic = 'L';
    private static char systemMnemonic = 'S';
    private static char textMnemonic = 'T';
    private static char urlMnemonic = 'U';


    /**
     * Constructs a menubar with the given Player data model.
     *
     * @param playerModel the Player data model
     */
    public PlayerMenuBar(Player player) {
	super();
	this.player = player;
	this.playerModel = player.getModel();
	this.fileChooser = new JFileChooser();
	fileChooser.setCurrentDirectory
	    (new File(System.getProperty("user.dir")));

	add(createFileMenu());
	add(createStyleMenu());
	add(createMonitorMenu());
    }


    /**
     * Creates the File menu.
     *
     * @return the File menu
     */
    private JMenu createFileMenu() {
	JMenu fileMenu = new JMenu("File");
	fileMenu.setMnemonic(fileMnemonic);
	add(fileMenu);
	
	JMenu fileSpeakMenu = new JMenu("Speak");
	fileSpeakMenu.setMnemonic(speakMnemonic);
	
	fileSpeakJSMLMenuItem = new JMenuItem("JSML File ...");
	fileSpeakTextMenuItem = new JMenuItem("Text File ...");
	fileSpeakURLMenuItem = new JMenuItem("URL ...");
	fileSpeakJSMLMenuItem.setMnemonic(jsmlMnemonic);
	fileSpeakTextMenuItem.setMnemonic(textMnemonic);
	fileSpeakURLMenuItem.setMnemonic(urlMnemonic);
		
	fileSpeakMenu.add(fileSpeakJSMLMenuItem);
	fileSpeakMenu.add(fileSpeakTextMenuItem);
	fileSpeakMenu.add(fileSpeakURLMenuItem);
				
	fileExitMenuItem = new JMenuItem("Exit");
	fileExitMenuItem.setMnemonic(exitMnemonic);
	
	fileMenu.add(fileSpeakMenu);
	fileMenu.addSeparator();
	fileMenu.add(fileExitMenuItem);
	
	addFileMenuListeners();
	
	return fileMenu;
    }


    /**
     * Adds the given Playaable to the play list, and plays it.
     */
    private void playPlayable(Playable playable) {
	playerModel.addPlayable(playable);
	player.getView().getPlayList().setSelectedValue(playable, true);
	playerModel.play(playable);
    }


    /**
     * Add listenrs to the file menu.
     */
    private void addFileMenuListeners() {
	fileExitMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    playerModel.close();
		    System.exit(0);
		}
	    });
	fileSpeakJSMLMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    File file = chooseFile();
		    if (file != null) {
			playPlayable(Playable.createJSMLFilePlayable(file));
		    }
		}
	    });
	fileSpeakTextMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    File file = chooseFile();
		    if (file != null) {
			playPlayable(Playable.createTextFilePlayable(file));
		    }
		}
	    });
	fileSpeakURLMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    String url = JOptionPane.showInputDialog
			(getParent(), "Enter URL:", "Speak URL",
			 JOptionPane.QUESTION_MESSAGE);
		    if (url != null && url.length() > 0) {
			playPlayable(Playable.createURLPlayable(url));
		    }
		}
	    });
    }


    /**
     * Creates the Style menu.
     *
     * @return the Style menu
     */
    private JMenu createStyleMenu() {
	JMenu styleMenu = new JMenu("Style");
	styleMenu.setMnemonic(styleMnemonic);
	
	JMenu styleLFMenu = new JMenu("Look & Feel");
	styleLFMenu.setMnemonic(styleLFMnemonic);
	
	styleLFCrossPlatformMenuItem = new JMenuItem("Cross Platform");
	styleLFCrossPlatformMenuItem.setMnemonic(crossPlatformMnemonic);
	
	styleLFSystemMenuItem = new JMenuItem("System");
	styleLFSystemMenuItem.setMnemonic(systemMnemonic);
	
	styleLFMenu.add(styleLFCrossPlatformMenuItem);
	styleLFMenu.add(styleLFSystemMenuItem);
	
	styleMenu.add(styleLFMenu);
	
	addStyleMenuListeners();
	
	return styleMenu;
    }


    /**
     * Add listeners to the Style menu.
     */
    private void addStyleMenuListeners() {
	styleLFCrossPlatformMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    player.setLookAndFeel
			(UIManager.getCrossPlatformLookAndFeelClassName());
		}
	    });
	styleLFSystemMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    player.setLookAndFeel
			(UIManager.getSystemLookAndFeelClassName());
		}
	    });
    }


    /**
     * Creates the Style menu.
     *
     * @return the Style menu
     */
    private JMenu createMonitorMenu() {
	JMenu monitorMenu = new JMenu("Monitor");
	monitorMenu.setMnemonic(monitorMnemonic);
	
	monitorShowMenuItem = new JMenuItem("Show");
	monitorShowMenuItem.setMnemonic(showMonitorMnemonic);
	monitorShowMenuItem.setEnabled(!playerModel.isMonitorVisible());
		
	monitorHideMenuItem = new JMenuItem("Hide");
	monitorHideMenuItem.setMnemonic(hideMonitorMnemonic);
	monitorHideMenuItem.setEnabled(playerModel.isMonitorVisible());
		
	monitorMenu.add(monitorShowMenuItem);
	monitorMenu.add(monitorHideMenuItem);
		
	addMonitorMenuListeners();
	
	return monitorMenu;
    }


    /**
     * Add listeners to the monitor menu.
     */
    private void addMonitorMenuListeners() {
	monitorShowMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    monitorShowMenuItem.setEnabled(false);
		    monitorHideMenuItem.setEnabled(true);

		    player.showMonitor(true);
		}
	    });
	monitorHideMenuItem.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    monitorShowMenuItem.setEnabled(true);
		    monitorHideMenuItem.setEnabled(false);
	
		    SwingUtilities.invokeLater
			(new Runnable() {
				public void run() {
				    player.showMonitor(false);
				}
			    });
		}
	    });
    }
    


    /**
     * Returns the JFrame which this menu bar belongs to.
     *
     * @return the JFrame which this menu bar belongs
     */
    private JFrame getFrame() {
	return (JFrame) player;
    }


    /**
     * Shows a JFileChooser screen and returns the chosen file.
     *
     * @return the chosen file
     */
    private File chooseFile() {
	int option = fileChooser.showOpenDialog(getParent());
	if (option == JFileChooser.APPROVE_OPTION) {
	    return fileChooser.getSelectedFile();
	} else {
	    return null;
	}
    }
}