001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.audiodemo;
033:
034: import java.io.*;
035:
036: import javax.microedition.lcdui.*;
037: import javax.microedition.media.*;
038: import javax.microedition.media.control.*;
039: import javax.microedition.midlet.*;
040:
041: public class MixCanvas extends Canvas implements CommandListener {
042: private static final String TITLE_TEXT = "Mix Demo";
043: private static final int[] notes = { 69, 70, 71, 72, 73, 74, 75, 76 };
044: static Player wavPlayer = null;
045: static Player tonePlayer = null;
046: static Image logo = null;
047: private int idx = 0;
048: private int ip = 0;
049: Display parentDisplay;
050: private Command backCommand = new Command("Back", Command.BACK, 1);
051: private Command playCommand = new Command("Play", Command.ITEM, 1);
052: private Command pauseCommand = new Command("Pause", Command.ITEM, 1);
053: private Command toneCommand = new Command("Tone", Command.ITEM, 1);
054: private Alert alert;
055:
056: //In case the user ended the player using the back command
057: //or the 'End' button (the red one), if the WavPlayer did not
058: //start yet there is a possibility that it will start after a while.
059: //stopSound is here to catch this case and avoid playing the sound.
060: private boolean stopSound = false;
061:
062: public MixCanvas(Display parentDisplay) {
063: super ();
064: this .idx = 0;
065: this .parentDisplay = parentDisplay;
066: initialize();
067: }
068:
069: void initialize() {
070: addCommand(backCommand);
071: setCommandListener(this );
072:
073: try {
074: logo = Image.createImage("/icons/Duke.png");
075: } catch (Exception ex) {
076: logo = null;
077: }
078:
079: if (logo == null) {
080: System.out.println("can not load Duke.png");
081: }
082:
083: alert = new Alert("Warning", "Can not create player", null,
084: null);
085: alert.setTimeout(1000);
086: }
087:
088: /*
089: * Respond to commands, including back
090: */
091: public void commandAction(Command c, Displayable s) {
092: if (s == this ) {
093: if (c == backCommand) {
094: stopSound();
095: parentDisplay.setCurrent(MixTest.getList());
096: } else if (c == toneCommand) {
097: try {
098: Manager.playTone(notes[ip], 1000, 100);
099: ip++;
100:
101: if (ip >= 8) {
102: ip = 0;
103: }
104: } catch (Exception ex) {
105: System.out.println("get an exception for tone");
106: }
107: } else if (c == playCommand) {
108: playSound();
109: } else if (c == pauseCommand) {
110: pauseSound();
111: }
112: }
113: }
114:
115: public void setIndex(int idx) {
116: this .idx = idx;
117: }
118:
119: private void createWavPlayer() {
120: try {
121: if (wavPlayer == null) {
122: if (MixTest.wavUrl.startsWith("resource")) {
123: int idx = MixTest.wavUrl.indexOf(':');
124: String loc = MixTest.wavUrl.substring(idx + 1);
125: InputStream is = getClass()
126: .getResourceAsStream(loc);
127: String ctype = guessContentType(MixTest.wavUrl);
128: wavPlayer = Manager.createPlayer(is, ctype);
129: } else {
130: wavPlayer = Manager.createPlayer(MixTest.wavUrl);
131: }
132:
133: wavPlayer.setLoopCount(-1);
134: }
135:
136: if (stopSound) {
137: return;
138: }
139:
140: wavPlayer.start();
141: } catch (Exception ex) {
142: // ex.printStackTrace();
143: if (wavPlayer != null) {
144: wavPlayer.close();
145: wavPlayer = null;
146: }
147:
148: parentDisplay.setCurrent(alert);
149: }
150: }
151:
152: private static String guessContentType(String url) throws Exception {
153: String ctype;
154:
155: // some simple test for the content type
156: if (url.endsWith("wav")) {
157: ctype = "audio/x-wav";
158: } else if (url.endsWith("jts")) {
159: ctype = "audio/x-tone-seq";
160: } else if (url.endsWith("mid")) {
161: ctype = "audio/midi";
162: } else {
163: throw new Exception("Cannot guess content type from URL: "
164: + url);
165: }
166:
167: return ctype;
168: }
169:
170: private void createTonePlayer() {
171: byte d = 8;
172: byte C4 = ToneControl.C4;
173: byte D4 = ToneControl.C4 + 2; // a whole step
174: byte E4 = ToneControl.C4 + 4; // a major third
175: byte G4 = ToneControl.C4 + 7; // a fifth
176: byte rest = ToneControl.SILENCE; // eighth-note rest
177:
178: byte[] mySequence = new byte[] { ToneControl.VERSION, 1,
179: ToneControl.TEMPO, 30, ToneControl.BLOCK_START, 0, E4,
180: d, D4, d, C4, d, D4, d, E4, d, E4, d, E4, d, rest, d,
181: ToneControl.BLOCK_END, 0, ToneControl.PLAY_BLOCK, 0,
182: D4, d, D4, d, D4, d, rest, d, E4, d, G4, d, G4, d,
183: rest, d, //play "B" section
184: ToneControl.PLAY_BLOCK, 0, // content of "A" section
185: D4, d, D4, d, E4, d, D4, d, C4, d, rest, d // play "C" section
186: };
187:
188: try {
189: if (tonePlayer == null) {
190: tonePlayer = Manager
191: .createPlayer(Manager.TONE_DEVICE_LOCATOR);
192: tonePlayer.setLoopCount(-1);
193: tonePlayer.realize();
194:
195: ToneControl tc = (ToneControl) tonePlayer
196: .getControl("javax.microedition.media.control.ToneControl");
197: tc.setSequence(mySequence);
198: }
199:
200: if ((tonePlayer != null) && !stopSound) {
201: tonePlayer.start();
202: }
203: } catch (Exception ex) {
204: // ex.printStackTrace();
205: if (tonePlayer != null) {
206: tonePlayer.close();
207: tonePlayer = null;
208: }
209:
210: parentDisplay.setCurrent(alert);
211: }
212: }
213:
214: public void playSound() {
215: stopSound = false;
216:
217: switch (idx) {
218: case 0: // wave + tone
219: addCommand(toneCommand);
220: removeCommand(pauseCommand);
221: removeCommand(playCommand);
222: createWavPlayer();
223:
224: break;
225:
226: case 1: // toneseq + tone
227: addCommand(toneCommand);
228: removeCommand(pauseCommand);
229: removeCommand(playCommand);
230: createTonePlayer();
231:
232: break;
233:
234: case 2: // toneseq + wave
235: removeCommand(playCommand);
236: removeCommand(toneCommand);
237: addCommand(pauseCommand);
238: createWavPlayer();
239: createTonePlayer();
240:
241: break;
242: }
243: }
244:
245: public void stopSound() {
246: stopSound = true;
247:
248: if (tonePlayer != null) {
249: tonePlayer.close();
250: tonePlayer = null;
251: }
252:
253: if (wavPlayer != null) {
254: wavPlayer.close();
255: wavPlayer = null;
256: }
257:
258: removeCommand(toneCommand);
259: removeCommand(pauseCommand);
260: removeCommand(playCommand);
261: }
262:
263: public void pauseSound() {
264: removeCommand(pauseCommand);
265: addCommand(playCommand);
266:
267: try {
268: if (wavPlayer != null) {
269: wavPlayer.stop();
270: }
271: } catch (MediaException me) {
272: System.err.println(me);
273: }
274:
275: try {
276: if (tonePlayer != null) {
277: tonePlayer.stop();
278: }
279: } catch (MediaException me) {
280: System.err.println(me);
281: }
282: }
283:
284: public boolean isPlaying() {
285: return ((tonePlayer != null) && (tonePlayer.getState() >= Player.STARTED))
286: || ((wavPlayer != null) && (wavPlayer.getState() >= Player.STARTED));
287: }
288:
289: public void paint(Graphics g) {
290: int w = getWidth();
291: int h = getHeight();
292: String cname = "";
293:
294: switch (idx) {
295: case 0:
296: cname = "test-wav.wav";
297:
298: break;
299:
300: case 1:
301: cname = "tone seq";
302:
303: break;
304:
305: case 2:
306: cname = "wave+toneseq";
307:
308: break;
309: }
310:
311: g.setColor(0);
312: g.fillRect(0, 0, w, h);
313:
314: if (logo != null) {
315: g.drawImage(logo, w / 2, 30, Graphics.TOP
316: | Graphics.HCENTER);
317: }
318:
319: g.setColor(0xFF7f00);
320: g.drawString(TITLE_TEXT, w / 2, 8, Graphics.TOP
321: | Graphics.HCENTER);
322: g.drawString(cname, 0, 84, Graphics.TOP | Graphics.LEFT);
323: }
324:
325: protected void keyPressed(int keycode) {
326: switch (keycode) {
327: case KEY_STAR:
328: changeVolume(-10);
329:
330: break;
331:
332: case KEY_POUND:
333: changeVolume(10);
334:
335: break;
336: }
337: }
338:
339: private void changeVolume(int diff) {
340: VolumeControl vc;
341:
342: if (wavPlayer != null) {
343: vc = (VolumeControl) wavPlayer.getControl("VolumeControl");
344:
345: if (vc != null) {
346: int cv = vc.getLevel();
347: cv += diff;
348: cv = vc.setLevel(cv);
349: }
350: }
351:
352: if (tonePlayer != null) {
353: vc = (VolumeControl) tonePlayer.getControl("VolumeControl");
354:
355: if (vc != null) {
356: int cv = vc.getLevel();
357: cv += diff;
358: cv = vc.setLevel(cv);
359: }
360: }
361: }
362: }
|