javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/tone/PlayBlockEvent.java
branchRCL_3
changeset 24 0fd27995241b
equal deleted inserted replaced
20:f9bb0fca356a 24:0fd27995241b
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Event processor class for tone sequence PLAY_BLOCK events
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.media.tone;
       
    20 
       
    21 import javax.microedition.media.control.ToneControl;
       
    22 
       
    23 /**
       
    24  * Processor for play block events
       
    25  */
       
    26 public class PlayBlockEvent extends Event
       
    27 {
       
    28 
       
    29     // CONSTANTS
       
    30 
       
    31     /* Maximum and minimum values for block number */
       
    32     public static final int PLAY_BLOCK_MAX_VALUE = 127;
       
    33     public static final int PLAY_BLOCK_MIN_VALUE = 0;
       
    34 
       
    35     PlayBlockEvent(byte[] aSequence, MidiSequence aMidiSequence)
       
    36     {
       
    37         super(aSequence, aMidiSequence);
       
    38     }
       
    39 
       
    40     /**
       
    41      * Inherited from Event
       
    42      */
       
    43     public int advance(int aPosition)
       
    44     {
       
    45         int retVal = doValidate(aPosition);
       
    46         if (retVal != 0)
       
    47         {
       
    48             // Push the position to stack that is going to be
       
    49             // played after this play block has completed
       
    50             iReturnPositionStack.push(new Integer(aPosition + EVENT_SIZE));
       
    51 
       
    52             int data = iSequence[ aPosition + 1 ];
       
    53             retVal = findBlock(data) - aPosition;
       
    54         }
       
    55         return retVal;
       
    56     }
       
    57 
       
    58     /**
       
    59      * Child class defined functionality for validate
       
    60      * @param aPosition position in tone sequence array where to validate
       
    61      * @return new position offset related to aPosition in tone sequence
       
    62      * array. Must be positive.
       
    63      */
       
    64     protected int doValidate(int aPosition)
       
    65     throws IllegalArgumentException
       
    66     {
       
    67         // it is already checked that there is at least two bytes left
       
    68         int type = iSequence[ aPosition ];
       
    69         int data = iSequence[ aPosition + 1 ];
       
    70         int retVal = 0;
       
    71 
       
    72         if (type == ToneControl.PLAY_BLOCK)
       
    73         {
       
    74             if (data < PLAY_BLOCK_MIN_VALUE ||
       
    75                     data > PLAY_BLOCK_MAX_VALUE)
       
    76             {
       
    77                 throw new IllegalArgumentException(
       
    78                     "Block number out of range");
       
    79             }
       
    80             findBlock(data);   // for check only
       
    81             retVal = EVENT_SIZE;
       
    82         }
       
    83         return retVal;
       
    84     }
       
    85 
       
    86     /**
       
    87      * Find block
       
    88      * @param block number
       
    89      * @return position of corresponding BLOCK_START event
       
    90      * @exception IllegalArgumentException if block is not found
       
    91      */
       
    92     private int findBlock(int aBlockNumber)
       
    93     {
       
    94         for (int i = 0; i < iSequence.length; i += EVENT_SIZE)
       
    95         {
       
    96             if (iSequence[ i ] == ToneControl.BLOCK_START &&
       
    97                     iSequence[ i + 1 ] == aBlockNumber)
       
    98             {
       
    99                 return i;
       
   100             }
       
   101         }
       
   102         // if block is not found, input is illegal
       
   103         throw new IllegalArgumentException("No block found, sequence is corrupted");
       
   104     }
       
   105 
       
   106     /**
       
   107      * Child class defined functionality for checkEventAtNextPosition
       
   108      * @param aPosition position in tone sequence array where to check
       
   109      */
       
   110     protected void checkEventAtNextPosition(int aPosition)
       
   111     throws IllegalArgumentException
       
   112     {
       
   113         // After this event there can be:
       
   114         // Tone, BlockEnd PlayBlock, Volume, Repeat or end of sequence
       
   115 
       
   116         int type = 0;
       
   117         try
       
   118         {
       
   119             type = iSequence[ aPosition ];
       
   120         }
       
   121         catch (ArrayIndexOutOfBoundsException aioobe)
       
   122         {
       
   123             return; // end of sequence is OK
       
   124         }
       
   125 
       
   126 
       
   127         if (type >= ToneControl.SILENCE ||
       
   128                 type == ToneControl.BLOCK_END ||
       
   129                 type == ToneControl.PLAY_BLOCK ||
       
   130                 type == ToneControl.SET_VOLUME ||
       
   131                 type == ToneControl.REPEAT)
       
   132         {
       
   133             return;
       
   134         }
       
   135         throw new IllegalArgumentException("Illegal event found, sequence is corrupted");
       
   136     }
       
   137 } // end of class
       
   138