Nokia N97 SDK
Example Applications Guide

CAudioStreamEngine Class Reference

#include <AudioStreamEngine.h>

Inheritance diagram for CAudioStreamEngine:

List of all members.

Detailed Description

CAudioStreamEngine application engine class.

Definition at line 40 of file AudioStreamEngine.h.


Public Member Functions

  ~CAudioStreamEngine ()
void  Play ()
void  Record ()
void  Stop ()
void  LoadAudioFileL ()
void  SaveAudioFileL ()
void  SetEncodingL (TBool aAmr)

Static Public Member Functions

static CAudioStreamEngine *  NewL (CAudioStreamAppUi *)
static CAudioStreamEngine *  NewLC (CAudioStreamAppUi *)

Private Types

enum   TStatus { ENotReady, EOpen }

Private Member Functions

TPtr8 &  GetFrame (TUint aFrameIdx)
TPtr8 &  GetPlaybackFrames (TUint aLastFrame)
void  ShowMessage (const TDesC &, TBool)
virtual void  MaiscOpenComplete (TInt aError)
virtual void  MaiscBufferCopied (TInt aError, const TDesC8 &aBuffer)
virtual void  MaiscRecordComplete (TInt aError)
virtual void  MaoscOpenComplete (TInt aError)
virtual void  MaoscBufferCopied (TInt aError, const TDesC8 &aBuffer)
virtual void  MaoscPlayComplete (TInt aError)
void  ConstructL ()
  CAudioStreamEngine (CAudioStreamAppUi *)

Private Attributes

CAudioStreamAppUi *  iAppUi
CMdaAudioInputStream *  iInputStream
CMdaAudioOutputStream *  iOutputStream
RFs  iFs
TBool  iUseAMR
TFileName  iAudioFilePath
TFileName  iAudioFile
TUint  iFrameSize
TUint  iFrameCount
HBufC8 *  iStreamBuffer
TPtr8  iFramePtr
TBool  iBufferOK
TFourCC  iDefaultEncoding
TFourCC  iCurrentEncoding
TMdaAudioDataSettings  iStreamSettings
TStatus  iInputStatus
TStatus  iOutputStatus
TUint  iStreamIdx
TBuf< 255 >  iMsg
TUint  iStreamStart
TUint  iStreamEnd


Constructor & Destructor Documentation

CAudioStreamEngine::~CAudioStreamEngine (   ) 
 

~CAudioStreamEngine()

discussion Destroy the object and release all memory objects

Definition at line 139 of file AudioStreamEngine.cpp.

References iInputStatus, iInputStream, iOutputStatus, iOutputStream, and iStreamBuffer.

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         }

CAudioStreamEngine::CAudioStreamEngine (  CAudioStreamAppUi *   )  [private]
 

CAudioStreamEngine()

discussion Perform the first phase of two phase construction

Definition at line 121 of file AudioStreamEngine.cpp.

References iStreamSettings.

Referenced by NewLC().

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         }


Member Function Documentation

CAudioStreamEngine * CAudioStreamEngine::NewL (  CAudioStreamAppUi *   )  [static]
 

NewL()

discussion Create new CAudioStreamEngine object return a pointer to the created instance of CAudioStreamEngine

Definition at line 59 of file AudioStreamEngine.cpp.

References NewLC().

Referenced by CAudioStreamAppUi::ConstructL().

00060         {
00061         CAudioStreamEngine* self = CAudioStreamEngine::NewLC(aAppUi);
00062     CleanupStack::Pop(self);
00063         return self;
00064         }

CAudioStreamEngine * CAudioStreamEngine::NewLC (  CAudioStreamAppUi *   )  [static]
 

NewLC()

discussion Create new CAudioStreamEngine object return a pointer to the created instance of CAudioStreamEngine which has also been pushed to cleanup stack

Definition at line 66 of file AudioStreamEngine.cpp.

References CAudioStreamEngine().

Referenced by NewL().

00067         {
00068         CAudioStreamEngine* self = new (ELeave) CAudioStreamEngine(aAppUi);
00069         CleanupStack::PushL(self);
00070         self->ConstructL();
00071         return self;
00072         }

