|
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 "ProcWAVInFileHandler.h" |
|
22 #include "ProcWAVFrameHandler.h" |
|
23 #include "AudPanic.h" |
|
24 #include "ProcTools.h" |
|
25 #include "audconstants.h" |
|
26 |
|
27 |
|
28 |
|
29 CProcWAVInFileHandler* CProcWAVInFileHandler::NewL(const TDesC& aFileName, |
|
30 RFile* aFileHandle, |
|
31 CAudClip* aClip, |
|
32 TInt aReadBufferSize, |
|
33 TInt aTargetSampleRate, |
|
34 TChannelMode aChannelMode) |
|
35 { |
|
36 |
|
37 CProcWAVInFileHandler* self = NewLC(aFileName, aFileHandle, aClip, |
|
38 aReadBufferSize, aTargetSampleRate, aChannelMode); |
|
39 CleanupStack::Pop(self); |
|
40 return self; |
|
41 } |
|
42 |
|
43 CProcWAVInFileHandler* CProcWAVInFileHandler::NewLC(const TDesC& aFileName, |
|
44 RFile* aFileHandle, |
|
45 CAudClip* aClip, |
|
46 TInt aReadBufferSize, |
|
47 TInt aTargetSampleRate, |
|
48 TChannelMode aChannelMode) |
|
49 { |
|
50 |
|
51 CProcWAVInFileHandler* self = new (ELeave) CProcWAVInFileHandler(); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(aFileName, aFileHandle, aClip, aReadBufferSize, aTargetSampleRate, aChannelMode); |
|
54 return self; |
|
55 } |
|
56 |
|
57 CProcWAVInFileHandler::CProcWAVInFileHandler() : CProcInFileHandler(), iNumberofSamplesInFrame(1) |
|
58 { |
|
59 |
|
60 |
|
61 } |
|
62 |
|
63 void CProcWAVInFileHandler::GetPropertiesL(TAudFileProperties* aProperties) |
|
64 { |
|
65 |
|
66 if (iProperties != 0) |
|
67 { |
|
68 *aProperties = *iProperties; |
|
69 return; |
|
70 } |
|
71 |
|
72 |
|
73 const TInt KFrameDurationMilli = 20; |
|
74 const TInt KFramesPerSecond = 50; |
|
75 |
|
76 if (iFileOpen) |
|
77 { |
|
78 aProperties->iFileFormat = EAudFormatUnrecognized; |
|
79 aProperties->iAudioType = EAudTypeUnrecognized; |
|
80 aProperties->iAudioTypeExtension = EAudExtensionTypeNoExtension; |
|
81 aProperties->iBitrate = 0; |
|
82 aProperties->iBitrateMode = EAudBitrateModeNotRecognized; |
|
83 aProperties->iChannelMode = EAudChannelModeNotRecognized; |
|
84 aProperties->iDuration = 0; |
|
85 aProperties->iSamplingRate = 0; |
|
86 aProperties->iFrameLen = 0; |
|
87 aProperties->iFrameCount = 0; |
|
88 |
|
89 TInt oldPos = iFilePos; |
|
90 |
|
91 // check the validity of WAVE header |
|
92 |
|
93 TBuf8<4> chunckID; |
|
94 TBuf8<4> format; |
|
95 TBuf8<4> fmt; |
|
96 TBuf8<4> data; |
|
97 |
|
98 TBuf8<1> audioFormat; |
|
99 |
|
100 BufferedFileRead(0, chunckID); |
|
101 BufferedFileRead(8, format); |
|
102 BufferedFileRead(12, fmt); |
|
103 BufferedFileRead(20, audioFormat); |
|
104 BufferedFileRead(36, data); |
|
105 |
|
106 |
|
107 if (chunckID.Compare(_L8("RIFF")) == 0 && |
|
108 format.Compare(_L8("WAVE")) == 0 && |
|
109 fmt.Compare(_L8("fmt ")) == 0 && |
|
110 data.Compare(_L8("data")) == 0 && |
|
111 audioFormat[0] == 0x01) |
|
112 { |
|
113 aProperties->iFileFormat = EAudFormatWAV; |
|
114 aProperties->iAudioType = EAudWAV; |
|
115 } |
|
116 else |
|
117 { |
|
118 aProperties->iFileFormat = EAudFormatUnrecognized; |
|
119 User::Leave(KErrNotSupported); |
|
120 return; |
|
121 |
|
122 } |
|
123 |
|
124 TBuf8<1> numChannels; |
|
125 BufferedFileRead(22, numChannels); |
|
126 if (numChannels[0] == 0x01) |
|
127 { |
|
128 aProperties->iChannelMode = EAudSingleChannel; |
|
129 } |
|
130 else if (numChannels[0] == 0x02) |
|
131 { |
|
132 aProperties->iChannelMode = EAudStereo; |
|
133 } |
|
134 else |
|
135 { |
|
136 aProperties->iFileFormat = EAudFormatUnrecognized; |
|
137 User::Leave(KErrNotSupported); |
|
138 return; |
|
139 } |
|
140 |
|
141 TBuf8<4> sr; |
|
142 BufferedFileRead(24, sr); |
|
143 |
|
144 TBuf8<8> tmpBin; |
|
145 TBuf8<8*4> srBin; |
|
146 |
|
147 // little endian: |
|
148 for (TInt a = 3 ; a >= 0; a--) |
|
149 { |
|
150 ProcTools::Dec2Bin(sr[a], tmpBin); |
|
151 srBin.Append(tmpBin); |
|
152 } |
|
153 |
|
154 TUint srDec = 0; |
|
155 ProcTools::Bin2Dec(srBin, srDec); |
|
156 |
|
157 aProperties->iSamplingRate = srDec; |
|
158 |
|
159 // Check that the sample rate is supported |
|
160 if( (aProperties->iSamplingRate != KAedSampleRate8kHz) && |
|
161 (aProperties->iSamplingRate != KAedSampleRate11kHz) && |
|
162 (aProperties->iSamplingRate != KAedSampleRate16kHz) && |
|
163 (aProperties->iSamplingRate != KAedSampleRate22kHz) && |
|
164 (aProperties->iSamplingRate != KAedSampleRate24kHz) && |
|
165 (aProperties->iSamplingRate != KAedSampleRate32kHz) && |
|
166 (aProperties->iSamplingRate != KAedSampleRate44kHz) && |
|
167 (aProperties->iSamplingRate != KAedSampleRate48kHz) ) |
|
168 { |
|
169 User::Leave(KErrNotSupported); |
|
170 } |
|
171 |
|
172 iNumberofSamplesInFrame = srDec/(KFramesPerSecond/numChannels[0]); |
|
173 if (iNumberofSamplesInFrame%2 != 0) iNumberofSamplesInFrame++; |
|
174 |
|
175 tmpBin.Delete(0, tmpBin.Length()); |
|
176 |
|
177 TBuf8<4> byteRate; |
|
178 TBuf8<8*4> byteRateBin; |
|
179 TUint byteRateDec = 0; |
|
180 |
|
181 BufferedFileRead(28, byteRate); |
|
182 for (TInt b = 3 ; b >= 0; b--) |
|
183 { |
|
184 ProcTools::Dec2Bin(byteRate[b], tmpBin); |
|
185 byteRateBin.Append(tmpBin); |
|
186 } |
|
187 ProcTools::Bin2Dec(byteRateBin, byteRateDec); |
|
188 |
|
189 aProperties->iBitrate = byteRateDec*8; |
|
190 |
|
191 TBuf8<1> numberOfBitsPerSample; |
|
192 BufferedFileRead(34, numberOfBitsPerSample); |
|
193 aProperties->iNumberOfBitsPerSample = numberOfBitsPerSample[0]; |
|
194 |
|
195 // other bit depths can be added later if (ever) needed |
|
196 if (aProperties->iNumberOfBitsPerSample != 16 && |
|
197 aProperties->iNumberOfBitsPerSample != 8) |
|
198 { |
|
199 aProperties->iFileFormat = EAudFormatUnrecognized; |
|
200 User::Leave(KErrNotSupported); |
|
201 return; |
|
202 } |
|
203 |
|
204 if (aProperties->iNumberOfBitsPerSample == 16) |
|
205 { |
|
206 iNumberofSamplesInFrame *= 2; |
|
207 } |
|
208 |
|
209 TBuf8<4> dataSize; |
|
210 BufferedFileRead(40, dataSize); |
|
211 |
|
212 TBuf8<8*4> dataSizeBin; |
|
213 TUint dataSizeDec = 0; |
|
214 tmpBin.Delete(0, tmpBin.Length()); |
|
215 |
|
216 for (TInt c = 3 ; c >= 0; c--) |
|
217 { |
|
218 ProcTools::Dec2Bin(dataSize[c], tmpBin); |
|
219 dataSizeBin.Append(tmpBin); |
|
220 } |
|
221 ProcTools::Bin2Dec(dataSizeBin, dataSizeDec); |
|
222 |
|
223 TTimeIntervalMicroSeconds durationMicro((TInt64)(TInt)((TReal(dataSizeDec)/byteRateDec)*1000000)); |
|
224 |
|
225 aProperties->iDuration = durationMicro; |
|
226 aProperties->iBitrateMode = EAudConstant; |
|
227 aProperties->iFrameLen = iNumberofSamplesInFrame; |
|
228 |
|
229 aProperties->iFrameDuration = KFrameDurationMilli*1000; |
|
230 |
|
231 aProperties->iFrameCount = (TInt)dataSizeDec/iNumberofSamplesInFrame; |
|
232 aProperties->iNumFramesPerSample = 1; |
|
233 |
|
234 |
|
235 |
|
236 BufferedFileSetFilePos(oldPos); |
|
237 } |
|
238 else |
|
239 { |
|
240 TAudPanic::Panic(TAudPanic::EInternal); |
|
241 } |
|
242 |
|
243 |
|
244 if (iProperties == 0) |
|
245 { |
|
246 iProperties = new (ELeave) TAudFileProperties; |
|
247 *iProperties = *aProperties; |
|
248 } |
|
249 |
|
250 |
|
251 } |
|
252 |
|
253 TBool CProcWAVInFileHandler::SeekAudioFrame(TInt32 aTime) |
|
254 { |
|
255 |
|
256 TInt frameLengthMilliSeconds = ProcTools::MilliSeconds(iProperties->iFrameDuration); |
|
257 |
|
258 TInt framesFromStart = aTime/frameLengthMilliSeconds; |
|
259 |
|
260 TInt bytesFromStart = framesFromStart*iNumberofSamplesInFrame; |
|
261 |
|
262 const TInt KWAVHeaderLength = 44; |
|
263 |
|
264 BufferedFileSetFilePos(bytesFromStart+KWAVHeaderLength); |
|
265 |
|
266 if (!iFileOpen) |
|
267 { |
|
268 TAudPanic::Panic(TAudPanic::EInternal); |
|
269 } |
|
270 |
|
271 iCurrentTimeMilliseconds = aTime; |
|
272 |
|
273 return ETrue; |
|
274 } |
|
275 |
|
276 TBool CProcWAVInFileHandler::SeekCutInFrame() |
|
277 { |
|
278 |
|
279 iCurrentTimeMilliseconds = iCutInTime; |
|
280 return SeekAudioFrame(iCutInTime); |
|
281 } |
|
282 |
|
283 |
|
284 |
|
285 void CProcWAVInFileHandler::ConstructL(const TDesC& aFileName, |
|
286 RFile* aFileHandle, |
|
287 CAudClip* aClip, |
|
288 TInt aReadBufferSize, |
|
289 TInt aTargetSampleRate, |
|
290 TChannelMode aChannelMode) |
|
291 { |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 iTargetSampleRate = aTargetSampleRate; |
|
298 iChannelMode = aChannelMode; |
|
299 |
|
300 InitAndOpenFileL(aFileName, aFileHandle, aReadBufferSize); |
|
301 |
|
302 |
|
303 if (aClip != 0) |
|
304 { |
|
305 iCutInTime = ProcTools::MilliSeconds(aClip->CutInTime()); |
|
306 |
|
307 } |
|
308 |
|
309 |
|
310 TAudFileProperties prop; |
|
311 GetPropertiesL(&prop); |
|
312 |
|
313 if (iProperties != 0) |
|
314 { |
|
315 TInt samplesIn20ms = ((iProperties->iSamplingRate) * |
|
316 (iProperties->iNumberOfBitsPerSample)/8)/50; |
|
317 |
|
318 if (iProperties->iChannelMode == EAudStereo) |
|
319 { |
|
320 samplesIn20ms *= 2; |
|
321 |
|
322 } |
|
323 if (samplesIn20ms % 2 != 0) samplesIn20ms--; |
|
324 |
|
325 iSilentFrame = HBufC8::NewL(samplesIn20ms); |
|
326 |
|
327 if (iProperties->iNumberOfBitsPerSample == 16) |
|
328 { |
|
329 iSilentFrame->Des().SetLength(samplesIn20ms); |
|
330 iSilentFrame->Des().Fill(0); |
|
331 |
|
332 } |
|
333 else if(iProperties->iNumberOfBitsPerSample == 8) |
|
334 { |
|
335 iSilentFrame->Des().SetLength(samplesIn20ms); |
|
336 iSilentFrame->Des().Fill(128); |
|
337 |
|
338 } |
|
339 iSilentFrameDuration = 20; |
|
340 } |
|
341 else |
|
342 { |
|
343 User::Leave(KErrNotSupported); |
|
344 } |
|
345 |
|
346 iClip = aClip; |
|
347 |
|
348 iFrameHandler = CProcWAVFrameHandler::NewL(iProperties->iNumberOfBitsPerSample); |
|
349 |
|
350 // Generate a decoder -----------------------> |
|
351 |
|
352 iDecoder = CProcDecoder::NewL(); |
|
353 |
|
354 iDecodingPossible = iDecoder->InitL(*iProperties, aTargetSampleRate, aChannelMode); |
|
355 |
|
356 |
|
357 // <----------------------- Generate a decoder |
|
358 |
|
359 if (iClip != 0 && iClip->Normalizing()) |
|
360 { |
|
361 SetNormalizingGainL(iFrameHandler); |
|
362 } |
|
363 |
|
364 } |
|
365 |
|
366 |
|
367 TBool CProcWAVInFileHandler::GetEncAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime) |
|
368 { |
|
369 |
|
370 if (!iFileOpen) |
|
371 { |
|
372 TAudPanic::Panic(TAudPanic::EInternal); |
|
373 } |
|
374 |
|
375 TInt numberOfBytesInFrame = iNumberofSamplesInFrame; |
|
376 TInt bufferSize = numberOfBytesInFrame; |
|
377 if (iProperties->iNumberOfBitsPerSample == 8) |
|
378 { |
|
379 // need to expand to 16 bits per sample since encoders and MMF sample rate converter assumes 16-bit signed input |
|
380 bufferSize *= 2; |
|
381 } |
|
382 |
|
383 |
|
384 aFrame = HBufC8::NewL(bufferSize); |
|
385 |
|
386 TPtr8 tmpDes((TPtr8)aFrame->Des()); |
|
387 |
|
388 BufferedFileRead((TPtr8&)tmpDes , numberOfBytesInFrame); |
|
389 |
|
390 if (aFrame->Des().Length() < numberOfBytesInFrame) |
|
391 { |
|
392 delete aFrame; |
|
393 aFrame = 0; |
|
394 return EFalse; |
|
395 } |
|
396 aTime = ProcTools::MilliSeconds(iProperties->iFrameDuration); |
|
397 iCurrentTimeMilliseconds += aTime; |
|
398 |
|
399 TRAPD(err, ManipulateGainL(aFrame)); |
|
400 |
|
401 if (err != KErrNone) |
|
402 { |
|
403 // something went wrong with the gain manipulation |
|
404 // continue by returning the original frame |
|
405 } |
|
406 |
|
407 if (iProperties->iNumberOfBitsPerSample == 8) |
|
408 { |
|
409 // need to expand to 16 bits per sample since encoders and MMF sample rate converter assumes 16-bit signed input |
|
410 TUint8* buffer = const_cast<TUint8*>(aFrame->Ptr()); |
|
411 // start from the end to avoid overlapping |
|
412 for ( TInt i = numberOfBytesInFrame-1; i >= 0 ; i-- ) |
|
413 { |
|
414 buffer[i*2+1] = TInt8(buffer[i]) - 128; // 8-bit Wav's are unsigned, 16-bit Wav's are signed |
|
415 buffer[i*2] = 0; |
|
416 } |
|
417 tmpDes.SetLength(bufferSize); |
|
418 |
|
419 } |
|
420 aSize = aFrame->Length(); |
|
421 |
|
422 |
|
423 return ETrue; |
|
424 |
|
425 } |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 CProcWAVInFileHandler::~CProcWAVInFileHandler() |
|
431 { |
|
432 |
|
433 ResetAndCloseFile(); |
|
434 |
|
435 if (iSilentFrame != 0) delete iSilentFrame; |
|
436 |
|
437 delete iFrameHandler; |
|
438 |
|
439 delete iDecoder; |
|
440 |
|
441 } |
|
442 |
|
443 |
|
444 TBool CProcWAVInFileHandler::SetNormalizingGainL(const CProcFrameHandler* aFrameHandler) |
|
445 { |
|
446 |
|
447 HBufC8* point = 0; |
|
448 TInt siz; |
|
449 TInt32 tim = 0; |
|
450 TInt8 margin = 0; |
|
451 TInt minMargin = KMaxTInt; |
|
452 |
|
453 while(GetEncAudioFrameL(point, siz, tim)) |
|
454 |
|
455 { |
|
456 aFrameHandler->GetNormalizingMargin(point, margin); |
|
457 |
|
458 delete point; |
|
459 point = 0; |
|
460 |
|
461 if (minMargin > margin) |
|
462 { |
|
463 minMargin = margin; |
|
464 } |
|
465 |
|
466 } |
|
467 |
|
468 iNormalizingMargin = static_cast<TInt8>(minMargin); |
|
469 |
|
470 return ETrue; |
|
471 |
|
472 } |
|
473 |