omxilcomp/omxilaudioemulator/pcmrenderer/src/rateconvertimpl.h
changeset 0 58be5850fb6c
equal deleted inserted replaced
-1:000000000000 0:58be5850fb6c
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef RATECONVERTIMPL_H
       
    17 #define RATECONVERTIMPL_H
       
    18 
       
    19 #include "rateconvert.h"
       
    20 
       
    21 /**
       
    22 Abstract class that all specific converters derive from.
       
    23 */
       
    24 NONSHARABLE_CLASS( CChannelAndSampleRateConverterCommon ) : public CChannelAndSampleRateConverter
       
    25 	{
       
    26 public:
       
    27 	virtual void SetRates(TInt aSrcRate,TInt aDstRate);
       
    28 	
       
    29 	// from CChannelAndSampleRateConverter
       
    30 	void Reset();
       
    31 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
    32 	
       
    33 protected:
       
    34 	CChannelAndSampleRateConverterCommon();
       
    35 	
       
    36 	static TInt NextPowerUp(TInt aValue);
       
    37 	inline static TInt StereoToMono(TInt aSample);
       
    38 	inline static TInt MonoToStereo(TInt aSample);
       
    39 	
       
    40 	inline static TInt LengthBytesTo16Bit(TInt aLength);
       
    41 	inline static TInt LengthBytesTo32Bit(TInt aLength);
       
    42 	inline static TInt Length16BitToBytes(TInt aLength);
       
    43 	inline static TInt Length32BitToBytes(TInt aLength);
       
    44 	};
       
    45 	
       
    46 /* functions for copying from mono to stereo and from stereo to mono */
       
    47 
       
    48 inline TInt CChannelAndSampleRateConverterCommon::StereoToMono(TInt aSample)
       
    49 	{
       
    50 	TInt topChannel = aSample>>16; // will naturally keep sign
       
    51 	TInt bottomChannel = TInt16(aSample&0xffff); // need to sign extend
       
    52 	TInt monoSample = (topChannel + bottomChannel)/2; // add two channels and divide
       
    53 	return monoSample;
       
    54 	}
       
    55 	
       
    56 inline TInt CChannelAndSampleRateConverterCommon::MonoToStereo(TInt aSample)
       
    57 	{
       
    58 	TInt32 stereoSample = (aSample<<16) | (aSample&0xffff);
       
    59 #ifdef _DEBUG
       
    60 	TInt monoSample = StereoToMono(stereoSample);
       
    61 	ASSERT(monoSample==aSample);
       
    62 #endif
       
    63 	return TInt(stereoSample);
       
    64 	}
       
    65 	
       
    66 // Convert various lengths by multipling or dividing by 2 or 4
       
    67 
       
    68 inline TInt CChannelAndSampleRateConverterCommon::LengthBytesTo16Bit(TInt aLength)
       
    69 	{
       
    70 	return aLength/sizeof(TInt16);
       
    71 	}
       
    72 	
       
    73 inline TInt CChannelAndSampleRateConverterCommon::LengthBytesTo32Bit(TInt aLength)
       
    74 	{
       
    75 	return aLength/sizeof(TInt32);
       
    76 	}
       
    77 	
       
    78 inline TInt CChannelAndSampleRateConverterCommon::Length16BitToBytes(TInt aLength)
       
    79 	{
       
    80 	return aLength*sizeof(TInt16);
       
    81 	}
       
    82 	
       
    83 inline TInt CChannelAndSampleRateConverterCommon::Length32BitToBytes(TInt aLength)
       
    84 	{
       
    85 	return aLength*sizeof(TInt32);
       
    86 	}
       
    87 
       
    88 /**
       
    89 Abstract class that all specific rate converters derive from - as opposed to channel converters
       
    90 */
       
    91 NONSHARABLE_CLASS( CChannelAndSampleRateConvert ) : public CChannelAndSampleRateConverterCommon
       
    92 	{
       
    93 public:
       
    94 	
       
    95 	// from CChannelAndSampleRateConverterCommon
       
    96 	void Reset();
       
    97 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
    98 	void SetRates(TInt aSrcRate,TInt aDstRate);
       
    99 	
       
   100 protected:
       
   101 	CChannelAndSampleRateConvert();
       
   102 	
       
   103 	TInt SizeOfUpsampleBuffer(TInt aBufferLength);
       
   104 protected:
       
   105 	/*
       
   106 	The sample rate of the data in the source buffer
       
   107 	*/
       
   108 	TInt iFromRate;
       
   109 	/*
       
   110 	The sample rate of the data in the destination buffer
       
   111 	*/
       
   112 	TInt iToRate;
       
   113 
       
   114 	TInt iFraction;
       
   115 	TInt iIndex;
       
   116 	};
       
   117 
       
   118 NONSHARABLE_CLASS( CStereoToMonoRateConverter ) : public CChannelAndSampleRateConvert
       
   119 	{
       
   120 public:
       
   121 	CStereoToMonoRateConverter();
       
   122 	
       
   123 protected:
       
   124 	// from CChannelAndSampleRateConvert
       
   125 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   126 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
   127 	};
       
   128 
       
   129 NONSHARABLE_CLASS( CStereoToMonoConverter ) : public CChannelAndSampleRateConverterCommon
       
   130 	{
       
   131 public:
       
   132 	CStereoToMonoConverter();
       
   133 	
       
   134 protected:
       
   135 	// from CChannelAndSampleRateConverterCommon
       
   136 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   137 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
   138 	};
       
   139 
       
   140 NONSHARABLE_CLASS( CStereoToStereoRateConverter ) : public CChannelAndSampleRateConvert
       
   141 	{
       
   142 public:
       
   143 	CStereoToStereoRateConverter();
       
   144 	
       
   145 protected:
       
   146 	// from CChannelAndSampleRateConvert
       
   147 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   148 	};
       
   149 
       
   150 NONSHARABLE_CLASS( CMonoToMonoRateConverter ) : public CChannelAndSampleRateConvert
       
   151 	{
       
   152 public:
       
   153 	CMonoToMonoRateConverter();
       
   154 	
       
   155 protected:
       
   156 	// from CChannelAndSampleRateConvert
       
   157 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   158 	};
       
   159 
       
   160 NONSHARABLE_CLASS( CMonoToStereoConverter ) : public CChannelAndSampleRateConverterCommon
       
   161 	{
       
   162 public:
       
   163 	CMonoToStereoConverter();
       
   164 	
       
   165 protected:
       
   166 	// from CChannelAndSampleRateConverterCommon
       
   167 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   168 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
   169 	};
       
   170 
       
   171 NONSHARABLE_CLASS( CMonoToStereoRateConverter ) : public CChannelAndSampleRateConvert
       
   172 	{
       
   173 public:
       
   174 	CMonoToStereoRateConverter();
       
   175 	
       
   176 protected:
       
   177 	// from CChannelAndSampleRateConvert
       
   178 	TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer);
       
   179 	TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower);
       
   180 	};
       
   181 
       
   182 #endif
       
   183