videoeditorengine/audioeditorengine/codecs/WAV/src/ProcWAVFrameHandler.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 
       
    22 #include "ProcWAVFrameHandler.h"
       
    23 #include "ProcFrameHandler.h"
       
    24 #include "ProcTools.h"
       
    25 #include "ProcConstants.h"
       
    26 
       
    27 #include <f32file.h>
       
    28 #include <e32math.h>
       
    29 
       
    30 
       
    31 TBool CProcWAVFrameHandler::ManipulateGainL(const HBufC8* aFrameIn, HBufC8*& aFrameOut, TInt8 aGain) 
       
    32     {
       
    33     TInt a = 0;
       
    34 
       
    35     aFrameOut = HBufC8::NewLC(aFrameIn->Size());
       
    36     
       
    37     aFrameOut->Des().Copy(aFrameIn->Ptr(), aFrameIn->Size());
       
    38 
       
    39     aGain = static_cast<TInt8>(aGain / 2);
       
    40 
       
    41     TPtr8 framePtr(aFrameOut->Des());
       
    42     if (iBitsPerSample == 8)
       
    43         {
       
    44 
       
    45         TReal multiplier = 0;
       
    46         TReal gaR(aGain);
       
    47         TReal exp = gaR/20;
       
    48         Math::Pow(multiplier, 10, exp);
       
    49 
       
    50         for (a = 0 ; a < aFrameOut->Length() ; a++)
       
    51             {
       
    52             TInt oldGain = (*aFrameOut)[a]-128;
       
    53             TInt newGain = 0;
       
    54             
       
    55             newGain = static_cast<TInt>(oldGain * multiplier);
       
    56             
       
    57             if (newGain > 128) 
       
    58                 {
       
    59                 newGain = 128;
       
    60                 }
       
    61             else if (newGain < -128)
       
    62                 {
       
    63                 newGain = -128;
       
    64                 }
       
    65         
       
    66             framePtr[a] = static_cast<TUint8>(newGain+128);
       
    67             
       
    68             }
       
    69         }
       
    70     else if (iBitsPerSample == 16)
       
    71         {
       
    72 
       
    73         TReal multiplier = 0;
       
    74         TReal gaR(aGain);
       
    75         TReal exp = gaR/20;
       
    76         Math::Pow(multiplier, 10, exp);
       
    77 
       
    78 
       
    79         for (a = 0 ; a < aFrameOut->Length()-1 ; a += 2)
       
    80             {
       
    81             
       
    82             TUint16 oldGain = static_cast<TUint16>((*aFrameOut)[a+1]*256 + (*aFrameOut)[a]);
       
    83 
       
    84             TBool negative = EFalse;
       
    85 
       
    86             if (oldGain > 32767) 
       
    87                 {
       
    88                 oldGain = static_cast<TUint16>(~oldGain+1);// - 65793;
       
    89                 negative = ETrue;
       
    90                 }
       
    91 
       
    92             TUint16 newGain = static_cast<TUint16>(oldGain * multiplier);
       
    93 
       
    94             if (newGain > 32727) 
       
    95                 {
       
    96                 newGain = 32727;
       
    97                 }
       
    98 
       
    99             
       
   100             if (negative)
       
   101                 {
       
   102                 newGain = static_cast<TUint16>(~newGain-1);// - 65793;
       
   103 
       
   104                 }
       
   105             framePtr[a+1] = static_cast<TUint8>(newGain/256);
       
   106             framePtr[a] = static_cast<TUint8>(newGain%256);
       
   107 
       
   108             
       
   109 
       
   110             }
       
   111         
       
   112         }
       
   113 
       
   114 
       
   115     CleanupStack::Pop(); // aFrameOut
       
   116     return ETrue;
       
   117     }
       
   118 
       
   119 TBool CProcWAVFrameHandler::GetGainL(const HBufC8* aFrame, RArray<TInt>& aGains, TInt& aMaxGain) const
       
   120     {
       
   121 
       
   122     TInt a = 0;
       
   123 
       
   124     TInt highest = 0;
       
   125 
       
   126     if (iBitsPerSample == 8)
       
   127         {
       
   128         
       
   129         for (a = 0 ; a < aFrame->Length() ; a++)
       
   130             {
       
   131 
       
   132 
       
   133             if (Abs((*aFrame)[a]-128) > highest)
       
   134                 highest = Abs((*aFrame)[a]-128);
       
   135     
       
   136             }
       
   137         }
       
   138     else if (iBitsPerSample == 16)
       
   139         {
       
   140         
       
   141         for (a = 0 ; a < aFrame->Length()-1 ; a += 2)
       
   142             {
       
   143             TInt ga = ((*aFrame)[a+1]*256 + (*aFrame)[a]);
       
   144 
       
   145             if (ga > 32767) ga-= 65792;
       
   146 
       
   147 
       
   148 
       
   149             if (ga > highest) highest = ga;
       
   150 
       
   151     
       
   152             }
       
   153         
       
   154         }
       
   155 
       
   156 
       
   157     aGains.Append(highest);
       
   158 
       
   159     if (iBitsPerSample == 8) aMaxGain = 128;
       
   160     else if (iBitsPerSample == 16) aMaxGain = 32767;
       
   161 
       
   162 
       
   163     return ETrue;
       
   164     }
       
   165 
       
   166 
       
   167 TBool CProcWAVFrameHandler::GetNormalizingMargin(const HBufC8* aFrame, TInt8& aMargin) const
       
   168 
       
   169     {
       
   170 
       
   171 
       
   172     TInt maxGain = 0;
       
   173     TInt highestGain = GetHighestGain(aFrame, maxGain);
       
   174 
       
   175 
       
   176     TInt ma = ((maxGain - highestGain)*2)/5;
       
   177     aMargin = static_cast<TInt8>(ma);
       
   178 
       
   179     return ETrue;
       
   180 
       
   181 
       
   182     }
       
   183 
       
   184 TBool CProcWAVFrameHandler::IsMixingAvailable() const
       
   185     {
       
   186     return ETrue;
       
   187     }
       
   188 TBool CProcWAVFrameHandler::MixL(const HBufC8* aFrame1, const HBufC8* aFrame2, HBufC8*& aMixedFrame)
       
   189     {
       
   190 
       
   191     if (aFrame1->Length() != aFrame2->Length()) 
       
   192         {
       
   193         aMixedFrame = 0;
       
   194         return EFalse;
       
   195         }
       
   196 
       
   197     aMixedFrame = HBufC8::NewL(aFrame1->Length());
       
   198 
       
   199     TInt a = 0;
       
   200     TInt newGain = 0;
       
   201     
       
   202     if (iBitsPerSample == 8)
       
   203         {
       
   204 
       
   205         for (a = 0 ; a < aFrame1->Length() ; a++)
       
   206             {
       
   207         
       
   208             TInt oldGain1 = (*aFrame1)[a]-128;
       
   209             TInt oldGain2 = (*aFrame2)[a]-128;
       
   210             
       
   211             newGain = oldGain1 + oldGain2;
       
   212             
       
   213             if (newGain > 128) 
       
   214                 {
       
   215                 newGain = 128;
       
   216                 }
       
   217             else if (newGain < -128)
       
   218                 {
       
   219                 newGain = -128;
       
   220                 }
       
   221         
       
   222             aMixedFrame->Des().Append(static_cast<TUint8>(newGain+128));
       
   223             
       
   224             }
       
   225         }
       
   226     else if (iBitsPerSample == 16)
       
   227         {
       
   228 
       
   229 
       
   230         for (a = 0 ; a < aFrame1->Length()-1 ; a += 2)
       
   231             {
       
   232             
       
   233             TUint16 oldGain1 = static_cast<TUint16>((*aFrame1)[a+1]*256 + (*aFrame1)[a]);
       
   234             TUint16 oldGain2 = static_cast<TUint16>((*aFrame2)[a+1]*256 + (*aFrame2)[a]);
       
   235 
       
   236 
       
   237             TBool negative1 = EFalse;
       
   238             TBool negative2 = EFalse;
       
   239 
       
   240             if (oldGain1 > 32767) 
       
   241                 {
       
   242                 //oldGain1 = ~oldGain1+1;// - 65793;
       
   243                 negative1 = ETrue;
       
   244 //                oldGain1 = oldGain1+((65536-oldGain1)/2);
       
   245 
       
   246                 }
       
   247             else
       
   248                 {
       
   249 
       
   250 //                oldGain1 /= 2; 
       
   251 
       
   252                 }
       
   253 
       
   254             if (oldGain2 > 32767) 
       
   255                 {
       
   256                 //oldGain2 = ~oldGain2+1;// - 65793;
       
   257                 negative2 = ETrue;
       
   258 //                oldGain2 = oldGain2+((65536-oldGain2)/2);
       
   259 
       
   260                 }
       
   261             else
       
   262                 {
       
   263 //                oldGain2 /= 2; 
       
   264                 }
       
   265 
       
   266 
       
   267             newGain = static_cast<TUint16>(oldGain1 + oldGain2);
       
   268 
       
   269 
       
   270             if (negative1 && negative2)
       
   271                 {
       
   272                 if (newGain < 32767)
       
   273                     {    
       
   274                     newGain = 32768;
       
   275                     }
       
   276                 }
       
   277             else if (!negative1 && !negative2)
       
   278                 {
       
   279                 if (newGain > 32767)
       
   280                     {    
       
   281                     newGain = 32767;
       
   282                     }
       
   283                 
       
   284                 }
       
   285 
       
   286 
       
   287             aMixedFrame->Des().Append(static_cast<TUint8>(newGain%256));
       
   288             aMixedFrame->Des().Append(static_cast<TUint8>(newGain/256));
       
   289 
       
   290             }
       
   291         
       
   292         }
       
   293 
       
   294     return ETrue;
       
   295 
       
   296     }
       
   297 
       
   298 
       
   299 
       
   300 CProcWAVFrameHandler::~CProcWAVFrameHandler() 
       
   301     {
       
   302 
       
   303     }
       
   304 
       
   305 CProcWAVFrameHandler* CProcWAVFrameHandler::NewL(TInt aBitsPerSample) 
       
   306     {
       
   307 
       
   308     
       
   309     CProcWAVFrameHandler* self = NewLC(aBitsPerSample);
       
   310     CleanupStack::Pop(self);
       
   311     return self;
       
   312 
       
   313     }
       
   314 CProcWAVFrameHandler* CProcWAVFrameHandler::NewLC(TInt aBitsPerSample) 
       
   315     {
       
   316 
       
   317     CProcWAVFrameHandler* self = new (ELeave) CProcWAVFrameHandler(aBitsPerSample);
       
   318     CleanupStack::PushL(self);
       
   319     self->ConstructL();
       
   320     return self;
       
   321 
       
   322     }
       
   323 
       
   324 void CProcWAVFrameHandler::ConstructL() 
       
   325     {
       
   326 
       
   327     }
       
   328 
       
   329 CProcWAVFrameHandler::CProcWAVFrameHandler(TInt aBitsPerSample) : iBitsPerSample(aBitsPerSample)
       
   330     {
       
   331 
       
   332     }
       
   333  
       
   334 TInt CProcWAVFrameHandler::GetHighestGain(const HBufC8* aFrame, TInt& aMaxGain) const
       
   335     {
       
   336 
       
   337     TInt maxGain = 0;
       
   338     TInt a = 0;
       
   339 
       
   340     if (iBitsPerSample == 8)
       
   341         {
       
   342         aMaxGain = 210;
       
   343             
       
   344         for (a = 0 ; a < aFrame->Length() ; a++)
       
   345             {
       
   346             
       
   347             TInt gain = Abs((*aFrame)[a]-128);
       
   348             if (gain > maxGain) maxGain = gain;
       
   349 
       
   350             }
       
   351         }
       
   352     else if (iBitsPerSample == 16)
       
   353         {
       
   354 
       
   355         aMaxGain = 452;
       
   356         for (a = 0 ; a < aFrame->Length()-1 ; a += 2)
       
   357             {
       
   358             TInt ga = ((*aFrame)[a+1]*256 + (*aFrame)[a]);
       
   359 
       
   360             if (ga > 32767) ga-= 65792;
       
   361 
       
   362             ga = Abs(ga);
       
   363             
       
   364             if (ga > maxGain) maxGain = ga;
       
   365 
       
   366             }
       
   367         
       
   368         }
       
   369 
       
   370     TReal result = 0;
       
   371     TReal gai(maxGain);
       
   372 
       
   373     if (gai!= 0) Math::Log(result, gai);
       
   374     result *= 100;
       
   375 
       
   376     return (TInt) result;
       
   377 
       
   378 
       
   379 
       
   380 
       
   381 
       
   382     }
       
   383     
       
   384