void CAudioStreamEngine::Play (   ) 
 

Play()

discussion Plays the audio data sample

Definition at line 167 of file AudioStreamEngine.cpp.

References iBufferOK, iInputStatus, iOutputStatus, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

void CAudioStreamEngine::Record (   ) 
 

Record()

discussion Records an audio data sample

Definition at line 204 of file AudioStreamEngine.cpp.

References iInputStatus, iInputStream, iOutputStatus, iStreamSettings, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

void CAudioStreamEngine::Stop (   ) 
 

Stop()

discussion Stops the playing/recording of the audio data

Definition at line 233 of file AudioStreamEngine.cpp.

References iBufferOK, iInputStatus, iInputStream, iOutputStatus, iOutputStream, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

void CAudioStreamEngine::LoadAudioFileL (   ) 
 

LoadAudioFileL()

discussion Loads an audio data from a file

Definition at line 255 of file AudioStreamEngine.cpp.

References GetFrame(), iAudioFile, iAudioFilePath, iBufferOK, iFrameCount, iFrameSize, iFs, iStreamBuffer, iStreamEnd, iStreamStart, iUseAMR, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

void CAudioStreamEngine::SaveAudioFileL (   ) 
 

SaveAudioFileL()

discussion Saves the audio data into a file

Definition at line 305 of file AudioStreamEngine.cpp.

References GetFrame(), iAudioFile, iAudioFilePath, iBufferOK, iFrameCount, iFrameSize, iFs, iStreamEnd, iStreamStart, iUseAMR, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

void CAudioStreamEngine::SetEncodingL (  TBool  aAmr  ) 
 

SetEncodingL()

discussion Sets the audio encoding to be used param aAmr If ETrue, AMR-NB encoding will be used, if EFalse, the platform's default encoding will be used

Definition at line 363 of file AudioStreamEngine.cpp.

References iAudioFile, iBufferOK, iCurrentEncoding, iDefaultEncoding, iFrameCount, iFrameSize, iInputStream, iStreamBuffer, iStreamEnd, iStreamStart, iUseAMR, and ShowMessage().

Referenced by CAudioStreamAppUi::HandleCommandL().

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         }

TPtr8 & CAudioStreamEngine::GetFrame (  TUint  aFrameIdx  )  [private]
 

GetFrame()

discussion Returns a modifiable pointer to a single frame inside the audio buffer

param aFrameIdx frame index return a TPtr8 reference to the requested frame

Definition at line 438 of file AudioStreamEngine.cpp.

References iFrameCount, iFramePtr, iFrameSize, and iStreamBuffer.

Referenced by LoadAudioFileL(), MaiscBufferCopied(), MaiscOpenComplete(), MaoscBufferCopied(), MaoscOpenComplete(), and SaveAudioFileL().

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         }

TPtr8 & CAudioStreamEngine::GetPlaybackFrames (  TUint  aLastFrame  )  [private]
 

GetPlaybackFrames()

discussion Returns a modifiable pointer to frames from the first frame to aLastFrame in the audio buffer

param aLastFrame Index of the last frame to be included in the pointer returned return a TPtr8 reference to the requested frames

Definition at line 455 of file AudioStreamEngine.cpp.

References iFrameCount, iFramePtr, iFrameSize, and iStreamBuffer.

Referenced by MaoscOpenComplete().

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         }

void CAudioStreamEngine::ShowMessage (  const TDesC &  ,
TBool 
)  [private]
 

ShowMessage()

discussion Displays application messages for user on a label

param aMsg text to be displayed param aReset if true, the label will be cleared before displaying aMsg, if false, aMsg text will be appended to existing message on label

Definition at line 424 of file AudioStreamEngine.cpp.

References CAudioStreamAppUi::GetView(), iAppUi, iMsg, and CAudioStreamView::ShowMessageL().

Referenced by LoadAudioFileL(), MaiscBufferCopied(), MaiscOpenComplete(), MaoscBufferCopied(), MaoscOpenComplete(), Play(), Record(), SaveAudioFileL(), SetEncodingL(), and Stop().

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         }

