videoeditorengine/audioeditorengine/src/ProcInFileHandler.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <f32file.h>
       
    22 #include "AudPanic.h"
       
    23 #include "ProcInFileHandler.h"
       
    24 #include "AudClipInfo.h"
       
    25 #include "ProcTools.h"
       
    26 #include "audconstants.h"
       
    27 
       
    28 // Debug print macro
       
    29 #if defined _DEBUG 
       
    30 #include <e32svr.h>
       
    31 #define PRINT(x) RDebug::Print x;
       
    32 #else
       
    33 #define PRINT(x)
       
    34 #endif
       
    35 
       
    36 CProcInFileHandler::~CProcInFileHandler() 
       
    37     {
       
    38 
       
    39     if (iFileOpen && iOwnsFile)
       
    40         {
       
    41         iFile.Close();
       
    42         iFs.Close();
       
    43         }
       
    44 
       
    45 
       
    46     if (iProperties != 0)
       
    47         {
       
    48         delete iProperties;
       
    49         iProperties = 0;
       
    50         }
       
    51 
       
    52 
       
    53     delete iRawSilentFrame;
       
    54     
       
    55     delete iInputBuffer;
       
    56     
       
    57     delete iWavFrameHandler;
       
    58 
       
    59     }
       
    60 
       
    61 
       
    62 TBool CProcInFileHandler::DecodingRequired()
       
    63     {
       
    64     return iDecodingRequired;
       
    65     }
       
    66     
       
    67 
       
    68 TInt CProcInFileHandler::GetDecodedFrameSize()
       
    69     {
       
    70     if (iProperties == 0)
       
    71         {
       
    72         return 0;
       
    73         }
       
    74     else
       
    75         {
       
    76         
       
    77         // divided by 1000000/2 cause time is in 
       
    78         // microseconds and 16 bits are used for one sample
       
    79 #ifdef EKA2
       
    80         TInt frameSize = (iProperties->iFrameDuration.Int64())*(iTargetSampleRate)/(1000000/2);
       
    81 #else
       
    82         TInt frameSize = (iProperties->iFrameDuration.Int64().GetTInt())*(iTargetSampleRate)/(1000000/2);
       
    83 #endif        
       
    84         
       
    85         
       
    86         if (iChannelMode == EAudStereo)
       
    87             {
       
    88             frameSize *= 2;
       
    89             }
       
    90         
       
    91         return frameSize;
       
    92         }
       
    93     
       
    94     
       
    95     }
       
    96 
       
    97 
       
    98 void CProcInFileHandler::SetDecodingRequired(TBool aDecodingRequired)
       
    99     {
       
   100     iDecodingRequired = aDecodingRequired;
       
   101     
       
   102     }
       
   103 
       
   104 TBool CProcInFileHandler::GetAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime, TBool& aRawFrame) 
       
   105     {
       
   106 
       
   107     if (iDecodingRequired)
       
   108         {
       
   109         aRawFrame = ETrue;
       
   110         return GetRawAudioFrameL(aFrame, aSize, aTime);
       
   111         }
       
   112     else
       
   113         {
       
   114         aRawFrame = EFalse;
       
   115         return GetEncAudioFrameL(aFrame, aSize, aTime);
       
   116         
       
   117         }
       
   118     }
       
   119     
       
   120     
       
   121 TBool CProcInFileHandler::GetRawAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime)
       
   122     {
       
   123     
       
   124     
       
   125     while (iInputBuffer == 0 || iInputBuffer->Length() < iRawFrameLength)
       
   126         {
       
   127         TBool ret = GetOneRawAudioFrameL(aFrame, aSize, aTime);
       
   128         if (!ret)
       
   129             {
       
   130             return ret;
       
   131             }
       
   132         
       
   133         WriteDataToInputBufferL(*aFrame);
       
   134         delete aFrame;
       
   135         aFrame = 0;
       
   136     
       
   137         }
       
   138         
       
   139     if (iInputBuffer->Length() >= iRawFrameLength)
       
   140         {
       
   141         aFrame = HBufC8::NewL(iRawFrameLength);
       
   142         aFrame->Des().Append(iInputBuffer->Left(iRawFrameLength));
       
   143         
       
   144         
       
   145         TInt bytesInSecond = iTargetSampleRate*2;
       
   146             
       
   147         if (iChannelMode == EAudStereo)
       
   148             {
       
   149             bytesInSecond *= 2;
       
   150             }
       
   151         
       
   152         
       
   153         // Fix for synchronizing problem ---------------------------------->
       
   154         // If the accurate frame length cannot be represented in milliseconds
       
   155         // store the remainder and increase the output frame lenght by one ms 
       
   156         // when needed. Accuracy depends on sampling rate
       
   157         
       
   158         TReal accurateFrameLen = TReal(aFrame->Length()*1000)/bytesInSecond;
       
   159         aTime = TUint((aFrame->Length()*1000)/bytesInSecond);
       
   160         
       
   161         iFrameLenRemainderMilli += accurateFrameLen - aTime;
       
   162         
       
   163         if (iFrameLenRemainderMilli > 1)
       
   164             {
       
   165             aTime += 1;
       
   166             iFrameLenRemainderMilli -= 1;
       
   167             }
       
   168         // <---------------------------------- Fix for synchronizing problem
       
   169             
       
   170         iInputBuffer->Des().Delete(0, iRawFrameLength);
       
   171 
       
   172         
       
   173         }
       
   174     
       
   175     return ETrue;
       
   176     
       
   177     
       
   178     }
       
   179 
       
   180     
       
   181 TBool CProcInFileHandler::GetOneRawAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime)
       
   182     {
       
   183     
       
   184 
       
   185     TInt size = 0;
       
   186         
       
   187     TInt32 time = 0;
       
   188         
       
   189     HBufC8* frame = 0;
       
   190         
       
   191     TBool encFrameRead = GetEncAudioFrameL(frame, size, time);    
       
   192     
       
   193     if (encFrameRead) 
       
   194         {
       
   195         CleanupStack::PushL(frame);
       
   196         }
       
   197     
       
   198     aTime = time;
       
   199     
       
   200     if (!encFrameRead)
       
   201         {
       
   202         return EFalse;
       
   203         }
       
   204     
       
   205     TRAPD(err, iDecoder->FillDecBufferL(frame, aFrame));
       
   206     if (err == KErrNotFound)
       
   207         {
       
   208         // S60 audio decoders seem to leave with KErrNotFound if they find problems in input clips. 
       
   209         // However, sometimes the problem is only in one frame, e.g. the 1st frame may contain some metadata that can't be decoded,
       
   210         // but decoding can continue after that. But if there are many errors, the clip is most likely unusable.
       
   211 	    PRINT((_L("CProcInFileHandler::GetOneRawAudioFrameL() iDecoder->FillDecBufferL leaved with %d"),err));
       
   212         if ( iDecoderErrors > 0 )
       
   213             {
       
   214             // several errors, leave, but change the error code to more readable
       
   215     	    PRINT((_L("CProcInFileHandler::GetOneRawAudioFrameL() leave with %d"),KErrCorrupt));
       
   216             User::Leave(KErrCorrupt);
       
   217             }
       
   218         iDecoderErrors++;
       
   219         }
       
   220     else if (err == KErrNone)
       
   221         {
       
   222         // keep filling the decoder buffer; the decoder seem to leave with KErrNone if it can't get enough data, ignore the leave
       
   223         }
       
   224     else
       
   225         {
       
   226         // some other error
       
   227 	    PRINT((_L("CProcInFileHandler::GetOneRawAudioFrameL() iDecoder->FillDecBufferL leaved with %d, leaving"),err));
       
   228         User::Leave(err);
       
   229         }
       
   230     
       
   231     if (encFrameRead) 
       
   232         {
       
   233         CleanupStack::PopAndDestroy(frame);
       
   234         frame = 0;
       
   235         
       
   236         }
       
   237     
       
   238     while (aFrame == 0 || aFrame->Size() == 0)
       
   239         {
       
   240         
       
   241         encFrameRead = GetEncAudioFrameL(frame, size, time);
       
   242     
       
   243         if (encFrameRead) 
       
   244             {
       
   245             CleanupStack::PushL(frame);
       
   246             }
       
   247         else
       
   248             {
       
   249             return EFalse;
       
   250             }
       
   251     
       
   252         
       
   253         aTime += time;
       
   254         
       
   255         
       
   256         
       
   257         iDecoder->FillDecBufferL(frame, aFrame);
       
   258         if (encFrameRead) 
       
   259             {
       
   260             CleanupStack::PopAndDestroy(frame);
       
   261             frame = 0;
       
   262             }        
       
   263         
       
   264         }
       
   265     
       
   266     
       
   267     aSize = aFrame->Length();
       
   268     
       
   269     if (iProperties->iAudioTypeExtension != EAudExtensionTypeNoExtension) 
       
   270         {
       
   271         
       
   272         // AACPlus is always decoded, therefore the gain manipulation can be done
       
   273         // in time domain
       
   274         
       
   275         ManipulateGainL(aFrame);
       
   276         aSize = aFrame->Length();
       
   277         
       
   278         
       
   279         }
       
   280     
       
   281     
       
   282     return ETrue;
       
   283         
       
   284     }
       
   285 
       
   286 
       
   287 TBool CProcInFileHandler::SetRawAudioFrameSize(TInt aSize)
       
   288     {
       
   289     
       
   290     const TInt KMaxRawFrameSize = 4096;
       
   291     if (aSize > 0 && aSize <= KMaxRawFrameSize)
       
   292         {
       
   293         iRawFrameLength = aSize;
       
   294         return ETrue;
       
   295         }
       
   296         
       
   297     return EFalse;
       
   298     }
       
   299 
       
   300 TBool CProcInFileHandler::GetSilentAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime, TBool& aRawFrame)
       
   301     {
       
   302     
       
   303     if (iDecodingRequired)
       
   304         {
       
   305         aRawFrame = ETrue;
       
   306         return GetRawSilentAudioFrameL(aFrame, aSize, aTime);
       
   307         
       
   308         }
       
   309         
       
   310     else
       
   311         {
       
   312         aRawFrame = EFalse;
       
   313         return GetEncSilentAudioFrameL(aFrame, aSize, aTime);
       
   314         }
       
   315     }
       
   316     
       
   317     
       
   318 
       
   319 
       
   320 TBool CProcInFileHandler::GetRawSilentAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aDuration)
       
   321     {
       
   322     
       
   323     
       
   324     aFrame = HBufC8::NewL(iRawFrameLength);    
       
   325     aFrame->Des().Fill(0, aFrame->Des().MaxLength());
       
   326 
       
   327     TInt bytesInSecond = iTargetSampleRate*2;
       
   328             
       
   329     if (iChannelMode == EAudStereo)
       
   330         {
       
   331         bytesInSecond *= 2;
       
   332         }
       
   333     
       
   334     aDuration = TUint((aFrame->Length()*1000)/bytesInSecond);
       
   335     aSize = aFrame->Length();
       
   336   
       
   337     
       
   338     // input buffer is created only if needed
       
   339     if (iInputBuffer)
       
   340         {
       
   341         iInputBuffer->Des().Delete(0, iInputBuffer->Size());
       
   342         
       
   343         }
       
   344     return ETrue;
       
   345         
       
   346     }
       
   347 
       
   348 TBool CProcInFileHandler::GetEncSilentAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aDuration)
       
   349     {
       
   350     
       
   351     if (iSilentFrame == 0)
       
   352         {
       
   353         return EFalse;
       
   354         }
       
   355     
       
   356     aFrame = HBufC8::NewL(iSilentFrame->Size());    
       
   357     aFrame->Des().Append(iSilentFrame->Des());
       
   358 
       
   359     aDuration = iSilentFrameDuration;
       
   360     aSize = iSilentFrame->Size();
       
   361     return ETrue;
       
   362 
       
   363     }
       
   364 
       
   365 
       
   366 CProcInFileHandler::CProcInFileHandler() : iFileOpen(EFalse)
       
   367     {
       
   368     
       
   369     
       
   370 
       
   371     }
       
   372     
       
   373 TInt32 CProcInFileHandler::GetCurrentTimeMilliseconds() 
       
   374     {
       
   375     return iCurrentTimeMilliseconds;
       
   376 
       
   377     }
       
   378 
       
   379 // default implementation for files that do not have buffered bytes
       
   380 TBool CProcInFileHandler::ReadAudioDecoderSpecificInfoL(HBufC8*& /*aBytes*/, TInt /*aBufferSize*/)
       
   381     {
       
   382     return EFalse;
       
   383     }
       
   384 
       
   385 TBool CProcInFileHandler::OpenFileForReadingL() 
       
   386     {   
       
   387 
       
   388     if (iFileOpen)
       
   389         {
       
   390         User::Leave(KErrGeneral);
       
   391         }
       
   392  
       
   393     TInt err = iFs.Connect();
       
   394     if (err != KErrNone) 
       
   395         {
       
   396         iFileOpen = EFalse;
       
   397         User::Leave(err);
       
   398         }
       
   399 
       
   400     err=iFile.Open(iFs, iFileName->Des(), EFileShareReadersOnly);
       
   401     if (err != KErrNone) 
       
   402         {
       
   403         err=iFile.Open(iFs, iFileName->Des(), EFileShareAny);
       
   404         }
       
   405     if (err != KErrNone) 
       
   406         {
       
   407         iFileOpen = EFalse;
       
   408         iFs.Close();
       
   409         User::Leave(err);
       
   410         }
       
   411     else 
       
   412         {
       
   413         iFileOpen = ETrue;
       
   414         return ETrue;
       
   415         }
       
   416 
       
   417     return EFalse;
       
   418 
       
   419     }
       
   420 
       
   421 TBool CProcInFileHandler::
       
   422 SetPropertiesL(TAudFileProperties aProperties) 
       
   423 {
       
   424   if(iProperties == 0)
       
   425     iProperties = new (ELeave) TAudFileProperties();
       
   426 
       
   427   iProperties->iAudioType = aProperties.iAudioType;
       
   428   iProperties->iBitrate = aProperties.iBitrate;
       
   429   iProperties->iBitrateMode = aProperties.iBitrateMode;
       
   430   iProperties->iChannelMode = aProperties.iChannelMode;
       
   431   iProperties->iDuration = 0;
       
   432   iProperties->iFileFormat = aProperties.iFileFormat;
       
   433   iProperties->iSamplingRate = aProperties.iSamplingRate;
       
   434   iProperties->iFrameLen = aProperties.iFrameLen;
       
   435   iProperties->iFrameDuration = aProperties.iFrameDuration;
       
   436   iProperties->iFrameCount = aProperties.iFrameCount;
       
   437   
       
   438   return (ETrue);
       
   439 }
       
   440 
       
   441 TBool CProcInFileHandler::SetPriority(TInt aPriority)
       
   442     {
       
   443     if (aPriority < 0) return EFalse;
       
   444 
       
   445     iPriority = aPriority;
       
   446 
       
   447     return ETrue;
       
   448 
       
   449     }
       
   450 TInt CProcInFileHandler::Priority() const
       
   451     {
       
   452     return iPriority;
       
   453 
       
   454     }
       
   455 
       
   456 TBool CProcInFileHandler::CloseFile() 
       
   457     {
       
   458 
       
   459     if (iOwnsFile)
       
   460     {        
       
   461         iFile.Close();
       
   462         iFs.Close();
       
   463         iFileOpen = EFalse;
       
   464     }
       
   465     
       
   466     return ETrue;
       
   467 
       
   468     }
       
   469 
       
   470 
       
   471 
       
   472 TBool CProcInFileHandler::InitAndOpenFileL(const TDesC& aFileName, 
       
   473                                            RFile* aFileHandle,
       
   474                                            TInt aReadBufferSize) 
       
   475     {
       
   476 
       
   477     
       
   478     iBufferStartOffset = 0;
       
   479     iBufferEndOffset = 0;
       
   480     iCutInTime = 0;
       
   481 
       
   482     if (!aFileHandle)
       
   483     {        
       
   484         iFileName = HBufC::NewL(aFileName.Length());
       
   485         *iFileName = aFileName;
       
   486     }
       
   487         
       
   488     iReadBufferSize = aReadBufferSize;
       
   489     iReadBuffer = HBufC8::NewL(iReadBufferSize);
       
   490     
       
   491     const TInt KVedACSizeAMRBuffer = 320;
       
   492     
       
   493     // [JK]: Moved from OpenFileForReadingL
       
   494     // AMR buffer is the smallest possible
       
   495     // buffersize fill be increased if needed
       
   496     iRawFrameLength = KVedACSizeAMRBuffer;
       
   497     iInputBuffer = HBufC8::NewL(iRawFrameLength);
       
   498 
       
   499     if (aFileHandle)
       
   500     {
       
   501         iFile = *aFileHandle;
       
   502         iOwnsFile = EFalse;
       
   503         iFileOpen = ETrue;
       
   504         return 0;
       
   505     }
       
   506     
       
   507     TBool err = OpenFileForReadingL();
       
   508     if (!err) iFileOpen = ETrue;
       
   509     
       
   510     iOwnsFile = ETrue;
       
   511     
       
   512     return err;
       
   513 
       
   514     }
       
   515 
       
   516 void CProcInFileHandler::ResetAndCloseFile()
       
   517     {
       
   518 
       
   519     if (iFileOpen) 
       
   520         {
       
   521         CloseFile();
       
   522         }
       
   523     if (iFileName != 0)
       
   524         delete iFileName;
       
   525     iFileName = 0;
       
   526     if (iReadBuffer != 0)
       
   527         delete iReadBuffer;
       
   528     iReadBuffer = 0;
       
   529     
       
   530     delete iRawSilentFrame;
       
   531     iRawSilentFrame = 0;
       
   532     
       
   533 
       
   534 
       
   535     }
       
   536 
       
   537 TInt CProcInFileHandler::BufferedFileRead(TInt aPos,TDes8& aDes) 
       
   538     {
       
   539     TInt bufSize;
       
   540     
       
   541     if (!iFileOpen) 
       
   542         {
       
   543         TAudPanic::Panic(TAudPanic::EInternal);
       
   544         }
       
   545     
       
   546     TBool readingNeeded = EFalse;
       
   547     
       
   548     if (aPos < iBufferStartOffset || aPos + aDes.MaxSize() > iBufferEndOffset) 
       
   549         {
       
   550         readingNeeded = ETrue;
       
   551         }
       
   552     
       
   553     if(readingNeeded) 
       
   554         {
       
   555         TInt fSize;
       
   556 
       
   557         iFile.Size(fSize);
       
   558         if(aPos >= fSize) 
       
   559             {
       
   560             aDes.SetLength(0);
       
   561             return 0;
       
   562             }
       
   563 
       
   564         TPtr8 tmpDes((TPtr8)iReadBuffer->Des());
       
   565 
       
   566         iFile.Read(aPos, (TPtr8&)tmpDes, iReadBufferSize);
       
   567 
       
   568         iBufferStartOffset = aPos;
       
   569         iBufferEndOffset = aPos+iReadBuffer->Des().Size()-1;        
       
   570         }
       
   571 
       
   572     if (iReadBuffer->Size() == 0) 
       
   573         {
       
   574         return 0;
       
   575         }
       
   576     else 
       
   577         {
       
   578         bufSize = Min(iReadBuffer->Des().Length(), aDes.MaxSize());
       
   579         aDes.Copy(iReadBuffer->Des().Mid(aPos-iBufferStartOffset, bufSize));
       
   580         iFilePos = aPos+aDes.Size();
       
   581 
       
   582         return aDes.Size();
       
   583         }
       
   584     }
       
   585 
       
   586 TInt CProcInFileHandler::BufferedFileRead(TDes8& aDes) 
       
   587     {
       
   588 
       
   589     if (!iFileOpen) 
       
   590         {
       
   591         TAudPanic::Panic(TAudPanic::EInternal);
       
   592         }
       
   593 
       
   594     return BufferedFileRead(iFilePos, aDes);
       
   595     
       
   596 
       
   597     }
       
   598 
       
   599 TInt CProcInFileHandler::BufferedFileRead(TDes8& aDes,TInt aLength) 
       
   600     {
       
   601     TInt bufSize;
       
   602 
       
   603     if (!iFileOpen) 
       
   604         {
       
   605         TAudPanic::Panic(TAudPanic::EInternal);
       
   606         }
       
   607     
       
   608     aDes.Zero();
       
   609 
       
   610     TBool readingNeeded = EFalse;
       
   611     
       
   612     if (iFilePos < iBufferStartOffset || iFilePos + aLength-1 > iBufferEndOffset) 
       
   613         {
       
   614         readingNeeded = ETrue;
       
   615         }
       
   616     
       
   617     if(readingNeeded) 
       
   618         {
       
   619         TPtr8 tmpDes((TPtr8)iReadBuffer->Des());
       
   620 
       
   621         iFile.Read(iFilePos, (TPtr8&)tmpDes, iReadBufferSize);
       
   622         
       
   623         if (iReadBuffer->Size() == 0) return 0; 
       
   624         iFile.Seek(ESeekStart, iFilePos);
       
   625         iBufferStartOffset = iFilePos;
       
   626         iBufferEndOffset = iFilePos+iReadBuffer->Des().Size()-1;        
       
   627         }
       
   628     bufSize = Min(iReadBuffer->Des().Length(), aLength);
       
   629     aDes.Copy(iReadBuffer->Des().Mid(iFilePos-iBufferStartOffset, bufSize));
       
   630     iFilePos = iFilePos+aDes.Size();
       
   631     return aDes.Size();
       
   632 
       
   633     }
       
   634 
       
   635 TInt CProcInFileHandler::BufferedFileReadChar(TInt aPos, TUint8& aChar)
       
   636     {
       
   637 
       
   638     TBuf8<1> cha;
       
   639     BufferedFileRead(aPos, cha);
       
   640 
       
   641     if (cha.Size() == 1)
       
   642         {
       
   643         aChar = cha[0];
       
   644         return 1;
       
   645         }
       
   646     else
       
   647         {
       
   648         return 0;
       
   649         }
       
   650 
       
   651     }
       
   652 
       
   653 TBool CProcInFileHandler::BufferedFileSetFilePos(TInt aPos) 
       
   654     {
       
   655 
       
   656     
       
   657     TInt fileSize = 0;
       
   658     iFile.Seek(ESeekEnd, fileSize);
       
   659 
       
   660     iFile.Seek(ESeekStart, aPos);
       
   661 
       
   662     if (aPos == 0)
       
   663         {
       
   664         iBufferStartOffset = 0;
       
   665         iBufferEndOffset = 0;
       
   666 
       
   667         }
       
   668     
       
   669     if (aPos < 0) 
       
   670         {
       
   671         return EFalse;
       
   672         }
       
   673     else if (aPos > fileSize) 
       
   674         {
       
   675         return EFalse;
       
   676         }
       
   677     else 
       
   678         {
       
   679         iFilePos = aPos;
       
   680         }
       
   681     return ETrue;
       
   682     }
       
   683 
       
   684 TInt CProcInFileHandler::BufferedFileGetFilePos() 
       
   685     {
       
   686 
       
   687     return iFilePos;
       
   688 
       
   689     }
       
   690 
       
   691 TInt CProcInFileHandler::BufferedFileGetSize()
       
   692     {
       
   693 
       
   694     TInt fileSize = 0;
       
   695     iFile.Size(fileSize);
       
   696 
       
   697     return fileSize;
       
   698 
       
   699 
       
   700     }
       
   701 
       
   702     
       
   703 TInt8 CProcInFileHandler::NormalizingMargin() const 
       
   704     {
       
   705     return iNormalizingMargin;
       
   706     }
       
   707     
       
   708 TBool CProcInFileHandler::ManipulateGainL(HBufC8*& aFrameIn) 
       
   709     {
       
   710     
       
   711     if (iClip == 0)
       
   712         {
       
   713         return ETrue;
       
   714         }
       
   715                 
       
   716     if (iProperties->iAudioTypeExtension != EAudExtensionTypeNoExtension)
       
   717         {
       
   718         
       
   719         // gain manipulation with AACPlus is done in time domain
       
   720         
       
   721         if (iWavFrameHandler == 0)
       
   722             {
       
   723             
       
   724             const TInt KBitDepth = 16;
       
   725             iWavFrameHandler = CProcWAVFrameHandler::NewL(KBitDepth);
       
   726             }
       
   727             
       
   728         
       
   729         }
       
   730                 
       
   731     
       
   732     // check if the clip is normalized
       
   733     TInt8 normalizingGain1 = iNormalizingMargin;
       
   734     
       
   735     // check what is the current gain according to dynamic level marks
       
   736     TInt8 newGain1 = GetGainNow();
       
   737     
       
   738     
       
   739     // the combination of dynamic level marks + normalizing
       
   740     TInt8 normalizedGain1 = 0;
       
   741     if ( ((TInt)newGain1+normalizingGain1) > (TInt)KMaxTInt8)
       
   742         {
       
   743         normalizedGain1 = KMaxTInt8;
       
   744         }
       
   745     else
       
   746         {
       
   747         normalizedGain1 = static_cast<TInt8>(newGain1+normalizingGain1); 
       
   748         }
       
   749         
       
   750     
       
   751     if (normalizedGain1 != 0)
       
   752         {
       
   753         HBufC8* frameOut = 0;    
       
   754         // if we need to adjust gain...
       
   755         TBool manip = ETrue;
       
   756         if (iProperties->iAudioTypeExtension != EAudExtensionTypeNoExtension)
       
   757             {
       
   758             
       
   759             manip = iWavFrameHandler->ManipulateGainL(aFrameIn, frameOut, normalizedGain1);
       
   760             }
       
   761         else
       
   762             {
       
   763             manip = iFrameHandler->ManipulateGainL(aFrameIn, frameOut, normalizedGain1);    
       
   764             }
       
   765         
       
   766         
       
   767         if (manip)
       
   768             {
       
   769         
       
   770             CleanupStack::PushL(aFrameIn);
       
   771             CleanupStack::PushL(frameOut);
       
   772             
       
   773             if (frameOut->Size() > aFrameIn->Size())
       
   774                 {
       
   775                 // if manipulated frame was longer than the original
       
   776                 aFrameIn = aFrameIn->ReAllocL(frameOut->Size());
       
   777                 
       
   778                 }
       
   779             aFrameIn->Des().Delete(0, aFrameIn->Size());
       
   780             aFrameIn->Des().Copy(frameOut->Des());
       
   781             
       
   782             CleanupStack::PopAndDestroy(frameOut);
       
   783             CleanupStack::Pop(); // aFrameIn
       
   784             }
       
   785         
       
   786     
       
   787         }
       
   788     else
       
   789         {
       
   790         return EFalse;
       
   791         }
       
   792         
       
   793     return ETrue;
       
   794     }
       
   795 
       
   796 TInt8 CProcInFileHandler::GetGainNow() 
       
   797     {
       
   798     TInt markAmount = iClip->DynamicLevelMarkCount();
       
   799     TTimeIntervalMicroSeconds ti(0);
       
   800     TAudDynamicLevelMark previous(ti,0);
       
   801     //TInt32 durationMilliseconds =     (aClip->Info()->Properties().iDuration.Int64()/1000).GetTInt();
       
   802     TAudDynamicLevelMark next(iClip->Info()->Properties().iDuration,0);
       
   803 
       
   804     for (TInt a = 0 ; a < markAmount ; a++) 
       
   805         {
       
   806         TAudDynamicLevelMark markNow = iClip->DynamicLevelMark(a);
       
   807         
       
   808         if (ProcTools::MilliSeconds(markNow.iTime) > iCurrentTimeMilliseconds) 
       
   809             {
       
   810             next = markNow;
       
   811             break;
       
   812             }
       
   813 
       
   814         previous = markNow;
       
   815 
       
   816         }
       
   817 
       
   818     TInt32 previousMilli = ProcTools::MilliSeconds(previous.iTime);
       
   819     
       
   820     TInt32 nextMilli = ProcTools::MilliSeconds(next.iTime);
       
   821     
       
   822     TInt32 timeDifference = nextMilli - previousMilli;
       
   823     
       
   824     TInt8 previousLevel = previous.iLevel;
       
   825     TInt8 nextLevel = next.iLevel;
       
   826     
       
   827     // If the levels are positive then the amount of gain needs to be reduced
       
   828     if (previousLevel > 0)
       
   829         {
       
   830         previousLevel /= KAedPositiveGainDivider;
       
   831         }
       
   832     if (nextLevel > 0)
       
   833         {
       
   834         nextLevel /= KAedPositiveGainDivider;
       
   835         }
       
   836 
       
   837     if (timeDifference == 0) 
       
   838         {
       
   839         return previousLevel;
       
   840         }
       
   841         
       
   842     TInt32 fraction = ((iCurrentTimeMilliseconds-previousMilli)*100)/timeDifference;
       
   843     TInt8 newGain = 0;
       
   844     
       
   845     TInt8 levelDifference = static_cast<TInt8>(Abs(nextLevel - previousLevel));
       
   846     TInt8 inc = 0;
       
   847     if (fraction > 0) 
       
   848         {
       
   849         inc = static_cast<TInt8>((levelDifference*fraction)/100);
       
   850         }
       
   851 
       
   852     if (nextLevel - previousLevel >= 0) 
       
   853         {
       
   854         newGain = static_cast<TInt8>(previousLevel + inc);
       
   855         }
       
   856     else 
       
   857         {
       
   858         newGain = static_cast<TInt8>(previousLevel - inc);
       
   859         }
       
   860     
       
   861     return newGain;
       
   862 
       
   863     }
       
   864     
       
   865 TBool CProcInFileHandler::WriteDataToInputBufferL(const TDesC8& aData)
       
   866     {
       
   867     
       
   868     if (iInputBuffer == 0)
       
   869         {
       
   870         iInputBuffer = HBufC8::NewL(aData.Size());
       
   871         }
       
   872     else if (iInputBuffer->Des().MaxSize() < iInputBuffer->Length() + aData.Length())
       
   873         {
       
   874         iInputBuffer = iInputBuffer->ReAllocL(iInputBuffer->Length() + aData.Length());
       
   875         }
       
   876      
       
   877     iInputBuffer->Des().Append(aData);
       
   878     
       
   879     return ETrue;
       
   880     
       
   881     }