javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/tone/VolumeEvent.java
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     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 VOLUME events
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.media.tone;
       
    20 
       
    21 import javax.microedition.media.control.ToneControl;
       
    22 
       
    23 /**
       
    24  * Event processor class for tone sequence VOLUME events
       
    25  */
       
    26 public class VolumeEvent extends Event
       
    27 {
       
    28     /**
       
    29      * ToneEvent constructor
       
    30      * @param aSequence tone sequence byte array (input)
       
    31      * @param aMidiSequence midi sequence object where to output midi events.
       
    32      */
       
    33     VolumeEvent(byte[] aSequence, MidiSequence aMidiSequence)
       
    34     {
       
    35         super(aSequence, aMidiSequence);
       
    36     }
       
    37 
       
    38     /**
       
    39      * Inherited from Event
       
    40      */
       
    41     public int advance(int aPosition) throws MidiSequenceException
       
    42     {
       
    43         int retVal = doValidate(aPosition);
       
    44         {
       
    45             if (retVal == 0)
       
    46             {
       
    47                 return 0;
       
    48             }
       
    49         }
       
    50         // it is already checked that there is at least two bytes left
       
    51         byte type = iSequence[ aPosition ];
       
    52         byte data = iSequence[ aPosition + 1 ];
       
    53 
       
    54         // calculate equivalent midi TONE_VOLUME
       
    55         float vol = (float)MidiToneConstants.MIDI_MAX_VOLUME /
       
    56                     (float)MidiToneConstants.TONE_MAX_VOLUME * (float)data;
       
    57 
       
    58         byte volume = (byte) vol;
       
    59         if (volume < MidiToneConstants.MIDI_MIN_VOLUME)
       
    60         {
       
    61             volume = MidiToneConstants.MIDI_MIN_VOLUME;
       
    62         }
       
    63 
       
    64         if (volume > MidiToneConstants.MIDI_MAX_VOLUME)
       
    65         {
       
    66             volume = MidiToneConstants.MIDI_MAX_VOLUME;
       
    67         }
       
    68         // write TONE_VOLUME change on delta time 0
       
    69         iMidiSequence.writeMidiEvent(
       
    70             0,
       
    71             MidiToneConstants.MIDI_CONTROL_CHANGE,
       
    72             MidiToneConstants.MIDI_CONTROL_MAIN_VOLUME,
       
    73             volume);
       
    74 
       
    75         // N.B.! Above MIDI_CONTROL_CHANGE can be written without channel
       
    76         // value because MidiSequence will attach correct channel value to them anyway.
       
    77         return EVENT_SIZE;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Child class defined functionality for validate
       
    82      * @param aPosition position in tone sequence array where to validate
       
    83      * @return new position offset related to aPosition in tone sequence
       
    84      * array. Must be positive.
       
    85      */
       
    86     protected int doValidate(int aPosition)
       
    87     throws IllegalArgumentException
       
    88     {
       
    89         byte type = iSequence[ aPosition ];
       
    90         byte data = iSequence[ aPosition + 1 ];
       
    91         int retVal = 0;
       
    92         if (type == ToneControl.SET_VOLUME)
       
    93         {
       
    94             if (data < MidiToneConstants.TONE_MIN_VOLUME  ||
       
    95                     data > MidiToneConstants.TONE_MAX_VOLUME)
       
    96             {
       
    97                 throw new IllegalArgumentException(
       
    98                     "Volume is out of range, valid range is  0 <= volume <= 100");
       
    99             }
       
   100             retVal = EVENT_SIZE;
       
   101         }
       
   102         return retVal;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Child class defined functionality for checkEventAtNextPosition
       
   107      * @param aPosition position in tone sequence array where to check
       
   108      */
       
   109     protected void checkEventAtNextPosition(int aPosition)
       
   110     throws IllegalArgumentException
       
   111     {
       
   112         // After this event there can be:
       
   113         // Tone, BlockEnd, PlayBlock, Volume, Repeat or
       
   114         // 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 for this event
       
   124         }
       
   125 
       
   126         if (type >= ToneControl.SILENCE ||
       
   127                 type == ToneControl.BLOCK_END ||
       
   128                 type == ToneControl.PLAY_BLOCK ||
       
   129                 type == ToneControl.SET_VOLUME ||
       
   130                 type == ToneControl.REPEAT)
       
   131         {
       
   132             return;
       
   133         }
       
   134         throw new IllegalArgumentException(
       
   135             "Illegal event found; sequence is corrupted");
       
   136     }
       
   137 
       
   138 } // end of class
       
   139