javauis/amms_qt/javasrc/com/nokia/amms/control/audio3d/OrientationControl.java
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2005 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:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.amms.control.audio3d;
       
    19 
       
    20 import com.nokia.microedition.media.NativeError;
       
    21 
       
    22 public class OrientationControl
       
    23         extends com.nokia.amms.control.ControlImpl
       
    24         implements javax.microedition.amms.control.audio3d.OrientationControl
       
    25 {
       
    26     private static final int REQUIRED_VECTOR_SIZE = 3;
       
    27     private static final int COMPONENT_X = 0;
       
    28     private static final int COMPONENT_Y = 1;
       
    29     private static final int COMPONENT_Z = 2;
       
    30 
       
    31     /**
       
    32      * Constructor
       
    33      */
       
    34     public OrientationControl()
       
    35     {
       
    36     }
       
    37 
       
    38     /**
       
    39     * Please refer to JSR 234 for more details.
       
    40     */
       
    41     public void setOrientation(int aHeading, int aPitch, int aRoll)
       
    42     {
       
    43         checkValid();
       
    44 
       
    45         int err = _setOrientation(
       
    46                       iEventSource,
       
    47                       iControlHandle,
       
    48                       aHeading,
       
    49                       aPitch,
       
    50                       aRoll);
       
    51 
       
    52         NativeError.check(err);
       
    53     }
       
    54 
       
    55     /**
       
    56     * Please refer to JSR 234 for more details.
       
    57     */
       
    58     public void setOrientation(int[] aFrontVector, int[] aAboveVector)
       
    59     throws IllegalArgumentException
       
    60     {
       
    61         checkValid();
       
    62 
       
    63         if ((aFrontVector == null) ||
       
    64                 (aAboveVector == null))
       
    65         {
       
    66             throw new IllegalArgumentException("Argument is null");
       
    67         }
       
    68 
       
    69         if ((aFrontVector.length < REQUIRED_VECTOR_SIZE) ||
       
    70                 (aAboveVector.length < REQUIRED_VECTOR_SIZE) ||
       
    71                 (aFrontVector.length > REQUIRED_VECTOR_SIZE) ||
       
    72                 (aAboveVector.length > REQUIRED_VECTOR_SIZE))
       
    73         {
       
    74             throw new IllegalArgumentException("Required orientation vector size is 3");
       
    75         }
       
    76 
       
    77         // Vector is a zero vector if all the components are zero
       
    78         if (((aFrontVector[ COMPONENT_X ] == 0) &&
       
    79                 (aFrontVector[ COMPONENT_Y ] == 0) &&
       
    80                 (aFrontVector[ COMPONENT_Z ] == 0)) ||
       
    81                 ((aAboveVector[ COMPONENT_X ] == 0) &&
       
    82                  (aAboveVector[ COMPONENT_Y ] == 0) &&
       
    83                  (aAboveVector[ COMPONENT_Z ] == 0)))
       
    84         {
       
    85             throw new IllegalArgumentException("Orientation vector cannot be a zero vector");
       
    86         }
       
    87 
       
    88         // Two vectors are parallel if their cross product is zero vector.
       
    89         // Cross product of vectors a1*i + a2*j + a3*k and b1*i + b2*j + b3*k :
       
    90         // (a2*b3 - a3*b2)i + (a3*b1 - a1*b3)j + (a1*b2 - a2*b1)k
       
    91         if ((
       
    92                     (aFrontVector[ COMPONENT_Y ] * aAboveVector[ COMPONENT_Z ]) -
       
    93                     (aFrontVector[ COMPONENT_Z ] * aAboveVector[ COMPONENT_Y ]) == 0
       
    94                 )
       
    95                 &&
       
    96                 (
       
    97                     (aFrontVector[ COMPONENT_Z ] * aAboveVector[ COMPONENT_X ]) -
       
    98                     (aFrontVector[ COMPONENT_X ] * aAboveVector[ COMPONENT_Z ]) == 0
       
    99                 )
       
   100                 &&
       
   101                 (
       
   102                     (aFrontVector[ COMPONENT_X ] * aAboveVector[ COMPONENT_Y ]) -
       
   103                     (aFrontVector[ COMPONENT_Y ] * aAboveVector[ COMPONENT_X ]) == 0
       
   104                 ))
       
   105         {
       
   106             throw new IllegalArgumentException("Orientation vectors cannot be parallel");
       
   107         }
       
   108 
       
   109         int err = _setOrientationVectors(
       
   110                       iEventSource,
       
   111                       iControlHandle,
       
   112                       aFrontVector,
       
   113                       aAboveVector);
       
   114 
       
   115         NativeError.check(err);
       
   116     }
       
   117 
       
   118     /**
       
   119     * Please refer to JSR 234 for more details.
       
   120     */
       
   121     public int[] getOrientationVectors()
       
   122     {
       
   123         checkValid();
       
   124 
       
   125         int[] error = new int[ 1 ];
       
   126 
       
   127         int[] orientation = _getOrientationVectors(
       
   128                                 iEventSource,
       
   129                                 iControlHandle,
       
   130                                 error);
       
   131 
       
   132         if (error[ 0 ] != NativeError.KErrNone)
       
   133         {
       
   134             throw new OutOfMemoryError("Obtaining orientation vectors failed: Symbian OS error " +
       
   135                                        error[ 0 ]);
       
   136         }
       
   137 
       
   138         if (orientation == null)
       
   139         {
       
   140             throw new OutOfMemoryError("Unable to obtain orientation vectors");
       
   141         }
       
   142         return orientation;
       
   143     }
       
   144 
       
   145     private static native int _setOrientation(
       
   146         int aEventSource,
       
   147         int aControlHandle,
       
   148         int heading,
       
   149         int pitch,
       
   150         int roll);
       
   151 
       
   152     private static native int _setOrientationVectors(
       
   153         int aEventSource,
       
   154         int aControlHandle,
       
   155         int[] frontVector,
       
   156         int[] aboveVector);
       
   157 
       
   158     private static native int[] _getOrientationVectors(
       
   159         int aEventSource,
       
   160         int aControlHandle,
       
   161         int[] error);
       
   162 }