sl@0: // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #ifndef RATECONVERTIMPL_H sl@0: #define RATECONVERTIMPL_H sl@0: sl@0: #include "mmf/utils/rateconvert.h" sl@0: sl@0: /** sl@0: Abstract class that all specific converters derive from. sl@0: */ sl@0: NONSHARABLE_CLASS( CChannelAndSampleRateConverterCommon ) : public CChannelAndSampleRateConverter sl@0: { sl@0: public: sl@0: virtual void SetRates(TInt aSrcRate,TInt aDstRate); sl@0: sl@0: // from CChannelAndSampleRateConverter sl@0: void Reset(); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: sl@0: protected: sl@0: CChannelAndSampleRateConverterCommon(); sl@0: sl@0: static TInt NextPowerUp(TInt aValue); sl@0: inline static TInt StereoToMono(TInt aSample); sl@0: inline static TInt MonoToStereo(TInt aSample); sl@0: sl@0: inline static TInt LengthBytesTo16Bit(TInt aLength); sl@0: inline static TInt LengthBytesTo32Bit(TInt aLength); sl@0: inline static TInt Length16BitToBytes(TInt aLength); sl@0: inline static TInt Length32BitToBytes(TInt aLength); sl@0: }; sl@0: sl@0: /* functions for copying from mono to stereo and from stereo to mono */ sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::StereoToMono(TInt aSample) sl@0: { sl@0: TInt topChannel = aSample>>16; // will naturally keep sign sl@0: TInt bottomChannel = TInt16(aSample&0xffff); // need to sign extend sl@0: TInt monoSample = (topChannel + bottomChannel)/2; // add two channels and divide sl@0: return monoSample; sl@0: } sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::MonoToStereo(TInt aSample) sl@0: { sl@0: TInt32 stereoSample = (aSample<<16) | (aSample&0xffff); sl@0: #ifdef _DEBUG sl@0: TInt monoSample = StereoToMono(stereoSample); sl@0: ASSERT(monoSample==aSample); sl@0: #endif sl@0: return TInt(stereoSample); sl@0: } sl@0: sl@0: // Convert various lengths by multipling or dividing by 2 or 4 sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::LengthBytesTo16Bit(TInt aLength) sl@0: { sl@0: return aLength/sizeof(TInt16); sl@0: } sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::LengthBytesTo32Bit(TInt aLength) sl@0: { sl@0: return aLength/sizeof(TInt32); sl@0: } sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::Length16BitToBytes(TInt aLength) sl@0: { sl@0: return aLength*sizeof(TInt16); sl@0: } sl@0: sl@0: inline TInt CChannelAndSampleRateConverterCommon::Length32BitToBytes(TInt aLength) sl@0: { sl@0: return aLength*sizeof(TInt32); sl@0: } sl@0: sl@0: /** sl@0: Abstract class that all specific rate converters derive from - as opposed to channel converters sl@0: */ sl@0: NONSHARABLE_CLASS( CChannelAndSampleRateConvert ) : public CChannelAndSampleRateConverterCommon sl@0: { sl@0: public: sl@0: sl@0: // from CChannelAndSampleRateConverterCommon sl@0: void Reset(); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: void SetRates(TInt aSrcRate,TInt aDstRate); sl@0: sl@0: protected: sl@0: CChannelAndSampleRateConvert(); sl@0: sl@0: TInt SizeOfUpsampleBuffer(TInt aBufferLength); sl@0: protected: sl@0: /* sl@0: The sample rate of the data in the source buffer sl@0: */ sl@0: TInt iFromRate; sl@0: /* sl@0: The sample rate of the data in the destination buffer sl@0: */ sl@0: TInt iToRate; sl@0: sl@0: TInt iFraction; sl@0: TInt iIndex; sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CStereoToMonoRateConverter ) : public CChannelAndSampleRateConvert sl@0: { sl@0: public: sl@0: CStereoToMonoRateConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConvert sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CStereoToMonoConverter ) : public CChannelAndSampleRateConverterCommon sl@0: { sl@0: public: sl@0: CStereoToMonoConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConverterCommon sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CStereoToStereoRateConverter ) : public CChannelAndSampleRateConvert sl@0: { sl@0: public: sl@0: CStereoToStereoRateConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConvert sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CMonoToMonoRateConverter ) : public CChannelAndSampleRateConvert sl@0: { sl@0: public: sl@0: CMonoToMonoRateConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConvert sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CMonoToStereoConverter ) : public CChannelAndSampleRateConverterCommon sl@0: { sl@0: public: sl@0: CMonoToStereoConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConverterCommon sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: }; sl@0: sl@0: NONSHARABLE_CLASS( CMonoToStereoRateConverter ) : public CChannelAndSampleRateConvert sl@0: { sl@0: public: sl@0: CMonoToStereoRateConverter(); sl@0: sl@0: protected: sl@0: // from CChannelAndSampleRateConvert sl@0: TInt Convert(const TDesC8& aSrcBuffer, TDes8& aDstBuffer); sl@0: TInt MaxConvertBufferSize(TInt aSrcBufferSize, TBool aRoundUpToPower); sl@0: }; sl@0: sl@0: #endif sl@0: