001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software 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 A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR 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.mmademo;
033:
034: import javax.microedition.midlet.*;
035: import javax.microedition.lcdui.*;
036: import javax.microedition.media.*;
037: import javax.microedition.media.control.*;
038:
039: /**
040: * An example MIDlet to demonstrate simple tones: Manager.playTone(), MIDIControl
041: *
042: * @version 1.3
043: */
044: public class SimpleTones extends BaseListMidlet implements
045: Utils.BreadCrumbTrail {
046:
047: private static final boolean USE_LONG_MIDI = false;
048:
049: // cache MIDIPlayer so that we don't open/close all the time
050: private Player mp;
051:
052: public SimpleTones() {
053: super ("MMAPI Simple Tones");
054: }
055:
056: protected void fillList(List list) {
057: list.append("Short Single Tone", null);
058: list.append("Long Single Tone", null);
059: list.append("Short MIDI event", null);
060: if (USE_LONG_MIDI) {
061: list.append("Long MIDI event", null);
062: }
063: list.append("MMAPI Drummer", null);
064: list.addCommand(exitCommand);
065: list.addCommand(playCommand);
066: }
067:
068: protected void selectCommand(int index) {
069: switch (index) {
070: case 0:
071: simpleTone(ToneControl.C4, 100);
072: break;
073: case 1:
074: simpleTone(ToneControl.C4 + 4, 1000);
075: break;
076: case 2:
077: midiShort();
078: break;
079: case 3:
080: if (USE_LONG_MIDI) {
081: midiLong();
082: break;
083: }
084: /* fall through */
085: case 4:
086: drummer();
087: break;
088: }
089: }
090:
091: public void destroyApp(boolean unconditional) {
092: if (mp != null) {
093: mp.close();
094: mp = null;
095: }
096: }
097:
098: private void simpleTone(int note, int duration) {
099: try {
100: Manager.playTone(note, duration, 80 /*vol*/);
101: } catch (Exception ex) {
102: Utils.error(ex, this );
103: }
104: }
105:
106: MIDIControl getMIDIControl() throws Exception {
107: if (mp == null) {
108: mp = Manager.createPlayer(Manager.MIDI_DEVICE_LOCATOR);
109: mp.prefetch();
110: }
111: return (MIDIControl) mp
112: .getControl("javax.microedition.media.control.MIDIControl");
113: }
114:
115: byte[] niceChord = new byte[] { 0x2B, 0x40, 0x44, 0x4C, 0x53, 0x58,
116: 0x23, 0x3B, 0x32, 0x1F };
117:
118: private void midiShort() {
119: try {
120: MIDIControl mc = getMIDIControl();
121: // some notes on channel 0
122: // 0x90: Note On
123: for (int i = 0; i < niceChord.length; i++) {
124: // Note On, note number, velocity
125: mc.shortMidiEvent(0x90, niceChord[i], 127);
126: }
127: // some drums on channel 9
128: mc.shortMidiEvent(0x99, 35, 127); // bass drum
129: mc.shortMidiEvent(0x99, 35, 0);
130: mc.shortMidiEvent(0x99, 58, 127); // vibraslap
131: mc.shortMidiEvent(0x99, 58, 0);
132: mc.shortMidiEvent(0x99, 57, 127); // crash cymbal
133: mc.shortMidiEvent(0x99, 57, 0);
134: Thread.sleep(200);
135: // turn off all notes: Note On event with 0 velocity
136: for (int i = 0; i < niceChord.length; i++) {
137: mc.shortMidiEvent(0x90, niceChord[i], 0);
138: }
139: } catch (Exception ex) {
140: Utils.error(ex, this );
141: }
142: }
143:
144: private void midiLong() {
145: try {
146: MIDIControl mc = getMIDIControl();
147: // send the chord as sys ex event
148: int len = niceChord.length * 3; // 3 bytes per event
149: byte[] data = new byte[len];
150: int c = 0;
151: for (int i = 0; i < len / 3; i++) {
152: data[c++] = (byte) 0x90;
153: data[c++] = niceChord[i % niceChord.length];
154: data[c++] = 127;
155: }
156: int count = mc.longMidiEvent(data, 0, len);
157: //System.out.println("1. longEvent returned "+count);
158: Thread.sleep(200);
159: // replace the velocity by 0
160: for (int i = 2; i < len; i += 3) {
161: data[i] = 0;
162: }
163: count = mc.longMidiEvent(data, 0, len);
164: //System.out.println("2. longEvent returned "+count);
165: } catch (Exception ex) {
166: Utils.error(ex, this );
167: }
168: }
169:
170: private synchronized void drummer() {
171: DrummerCanvas dc = new DrummerCanvas(this , this );
172: go(dc);
173: dc.show();
174: }
175:
176: public void handle(String name, String url) {
177: throw new RuntimeException(
178: "SimpleTones.handle() must not be called");
179: }
180:
181: }
|