javauis/nokiasound_akn/src/cmidtone.cpp
branchRCL_3
changeset 83 26b2b12093af
parent 19 04becd199f91
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  This class implements beep playing and OTA ringingtone playing.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <e32svr.h>
       
    21 
       
    22 #include "com_nokia_mid_sound_Sound.h"
       
    23 #include "CMIDTone.h"
       
    24 #include "MMIDEventSender.h"
       
    25 
       
    26 namespace
       
    27 {
       
    28 const TInt KMIDMaxJavaVolume = 255;
       
    29 const TInt KMIDMinNativeVolume = 1;
       
    30 const TInt KMIDStopSleepTime = 10000;
       
    31 const TInt KMIDMicrosInMilli = 1000;
       
    32 const TInt KMIDMinDataLength = 4;
       
    33 const TInt KMIDLoopForever = 999999;
       
    34 }
       
    35 
       
    36 CMIDTone* CMIDTone::NewL(TInt aFreq, TInt64 aDuration,
       
    37                          MMIDEventSender* aEventSender)
       
    38 {
       
    39     CMIDTone* self = new(ELeave) CMIDTone(aEventSender);
       
    40     CleanupStack::PushL(self);
       
    41     self->ConstructL(aFreq, aDuration);
       
    42     CleanupStack::Pop(self);
       
    43     return self;
       
    44 }
       
    45 
       
    46 CMIDTone* CMIDTone::NewL(const TDesC8& aData, MMIDEventSender* aEventSender)
       
    47 {
       
    48     CMIDTone* self = new(ELeave) CMIDTone(aEventSender);
       
    49     CleanupStack::PushL(self);
       
    50     self->ConstructL(aData);
       
    51     CleanupStack::Pop(self);
       
    52     return self;
       
    53 }
       
    54 
       
    55 
       
    56 CMIDTone::~CMIDTone()
       
    57 {
       
    58     if (iMdaAudioToneUtility)
       
    59     {
       
    60         if (iMdaAudioToneUtility->State() == EMdaAudioToneUtilityNotReady)
       
    61         {
       
    62             iMdaAudioToneUtility->CancelPrepare();
       
    63         }
       
    64         else if (iMdaAudioToneUtility->State()
       
    65                  == EMdaAudioToneUtilityPlaying)
       
    66         {
       
    67             iMdaAudioToneUtility->CancelPlay();
       
    68         }
       
    69     }
       
    70     delete iMdaAudioToneUtility;
       
    71     delete iSchedulerWait;
       
    72 }
       
    73 
       
    74 CMIDTone::CMIDTone(MMIDEventSender* aEventSender)
       
    75         : CMIDSoundImpl(aEventSender)
       
    76 {
       
    77 }
       
    78 
       
    79 void CMIDTone::ConstructL(TInt aFreq, TInt64 aDuration)
       
    80 {
       
    81     CMIDSoundImpl::ConstructL();
       
    82     iSchedulerWait = new(ELeave) CActiveSchedulerWait;
       
    83     iFreq = aFreq;
       
    84     iDuration = aDuration;
       
    85     iState = EInitialising;
       
    86     iMdaAudioToneUtility = CMdaAudioToneUtility::NewL(*this);
       
    87     iMdaAudioToneUtility->PrepareToPlayTone(iFreq,
       
    88                                             TTimeIntervalMicroSeconds(iDuration * KMIDMicrosInMilli));
       
    89     iSchedulerWait->Start();
       
    90 }
       
    91 
       
    92 void CMIDTone::ConstructL(const TDesC8& aData)
       
    93 {
       
    94     CMIDSoundImpl::ConstructL();
       
    95     iSchedulerWait = new(ELeave) CActiveSchedulerWait;
       
    96     // first byte is the number of command parts
       
    97     iFreq = 0;
       
    98     iDuration = 0;
       
    99     iState = EInitialising;
       
   100 
       
   101     if (aData.Length() < KMIDMinDataLength)
       
   102     {
       
   103         iState = ENotSupported;
       
   104         User::Leave(KErrArgument);
       
   105     }
       
   106 
       
   107     if (aData[0x000] == 0x02 && aData[0x001] == 0x4a && aData[0x002] == 0x3a)
       
   108     {
       
   109         iState = EInitialising;
       
   110     }
       
   111     else if (aData[0x000] == 0x03 && aData[0x001] == 0x4a && aData[0x002] ==
       
   112              0x44 && aData[0x003] == 0x3a)
       
   113     {
       
   114         iState = EInitialising;
       
   115     }
       
   116     else if (aData[0] == 0x00 && aData[1] == 0x11)
       
   117     {
       
   118         iState = EInitialising;
       
   119     }
       
   120     else
       
   121     {
       
   122         iState = ENotSupported;
       
   123         User::Leave(KErrArgument);
       
   124     }
       
   125     iMdaAudioToneUtility = CMdaAudioToneUtility::NewL(*this);
       
   126     iMdaAudioToneUtility->PrepareToPlayDesSequence(aData);
       
   127     iSchedulerWait->Start();
       
   128 
       
   129 }
       
   130 
       
   131 TInt CMIDTone::Play(TInt aLoop)
       
   132 {
       
   133     __ASSERT_DEBUG(iState == EReadyToPlay, User::Invariant());
       
   134 
       
   135     if (aLoop == 0)
       
   136     {
       
   137         // There is no known working method to play tone forever
       
   138         aLoop = KMIDLoopForever;
       
   139     }
       
   140 
       
   141     // Setting repeats to 1 causes sound played twice
       
   142     if (aLoop == 1)
       
   143     {
       
   144         iMdaAudioToneUtility->SetRepeats(0, TTimeIntervalMicroSeconds(0));
       
   145     }
       
   146     else if (aLoop > 1)
       
   147     {
       
   148         iMdaAudioToneUtility->SetRepeats(aLoop, TTimeIntervalMicroSeconds(0));
       
   149     }
       
   150 
       
   151     iMdaAudioToneUtility->Play();
       
   152 
       
   153     iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_PLAYING);
       
   154     iState = EPlaying;
       
   155     iPlayed = ETrue;
       
   156 
       
   157     return KErrNone;
       
   158 }
       
   159 
       
   160 void CMIDTone::Stop()
       
   161 {
       
   162     if (iState == EPlaying)
       
   163     {
       
   164         iMdaAudioToneUtility->CancelPlay();
       
   165         // sleep to make sure the device has time to stop
       
   166         User::After(TTimeIntervalMicroSeconds32(KMIDStopSleepTime));      // CSI: 92 MdaAudioToneUtility does not send event when stopping has finished #
       
   167         iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_STOPPED);
       
   168         iState = EReadyToPlay;
       
   169     }
       
   170 }
       
   171 
       
   172 void CMIDTone::SetVolume(TInt aVolume)
       
   173 {
       
   174     TInt maxVolume = iMdaAudioToneUtility->MaxVolume();
       
   175     TInt volume = 0;
       
   176     if (aVolume)
       
   177     {
       
   178         volume = (((maxVolume
       
   179                     - KMIDMinNativeVolume + 1)
       
   180                    * aVolume)
       
   181                   / (KMIDMaxJavaVolume + 1)) + KMIDMinNativeVolume;
       
   182     }
       
   183     iMdaAudioToneUtility->SetVolume(volume);
       
   184 }
       
   185 
       
   186 TInt CMIDTone::Volume()
       
   187 {
       
   188     TInt maxVolume = iMdaAudioToneUtility->MaxVolume();
       
   189     TInt volume = (iMdaAudioToneUtility->Volume() *
       
   190                    KMIDMaxJavaVolume) / maxVolume;
       
   191 
       
   192     return volume;
       
   193 }
       
   194 
       
   195 void CMIDTone::MatoPrepareComplete(TInt aError)
       
   196 {
       
   197     if (aError == KErrNone)
       
   198     {
       
   199         iState = EReadyToPlay;
       
   200     }
       
   201     else
       
   202     {
       
   203         if (aError == KErrNotSupported)
       
   204         {
       
   205             iState = ENotSupported;
       
   206         }
       
   207         else
       
   208         {
       
   209             iState = ENotReady;
       
   210         }
       
   211     }
       
   212     iSchedulerWait->AsyncStop();
       
   213 }
       
   214 
       
   215 void CMIDTone::MatoPlayComplete(TInt aError)
       
   216 {
       
   217     if (KErrNone == aError)
       
   218     {
       
   219         iEventSender->SendEvent(com_nokia_mid_sound_Sound_SOUND_STOPPED);
       
   220         iState = EReadyToPlay;
       
   221     }
       
   222 }
       
   223 
       
   224 void CMIDTone::Release()
       
   225 {
       
   226     iState = ENotReady;
       
   227     if (iMdaAudioToneUtility)
       
   228     {
       
   229         if (iMdaAudioToneUtility->State() == EMdaAudioToneUtilityNotReady)
       
   230         {
       
   231             iMdaAudioToneUtility->CancelPrepare();
       
   232         }
       
   233         else if (iMdaAudioToneUtility->State() == EMdaAudioToneUtilityPlaying
       
   234                  || iState == EPlaying)
       
   235         {
       
   236             iMdaAudioToneUtility->CancelPlay();
       
   237         }
       
   238     }
       
   239     delete iMdaAudioToneUtility;
       
   240     iMdaAudioToneUtility = NULL;
       
   241     iState = ENotReady;
       
   242 }
       
   243 
       
   244 //  End of File