001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.mmedia;
025:
026: import javax.microedition.media.*;
027: import javax.microedition.media.control.*;
028:
029: abstract class QSoundMIDIPlayBase {
030:
031: private int qsMIDI;
032: private int globMan;
033:
034: private Player player;
035:
036: private int loopCount;
037: private int currentLoopCount;
038: private int lastLoopCount;
039:
040: private native int nOpen(int gm);
041:
042: private native void nClose(int gm, int qsMpeer);
043:
044: private native boolean nFillBuffer(int gm, int qsMpeer,
045: byte[] buffer);
046:
047: private native void nDeallocateBuffer(int qsMpeer);
048:
049: private native void nStart(int qsMpeer);
050:
051: private native void nStop(int qsMpeer);
052:
053: private native boolean nIsDone(int qsMpeer);
054:
055: private native int nLoopsDone(int qsMpeer);
056:
057: private native int nSetPosition(int peer, int now);
058:
059: private native int nGetPosition(int peer);
060:
061: private native int nGetDuration(int peer);
062:
063: private native void nSetLoopCount(int peer, int count);
064:
065: private boolean opened;
066:
067: QSoundMIDIPlayBase() {
068: globMan = QSoundHiddenManager.getMIDIGlobalPeer();
069: opened = false;
070: }
071:
072: QSoundMIDIPlayBase(Player p) {
073: globMan = QSoundHiddenManager.getMIDIGlobalPeer();
074: player = p;
075: opened = false;
076: }
077:
078: abstract Control getControl(String controlType);
079:
080: boolean open() {
081: return open(false);
082: }
083:
084: boolean open(boolean forceOpen) {
085: if (opened && !forceOpen)
086: return opened;
087:
088: qsMIDI = nOpen(globMan);
089:
090: opened = true;
091: return opened;
092: }
093:
094: void close() {
095: nDeallocateBuffer(qsMIDI);
096: nClose(globMan, qsMIDI);
097: qsMIDI = 0;
098: }
099:
100: boolean fillBuffer(byte[] b) {
101: boolean r = nFillBuffer(globMan, qsMIDI, b);
102:
103: if (r) {
104: if (loopCount != 0)
105: nSetLoopCount(qsMIDI, loopCount);
106: }
107:
108: return r;
109: }
110:
111: void start() {
112: lastLoopCount = loopCount == -1 ? 0 : loopCount;
113: nStart(qsMIDI);
114: }
115:
116: void stop() {
117: nStop(qsMIDI);
118: }
119:
120: boolean isDone() {
121: boolean r = nIsDone(qsMIDI);
122:
123: if (!r && (loopCount != 0))
124: currentLoopCount = nLoopsDone(qsMIDI);
125:
126: return r;
127: }
128:
129: long setMediaTime(long now) throws MediaException {
130: if (qsMIDI == 0)
131: return 0;
132:
133: int pos = nSetPosition(qsMIDI, ((int) now / 100));
134:
135: return ((long) pos) * 100L;
136: }
137:
138: long getMediaTime() {
139: if (qsMIDI == 0)
140: return Player.TIME_UNKNOWN;
141:
142: return ((long) (nGetPosition(qsMIDI))) * 100L;
143: }
144:
145: long getDuration() {
146: if (qsMIDI == 0)
147: return Player.TIME_UNKNOWN;
148:
149: return ((long) (nGetDuration(qsMIDI))) * 100L;
150: }
151:
152: void setLoopCount(int count) {
153: loopCount = count;
154: nSetLoopCount(qsMIDI, loopCount);
155: }
156:
157: int numLoopComplete() {
158: int numLoops = currentLoopCount - lastLoopCount;
159:
160: lastLoopCount = currentLoopCount;
161:
162: return numLoops;
163: }
164:
165: Player player() {
166: return player;
167: }
168:
169: void setPlayer(Player p) {
170: player = p;
171: }
172:
173: int peer() {
174: return qsMIDI;
175: }
176: }
|