1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media; 18 19 20 import java.io.FileDescriptor; 21 import java.lang.ref.WeakReference; 22 import java.lang.CloneNotSupportedException; 23 24 import android.annotation.UnsupportedAppUsage; 25 import android.content.res.AssetFileDescriptor; 26 import android.os.Looper; 27 import android.os.Handler; 28 import android.os.Message; 29 import android.util.AndroidRuntimeException; 30 import android.util.Log; 31 32 /** 33 * JetPlayer provides access to JET content playback and control. 34 * 35 * <p>Please refer to the JET Creator User Manual for a presentation of the JET interactive 36 * music concept and how to use the JetCreator tool to create content to be player by JetPlayer. 37 * 38 * <p>Use of the JetPlayer class is based around the playback of a number of JET segments 39 * sequentially added to a playback FIFO queue. The rendering of the MIDI content stored in each 40 * segment can be dynamically affected by two mechanisms: 41 * <ul> 42 * <li>tracks in a segment can be muted or unmuted at any moment, individually or through 43 * a mask (to change the mute state of multiple tracks at once)</li> 44 * <li>parts of tracks in a segment can be played at predefined points in the segment, in order 45 * to maintain synchronization with the other tracks in the segment. This is achieved through 46 * the notion of "clips", which can be triggered at any time, but that will play only at the 47 * right time, as authored in the corresponding JET file.</li> 48 * </ul> 49 * As a result of the rendering and playback of the JET segments, the user of the JetPlayer instance 50 * can receive notifications from the JET engine relative to: 51 * <ul> 52 * <li>the playback state,</li> 53 * <li>the number of segments left to play in the queue,</li> 54 * <li>application controller events (CC80-83) to mark points in the MIDI segments.</li> 55 * </ul> 56 * Use {@link #getJetPlayer()} to construct a JetPlayer instance. JetPlayer is a singleton class. 57 * </p> 58 * 59 * <div class="special reference"> 60 * <h3>Developer Guides</h3> 61 * <p>For more information about how to use JetPlayer, read the 62 * <a href="{@docRoot}guide/topics/media/jetplayer.html">JetPlayer</a> developer guide.</p></div> 63 */ 64 public class JetPlayer 65 { 66 //-------------------------------------------- 67 // Constants 68 //------------------------ 69 /** 70 * The maximum number of simultaneous tracks. Use {@link #getMaxTracks()} to 71 * access this value. 72 */ 73 private static int MAXTRACKS = 32; 74 75 // to keep in sync with the JetPlayer class constants 76 // defined in frameworks/base/include/media/JetPlayer.h 77 private static final int JET_EVENT = 1; 78 private static final int JET_USERID_UPDATE = 2; 79 private static final int JET_NUMQUEUEDSEGMENT_UPDATE = 3; 80 private static final int JET_PAUSE_UPDATE = 4; 81 82 // to keep in sync with external/sonivox/arm-wt-22k/lib_src/jet_data.h 83 // Encoding of event information on 32 bits 84 private static final int JET_EVENT_VAL_MASK = 0x0000007f; // mask for value 85 private static final int JET_EVENT_CTRL_MASK = 0x00003f80; // mask for controller 86 private static final int JET_EVENT_CHAN_MASK = 0x0003c000; // mask for channel 87 private static final int JET_EVENT_TRACK_MASK = 0x00fc0000; // mask for track number 88 private static final int JET_EVENT_SEG_MASK = 0xff000000; // mask for segment ID 89 private static final int JET_EVENT_CTRL_SHIFT = 7; // shift to get controller number to bit 0 90 private static final int JET_EVENT_CHAN_SHIFT = 14; // shift to get MIDI channel to bit 0 91 private static final int JET_EVENT_TRACK_SHIFT = 18; // shift to get track ID to bit 0 92 private static final int JET_EVENT_SEG_SHIFT = 24; // shift to get segment ID to bit 0 93 94 // to keep in sync with values used in external/sonivox/arm-wt-22k/Android.mk 95 // Jet rendering audio parameters 96 private static final int JET_OUTPUT_RATE = 22050; // _SAMPLE_RATE_22050 in Android.mk 97 private static final int JET_OUTPUT_CHANNEL_CONFIG = 98 AudioFormat.CHANNEL_OUT_STEREO; // NUM_OUTPUT_CHANNELS=2 in Android.mk 99 100 101 //-------------------------------------------- 102 // Member variables 103 //------------------------ 104 /** 105 * Handler for jet events and status updates coming from the native code 106 */ 107 private NativeEventHandler mEventHandler = null; 108 109 /** 110 * Looper associated with the thread that creates the AudioTrack instance 111 */ 112 private Looper mInitializationLooper = null; 113 114 /** 115 * Lock to protect the event listener updates against event notifications 116 */ 117 private final Object mEventListenerLock = new Object(); 118 119 private OnJetEventListener mJetEventListener = null; 120 121 private static JetPlayer singletonRef; 122 123 124 //-------------------------------- 125 // Used exclusively by native code 126 //-------------------- 127 /** 128 * Accessed by native methods: provides access to C++ JetPlayer object 129 */ 130 @SuppressWarnings("unused") 131 @UnsupportedAppUsage 132 private long mNativePlayerInJavaObj; 133 134 135 //-------------------------------------------- 136 // Constructor, finalize 137 //------------------------ 138 /** 139 * Factory method for the JetPlayer class. 140 * @return the singleton JetPlayer instance 141 */ getJetPlayer()142 public static JetPlayer getJetPlayer() { 143 if (singletonRef == null) { 144 singletonRef = new JetPlayer(); 145 } 146 return singletonRef; 147 } 148 149 /** 150 * Cloning a JetPlayer instance is not supported. Calling clone() will generate an exception. 151 */ clone()152 public Object clone() throws CloneNotSupportedException { 153 // JetPlayer is a singleton class, 154 // so you can't clone a JetPlayer instance 155 throw new CloneNotSupportedException(); 156 } 157 158 JetPlayer()159 private JetPlayer() { 160 161 // remember which looper is associated with the JetPlayer instanciation 162 if ((mInitializationLooper = Looper.myLooper()) == null) { 163 mInitializationLooper = Looper.getMainLooper(); 164 } 165 166 int buffSizeInBytes = AudioTrack.getMinBufferSize(JET_OUTPUT_RATE, 167 JET_OUTPUT_CHANNEL_CONFIG, AudioFormat.ENCODING_PCM_16BIT); 168 169 if ((buffSizeInBytes != AudioTrack.ERROR) 170 && (buffSizeInBytes != AudioTrack.ERROR_BAD_VALUE)) { 171 172 native_setup(new WeakReference<JetPlayer>(this), 173 JetPlayer.getMaxTracks(), 174 // bytes to frame conversion: 175 // 1200 == minimum buffer size in frames on generation 1 hardware 176 Math.max(1200, buffSizeInBytes / 177 (AudioFormat.getBytesPerSample(AudioFormat.ENCODING_PCM_16BIT) * 178 2 /*channels*/))); 179 } 180 } 181 182 finalize()183 protected void finalize() { 184 native_finalize(); 185 } 186 187 188 /** 189 * Stops the current JET playback, and releases all associated native resources. 190 * The object can no longer be used and the reference should be set to null 191 * after a call to release(). 192 */ release()193 public void release() { 194 native_release(); 195 singletonRef = null; 196 } 197 198 199 //-------------------------------------------- 200 // Getters 201 //------------------------ 202 /** 203 * Returns the maximum number of simultaneous MIDI tracks supported by JetPlayer 204 */ getMaxTracks()205 public static int getMaxTracks() { 206 return JetPlayer.MAXTRACKS; 207 } 208 209 210 //-------------------------------------------- 211 // Jet functionality 212 //------------------------ 213 /** 214 * Loads a .jet file from a given path. 215 * @param path the path to the .jet file, for instance "/sdcard/mygame/music.jet". 216 * @return true if loading the .jet file was successful, false if loading failed. 217 */ loadJetFile(String path)218 public boolean loadJetFile(String path) { 219 return native_loadJetFromFile(path); 220 } 221 222 223 /** 224 * Loads a .jet file from an asset file descriptor. 225 * @param afd the asset file descriptor. 226 * @return true if loading the .jet file was successful, false if loading failed. 227 */ loadJetFile(AssetFileDescriptor afd)228 public boolean loadJetFile(AssetFileDescriptor afd) { 229 long len = afd.getLength(); 230 if (len < 0) { 231 throw new AndroidRuntimeException("no length for fd"); 232 } 233 return native_loadJetFromFileD( 234 afd.getFileDescriptor(), afd.getStartOffset(), len); 235 } 236 237 /** 238 * Closes the resource containing the JET content. 239 * @return true if successfully closed, false otherwise. 240 */ closeJetFile()241 public boolean closeJetFile() { 242 return native_closeJetFile(); 243 } 244 245 246 /** 247 * Starts playing the JET segment queue. 248 * @return true if rendering and playback is successfully started, false otherwise. 249 */ play()250 public boolean play() { 251 return native_playJet(); 252 } 253 254 255 /** 256 * Pauses the playback of the JET segment queue. 257 * @return true if rendering and playback is successfully paused, false otherwise. 258 */ pause()259 public boolean pause() { 260 return native_pauseJet(); 261 } 262 263 264 /** 265 * Queues the specified segment in the JET queue. 266 * @param segmentNum the identifier of the segment. 267 * @param libNum the index of the sound bank associated with the segment. Use -1 to indicate 268 * that no sound bank (DLS file) is associated with this segment, in which case JET will use 269 * the General MIDI library. 270 * @param repeatCount the number of times the segment will be repeated. 0 means the segment will 271 * only play once. -1 means the segment will repeat indefinitely. 272 * @param transpose the amount of pitch transposition. Set to 0 for normal playback. 273 * Range is -12 to +12. 274 * @param muteFlags a bitmask to specify which MIDI tracks will be muted during playback. Bit 0 275 * affects track 0, bit 1 affects track 1 etc. 276 * @param userID a value specified by the application that uniquely identifies the segment. 277 * this value is received in the 278 * {@link OnJetEventListener#onJetUserIdUpdate(JetPlayer, int, int)} event listener method. 279 * Normally, the application will keep a byte value that is incremented each time a new 280 * segment is queued up. This can be used to look up any special characteristics of that 281 * track including trigger clips and mute flags. 282 * @return true if the segment was successfully queued, false if the queue is full or if the 283 * parameters are invalid. 284 */ queueJetSegment(int segmentNum, int libNum, int repeatCount, int transpose, int muteFlags, byte userID)285 public boolean queueJetSegment(int segmentNum, int libNum, int repeatCount, 286 int transpose, int muteFlags, byte userID) { 287 return native_queueJetSegment(segmentNum, libNum, repeatCount, 288 transpose, muteFlags, userID); 289 } 290 291 292 /** 293 * Queues the specified segment in the JET queue. 294 * @param segmentNum the identifier of the segment. 295 * @param libNum the index of the soundbank associated with the segment. Use -1 to indicate that 296 * no sound bank (DLS file) is associated with this segment, in which case JET will use 297 * the General MIDI library. 298 * @param repeatCount the number of times the segment will be repeated. 0 means the segment will 299 * only play once. -1 means the segment will repeat indefinitely. 300 * @param transpose the amount of pitch transposition. Set to 0 for normal playback. 301 * Range is -12 to +12. 302 * @param muteArray an array of booleans to specify which MIDI tracks will be muted during 303 * playback. The value at index 0 affects track 0, value at index 1 affects track 1 etc. 304 * The length of the array must be {@link #getMaxTracks()} for the call to succeed. 305 * @param userID a value specified by the application that uniquely identifies the segment. 306 * this value is received in the 307 * {@link OnJetEventListener#onJetUserIdUpdate(JetPlayer, int, int)} event listener method. 308 * Normally, the application will keep a byte value that is incremented each time a new 309 * segment is queued up. This can be used to look up any special characteristics of that 310 * track including trigger clips and mute flags. 311 * @return true if the segment was successfully queued, false if the queue is full or if the 312 * parameters are invalid. 313 */ queueJetSegmentMuteArray(int segmentNum, int libNum, int repeatCount, int transpose, boolean[] muteArray, byte userID)314 public boolean queueJetSegmentMuteArray(int segmentNum, int libNum, int repeatCount, 315 int transpose, boolean[] muteArray, byte userID) { 316 if (muteArray.length != JetPlayer.getMaxTracks()) { 317 return false; 318 } 319 return native_queueJetSegmentMuteArray(segmentNum, libNum, repeatCount, 320 transpose, muteArray, userID); 321 } 322 323 324 /** 325 * Modifies the mute flags. 326 * @param muteFlags a bitmask to specify which MIDI tracks are muted. Bit 0 affects track 0, 327 * bit 1 affects track 1 etc. 328 * @param sync if false, the new mute flags will be applied as soon as possible by the JET 329 * render and playback engine. If true, the mute flags will be updated at the start of the 330 * next segment. If the segment is repeated, the flags will take effect the next time 331 * segment is repeated. 332 * @return true if the mute flags were successfully updated, false otherwise. 333 */ setMuteFlags(int muteFlags, boolean sync)334 public boolean setMuteFlags(int muteFlags, boolean sync) { 335 return native_setMuteFlags(muteFlags, sync); 336 } 337 338 339 /** 340 * Modifies the mute flags for the current active segment. 341 * @param muteArray an array of booleans to specify which MIDI tracks are muted. The value at 342 * index 0 affects track 0, value at index 1 affects track 1 etc. 343 * The length of the array must be {@link #getMaxTracks()} for the call to succeed. 344 * @param sync if false, the new mute flags will be applied as soon as possible by the JET 345 * render and playback engine. If true, the mute flags will be updated at the start of the 346 * next segment. If the segment is repeated, the flags will take effect the next time 347 * segment is repeated. 348 * @return true if the mute flags were successfully updated, false otherwise. 349 */ setMuteArray(boolean[] muteArray, boolean sync)350 public boolean setMuteArray(boolean[] muteArray, boolean sync) { 351 if(muteArray.length != JetPlayer.getMaxTracks()) 352 return false; 353 return native_setMuteArray(muteArray, sync); 354 } 355 356 357 /** 358 * Mutes or unmutes a single track. 359 * @param trackId the index of the track to mute. 360 * @param muteFlag set to true to mute, false to unmute. 361 * @param sync if false, the new mute flags will be applied as soon as possible by the JET 362 * render and playback engine. If true, the mute flag will be updated at the start of the 363 * next segment. If the segment is repeated, the flag will take effect the next time 364 * segment is repeated. 365 * @return true if the mute flag was successfully updated, false otherwise. 366 */ setMuteFlag(int trackId, boolean muteFlag, boolean sync)367 public boolean setMuteFlag(int trackId, boolean muteFlag, boolean sync) { 368 return native_setMuteFlag(trackId, muteFlag, sync); 369 } 370 371 372 /** 373 * Schedules the playback of a clip. 374 * This will automatically update the mute flags in sync with the JET Clip Marker (controller 375 * 103). The parameter clipID must be in the range of 0-63. After the call to triggerClip, when 376 * JET next encounters a controller event 103 with bits 0-5 of the value equal to clipID and 377 * bit 6 set to 1, it will automatically unmute the track containing the controller event. 378 * When JET encounters the complementary controller event 103 with bits 0-5 of the value equal 379 * to clipID and bit 6 set to 0, it will mute the track again. 380 * @param clipId the identifier of the clip to trigger. 381 * @return true if the clip was successfully triggered, false otherwise. 382 */ triggerClip(int clipId)383 public boolean triggerClip(int clipId) { 384 return native_triggerClip(clipId); 385 } 386 387 388 /** 389 * Empties the segment queue, and clears all clips that are scheduled for playback. 390 * @return true if the queue was successfully cleared, false otherwise. 391 */ clearQueue()392 public boolean clearQueue() { 393 return native_clearQueue(); 394 } 395 396 397 //--------------------------------------------------------- 398 // Internal class to handle events posted from native code 399 //------------------------ 400 private class NativeEventHandler extends Handler 401 { 402 private JetPlayer mJet; 403 NativeEventHandler(JetPlayer jet, Looper looper)404 public NativeEventHandler(JetPlayer jet, Looper looper) { 405 super(looper); 406 mJet = jet; 407 } 408 409 @Override handleMessage(Message msg)410 public void handleMessage(Message msg) { 411 OnJetEventListener listener = null; 412 synchronized (mEventListenerLock) { 413 listener = mJet.mJetEventListener; 414 } 415 switch(msg.what) { 416 case JET_EVENT: 417 if (listener != null) { 418 // call the appropriate listener after decoding the event parameters 419 // encoded in msg.arg1 420 mJetEventListener.onJetEvent( 421 mJet, 422 (short)((msg.arg1 & JET_EVENT_SEG_MASK) >> JET_EVENT_SEG_SHIFT), 423 (byte) ((msg.arg1 & JET_EVENT_TRACK_MASK) >> JET_EVENT_TRACK_SHIFT), 424 // JETCreator channel numbers start at 1, but the index starts at 0 425 // in the .jet files 426 (byte)(((msg.arg1 & JET_EVENT_CHAN_MASK) >> JET_EVENT_CHAN_SHIFT) + 1), 427 (byte) ((msg.arg1 & JET_EVENT_CTRL_MASK) >> JET_EVENT_CTRL_SHIFT), 428 (byte) (msg.arg1 & JET_EVENT_VAL_MASK) ); 429 } 430 return; 431 case JET_USERID_UPDATE: 432 if (listener != null) { 433 listener.onJetUserIdUpdate(mJet, msg.arg1, msg.arg2); 434 } 435 return; 436 case JET_NUMQUEUEDSEGMENT_UPDATE: 437 if (listener != null) { 438 listener.onJetNumQueuedSegmentUpdate(mJet, msg.arg1); 439 } 440 return; 441 case JET_PAUSE_UPDATE: 442 if (listener != null) 443 listener.onJetPauseUpdate(mJet, msg.arg1); 444 return; 445 446 default: 447 loge("Unknown message type " + msg.what); 448 return; 449 } 450 } 451 } 452 453 454 //-------------------------------------------- 455 // Jet event listener 456 //------------------------ 457 /** 458 * Sets the listener JetPlayer notifies when a JET event is generated by the rendering and 459 * playback engine. 460 * Notifications will be received in the same thread as the one in which the JetPlayer 461 * instance was created. 462 * @param listener 463 */ setEventListener(OnJetEventListener listener)464 public void setEventListener(OnJetEventListener listener) { 465 setEventListener(listener, null); 466 } 467 468 /** 469 * Sets the listener JetPlayer notifies when a JET event is generated by the rendering and 470 * playback engine. 471 * Use this method to receive JET events in the Handler associated with another 472 * thread than the one in which you created the JetPlayer instance. 473 * @param listener 474 * @param handler the Handler that will receive the event notification messages. 475 */ setEventListener(OnJetEventListener listener, Handler handler)476 public void setEventListener(OnJetEventListener listener, Handler handler) { 477 synchronized(mEventListenerLock) { 478 479 mJetEventListener = listener; 480 481 if (listener != null) { 482 if (handler != null) { 483 mEventHandler = new NativeEventHandler(this, handler.getLooper()); 484 } else { 485 // no given handler, use the looper the AudioTrack was created in 486 mEventHandler = new NativeEventHandler(this, mInitializationLooper); 487 } 488 } else { 489 mEventHandler = null; 490 } 491 492 } 493 } 494 495 496 /** 497 * Handles the notification when the JET engine generates an event. 498 */ 499 public interface OnJetEventListener { 500 /** 501 * Callback for when the JET engine generates a new event. 502 * 503 * @param player the JET player the event is coming from 504 * @param segment 8 bit unsigned value 505 * @param track 6 bit unsigned value 506 * @param channel 4 bit unsigned value 507 * @param controller 7 bit unsigned value 508 * @param value 7 bit unsigned value 509 */ onJetEvent(JetPlayer player, short segment, byte track, byte channel, byte controller, byte value)510 void onJetEvent(JetPlayer player, 511 short segment, byte track, byte channel, byte controller, byte value); 512 /** 513 * Callback for when JET's currently playing segment's userID is updated. 514 * 515 * @param player the JET player the status update is coming from 516 * @param userId the ID of the currently playing segment 517 * @param repeatCount the repetition count for the segment (0 means it plays once) 518 */ onJetUserIdUpdate(JetPlayer player, int userId, int repeatCount)519 void onJetUserIdUpdate(JetPlayer player, int userId, int repeatCount); 520 521 /** 522 * Callback for when JET's number of queued segments is updated. 523 * 524 * @param player the JET player the status update is coming from 525 * @param nbSegments the number of segments in the JET queue 526 */ onJetNumQueuedSegmentUpdate(JetPlayer player, int nbSegments)527 void onJetNumQueuedSegmentUpdate(JetPlayer player, int nbSegments); 528 529 /** 530 * Callback for when JET pause state is updated. 531 * 532 * @param player the JET player the status update is coming from 533 * @param paused indicates whether JET is paused (1) or not (0) 534 */ onJetPauseUpdate(JetPlayer player, int paused)535 void onJetPauseUpdate(JetPlayer player, int paused); 536 } 537 538 539 //-------------------------------------------- 540 // Native methods 541 //------------------------ native_setup(Object Jet_this, int maxTracks, int trackBufferSize)542 private native final boolean native_setup(Object Jet_this, 543 int maxTracks, int trackBufferSize); native_finalize()544 private native final void native_finalize(); native_release()545 private native final void native_release(); native_loadJetFromFile(String pathToJetFile)546 private native final boolean native_loadJetFromFile(String pathToJetFile); native_loadJetFromFileD(FileDescriptor fd, long offset, long len)547 private native final boolean native_loadJetFromFileD(FileDescriptor fd, long offset, long len); native_closeJetFile()548 private native final boolean native_closeJetFile(); native_playJet()549 private native final boolean native_playJet(); native_pauseJet()550 private native final boolean native_pauseJet(); native_queueJetSegment(int segmentNum, int libNum, int repeatCount, int transpose, int muteFlags, byte userID)551 private native final boolean native_queueJetSegment(int segmentNum, int libNum, 552 int repeatCount, int transpose, int muteFlags, byte userID); native_queueJetSegmentMuteArray(int segmentNum, int libNum, int repeatCount, int transpose, boolean[] muteArray, byte userID)553 private native final boolean native_queueJetSegmentMuteArray(int segmentNum, int libNum, 554 int repeatCount, int transpose, boolean[] muteArray, byte userID); native_setMuteFlags(int muteFlags, boolean sync)555 private native final boolean native_setMuteFlags(int muteFlags, boolean sync); native_setMuteArray(boolean[]muteArray, boolean sync)556 private native final boolean native_setMuteArray(boolean[]muteArray, boolean sync); native_setMuteFlag(int trackId, boolean muteFlag, boolean sync)557 private native final boolean native_setMuteFlag(int trackId, boolean muteFlag, boolean sync); native_triggerClip(int clipId)558 private native final boolean native_triggerClip(int clipId); native_clearQueue()559 private native final boolean native_clearQueue(); 560 561 //--------------------------------------------------------- 562 // Called exclusively by native code 563 //-------------------- 564 @SuppressWarnings("unused") 565 @UnsupportedAppUsage postEventFromNative(Object jetplayer_ref, int what, int arg1, int arg2)566 private static void postEventFromNative(Object jetplayer_ref, 567 int what, int arg1, int arg2) { 568 //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2); 569 JetPlayer jet = (JetPlayer)((WeakReference)jetplayer_ref).get(); 570 571 if ((jet != null) && (jet.mEventHandler != null)) { 572 Message m = 573 jet.mEventHandler.obtainMessage(what, arg1, arg2, null); 574 jet.mEventHandler.sendMessage(m); 575 } 576 577 } 578 579 580 //--------------------------------------------------------- 581 // Utils 582 //-------------------- 583 private final static String TAG = "JetPlayer-J"; 584 logd(String msg)585 private static void logd(String msg) { 586 Log.d(TAG, "[ android.media.JetPlayer ] " + msg); 587 } 588 loge(String msg)589 private static void loge(String msg) { 590 Log.e(TAG, "[ android.media.JetPlayer ] " + msg); 591 } 592 593 } 594