void CAudioStreamEngine::MaiscOpenComplete (  TInt  aError  )  [private, virtual]
 

MaiscOpenComplete()

discussion A callback function that is called when CMdaAudioInputStream::Open() has completed, indicating that the audio input stream is ready for use.

param aError KErrNone if the open succeeded, otherwise one of the system error codes.

Definition at line 479 of file AudioStreamEngine.cpp.

References GetFrame(), iCurrentEncoding, iFrameCount, iFrameSize, iInputStatus, iInputStream, iStreamBuffer, iStreamIdx, and ShowMessage().

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         }

void CAudioStreamEngine::MaiscBufferCopied (  TInt  aError,
const TDesC8 &  aBuffer
)  [private, virtual]
 

MaiscBufferCopied()

discussion A callback function that is called when a chunk of audio data has been copied to the descriptor specified in a CMdaAudioInputStream::ReadL().

param aError KErrNone if the copy succeeded, KErrAbort if the input stream was closed for some reason, otherwise one of the system error codes.

Definition at line 519 of file AudioStreamEngine.cpp.

References GetFrame(), iBufferOK, iFrameCount, iInputStatus, iInputStream, iStreamEnd, iStreamIdx, and ShowMessage().

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         }

void CAudioStreamEngine::MaiscRecordComplete (  TInt  aError  )  [private, virtual]
 

MaiscRecordComplete()

discussion A callback function that is called when the input stream is closed using CMdaAudioInputStream::Stop().

param aError KErrNone if the stop succeeded, otherwise one of the system error codes.

Definition at line 575 of file AudioStreamEngine.cpp.

References iInputStatus, and iInputStream.

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         }

void CAudioStreamEngine::MaoscOpenComplete (  TInt  aError  )  [private, virtual]
 

MaoscOpenComplete()

discussion A callback function that is called when CMdaAudioOutputStream::Open() has completed, indicating that the audio output stream is ready for use.

param aError KErrNone if the open succeeded, otherwise one of the system error codes.

Definition at line 603 of file AudioStreamEngine.cpp.

References GetFrame(), GetPlaybackFrames(), iCurrentEncoding, iOutputStatus, iOutputStream, iStreamEnd, iStreamIdx, iUseAMR, and ShowMessage().

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         }

void CAudioStreamEngine::MaoscBufferCopied (  TInt  aError,
const TDesC8 &  aBuffer
)  [private, virtual]
 

MaoscBufferCopied()

discussion A callback function that is called when a descriptor has been copied to the lower layers of MMF. It is also called when an error has occurred or when the client has stopped the stream playing before the descriptor has been fully copied (by calling CMdaAudioOutputStream::Stop())

param aError KErrNone if the copy succeeded, otherwise one of the system error codes. KErrAbort indicates that the client has stopped the stream playing before the descriptor has been copied. param aBuffer reference to the buffer that has been copied.

Definition at line 656 of file AudioStreamEngine.cpp.

References GetFrame(), iOutputStatus, iOutputStream, iStreamEnd, iStreamIdx, and ShowMessage().

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         }

void CAudioStreamEngine::MaoscPlayComplete (  TInt  aError  )  [private, virtual]
 

MaoscPlayComplete()

discussion A callback function that is called when playback terminates as a result of a CMdaAudioOutputStream::Stop().

param aError KErrNone if the close succeeded, otherwise one of the system error codes.

Definition at line 703 of file AudioStreamEngine.cpp.

References iOutputStatus.

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         }

void CAudioStreamEngine::ConstructL (   )  [private]
 

ConstructL()

discussion Perform the second phase construction of a CAudioStreamEngine object

Definition at line 75 of file AudioStreamEngine.cpp.

References iAppUi, iAudioFilePath, iCurrentEncoding, iDefaultEncoding, iFrameCount, iFrameSize, iFs, iInputStream, iOutputStream, iStreamBuffer, iStreamEnd, and iStreamStart.

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         }


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top