Nokia N97 SDK
Example Applications Guide

AudioStreamEngine.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : AudioStreamEngine.cpp
00004 *  Part of     : AudioStream
00005 *  Created     : April 28, 2006 by Forum Nokia
00006 *  Description : 
00007 *  Version     : 2.0
00008 *
00009 *  Copyright (c) 2006 - 2007 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 #include <mda\common\audio.h>
00017 #include <mmf\common\mmfutilities.h>
00018 #include <MdaAudioInputStream.h>        // audio input stream
00019 #include <MdaAudioOutputStream.h>       // audio output stream
00020 #include <s32file.h>                            // RFileWriteStream and RFileReadStream
00021 
00022 #include "AudioStreamEngine.h"
00023 #include "audiostream.pan"
00024 
00025 // Audio data buffer size.
00026 // In both 3rd Edition and 2nd Edition the total buffer (iStreamBuffer) size is 
00027 // KFrameSizePCM * KFrameCountPCM = 40960 bytes. This will contain 2560 ms 
00028 // of 16-bit audio data. 
00029 // In 3rd Edition the KFrameSizePCM is 4096 bytes, because CMdaAudioInputStream::ReadL() 
00030 // returns audio data in 4096-byte chunks. In 2nd Edition, ReadL() returns data in 320-byte
00031 // chunks.
00032 
00033 #ifdef __SERIES60_3X__  // 3rd Edition
00034 const TInt KFrameSizePCM = 4096;
00035 const TInt KFrameCountPCM = 10;
00036 #else // 2nd Edition
00037 const TInt KFrameSizePCM = 320;
00038 const TInt KFrameCountPCM = 128;
00039 #endif
00040 
00041 // Audio data buffer size for AMR encoding. For AMR, the buffer size is the same in
00042 // both 2nd and 3rd Edition devices (20 ms per frame, a total of 2560 ms in 128 frames).
00043 const TInt KFrameSizeAMR = 14;
00044 const TInt KFrameCountAMR = 128;
00045 
00046 // Header data for an AMR-encoded audio file
00047 const TInt KAMRHeaderLength=6;
00048 const TUint8 KAMRNBHeader[KAMRHeaderLength] = { 0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a };
00049 
00050 // Files to store the sample audio clips
00051 _LIT(KAudioFilePCM, "sample.aud");
00052 _LIT(KAudioFileAMR, "sample.amr");
00053 
00054 #ifdef __WINS__
00055 // The path to the sample files in 2nd Ed emulator
00056 _LIT(KEmulatorPath, "c:\\system\\apps\\audiostream\\");
00057 #endif
00058 
00059 CAudioStreamEngine* CAudioStreamEngine::NewL(CAudioStreamAppUi* aAppUi)
00060         {
00061         CAudioStreamEngine* self = CAudioStreamEngine::NewLC(aAppUi);
00062     CleanupStack::Pop(self);
00063         return self;
00064         }
00065 
00066 CAudioStreamEngine* CAudioStreamEngine::NewLC(CAudioStreamAppUi* aAppUi)
00067         {
00068         CAudioStreamEngine* self = new (ELeave) CAudioStreamEngine(aAppUi);
00069         CleanupStack::PushL(self);
00070         self->ConstructL();
00071         return self;
00072         }
00073 
00074 // Standard EPOC 2nd phase constructor
00075 void CAudioStreamEngine::ConstructL()
00076         {
00077         // Construct streams. We need to construct these here, so that at least the input stream
00078         // exists if SetEncodingL() is called before any recording has taken place 
00079         iInputStream = CMdaAudioInputStream::NewL(*this);
00080         iOutputStream = CMdaAudioOutputStream::NewL(*this);
00081 
00082         // Get a handle to the RFs session to be used (owned by CEikonEnv, NOT to be closed
00083         // when this application exits!)                
00084         iFs = CEikonEnv::Static()->FsSession();
00085 
00086         // Save the default encoding for later reference (the encoding is the same for
00087         // both input and output streams).
00088         iDefaultEncoding = iInputStream->DataType();
00089         // At first we are using the default encoding.
00090         iCurrentEncoding = iDefaultEncoding;
00091 
00092         // Stream buffer allocation (by default for PCM)
00093         iStreamBuffer = HBufC8::NewMaxL(iFrameSize * iFrameCount);
00094         iStreamStart=0;
00095         iStreamEnd=iFrameCount - 1;
00096         
00097         #ifdef __SERIES60_3X__    
00098         // Only in 3rd Edition. The sample.aud/amr can be found in \private<UID3>\ folder.
00099         User::LeaveIfError( iFs.CreatePrivatePath( EDriveC ) );
00100                 User::LeaveIfError( iFs.SetSessionToPrivate( EDriveC ) );
00101         #else
00102                 #ifndef __WINS__  // don't save settings to z-drive in emulator
00103                         // In 2nd Ed device the sample.aud/amr will be in \system\apps\audiostream\ folder.
00104                 TFileName appFullName = iAppUi->Application()->AppFullName();
00105                 TParsePtr appPath(appFullName);
00106                 iAudioFilePath = appPath.DriveAndPath();
00107                 #else 
00108                         // For 2nd Ed emulator
00109                         iAudioFilePath.Append(KEmulatorPath);
00110                 #endif //__WINS__
00111                 
00112         #endif
00113         }
00114 
00115 // ----------------------------------------------------------------------------
00116 // CAudioStreamEngine::CAudioStreamEngine(
00117 //     CAudioStreamAppUi* aAppUi)
00118 //
00119 // onstructor
00120 // ----------------------------------------------------------------------------
00121 CAudioStreamEngine::CAudioStreamEngine(CAudioStreamAppUi* aAppUi)
00122 : iAppUi(aAppUi), iUseAMR(EFalse), iAudioFile(KAudioFilePCM), iFrameSize(KFrameSizePCM), 
00123 iFrameCount(KFrameCountPCM), iStreamBuffer(0), iFramePtr(0,0), iBufferOK(EFalse)
00124         {
00125         // By default we use PCM and initialise the instance variables accordingly above.
00126                 
00127         // Initial audio stream properties for input and output, 8KHz mono. 
00128         // These settings could also be set/changed using method SetAudioPropertiesL() of
00129         // the input and output streams.
00130         iStreamSettings.iChannels=TMdaAudioDataSettings::EChannelsMono;
00131         iStreamSettings.iSampleRate=TMdaAudioDataSettings::ESampleRate8000Hz;
00132         }
00133 
00134 // ----------------------------------------------------------------------------
00135 // CAudioStreamEngine::~CAudioStreamEngine()
00136 //
00137 // destructor
00138 // ----------------------------------------------------------------------------
00139 CAudioStreamEngine::~CAudioStreamEngine()
00140         { 
00141         // close and delete streams
00142         if (iInputStream)
00143                 {
00144                 if (iInputStatus!=ENotReady) iInputStream->Stop();
00145             delete iInputStream;
00146         iInputStream=NULL;
00147                 }
00148         if (iOutputStream)
00149                 {
00150                 if (iOutputStatus!=ENotReady) iOutputStream->Stop();
00151             delete iOutputStream;
00152         iOutputStream=NULL;
00153                 }
00154         if (iStreamBuffer)
00155                 {
00156                 delete iStreamBuffer;
00157                 iStreamBuffer = NULL;
00158                 }
00159         }
00160 
00161 
00162 // ----------------------------------------------------------------------------
00163 // CAudioStreamEngine::Play()
00164 //
00165 // plays the audio data contained in the buffer
00166 // ----------------------------------------------------------------------------
00167 void CAudioStreamEngine::Play()
00168         {
00169         ShowMessage(_L("Play "), ETrue);
00170         // if either stream is active, return
00171         if (iInputStatus!=ENotReady || iOutputStatus!=ENotReady) 
00172                 {
00173             ShowMessage(_L("Stream in use, \ncannot play audio."), ETrue);
00174                 return;
00175                 }
00176 
00177         if(!iBufferOK)
00178             {
00179             ShowMessage(_L("Nothing to play - \nrecord or load \na file first."), ETrue);
00180             return;
00181             }
00182                 
00183         // Open output stream.
00184         // Upon completion will receive callback in 
00185         // MMdaAudioOutputStreamCallback::MaoscOpenComplete().
00186         #ifndef __SERIES60_3X__  // Not 3rd Ed
00187                 // Some 2nd Edition, FP2 devices (such as Nokia 6630) require the stream to be
00188                 // reconstructed each time before calling Open() - otherwise the callback
00189                 // never gets called.
00190                 if (iOutputStream) delete iOutputStream;
00191                 iOutputStream = NULL; // In case the following NewL leaves
00192                 TRAPD(err, iOutputStream = CMdaAudioOutputStream::NewL(*this));
00193                 PanicIfError(err);
00194         #endif
00195         iOutputStream->Open(&iStreamSettings);
00196         }
00197 
00198 
00199 // ----------------------------------------------------------------------------
00200 // CAudioStreamEngine::Record()
00201 //
00202 // records audio data into the buffer
00203 // ----------------------------------------------------------------------------
00204 void CAudioStreamEngine::Record()
00205         {
00206         // If either stream is active, return
00207         if (iInputStatus!=ENotReady || iOutputStatus!=ENotReady) 
00208         {
00209             ShowMessage(_L("Stream in use, \ncannot record audio."), ETrue);
00210                 return;
00211         }
00212 
00213         // Open input stream.
00214         // Upon completion will receive callback in 
00215         // MMdaAudioInputStreamCallback::MaiscOpenComplete().
00216         #ifndef __SERIES60_3X__  // Not 3rd Ed
00217                 // Some 2nd Edition, FP2 devices (such as Nokia 6630) require the stream to be
00218                 // reconstructed each time before calling Open() - otherwise the callback
00219                 // never gets called.
00220                 if (iInputStream) delete iInputStream;
00221                 iInputStream = NULL; // In case the following NewL leaves
00222                 TRAPD(err, iInputStream = CMdaAudioInputStream::NewL(*this));
00223                 PanicIfError(err);
00224         #endif
00225         iInputStream->Open(&iStreamSettings);
00226         }
00227 
00228 // ----------------------------------------------------------------------------
00229 // CAudioStreamEngine::Stop()
00230 //
00231 // stops playing/recording
00232 // ----------------------------------------------------------------------------
00233 void CAudioStreamEngine::Stop()
00234         {
00235         // if input or output streams are active, close them
00236         if (iInputStatus!=ENotReady) 
00237                 {
00238                 iInputStream->Stop();
00239                 ShowMessage(_L("\nRecording stopped!"), EFalse);
00240                 iBufferOK = ETrue;
00241                 }               
00242         if (iOutputStatus!=ENotReady) 
00243                 {
00244                 iOutputStream->Stop();
00245                 ShowMessage(_L("\nPlayback stopped!"), ETrue);
00246                 }
00247         }
00248 
00249 
00250 // ----------------------------------------------------------------------------
00251 // CAudioStreamEngine::LoadAudioFileL()
00252 //
00253 // loads the audio data from a file into the buffer
00254 // ----------------------------------------------------------------------------
00255 void CAudioStreamEngine::LoadAudioFileL()
00256         {
00257         RFileReadStream audiofile;
00258 
00259         // open file
00260         TFileName fileName;
00261         fileName.Copy(iAudioFilePath);
00262         fileName.Append(iAudioFile);
00263 
00264         TInt err = audiofile.Open(iFs, fileName, EFileRead|EFileStream);
00265         iStreamBuffer->Des().FillZ(iFrameCount * iFrameSize);  // Empty the stream buffer
00266         if (err==KErrNone) 
00267                 {
00268                 // file opened ok, proceed reading
00269                 if (iUseAMR)
00270                         {
00271                         // Read the AMR header (the first 6 bytes). We don't need to save/use the header,
00272                         // since while playback we already know it's an AMR-NB encoded stream.
00273                         TBuf8<KAMRHeaderLength> temp;
00274                         audiofile.ReadL(temp, KAMRHeaderLength);
00275                         }
00276 
00277                 TUint idx=0;
00278                 while (idx < iFrameCount)
00279                         {
00280                         TRAPD(fstatus, audiofile.ReadL(GetFrame(idx), iFrameSize));
00281                         if (fstatus!=KErrNone)
00282                                 break;
00283                         idx++;
00284                         }
00285                 iStreamStart=0;
00286                 iStreamEnd=idx-1;
00287                 ShowMessage(_L("Loading complete!"), ETrue);
00288                 iBufferOK = ETrue;      
00289                 }       
00290         else 
00291                 {
00292                 // failed to open file
00293                 ShowMessage(_L("Error loading \naudio sample!"), ETrue); 
00294                 iBufferOK = EFalse;
00295                 }
00296         audiofile.Close();
00297         }
00298 
00299 
00300 // ----------------------------------------------------------------------------
00301 // CAudioStreamEngine::SaveAudioFileL()
00302 //
00303 // saves the audio data in the buffer into a file
00304 // ----------------------------------------------------------------------------
00305 void CAudioStreamEngine::SaveAudioFileL()
00306         {
00307         if (!iBufferOK)
00308         {
00309                 // In case the encoding was changed between recording and trying to save the file
00310                 ShowMessage(_L("Recorded buffer does not \nmatch current encoding."), ETrue);   
00311                 ShowMessage(_L("\nPlease re-record and \ntry again."), EFalse); 
00312                 return;
00313         }
00314         RFileWriteStream audiofile;
00315 
00316         // Check for free space for saving the sample
00317         TVolumeInfo volinfo;
00318         TInt err=iFs.Volume(volinfo,EDriveC);
00319         if ( volinfo.iFree<(iFrameCount*iFrameSize))
00320                 {
00321                 // Not enough free space on drive for saving, report and exit
00322                 ShowMessage(_L("Cannot save file:\nnot enough space!"), ETrue); 
00323                 return;
00324                 }
00325 
00326         TFileName fileName;
00327         fileName.Copy(iAudioFilePath);
00328         fileName.Append(iAudioFile);
00329         err = audiofile.Replace(iFs, fileName, EFileWrite|EFileStream);
00330         if (err==KErrNone) 
00331                 {
00332                 if (iUseAMR)
00333                         {
00334                                 // Write the six-byte AMR header, so that the file can be used by other
00335                                 // applications as well.
00336                                 for (int i = 0; i < KAMRHeaderLength; i++)
00337                                         audiofile.WriteUint8L(KAMRNBHeader[i]);
00338                         }
00339                         
00340                 // File opened ok, proceed writing.
00341                 // Write audio data directly from iStreamBuffer
00342                 for (TUint idx=iStreamStart; idx<=iStreamEnd; idx++)//iFrameCount; idx++)
00343                         {
00344                         audiofile.WriteL(GetFrame(idx));
00345                         }
00346                 ShowMessage(_L("Saving complete!"), ETrue);     
00347                 }       
00348         else 
00349                 {
00350                 // failed to open file
00351                 ShowMessage(_L("Error saving \naudio sample!"), ETrue); 
00352                 }
00353         audiofile.Close();
00354         }
00355 
00356 // ----------------------------------------------------------------------------
00357 // CAudioStreamEngine::SetEncodingL(TBool aAmr)
00358 //
00359 // If argument is ETrue, AMR-NB encoding will be used in audio input/output.
00360 // If EFalse, the default PCM is used. If the platform does not support AMR-NB,
00361 // PCM will be used no matter what the argument's value is.
00362 // ----------------------------------------------------------------------------
00363 void CAudioStreamEngine::SetEncodingL(TBool aAmr)
00364         {
00365         // Act only if the new encoding differs from the current one
00366         if (iUseAMR != aAmr)
00367                 {
00368                 iUseAMR = aAmr;
00369                 if (iUseAMR)
00370                         {
00371                         // Try to set AMR-NB encoding, this will indicate whether it is supported
00372                         // by the platform or not.
00373                         TRAPD(err, iInputStream->SetDataTypeL(KMMFFourCCCodeAMR));
00374                         if (err != KErrNone)
00375                                 {
00376                                 ShowMessage(_L("AMR-NB not supported,\nusing PCM."), ETrue);    
00377                                 iCurrentEncoding = iDefaultEncoding;
00378                                 iUseAMR = EFalse;
00379                                 // We do not need to invalidate the buffer or change buffer settings, 
00380                                 // since the encoding was not changed -> just return.
00381                                 return;  
00382                                 }
00383                         else
00384                                 {
00385                                 iCurrentEncoding = KMMFFourCCCodeAMR;
00386                                 iAudioFile.Zero();  // Empty the audio file name                                
00387                                 iAudioFile.Append(KAudioFileAMR);
00388                                 iFrameCount = KFrameCountAMR;
00389                                 iFrameSize = KFrameSizeAMR;
00390                                 ShowMessage(_L("Encoding set to AMR-NB."), ETrue);      
00391                                 }
00392                         }
00393                 else
00394                         {
00395                         // If we get here, the encoding has previously been changed to AMR. Switch back to
00396                         // PCM.
00397                         iCurrentEncoding = iDefaultEncoding;
00398                         iAudioFile.Zero();  // Empty the audio file name                                
00399                         iAudioFile.Append(KAudioFilePCM);
00400                         iFrameCount = KFrameCountPCM;
00401                         iFrameSize = KFrameSizePCM;
00402                         ShowMessage(_L("Encoding set to PCM."), ETrue); 
00403                         }
00404 
00405                 // Make sure the user re-records or reloads the audio file, so that we do not 
00406                 // accidentally try to play PCM data using AMR or vice versa.
00407                 iBufferOK = EFalse;     
00408                 if (iStreamBuffer) delete iStreamBuffer;
00409                 iStreamBuffer = NULL; // In case the following NewL leaves
00410                 iStreamBuffer = HBufC8::NewMaxL(iFrameSize * iFrameCount);
00411                 iStreamStart=0;
00412                 iStreamEnd=iFrameCount - 1;
00413                 }       
00414         }
00415 
00416 // ----------------------------------------------------------------------------
00417 // CAudioStreamEngine::ShowMessage(
00418 //     const TDesC& aMsg, TBool aReset=EFalse)
00419 //
00420 // displays text referenced by aMsg in the label, will append the aMsg in the 
00421 // existing text in label if aReset is EFalse, otherwise will reset the label 
00422 // text.
00423 // ----------------------------------------------------------------------------
00424 void CAudioStreamEngine::ShowMessage(const TDesC& aMsg, TBool aReset=EFalse)
00425         {
00426         if (aReset)     // if ETrue, clear the message on the label prior to output
00427                 iMsg.Zero();
00428         iMsg.Append(aMsg);
00429         TRAPD(error, iAppUi->GetView()->ShowMessageL(iMsg));
00430         PanicIfError(error);
00431         }
00432 
00433 // ----------------------------------------------------------------------------
00434 // TPtr8& CAudioStreamEngine::GetFrame(TUint aFrameIdx)
00435 //
00436 // Returns a modifiable pointer to a single frame inside the audio buffer 
00437 // ----------------------------------------------------------------------------
00438 TPtr8& CAudioStreamEngine::GetFrame(TUint aFrameIdx)
00439         {
00440           __ASSERT_ALWAYS(aFrameIdx < iFrameCount, 
00441                                                                         User::Panic(_L("AudioStreamEx"), 1));
00442                                                                 
00443           iFramePtr.Set((TUint8*)(iStreamBuffer->Ptr() + (aFrameIdx * iFrameSize)),
00444                                                                  iFrameSize,
00445                                                                  iFrameSize);
00446           return iFramePtr;
00447         }
00448 
00449 // ----------------------------------------------------------------------------
00450 // TPtr8& CAudioStreamEngine::GetPlaybackFrames(TUint aLastFrame)
00451 //
00452 // Returns a modifiable pointer to the requested frames inside the audio buffer
00453 // (from the first frame to aLastFrame). 
00454 // ----------------------------------------------------------------------------
00455 TPtr8& CAudioStreamEngine::GetPlaybackFrames(TUint aLastFrame)
00456         {
00457         __ASSERT_ALWAYS(aLastFrame < iFrameCount, 
00458                                                                 User::Panic(_L("AudioStreamEx"), 2));
00459                                                                 
00460         iFramePtr.Set((TUint8*)(iStreamBuffer->Ptr()),
00461                                                                  (aLastFrame + 1) * iFrameSize,
00462                                                                  (aLastFrame + 1) * iFrameSize);
00463         return iFramePtr;
00464         }
00465 
00466 
00467 //
00468 // MMdaAudioInputStream callbacks (MMdaAudioInputStreamCallback)
00469 //
00470 // ----------------------------------------------------------------------------
00471 // CAudioStreamEngine::MaiscOpenComplete(
00472 //     TInt aError)
00473 //
00474 // called upon completion of CMdaAudioInputStream::Open(),
00475 // if the stream was opened succesfully (aError==KErrNone), it's ready for use.
00476 // upon succesful open, the first audio data block will be read from the input
00477 // stream.
00478 // ----------------------------------------------------------------------------
00479 void CAudioStreamEngine::MaiscOpenComplete(TInt aError)
00480         {
00481         if (aError==KErrNone) 
00482                 {
00483                 // Input stream opened succesfully, set status
00484                 iInputStatus = EOpen;
00485                 // Set the data type (encoding)
00486                 TRAPD(error, iInputStream->SetDataTypeL(iCurrentEncoding));
00487                 PanicIfError(error);
00488 
00489                 // set stream input gain to maximum
00490                 iInputStream->SetGain(iInputStream->MaxGain()); 
00491                 // set stream priority to normal and time sensitive
00492                 iInputStream->SetPriority(EPriorityNormal, EMdaPriorityPreferenceTime);                         
00493                 ShowMessage(_L("Recording..."), ETrue);
00494                 
00495                 // Emtpy the buffer and issue ReadL() to read the first audio data block, 
00496                 // subsequent calls to ReadL() will be issued 
00497                 // in MMdaAudioInputStreamCallback::MaiscBufferCopied()
00498                 iStreamBuffer->Des().FillZ(iFrameCount * iFrameSize);
00499                 iStreamIdx=0;
00500                 TRAPD(error2, iInputStream->ReadL(GetFrame(iStreamIdx)));
00501                 PanicIfError(error2);
00502                 } 
00503         else 
00504                 {
00505                 // input stream open failed
00506                 iInputStatus = ENotReady;
00507                 ShowMessage(_L("Recording failed!"), ETrue);
00508                 }
00509         }
00510 
00511 // ----------------------------------------------------------------------------
00512 // CAudioStreamEngine::MaiscBufferCopied(
00513 //     TInt aError, const TDesC8& aBuffer)
00514 //
00515 // called when a block of audio data has been read and is available at the 
00516 // buffer reference *aBuffer.  calls to ReadL() will be issued until all blocks
00517 // in the audio data buffer (iStreamBuffer) are filled.
00518 // ----------------------------------------------------------------------------
00519 void CAudioStreamEngine::MaiscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
00520         {
00521         
00522         if (aError==KErrNone) 
00523                 {
00524                 // stop recording if at the end of the buffer
00525                 iStreamIdx++;
00526                 if (iStreamIdx == iFrameCount)
00527                     {
00528                     ShowMessage(_L("\nRecording complete!"), EFalse);
00529                     iStreamEnd = iStreamIdx - 1;
00530                 iBufferOK = ETrue;
00531                         iInputStatus = ENotReady;
00532                         // NOTE: In 2nd Edition we MUST NOT call iInputStream->Stop() here, because
00533                         // this will cause a crash on 2nd Edition, FP1 devices.
00534                         // Since iInputStream->Stop() is not called, the callback method
00535                         // MaiscRecordComplete() will not be called either after exiting this method.
00536                         // In 3rd Edition, however, iInputStream->Stop() MUST be called in order to reach
00537                         // MaiscRecordComplete(), otherwise the stream will "hang".
00538                         #ifdef __SERIES60_3X__
00539                                 // It appears that in 3.2 also calling Stop() here causes crash.
00540                                 // Instead we let MMF to detect that buffer becomes full. It will
00541                                 // then call MaiscRecordComplete() with status KErrOverflow.
00542                                 // Input stream will be stopped there.
00543 
00544                                 // iInputStream->Stop();
00545                         #endif
00546                     return;
00547                         }               
00548                 
00549                 // issue ReadL() for next frame         
00550                 TRAPD(error, iInputStream->ReadL(GetFrame(iStreamIdx)));
00551                 PanicIfError(error);
00552                 }
00553         else if (aError==KErrAbort) 
00554                 {
00555                 // Recording was aborted, due to call to CMdaAudioInputStream::Stop()
00556                 // This KErrAbort will occur each time the Stop() method in this class is executed.
00557                 // Also, MaiscRecordComplete() will be called after exiting this method.
00558             iStreamEnd = iStreamIdx - 1;
00559             iBufferOK = ETrue;
00560                 iInputStatus = ENotReady;
00561                 }
00562         else 
00563                 {
00564                 ShowMessage(_L("\nError reading data \nfrom input"), EFalse);
00565                 iInputStatus = ENotReady;
00566                 }
00567         }
00568 
00569 // ----------------------------------------------------------------------------
00570 // CAudioStreamEngine::MaiscRecordComplete(
00571 //     TInt aError)
00572 //
00573 // called when input stream is closed by CMdaAudioInputStream::Stop()
00574 // ----------------------------------------------------------------------------
00575 void CAudioStreamEngine::MaiscRecordComplete(TInt aError)
00576         {       
00577         iInputStatus = ENotReady;
00578         if (aError==KErrNone) 
00579                 {
00580                 // normal stream closure
00581                 }
00582         else 
00583                 {
00584                 // completed with error(s)
00585                 }
00586 
00587     // see comments in MaiscBufferCopied() regarding stopping the stream.
00588     iInputStream->Stop();
00589         }
00590 
00591 
00592 // MMdaAudioOutputStream callbacks (MMdaAudioOutputStreamCallback)
00593 
00594 // ----------------------------------------------------------------------------
00595 // CAudioStreamEngine::MaoscOpenComplete(
00596 //     TInt aError)
00597 //
00598 // called upon completion of CMdaAudioOutputStream::Open(),
00599 // if the stream was opened succesfully (aError==KErrNone), it's ready for use.
00600 // upon succesful open, the first audio data block will be written to the 
00601 // output stream.
00602 // ----------------------------------------------------------------------------
00603 void CAudioStreamEngine::MaoscOpenComplete(TInt aError)
00604         {
00605         if (aError==KErrNone) 
00606                 {
00607                 // output stream opened succesfully, set status
00608                 iOutputStatus = EOpen;
00609                 // Set the data type (encoding). Should not fail, since we already
00610                 // have tested support for this encoding in SetEncodingL with the 
00611                 // corresponding input stream!
00612                 TRAPD(error, iOutputStream->SetDataTypeL(iCurrentEncoding));
00613                 PanicIfError(error);
00614                 
00615                 // set volume to 1/4th of stream max volume
00616                 iOutputStream->SetVolume(iOutputStream->MaxVolume()/4);
00617                 // set stream priority to normal and time sensitive
00618                 iOutputStream->SetPriority(EPriorityNormal, 
00619                         EMdaPriorityPreferenceTime);                            
00620                 ShowMessage(_L("Playing "), ETrue);
00621 
00622                 if (iUseAMR)
00623                         {
00624                         // In case of AMR, the whole recorded/loaded buffer is played back at once, not frame by frame. 
00625                         // The buffer might not be fully recorded, so we will only play back the part
00626                         // that is filled with data.
00627                         iStreamIdx = iStreamEnd;
00628                         TRAPD(error2, iOutputStream->WriteL(GetPlaybackFrames(iStreamEnd)));
00629                         PanicIfError(error2);
00630                         }
00631                 else
00632                         {
00633                         // PCM needs to be played back frame by frame, otherwise some older devices might
00634                         // run into buffer overflow situations.
00635                         iStreamIdx = 0;
00636                         TRAPD(error3, iOutputStream->WriteL(GetFrame(iStreamIdx)));
00637                         PanicIfError(error3);
00638                         }
00639                 }
00640         else 
00641                 {
00642                 // output stream open failed
00643                 iOutputStatus = ENotReady;
00644                 ShowMessage(_L("Playback failed!"), ETrue);
00645                 }               
00646         }
00647 
00648 // ----------------------------------------------------------------------------
00649 // CAudioStreamEngine::MaoscBufferCopied(
00650 //     TInt aError, const TDesC8& aBuffer)
00651 //
00652 // called when a block of audio data has been written to MMF. calls to WriteL() 
00653 // will be issued until all blocks in the audio data buffer (iStreamBuffer) are 
00654 // written.
00655 // ----------------------------------------------------------------------------
00656 void CAudioStreamEngine::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
00657         {       
00658         if (aError==KErrNone) 
00659                 {
00660                 if (iStreamIdx==iStreamEnd)
00661                         {
00662                         ShowMessage(_L("\nPlayback complete!"), EFalse);
00663                         iOutputStatus = ENotReady;
00664                         // NOTE: In 2nd Edition we MUST NOT call iOutputStream->Stop() here, because
00665                         // this will cause a crash on 2nd Edition, FP1 devices.
00666                         // Since iOutputStream->Stop() is not called, the callback method
00667                         // MaiscRecordComplete() will not be called either after exiting this method.
00668                         // In 3rd Edition, however, iOutputStream->Stop() MUST be called in order to reach
00669                         // MaiscRecordComplete(), otherwise the stream will "hang".
00670                         #ifdef __SERIES60_3X__
00671                                 iOutputStream->Stop();
00672                         #endif
00673                         }
00674                 else 
00675                         {
00676                         iStreamIdx++;
00677                         TRAPD(error, iOutputStream->WriteL(GetFrame(iStreamIdx)));      
00678                         PanicIfError(error);
00679                         }
00680                 }
00681         else if (aError==KErrAbort) 
00682                 {
00683                 // Playing was aborted, due to call to CMdaAudioOutputStream::Stop().
00684                 // MaoscRecordComplete() will be called after exiting this method.
00685                 iOutputStatus = ENotReady;
00686                 }
00687         else 
00688                 {
00689                 ShowMessage(_L("\nError writing data \nto output"), EFalse);                    
00690                 iOutputStatus = ENotReady;
00691                 }
00692         }
00693 
00694 
00695 // ----------------------------------------------------------------------------
00696 // CAudioStreamEngine::MaoscPlayComplete(
00697 //     TInt aError)
00698 //
00699 // called when output stream is closed by CMdaAudioOutputStream::Stop() or if 
00700 // end of audio data has been reached, in this case KErrUnderflow will be 
00701 // returned.
00702 // ----------------------------------------------------------------------------
00703 void CAudioStreamEngine::MaoscPlayComplete(TInt aError)
00704         {
00705         iOutputStatus = ENotReady;
00706         if (aError==KErrNone) 
00707                 {
00708                 // normal stream closure
00709                 }       
00710         else if (aError==KErrUnderflow) 
00711                 {
00712                 // end of audio data stream was reached because of stream underflow,
00713                 }
00714         else 
00715                 {
00716                 // completed with error(s)
00717                 }       
00718         }
00719 
00720 // END OF FILE
00721 

© Nokia 2009

Back to top