sl@0
|
1 |
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "mmfclientaudiostreamutils.h"
|
sl@0
|
17 |
#include <mmf/common/mmfstandardcustomcommands.h>
|
sl@0
|
18 |
#include <mda/common/audio.h>
|
sl@0
|
19 |
|
sl@0
|
20 |
const TInt KSampleRate8000 = 8000;
|
sl@0
|
21 |
const TInt KSampleRate11025 = 11025;
|
sl@0
|
22 |
const TInt KSampleRate12000 = 12000;
|
sl@0
|
23 |
const TInt KSampleRate16000 = 16000;
|
sl@0
|
24 |
const TInt KSampleRate22050 = 22050;
|
sl@0
|
25 |
const TInt KSampleRate24000 = 24000;
|
sl@0
|
26 |
const TInt KSampleRate32000 = 32000;
|
sl@0
|
27 |
const TInt KSampleRate44100 = 44100;
|
sl@0
|
28 |
const TInt KSampleRate48000 = 48000;
|
sl@0
|
29 |
const TInt KSampleRate64000 = 64000;
|
sl@0
|
30 |
const TInt KSampleRate88200 = 88200;
|
sl@0
|
31 |
const TInt KSampleRate96000 = 96000;
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
_LIT(KStreamUtilsPanicCategory, "MMFStreamUtils");
|
sl@0
|
35 |
|
sl@0
|
36 |
enum TStreamUtilsPanic
|
sl@0
|
37 |
{
|
sl@0
|
38 |
EPanicBadArgument,
|
sl@0
|
39 |
EPanicPostConditionViolation,
|
sl@0
|
40 |
EPanicUnknownSampleRate,
|
sl@0
|
41 |
EPanicUnknownChannelSetting,
|
sl@0
|
42 |
EPanicUnknownEncoding
|
sl@0
|
43 |
};
|
sl@0
|
44 |
|
sl@0
|
45 |
LOCAL_C void Panic(const TStreamUtilsPanic aReason)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
User::Panic(KStreamUtilsPanicCategory, aReason);
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
/**
|
sl@0
|
51 |
*
|
sl@0
|
52 |
* Function to map the channels enum defined in Mda to the one
|
sl@0
|
53 |
* defined in MMF
|
sl@0
|
54 |
*
|
sl@0
|
55 |
*/
|
sl@0
|
56 |
TUint StreamUtils::MapChannelsMdaToMMFL(TInt aMdaChannels)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
if (aMdaChannels >= 0)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
if (aMdaChannels & TMdaAudioDataSettings::EChannelsStereo)
|
sl@0
|
61 |
{
|
sl@0
|
62 |
return EMMFStereo;
|
sl@0
|
63 |
}
|
sl@0
|
64 |
else if ((aMdaChannels == 0) || // zeroed settings (it's valid.) return a default value
|
sl@0
|
65 |
(aMdaChannels & TMdaAudioDataSettings::EChannelsMono))
|
sl@0
|
66 |
{
|
sl@0
|
67 |
return EMMFMono;
|
sl@0
|
68 |
}
|
sl@0
|
69 |
}
|
sl@0
|
70 |
// Invalid value
|
sl@0
|
71 |
User::Leave(KErrNotSupported);
|
sl@0
|
72 |
return 0;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
/**
|
sl@0
|
76 |
*
|
sl@0
|
77 |
* Function to map the sample rate enum defined in Mda to the one
|
sl@0
|
78 |
* defined in MMF
|
sl@0
|
79 |
*
|
sl@0
|
80 |
*/
|
sl@0
|
81 |
TUint StreamUtils::MapSampleRateMdaToMMFL(TInt aMdaSampleRate)
|
sl@0
|
82 |
{
|
sl@0
|
83 |
switch (aMdaSampleRate)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
case TMdaAudioDataSettings::ESampleRate8000Hz:
|
sl@0
|
86 |
return EMMFSampleRate8000Hz;
|
sl@0
|
87 |
case TMdaAudioDataSettings::ESampleRate11025Hz:
|
sl@0
|
88 |
return EMMFSampleRate11025Hz;
|
sl@0
|
89 |
case TMdaAudioDataSettings::ESampleRate12000Hz:
|
sl@0
|
90 |
return EMMFSampleRate12000Hz;
|
sl@0
|
91 |
case TMdaAudioDataSettings::ESampleRate16000Hz:
|
sl@0
|
92 |
return EMMFSampleRate16000Hz;
|
sl@0
|
93 |
case TMdaAudioDataSettings::ESampleRate22050Hz:
|
sl@0
|
94 |
return EMMFSampleRate22050Hz;
|
sl@0
|
95 |
case TMdaAudioDataSettings::ESampleRate24000Hz:
|
sl@0
|
96 |
return EMMFSampleRate24000Hz;
|
sl@0
|
97 |
case TMdaAudioDataSettings::ESampleRate32000Hz:
|
sl@0
|
98 |
return EMMFSampleRate32000Hz;
|
sl@0
|
99 |
case TMdaAudioDataSettings::ESampleRate44100Hz:
|
sl@0
|
100 |
return EMMFSampleRate44100Hz;
|
sl@0
|
101 |
case TMdaAudioDataSettings::ESampleRate48000Hz:
|
sl@0
|
102 |
return EMMFSampleRate48000Hz;
|
sl@0
|
103 |
case TMdaAudioDataSettings::ESampleRate64000Hz:
|
sl@0
|
104 |
return EMMFSampleRate64000Hz;
|
sl@0
|
105 |
case TMdaAudioDataSettings::ESampleRate96000Hz:
|
sl@0
|
106 |
return EMMFSampleRate96000Hz;
|
sl@0
|
107 |
case TMdaAudioDataSettings::ESampleRateFixed:
|
sl@0
|
108 |
case TMdaAudioDataSettings::ESampleRateAnyInRange:
|
sl@0
|
109 |
User::Leave(KErrNotSupported);
|
sl@0
|
110 |
return 0;
|
sl@0
|
111 |
case 0: // zeroed settings (it's valid.) return a default value
|
sl@0
|
112 |
return EMMFSampleRate8000Hz;
|
sl@0
|
113 |
default:
|
sl@0
|
114 |
User::Leave(KErrNotSupported);
|
sl@0
|
115 |
return 0;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
}
|
sl@0
|
118 |
|
sl@0
|
119 |
/**
|
sl@0
|
120 |
* Function to convert the mmf sample rate enum to an actual sample rate value
|
sl@0
|
121 |
*/
|
sl@0
|
122 |
TInt StreamUtils::SampleRateAsValue(const TMMFCapabilities& aCaps)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
switch (aCaps.iRate)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
case EMMFSampleRate8000Hz:
|
sl@0
|
127 |
return KSampleRate8000;
|
sl@0
|
128 |
case EMMFSampleRate11025Hz:
|
sl@0
|
129 |
return KSampleRate11025;
|
sl@0
|
130 |
case EMMFSampleRate12000Hz:
|
sl@0
|
131 |
return KSampleRate12000;
|
sl@0
|
132 |
case EMMFSampleRate16000Hz:
|
sl@0
|
133 |
return KSampleRate16000;
|
sl@0
|
134 |
case EMMFSampleRate22050Hz:
|
sl@0
|
135 |
return KSampleRate22050;
|
sl@0
|
136 |
case EMMFSampleRate24000Hz:
|
sl@0
|
137 |
return KSampleRate24000;
|
sl@0
|
138 |
case EMMFSampleRate32000Hz:
|
sl@0
|
139 |
return KSampleRate32000;
|
sl@0
|
140 |
case EMMFSampleRate44100Hz:
|
sl@0
|
141 |
return KSampleRate44100;
|
sl@0
|
142 |
case EMMFSampleRate48000Hz:
|
sl@0
|
143 |
return KSampleRate48000;
|
sl@0
|
144 |
case EMMFSampleRate64000Hz:
|
sl@0
|
145 |
return KSampleRate64000;
|
sl@0
|
146 |
case EMMFSampleRate88200Hz:
|
sl@0
|
147 |
return KSampleRate88200;
|
sl@0
|
148 |
case EMMFSampleRate96000Hz:
|
sl@0
|
149 |
return KSampleRate96000;
|
sl@0
|
150 |
default:
|
sl@0
|
151 |
Panic(EPanicUnknownSampleRate);
|
sl@0
|
152 |
return 0;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|
sl@0
|
156 |
/**
|
sl@0
|
157 |
* Return the current number of bytes required to render each sample, based
|
sl@0
|
158 |
* on the given capabilities. This depends on the current encoding and
|
sl@0
|
159 |
* whether the sample is mono or stereo
|
sl@0
|
160 |
*/
|
sl@0
|
161 |
TInt StreamUtils::BytesPerSample(const TMMFCapabilities& aCaps)
|
sl@0
|
162 |
{
|
sl@0
|
163 |
TInt noOfChannels = (aCaps.iChannels == EMMFStereo) ? 2 : 1;
|
sl@0
|
164 |
switch (aCaps.iEncoding)
|
sl@0
|
165 |
{
|
sl@0
|
166 |
case EMMFSoundEncoding8BitPCM:
|
sl@0
|
167 |
case EMMFSoundEncoding8BitALaw:
|
sl@0
|
168 |
case EMMFSoundEncoding8BitMuLaw:
|
sl@0
|
169 |
return (1 * noOfChannels);
|
sl@0
|
170 |
case EMMFSoundEncoding16BitPCM:
|
sl@0
|
171 |
return (2 * noOfChannels);
|
sl@0
|
172 |
default:
|
sl@0
|
173 |
Panic(EPanicUnknownEncoding);
|
sl@0
|
174 |
return 0;
|
sl@0
|
175 |
}
|
sl@0
|
176 |
}
|
sl@0
|
177 |
/**
|
sl@0
|
178 |
* CalculateLeftRightBalance
|
sl@0
|
179 |
* @param aLeft
|
sl@0
|
180 |
* @param aRight
|
sl@0
|
181 |
* @param aBalance
|
sl@0
|
182 |
* Preconditions:
|
sl@0
|
183 |
* !(aBalance < KMMFBalanceMaxLeft || aBalance > KMMFBalanceMaxRight)
|
sl@0
|
184 |
* y = m x + c
|
sl@0
|
185 |
* aLeft = m ( aBalance ) + c
|
sl@0
|
186 |
* when aBalance = KMMFBalanceMaxLeft aLeft = 100
|
sl@0
|
187 |
* when aBalance = KMMFBalanceMaxRight aLeft = 0
|
sl@0
|
188 |
* 100 = m( KMMFBalanceMaxLeft ) + c
|
sl@0
|
189 |
* 0 = m( KMMFBalanceMaxRight ) + c
|
sl@0
|
190 |
* c = -(KMMFBalanceMaxRight) m
|
sl@0
|
191 |
* 100 = m(KMMFBalanceMaxLeft ) - m(KMMFBalanceMaxRight)
|
sl@0
|
192 |
* m = 100/(KMMFBalanceMaxLeft - KMMFBalanceMaxRight )
|
sl@0
|
193 |
* c = -(KMMFBalanceMaxRight) * 100 /(KMMFBalanceMaxLeft - KMMFBalanceMaxRight )
|
sl@0
|
194 |
* aLeft = ( aBalance - KMMFBalanceMaxRight ) * 100 /( KMMFBalanceMaxLeft - KMMFBalanceMaxRight )
|
sl@0
|
195 |
*/
|
sl@0
|
196 |
void StreamUtils::CalculateLeftRightBalance( TInt& aLeft, TInt& aRight, TInt aBalance )
|
sl@0
|
197 |
{
|
sl@0
|
198 |
// [ assert precondition that aBalance is within limits ]
|
sl@0
|
199 |
__ASSERT_DEBUG( !(aBalance < KMMFBalanceMaxLeft || aBalance > KMMFBalanceMaxRight), Panic(EPanicBadArgument));
|
sl@0
|
200 |
|
sl@0
|
201 |
//[ Now separate percentage balances out from aBalance ]
|
sl@0
|
202 |
aLeft = (100 * (aBalance-KMMFBalanceMaxRight)) / (KMMFBalanceMaxLeft-KMMFBalanceMaxRight);
|
sl@0
|
203 |
aRight = 100 - aLeft;
|
sl@0
|
204 |
|
sl@0
|
205 |
//[ assert post condition that left and right are within range ]
|
sl@0
|
206 |
__ASSERT_DEBUG( ( (aLeft <= 100) && (aLeft >= 0) ), Panic(EPanicPostConditionViolation));
|
sl@0
|
207 |
__ASSERT_DEBUG( ( (aRight <= 100) && (aRight >= 0) ), Panic(EPanicPostConditionViolation));
|
sl@0
|
208 |
}
|
sl@0
|
209 |
|
sl@0
|
210 |
/**
|
sl@0
|
211 |
* CalculateBalance
|
sl@0
|
212 |
* @param aBalance
|
sl@0
|
213 |
* @param aLeft
|
sl@0
|
214 |
* @param aRight
|
sl@0
|
215 |
*
|
sl@0
|
216 |
* follows a simple straight line transformation
|
sl@0
|
217 |
* y = m x + c
|
sl@0
|
218 |
* m = (KMMFBalanceMaxLeft-KMMFBalanceMaxRight)/ 100
|
sl@0
|
219 |
* c = KMMFBalanceMaxRight
|
sl@0
|
220 |
* by substitution
|
sl@0
|
221 |
* when aLeft = 0
|
sl@0
|
222 |
* KMMFBalanceMaxRight = m * 0 + c
|
sl@0
|
223 |
* c = KMMFBalanceMaxRight
|
sl@0
|
224 |
* when aLeft = 100
|
sl@0
|
225 |
* KMMFBalanceMaxLeft = m * 100 + KMMFBalanceMaxRight
|
sl@0
|
226 |
* m = ( KMMFBalanceMaxLeft - KMMFBalanceMaxRight ) /100
|
sl@0
|
227 |
*/
|
sl@0
|
228 |
#ifdef _DEBUG
|
sl@0
|
229 |
void StreamUtils::CalculateBalance( TInt& aBalance, TInt aLeft, TInt aRight )
|
sl@0
|
230 |
#else
|
sl@0
|
231 |
void StreamUtils::CalculateBalance( TInt& aBalance, TInt aLeft, TInt /*aRight*/ )
|
sl@0
|
232 |
#endif // _DEBUG
|
sl@0
|
233 |
{
|
sl@0
|
234 |
//[ assert pre conditions ]
|
sl@0
|
235 |
__ASSERT_DEBUG( (( aLeft + aRight ) == 100 ), Panic( EPanicBadArgument ));
|
sl@0
|
236 |
__ASSERT_DEBUG( (( 0 <= aLeft) && ( 100 >= aLeft)), Panic( EPanicBadArgument) );
|
sl@0
|
237 |
__ASSERT_DEBUG( (( 0 <= aRight) && ( 100 >= aRight)), Panic( EPanicBadArgument) );
|
sl@0
|
238 |
|
sl@0
|
239 |
aBalance = (aLeft * (KMMFBalanceMaxLeft-KMMFBalanceMaxRight))/100 + KMMFBalanceMaxRight;
|
sl@0
|
240 |
|
sl@0
|
241 |
//[ assert post condition that aBalance is within limits ]
|
sl@0
|
242 |
__ASSERT_DEBUG( !(aBalance < KMMFBalanceMaxLeft || aBalance > KMMFBalanceMaxRight), Panic(EPanicPostConditionViolation));
|
sl@0
|
243 |
|
sl@0
|
244 |
}
|