0
|
1 |
/*
|
|
2 |
* Copyright (c) 2006 - 2007 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
1
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
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 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include <mda\common\audio.h>
|
|
20 |
#include <mmf\common\mmfutilities.h>
|
|
21 |
#include <MdaAudioInputStream.h> // audio input stream
|
|
22 |
#include <MdaAudioOutputStream.h> // audio output stream
|
|
23 |
#include <s32file.h> // RFileWriteStream and RFileReadStream
|
|
24 |
|
|
25 |
#include "AudioStreamEngine.h"
|
|
26 |
#include "audiostream.pan"
|
|
27 |
|
|
28 |
// Audio data buffer size.
|
|
29 |
// In both 3rd Edition and 2nd Edition the total buffer (iStreamBuffer) size is
|
|
30 |
// KFrameSizePCM * KFrameCountPCM = 40960 bytes. This will contain 2560 ms
|
|
31 |
// of 16-bit audio data.
|
|
32 |
// In 3rd Edition the KFrameSizePCM is 4096 bytes, because CMdaAudioInputStream::ReadL()
|
|
33 |
// returns audio data in 4096-byte chunks. In 2nd Edition, ReadL() returns data in 320-byte
|
|
34 |
// chunks.
|
|
35 |
|
1
|
36 |
|
0
|
37 |
const TInt KFrameSizePCM = 4096;
|
|
38 |
const TInt KFrameCountPCM = 10;
|
|
39 |
|
|
40 |
// Audio data buffer size for AMR encoding. For AMR, the buffer size is the same in
|
|
41 |
// both 2nd and 3rd Edition devices (20 ms per frame, a total of 2560 ms in 128 frames).
|
|
42 |
const TInt KFrameSizeAMR = 14;
|
|
43 |
const TInt KFrameCountAMR = 128;
|
|
44 |
|
|
45 |
// Header data for an AMR-encoded audio file
|
|
46 |
const TInt KAMRHeaderLength=6;
|
|
47 |
const TUint8 KAMRNBHeader[KAMRHeaderLength] = { 0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a };
|
|
48 |
|
|
49 |
// Files to store the sample audio clips
|
|
50 |
_LIT(KAudioFilePCM, "sample.aud");
|
|
51 |
_LIT(KAudioFileAMR, "sample.amr");
|
|
52 |
|
|
53 |
#ifdef __WINS__
|
|
54 |
// The path to the sample files in 2nd Ed emulator
|
|
55 |
_LIT(KEmulatorPath, "c:\\system\\apps\\audiostream\\");
|
|
56 |
#endif
|
|
57 |
|
|
58 |
CAudioStreamEngine* CAudioStreamEngine::NewL(CAudioStreamAppUi* aAppUi)
|
|
59 |
{
|
|
60 |
CAudioStreamEngine* self = CAudioStreamEngine::NewLC(aAppUi);
|
|
61 |
CleanupStack::Pop(self);
|
|
62 |
return self;
|
|
63 |
}
|
|
64 |
|
|
65 |
CAudioStreamEngine* CAudioStreamEngine::NewLC(CAudioStreamAppUi* aAppUi)
|
|
66 |
{
|
|
67 |
CAudioStreamEngine* self = new (ELeave) CAudioStreamEngine(aAppUi);
|
|
68 |
CleanupStack::PushL(self);
|
|
69 |
self->ConstructL();
|
|
70 |
return self;
|
|
71 |
}
|
|
72 |
|
|
73 |
// Standard EPOC 2nd phase constructor
|
|
74 |
void CAudioStreamEngine::ConstructL()
|
|
75 |
{
|
|
76 |
// Construct streams. We need to construct these here, so that at least the input stream
|
|
77 |
// exists if SetEncodingL() is called before any recording has taken place
|
|
78 |
iInputStream = CMdaAudioInputStream::NewL(*this);
|
|
79 |
iOutputStream = CMdaAudioOutputStream::NewL(*this);
|
|
80 |
|
|
81 |
// Get a handle to the RFs session to be used (owned by CEikonEnv, NOT to be closed
|
|
82 |
// when this application exits!)
|
|
83 |
iFs = CEikonEnv::Static()->FsSession();
|
|
84 |
|
|
85 |
// Save the default encoding for later reference (the encoding is the same for
|
|
86 |
// both input and output streams).
|
|
87 |
iDefaultEncoding = iInputStream->DataType();
|
|
88 |
// At first we are using the default encoding.
|
|
89 |
iCurrentEncoding = iDefaultEncoding;
|
|
90 |
|
|
91 |
// Stream buffer allocation (by default for PCM)
|
|
92 |
iStreamBuffer = HBufC8::NewMaxL(iFrameSize * iFrameCount);
|
|
93 |
iStreamStart=0;
|
|
94 |
iStreamEnd=iFrameCount - 1;
|
|
95 |
|
1
|
96 |
|
|
97 |
|
|
98 |
User::LeaveIfError( iFs.CreatePrivatePath( EDriveC ) );
|
0
|
99 |
User::LeaveIfError( iFs.SetSessionToPrivate( EDriveC ) );
|
|
100 |
}
|
|
101 |
|
|
102 |
// ----------------------------------------------------------------------------
|
|
103 |
// CAudioStreamEngine::CAudioStreamEngine(
|
|
104 |
// CAudioStreamAppUi* aAppUi)
|
|
105 |
//
|
|
106 |
// onstructor
|
|
107 |
// ----------------------------------------------------------------------------
|
|
108 |
CAudioStreamEngine::CAudioStreamEngine(CAudioStreamAppUi* aAppUi)
|
|
109 |
: iAppUi(aAppUi), iUseAMR(EFalse), iAudioFile(KAudioFilePCM), iFrameSize(KFrameSizePCM),
|
|
110 |
iFrameCount(KFrameCountPCM), iStreamBuffer(0), iFramePtr(0,0), iBufferOK(EFalse)
|
|
111 |
{
|
|
112 |
// By default we use PCM and initialise the instance variables accordingly above.
|
|
113 |
|
|
114 |
// Initial audio stream properties for input and output, 8KHz mono.
|
|
115 |
// These settings could also be set/changed using method SetAudioPropertiesL() of
|
|
116 |
// the input and output streams.
|
|
117 |
iStreamSettings.iChannels=TMdaAudioDataSettings::EChannelsMono;
|
|
118 |
iStreamSettings.iSampleRate=TMdaAudioDataSettings::ESampleRate8000Hz;
|
|
119 |
}
|
|
120 |
|
|
121 |
// ----------------------------------------------------------------------------
|
|
122 |
// CAudioStreamEngine::~CAudioStreamEngine()
|
|
123 |
//
|
|
124 |
// destructor
|
|
125 |
// ----------------------------------------------------------------------------
|
|
126 |
CAudioStreamEngine::~CAudioStreamEngine()
|
|
127 |
{
|
|
128 |
// close and delete streams
|
|
129 |
if (iInputStream)
|
|
130 |
{
|
|
131 |
if (iInputStatus!=ENotReady) iInputStream->Stop();
|
|
132 |
delete iInputStream;
|
|
133 |
iInputStream=NULL;
|
|
134 |
}
|
|
135 |
if (iOutputStream)
|
|
136 |
{
|
|
137 |
if (iOutputStatus!=ENotReady) iOutputStream->Stop();
|
|
138 |
delete iOutputStream;
|
|
139 |
iOutputStream=NULL;
|
|
140 |
}
|
|
141 |
if (iStreamBuffer)
|
|
142 |
{
|
|
143 |
delete iStreamBuffer;
|
|
144 |
iStreamBuffer = NULL;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
|
|
149 |
// ----------------------------------------------------------------------------
|
|
150 |
// CAudioStreamEngine::Play()
|
|
151 |
//
|
|
152 |
// plays the audio data contained in the buffer
|
|
153 |
// ----------------------------------------------------------------------------
|
|
154 |
void CAudioStreamEngine::Play()
|
|
155 |
{
|
|
156 |
ShowMessage(_L("Play "), ETrue);
|
|
157 |
// if either stream is active, return
|
|
158 |
if (iInputStatus!=ENotReady || iOutputStatus!=ENotReady)
|
|
159 |
{
|
|
160 |
ShowMessage(_L("Stream in use, \ncannot play audio."), ETrue);
|
|
161 |
return;
|
|
162 |
}
|
|
163 |
|
|
164 |
if(!iBufferOK)
|
|
165 |
{
|
|
166 |
ShowMessage(_L("Nothing to play - \nrecord or load \na file first."), ETrue);
|
|
167 |
return;
|
|
168 |
}
|
|
169 |
|
|
170 |
// Open output stream.
|
|
171 |
// Upon completion will receive callback in
|
|
172 |
// MMdaAudioOutputStreamCallback::MaoscOpenComplete().
|
1
|
173 |
|
0
|
174 |
iOutputStream->Open(&iStreamSettings);
|
|
175 |
}
|
|
176 |
|
|
177 |
|
|
178 |
// ----------------------------------------------------------------------------
|
|
179 |
// CAudioStreamEngine::Record()
|
|
180 |
//
|
|
181 |
// records audio data into the buffer
|
|
182 |
// ----------------------------------------------------------------------------
|
|
183 |
void CAudioStreamEngine::Record()
|
|
184 |
{
|
|
185 |
// If either stream is active, return
|
|
186 |
if (iInputStatus!=ENotReady || iOutputStatus!=ENotReady)
|
|
187 |
{
|
|
188 |
ShowMessage(_L("Stream in use, \ncannot record audio."), ETrue);
|
|
189 |
return;
|
|
190 |
}
|
|
191 |
|
|
192 |
// Open input stream.
|
|
193 |
// Upon completion will receive callback in
|
|
194 |
// MMdaAudioInputStreamCallback::MaiscOpenComplete().
|
1
|
195 |
|
0
|
196 |
iInputStream->Open(&iStreamSettings);
|
|
197 |
}
|
|
198 |
|
|
199 |
// ----------------------------------------------------------------------------
|
|
200 |
// CAudioStreamEngine::Stop()
|
|
201 |
//
|
|
202 |
// stops playing/recording
|
|
203 |
// ----------------------------------------------------------------------------
|
|
204 |
void CAudioStreamEngine::Stop()
|
|
205 |
{
|
|
206 |
// if input or output streams are active, close them
|
|
207 |
if (iInputStatus!=ENotReady)
|
|
208 |
{
|
|
209 |
iInputStream->Stop();
|
|
210 |
ShowMessage(_L("\nRecording stopped!"), EFalse);
|
|
211 |
iBufferOK = ETrue;
|
|
212 |
}
|
|
213 |
if (iOutputStatus!=ENotReady)
|
|
214 |
{
|
|
215 |
iOutputStream->Stop();
|
|
216 |
ShowMessage(_L("\nPlayback stopped!"), ETrue);
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
// ----------------------------------------------------------------------------
|
|
222 |
// CAudioStreamEngine::LoadAudioFileL()
|
|
223 |
//
|
|
224 |
// loads the audio data from a file into the buffer
|
|
225 |
// ----------------------------------------------------------------------------
|
|
226 |
void CAudioStreamEngine::LoadAudioFileL()
|
|
227 |
{
|
|
228 |
RFileReadStream audiofile;
|
|
229 |
|
|
230 |
// open file
|
|
231 |
TFileName fileName;
|
|
232 |
fileName.Copy(iAudioFilePath);
|
|
233 |
fileName.Append(iAudioFile);
|
|
234 |
|
|
235 |
TInt err = audiofile.Open(iFs, fileName, EFileRead|EFileStream);
|
|
236 |
iStreamBuffer->Des().FillZ(iFrameCount * iFrameSize); // Empty the stream buffer
|
|
237 |
if (err==KErrNone)
|
|
238 |
{
|
|
239 |
// file opened ok, proceed reading
|
|
240 |
if (iUseAMR)
|
|
241 |
{
|
|
242 |
// Read the AMR header (the first 6 bytes). We don't need to save/use the header,
|
|
243 |
// since while playback we already know it's an AMR-NB encoded stream.
|
|
244 |
TBuf8<KAMRHeaderLength> temp;
|
|
245 |
audiofile.ReadL(temp, KAMRHeaderLength);
|
|
246 |
}
|
|
247 |
|
|
248 |
TUint idx=0;
|
|
249 |
while (idx < iFrameCount)
|
|
250 |
{
|
|
251 |
TRAPD(fstatus, audiofile.ReadL(GetFrame(idx), iFrameSize));
|
|
252 |
if (fstatus!=KErrNone)
|
|
253 |
break;
|
|
254 |
idx++;
|
|
255 |
}
|
|
256 |
iStreamStart=0;
|
|
257 |
iStreamEnd=idx-1;
|
|
258 |
ShowMessage(_L("Loading complete!"), ETrue);
|
|
259 |
iBufferOK = ETrue;
|
|
260 |
}
|
|
261 |
else
|
|
262 |
{
|
|
263 |
// failed to open file
|
|
264 |
ShowMessage(_L("Error loading \naudio sample!"), ETrue);
|
|
265 |
iBufferOK = EFalse;
|
|
266 |
}
|
|
267 |
audiofile.Close();
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
// ----------------------------------------------------------------------------
|
|
272 |
// CAudioStreamEngine::SaveAudioFileL()
|
|
273 |
//
|
|
274 |
// saves the audio data in the buffer into a file
|
|
275 |
// ----------------------------------------------------------------------------
|
|
276 |
void CAudioStreamEngine::SaveAudioFileL()
|
|
277 |
{
|
|
278 |
if (!iBufferOK)
|
|
279 |
{
|
|
280 |
// In case the encoding was changed between recording and trying to save the file
|
|
281 |
ShowMessage(_L("Recorded buffer does not \nmatch current encoding."), ETrue);
|
|
282 |
ShowMessage(_L("\nPlease re-record and \ntry again."), EFalse);
|
|
283 |
return;
|
|
284 |
}
|
|
285 |
RFileWriteStream audiofile;
|
|
286 |
|
|
287 |
// Check for free space for saving the sample
|
|
288 |
TVolumeInfo volinfo;
|
|
289 |
TInt err=iFs.Volume(volinfo,EDriveC);
|
|
290 |
if ( volinfo.iFree<(iFrameCount*iFrameSize))
|
|
291 |
{
|
|
292 |
// Not enough free space on drive for saving, report and exit
|
|
293 |
ShowMessage(_L("Cannot save file:\nnot enough space!"), ETrue);
|
|
294 |
return;
|
|
295 |
}
|
|
296 |
|
|
297 |
TFileName fileName;
|
|
298 |
fileName.Copy(iAudioFilePath);
|
|
299 |
fileName.Append(iAudioFile);
|
|
300 |
err = audiofile.Replace(iFs, fileName, EFileWrite|EFileStream);
|
|
301 |
if (err==KErrNone)
|
|
302 |
{
|
|
303 |
if (iUseAMR)
|
|
304 |
{
|
|
305 |
// Write the six-byte AMR header, so that the file can be used by other
|
|
306 |
// applications as well.
|
|
307 |
for (int i = 0; i < KAMRHeaderLength; i++)
|
|
308 |
audiofile.WriteUint8L(KAMRNBHeader[i]);
|
|
309 |
}
|
|
310 |
|
|
311 |
// File opened ok, proceed writing.
|
|
312 |
// Write audio data directly from iStreamBuffer
|
|
313 |
for (TUint idx=iStreamStart; idx<=iStreamEnd; idx++)//iFrameCount; idx++)
|
|
314 |
{
|
|
315 |
audiofile.WriteL(GetFrame(idx));
|
|
316 |
}
|
|
317 |
ShowMessage(_L("Saving complete!"), ETrue);
|
|
318 |
}
|
|
319 |
else
|
|
320 |
{
|
|
321 |
// failed to open file
|
|
322 |
ShowMessage(_L("Error saving \naudio sample!"), ETrue);
|
|
323 |
}
|
|
324 |
audiofile.Close();
|
|
325 |
}
|
|
326 |
|
|
327 |
// ----------------------------------------------------------------------------
|
|
328 |
// CAudioStreamEngine::SetEncodingL(TBool aAmr)
|
|
329 |
//
|
|
330 |
// If argument is ETrue, AMR-NB encoding will be used in audio input/output.
|
|
331 |
// If EFalse, the default PCM is used. If the platform does not support AMR-NB,
|
|
332 |
// PCM will be used no matter what the argument's value is.
|
|
333 |
// ----------------------------------------------------------------------------
|
|
334 |
void CAudioStreamEngine::SetEncodingL(TBool aAmr)
|
|
335 |
{
|
|
336 |
// Act only if the new encoding differs from the current one
|
|
337 |
if (iUseAMR != aAmr)
|
|
338 |
{
|
|
339 |
iUseAMR = aAmr;
|
|
340 |
if (iUseAMR)
|
|
341 |
{
|
|
342 |
// Try to set AMR-NB encoding, this will indicate whether it is supported
|
|
343 |
// by the platform or not.
|
|
344 |
TRAPD(err, iInputStream->SetDataTypeL(KMMFFourCCCodeAMR));
|
|
345 |
if (err != KErrNone)
|
|
346 |
{
|
|
347 |
ShowMessage(_L("AMR-NB not supported,\nusing PCM."), ETrue);
|
|
348 |
iCurrentEncoding = iDefaultEncoding;
|
|
349 |
iUseAMR = EFalse;
|
|
350 |
// We do not need to invalidate the buffer or change buffer settings,
|
|
351 |
// since the encoding was not changed -> just return.
|
|
352 |
return;
|
|
353 |
}
|
|
354 |
else
|
|
355 |
{
|
|
356 |
iCurrentEncoding = KMMFFourCCCodeAMR;
|
|
357 |
iAudioFile.Zero(); // Empty the audio file name
|
|
358 |
iAudioFile.Append(KAudioFileAMR);
|
|
359 |
iFrameCount = KFrameCountAMR;
|
|
360 |
iFrameSize = KFrameSizeAMR;
|
|
361 |
ShowMessage(_L("Encoding set to AMR-NB."), ETrue);
|
|
362 |
}
|
|
363 |
}
|
|
364 |
else
|
|
365 |
{
|
|
366 |
// If we get here, the encoding has previously been changed to AMR. Switch back to
|
|
367 |
// PCM.
|
|
368 |
iCurrentEncoding = iDefaultEncoding;
|
|
369 |
iAudioFile.Zero(); // Empty the audio file name
|
|
370 |
iAudioFile.Append(KAudioFilePCM);
|
|
371 |
iFrameCount = KFrameCountPCM;
|
|
372 |
iFrameSize = KFrameSizePCM;
|
|
373 |
ShowMessage(_L("Encoding set to PCM."), ETrue);
|
|
374 |
}
|
|
375 |
|
|
376 |
// Make sure the user re-records or reloads the audio file, so that we do not
|
|
377 |
// accidentally try to play PCM data using AMR or vice versa.
|
|
378 |
iBufferOK = EFalse;
|
|
379 |
if (iStreamBuffer) delete iStreamBuffer;
|
|
380 |
iStreamBuffer = NULL; // In case the following NewL leaves
|
|
381 |
iStreamBuffer = HBufC8::NewMaxL(iFrameSize * iFrameCount);
|
|
382 |
iStreamStart=0;
|
|
383 |
iStreamEnd=iFrameCount - 1;
|
|
384 |
}
|
|
385 |
}
|
|
386 |
|
|
387 |
// ----------------------------------------------------------------------------
|
|
388 |
// CAudioStreamEngine::ShowMessage(
|
|
389 |
// const TDesC& aMsg, TBool aReset=EFalse)
|
|
390 |
//
|
|
391 |
// displays text referenced by aMsg in the label, will append the aMsg in the
|
|
392 |
// existing text in label if aReset is EFalse, otherwise will reset the label
|
|
393 |
// text.
|
|
394 |
// ----------------------------------------------------------------------------
|
|
395 |
void CAudioStreamEngine::ShowMessage(const TDesC& aMsg, TBool aReset=EFalse)
|
|
396 |
{
|
|
397 |
if (aReset) // if ETrue, clear the message on the label prior to output
|
|
398 |
iMsg.Zero();
|
|
399 |
iMsg.Append(aMsg);
|
|
400 |
TRAPD(error, iAppUi->GetView()->ShowMessageL(iMsg));
|
|
401 |
PanicIfError(error);
|
|
402 |
}
|
|
403 |
|
|
404 |
// ----------------------------------------------------------------------------
|
|
405 |
// TPtr8& CAudioStreamEngine::GetFrame(TUint aFrameIdx)
|
|
406 |
//
|
|
407 |
// Returns a modifiable pointer to a single frame inside the audio buffer
|
|
408 |
// ----------------------------------------------------------------------------
|
|
409 |
TPtr8& CAudioStreamEngine::GetFrame(TUint aFrameIdx)
|
|
410 |
{
|
|
411 |
__ASSERT_ALWAYS(aFrameIdx < iFrameCount,
|
|
412 |
User::Panic(_L("AudioStreamEx"), 1));
|
|
413 |
|
|
414 |
iFramePtr.Set((TUint8*)(iStreamBuffer->Ptr() + (aFrameIdx * iFrameSize)),
|
|
415 |
iFrameSize,
|
|
416 |
iFrameSize);
|
|
417 |
return iFramePtr;
|
|
418 |
}
|
|
419 |
|
|
420 |
// ----------------------------------------------------------------------------
|
|
421 |
// TPtr8& CAudioStreamEngine::GetPlaybackFrames(TUint aLastFrame)
|
|
422 |
//
|
|
423 |
// Returns a modifiable pointer to the requested frames inside the audio buffer
|
|
424 |
// (from the first frame to aLastFrame).
|
|
425 |
// ----------------------------------------------------------------------------
|
|
426 |
TPtr8& CAudioStreamEngine::GetPlaybackFrames(TUint aLastFrame)
|
|
427 |
{
|
|
428 |
__ASSERT_ALWAYS(aLastFrame < iFrameCount,
|
|
429 |
User::Panic(_L("AudioStreamEx"), 2));
|
|
430 |
|
|
431 |
iFramePtr.Set((TUint8*)(iStreamBuffer->Ptr()),
|
|
432 |
(aLastFrame + 1) * iFrameSize,
|
|
433 |
(aLastFrame + 1) * iFrameSize);
|
|
434 |
return iFramePtr;
|
|
435 |
}
|
|
436 |
|
|
437 |
|
|
438 |
//
|
|
439 |
// MMdaAudioInputStream callbacks (MMdaAudioInputStreamCallback)
|
|
440 |
//
|
|
441 |
// ----------------------------------------------------------------------------
|
|
442 |
// CAudioStreamEngine::MaiscOpenComplete(
|
|
443 |
// TInt aError)
|
|
444 |
//
|
|
445 |
// called upon completion of CMdaAudioInputStream::Open(),
|
|
446 |
// if the stream was opened succesfully (aError==KErrNone), it's ready for use.
|
|
447 |
// upon succesful open, the first audio data block will be read from the input
|
|
448 |
// stream.
|
|
449 |
// ----------------------------------------------------------------------------
|
|
450 |
void CAudioStreamEngine::MaiscOpenComplete(TInt aError)
|
|
451 |
{
|
|
452 |
if (aError==KErrNone)
|
|
453 |
{
|
|
454 |
// Input stream opened succesfully, set status
|
|
455 |
iInputStatus = EOpen;
|
|
456 |
// Set the data type (encoding)
|
|
457 |
TRAPD(error, iInputStream->SetDataTypeL(iCurrentEncoding));
|
|
458 |
PanicIfError(error);
|
|
459 |
|
|
460 |
// set stream input gain to maximum
|
|
461 |
iInputStream->SetGain(iInputStream->MaxGain());
|
|
462 |
// set stream priority to normal and time sensitive
|
|
463 |
iInputStream->SetPriority(EPriorityNormal, EMdaPriorityPreferenceTime);
|
|
464 |
ShowMessage(_L("Recording..."), ETrue);
|
|
465 |
|
|
466 |
// Emtpy the buffer and issue ReadL() to read the first audio data block,
|
|
467 |
// subsequent calls to ReadL() will be issued
|
|
468 |
// in MMdaAudioInputStreamCallback::MaiscBufferCopied()
|
|
469 |
iStreamBuffer->Des().FillZ(iFrameCount * iFrameSize);
|
|
470 |
iStreamIdx=0;
|
|
471 |
TRAPD(error2, iInputStream->ReadL(GetFrame(iStreamIdx)));
|
|
472 |
PanicIfError(error2);
|
|
473 |
}
|
|
474 |
else
|
|
475 |
{
|
|
476 |
// input stream open failed
|
|
477 |
iInputStatus = ENotReady;
|
|
478 |
ShowMessage(_L("Recording failed!"), ETrue);
|
|
479 |
}
|
|
480 |
}
|
|
481 |
|
|
482 |
// ----------------------------------------------------------------------------
|
|
483 |
// CAudioStreamEngine::MaiscBufferCopied(
|
|
484 |
// TInt aError, const TDesC8& aBuffer)
|
|
485 |
//
|
|
486 |
// called when a block of audio data has been read and is available at the
|
|
487 |
// buffer reference *aBuffer. calls to ReadL() will be issued until all blocks
|
|
488 |
// in the audio data buffer (iStreamBuffer) are filled.
|
|
489 |
// ----------------------------------------------------------------------------
|
|
490 |
void CAudioStreamEngine::MaiscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
|
|
491 |
{
|
|
492 |
|
|
493 |
if (aError==KErrNone)
|
|
494 |
{
|
|
495 |
// stop recording if at the end of the buffer
|
|
496 |
iStreamIdx++;
|
|
497 |
if (iStreamIdx == iFrameCount)
|
|
498 |
{
|
|
499 |
ShowMessage(_L("\nRecording complete!"), EFalse);
|
|
500 |
iStreamEnd = iStreamIdx - 1;
|
|
501 |
iBufferOK = ETrue;
|
|
502 |
iInputStatus = ENotReady;
|
|
503 |
// NOTE: In 2nd Edition we MUST NOT call iInputStream->Stop() here, because
|
|
504 |
// this will cause a crash on 2nd Edition, FP1 devices.
|
|
505 |
// Since iInputStream->Stop() is not called, the callback method
|
|
506 |
// MaiscRecordComplete() will not be called either after exiting this method.
|
|
507 |
// In 3rd Edition, however, iInputStream->Stop() MUST be called in order to reach
|
|
508 |
// MaiscRecordComplete(), otherwise the stream will "hang".
|
1
|
509 |
|
0
|
510 |
return;
|
|
511 |
}
|
|
512 |
|
|
513 |
// issue ReadL() for next frame
|
|
514 |
TRAPD(error, iInputStream->ReadL(GetFrame(iStreamIdx)));
|
|
515 |
PanicIfError(error);
|
|
516 |
}
|
|
517 |
else if (aError==KErrAbort)
|
|
518 |
{
|
|
519 |
// Recording was aborted, due to call to CMdaAudioInputStream::Stop()
|
|
520 |
// This KErrAbort will occur each time the Stop() method in this class is executed.
|
|
521 |
// Also, MaiscRecordComplete() will be called after exiting this method.
|
|
522 |
iStreamEnd = iStreamIdx - 1;
|
|
523 |
iBufferOK = ETrue;
|
|
524 |
iInputStatus = ENotReady;
|
|
525 |
}
|
|
526 |
else
|
|
527 |
{
|
|
528 |
ShowMessage(_L("\nError reading data \nfrom input"), EFalse);
|
|
529 |
iInputStatus = ENotReady;
|
|
530 |
}
|
|
531 |
}
|
|
532 |
|
|
533 |
// ----------------------------------------------------------------------------
|
|
534 |
// CAudioStreamEngine::MaiscRecordComplete(
|
|
535 |
// TInt aError)
|
|
536 |
//
|
|
537 |
// called when input stream is closed by CMdaAudioInputStream::Stop()
|
|
538 |
// ----------------------------------------------------------------------------
|
|
539 |
void CAudioStreamEngine::MaiscRecordComplete(TInt aError)
|
|
540 |
{
|
|
541 |
iInputStatus = ENotReady;
|
|
542 |
if (aError==KErrNone)
|
|
543 |
{
|
|
544 |
// normal stream closure
|
|
545 |
}
|
|
546 |
else
|
|
547 |
{
|
|
548 |
// completed with error(s)
|
|
549 |
}
|
|
550 |
|
|
551 |
// see comments in MaiscBufferCopied() regarding stopping the stream.
|
|
552 |
iInputStream->Stop();
|
|
553 |
}
|
|
554 |
|
|
555 |
|
|
556 |
// MMdaAudioOutputStream callbacks (MMdaAudioOutputStreamCallback)
|
|
557 |
|
|
558 |
// ----------------------------------------------------------------------------
|
|
559 |
// CAudioStreamEngine::MaoscOpenComplete(
|
|
560 |
// TInt aError)
|
|
561 |
//
|
|
562 |
// called upon completion of CMdaAudioOutputStream::Open(),
|
|
563 |
// if the stream was opened succesfully (aError==KErrNone), it's ready for use.
|
|
564 |
// upon succesful open, the first audio data block will be written to the
|
|
565 |
// output stream.
|
|
566 |
// ----------------------------------------------------------------------------
|
|
567 |
void CAudioStreamEngine::MaoscOpenComplete(TInt aError)
|
|
568 |
{
|
|
569 |
if (aError==KErrNone)
|
|
570 |
{
|
|
571 |
// output stream opened succesfully, set status
|
|
572 |
iOutputStatus = EOpen;
|
|
573 |
// Set the data type (encoding). Should not fail, since we already
|
|
574 |
// have tested support for this encoding in SetEncodingL with the
|
|
575 |
// corresponding input stream!
|
|
576 |
TRAPD(error, iOutputStream->SetDataTypeL(iCurrentEncoding));
|
|
577 |
PanicIfError(error);
|
|
578 |
|
|
579 |
// set volume to 1/4th of stream max volume
|
|
580 |
iOutputStream->SetVolume(iOutputStream->MaxVolume()/4);
|
|
581 |
// set stream priority to normal and time sensitive
|
|
582 |
iOutputStream->SetPriority(EPriorityNormal,
|
|
583 |
EMdaPriorityPreferenceTime);
|
|
584 |
ShowMessage(_L("Playing "), ETrue);
|
|
585 |
|
|
586 |
if (iUseAMR)
|
|
587 |
{
|
|
588 |
// In case of AMR, the whole recorded/loaded buffer is played back at once, not frame by frame.
|
|
589 |
// The buffer might not be fully recorded, so we will only play back the part
|
|
590 |
// that is filled with data.
|
|
591 |
iStreamIdx = iStreamEnd;
|
|
592 |
TRAPD(error2, iOutputStream->WriteL(GetPlaybackFrames(iStreamEnd)));
|
|
593 |
PanicIfError(error2);
|
|
594 |
}
|
|
595 |
else
|
|
596 |
{
|
|
597 |
// PCM needs to be played back frame by frame, otherwise some older devices might
|
|
598 |
// run into buffer overflow situations.
|
|
599 |
iStreamIdx = 0;
|
|
600 |
TRAPD(error3, iOutputStream->WriteL(GetFrame(iStreamIdx)));
|
|
601 |
PanicIfError(error3);
|
|
602 |
}
|
|
603 |
}
|
|
604 |
else
|
|
605 |
{
|
|
606 |
// output stream open failed
|
|
607 |
iOutputStatus = ENotReady;
|
|
608 |
ShowMessage(_L("Playback failed!"), ETrue);
|
|
609 |
}
|
|
610 |
}
|
|
611 |
|
|
612 |
// ----------------------------------------------------------------------------
|
|
613 |
// CAudioStreamEngine::MaoscBufferCopied(
|
|
614 |
// TInt aError, const TDesC8& aBuffer)
|
|
615 |
//
|
|
616 |
// called when a block of audio data has been written to MMF. calls to WriteL()
|
|
617 |
// will be issued until all blocks in the audio data buffer (iStreamBuffer) are
|
|
618 |
// written.
|
|
619 |
// ----------------------------------------------------------------------------
|
|
620 |
void CAudioStreamEngine::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
|
|
621 |
{
|
|
622 |
if (aError==KErrNone)
|
|
623 |
{
|
|
624 |
if (iStreamIdx==iStreamEnd)
|
|
625 |
{
|
|
626 |
ShowMessage(_L("\nPlayback complete!"), EFalse);
|
|
627 |
iOutputStatus = ENotReady;
|
|
628 |
// NOTE: In 2nd Edition we MUST NOT call iOutputStream->Stop() here, because
|
|
629 |
// this will cause a crash on 2nd Edition, FP1 devices.
|
|
630 |
// Since iOutputStream->Stop() is not called, the callback method
|
|
631 |
// MaiscRecordComplete() will not be called either after exiting this method.
|
|
632 |
// In 3rd Edition, however, iOutputStream->Stop() MUST be called in order to reach
|
|
633 |
// MaiscRecordComplete(), otherwise the stream will "hang".
|
1
|
634 |
|
0
|
635 |
iOutputStream->Stop();
|
1
|
636 |
|
0
|
637 |
}
|
|
638 |
else
|
|
639 |
{
|
|
640 |
iStreamIdx++;
|
|
641 |
TRAPD(error, iOutputStream->WriteL(GetFrame(iStreamIdx)));
|
|
642 |
PanicIfError(error);
|
|
643 |
}
|
|
644 |
}
|
|
645 |
else if (aError==KErrAbort)
|
|
646 |
{
|
|
647 |
// Playing was aborted, due to call to CMdaAudioOutputStream::Stop().
|
|
648 |
// MaoscRecordComplete() will be called after exiting this method.
|
|
649 |
iOutputStatus = ENotReady;
|
|
650 |
}
|
|
651 |
else
|
|
652 |
{
|
|
653 |
ShowMessage(_L("\nError writing data \nto output"), EFalse);
|
|
654 |
iOutputStatus = ENotReady;
|
|
655 |
}
|
|
656 |
}
|
|
657 |
|
|
658 |
|
|
659 |
// ----------------------------------------------------------------------------
|
|
660 |
// CAudioStreamEngine::MaoscPlayComplete(
|
|
661 |
// TInt aError)
|
|
662 |
//
|
|
663 |
// called when output stream is closed by CMdaAudioOutputStream::Stop() or if
|
|
664 |
// end of audio data has been reached, in this case KErrUnderflow will be
|
|
665 |
// returned.
|
|
666 |
// ----------------------------------------------------------------------------
|
|
667 |
void CAudioStreamEngine::MaoscPlayComplete(TInt aError)
|
|
668 |
{
|
|
669 |
iOutputStatus = ENotReady;
|
|
670 |
if (aError==KErrNone)
|
|
671 |
{
|
|
672 |
// normal stream closure
|
|
673 |
}
|
|
674 |
else if (aError==KErrUnderflow)
|
|
675 |
{
|
|
676 |
// end of audio data stream was reached because of stream underflow,
|
|
677 |
}
|
|
678 |
else
|
|
679 |
{
|
|
680 |
// completed with error(s)
|
|
681 |
}
|
|
682 |
}
|
|
683 |
|
|
684 |
// END OF FILE
|
|
685 |
|