📄 audio3eng.cpp
字号:
/* Copyright (c) 2004, Symbian Software Ltd. All rights reserved */
#ifndef __Audio3ENG_H_
#define __Audio3ENG_H_
#include "Audio3eng.h"
#include "Audio3Ui.h"
#include "Audio3.hrh"
#include <mda\common\audio.h>
#include <e32math.h>
CAudio3Engine::CAudio3Engine(CAudio3Ui& aAppUi) :
iState(ENotReady),
iAppUi(aAppUi)
{
}
CAudio3Engine* CAudio3Engine::NewL(CAudio3Ui& aAppUi)
{
CAudio3Engine* self = NewLC(aAppUi);
CleanupStack::Pop(); // self
return self;
}
CAudio3Engine* CAudio3Engine::NewLC(CAudio3Ui& aAppUi)
{
CAudio3Engine* self = new (ELeave) CAudio3Engine(aAppUi);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
void CAudio3Engine::ConstructL()
{
}
void CAudio3Engine::SetFileL(const TDesC& /*aFileName*/)
{
}
CAudio3Engine::~CAudio3Engine()
{
delete iSynchronousExampleStream;
iSynchronousExampleStream = NULL;
delete iAsynchronousExampleStream;
iAsynchronousExampleStream = NULL;
}
void CAudio3Engine::StopL()
{
if (iSynchronousExampleStream)
{
iSynchronousExampleStream->Stop();
iState=EReadySynchronous;
}
else
{
iAsynchronousExampleStream->Stop();
iState=EReadyAsynchronous;
}
}
void CAudio3Engine::CreateSynchronousExample()
{
delete iSynchronousExampleStream;
iSynchronousExampleStream=NULL;
delete iAsynchronousExampleStream;
iAsynchronousExampleStream=NULL;
iSynchronousExampleStream= new (ELeave) CSynchronousExampleStream;
iSynchronousExampleStream->ConstructL(this);
}
void CAudio3Engine::CreateAsynchronousExample()
{
delete iSynchronousExampleStream;
iSynchronousExampleStream=NULL;
delete iAsynchronousExampleStream;
iAsynchronousExampleStream=NULL;
iAsynchronousExampleStream= new (ELeave) CAsynchronousExampleStream;
iAsynchronousExampleStream->ConstructL(this);
}
void CAudio3Engine::PlayL()
{
if (iSynchronousExampleStream)
iSynchronousExampleStream->StartPlayL();
else
iAsynchronousExampleStream->StartPlayL();
iState=EPlaying;
}
void CSynchronousExampleStream::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
{
if (aError==KErrNone || aError==KErrAbort)//user selected "Stop"
NextBuffer();
else
User::Invariant();
}
void CSynchronousExampleStream::MaoscOpenComplete(TInt aError)
{
if (aError==KErrNone)
{
iStream->SetAudioPropertiesL(TMdaAudioDataSettings::ESampleRate8000Hz,TMdaAudioDataSettings::EChannelsMono);
iStream->SetVolume(iStream->MaxVolume());
iEngine->SetState(CAudio3Engine::EReadySynchronous);
}
}
void CSynchronousExampleStream::MaoscPlayComplete(TInt aError)
{
if (aError==KErrNone || aError==KErrCancel)//user selected "Stop"
{
iEngine->SetState(CAudio3Engine::EReadySynchronous);
PlayEnded(aError);
}
else
User::Invariant();
}
_LIT(KFileName, "testing123.raw");
void CSynchronousExampleStream::OpenFileL()
{
User::LeaveIfError(iSession.Connect());
User::LeaveIfError(iFile.Open(iSession,KFileName,EFileShareAny));
}
void CSynchronousExampleStream::CloseFile()
{
iFile.Close();
iSession.Close();
}
void CSynchronousExampleStream::StartPlayL()
{
iFileError=KErrNone;
OpenFileL();
NextBuffer();
}
CSynchronousExampleStream::~CSynchronousExampleStream()
{
delete iStream;
CloseFile();
}
void CSynchronousExampleStream::PlayEnded(TInt /*aError*/)
{
CloseFile();
}
void CSynchronousExampleStream::ConstructL(CAudio3Engine* aEngine)
{
iEngine=aEngine;
iStream=CMdaAudioOutputStream::NewL(*this);
iStream->Open(NULL);
}
TInt CSynchronousExampleStream::FillBuffer()
{
TInt err=iFile.Read(iBuffer);
if (err==KErrNone)
if (iBuffer.Length() < KBufferSize)
err=KErrEof;
return err;
}
void CSynchronousExampleStream::Stop()
{
iStream->Stop();
}
void CAsynchronousExampleStream::Stop()
{
iStream->Stop();
Cancel(); //stop any more data arriving
}
void CSynchronousExampleStream::NextBuffer()
{
if (iFileError==KErrNone)
{
iFileError=FillBuffer();
TRAPD(err,iStream->WriteL(iBuffer));
if (err!=KErrNone)
Stop();
}
}
CAsynchronousExampleStream::CAsynchronousExampleStream()
: CActive(EPriorityNormal)
{
}
const TInt KMaxDelay=50000;
void CAsynchronousExampleStream::StartPlayL()
{
OpenFileL();
iState=ETimer;
iSeed=100;
iTimer.After(iStatus,1);//activate RunL()
SetActive();
}
CAsynchronousExampleStream::~CAsynchronousExampleStream()
{
Cancel();
iTimer.Close();
delete iStream;
CloseFile();
}
void CAsynchronousExampleStream::ConstructL(CAudio3Engine *aEngine)
{
iEngine=aEngine;
iTimer.CreateLocal();
iStream=CMdaAudioOutputStream::NewL(*this);
iStream->Open(NULL);
CActiveScheduler::Add(this);
}
void CAsynchronousExampleStream::NextBuffer(const TDesC8& aBuffer)
{
//assume the buffer is already there!
ASSERT(iBufList[iReturned]==aBuffer);
iBufList[iReturned].SetLength(0);
if (++iReturned == KNumBuf)
iReturned=0;
}
void CAsynchronousExampleStream::PlayEnded(TInt /*aError*/)
{
CloseFile();
iReturned=0;
iEnd=0;
}
void CAsynchronousExampleStream::RandomDelay()
{
TInt delay= Math::Rand(iSeed) % KMaxDelay;
iTimer.After(iStatus, delay);
SetActive();
}
void CAsynchronousExampleStream::DoCancel()
{
iTimer.Cancel();
//if there is an outstanding file read, this can't be cancelled.
}
void CAsynchronousExampleStream::RunL()
{
switch (iState)
{
case ETimer:
//A buffer is available to read from the source
if (iBufList[iEnd].Length()==0)
{
iFile.Read(iBufList[iEnd],iStatus);
iState=EReading;
SetActive();
break;
}
else
{//... but ignore it because there are no buffers free
TInt pos=KBufferSize;
iFile.Seek(ESeekCurrent, pos);
RandomDelay();
break;
}
case EReading:
//Got the buffer from the source, so send it
if (iBufList[iEnd].Length())
{
TRAPD(err,iStream->WriteL(iBufList[iEnd]));
if (++iEnd==KNumBuf)
iEnd=0;
iState=ETimer;
RandomDelay();
break;
}
else
{
iFinished=ETrue;
}
}
}
void CAsynchronousExampleStream::MaoscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
if (aError==KErrNone || aError==KErrAbort)//user selected "Stop"
NextBuffer(aBuffer);
else
User::Invariant();
}
void CAsynchronousExampleStream::MaoscOpenComplete(TInt aError)
{
if (aError==KErrNone)
{
iStream->SetAudioPropertiesL(TMdaAudioDataSettings::ESampleRate8000Hz,TMdaAudioDataSettings::EChannelsMono);
iStream->SetVolume(iStream->MaxVolume());
iEngine->SetState(CAudio3Engine::EReadyAsynchronous);
}
}
void CAsynchronousExampleStream::MaoscPlayComplete(TInt aError)
{
if (aError==KErrNone || aError==KErrCancel)//user selected "Stop"
{
iEngine->SetState(CAudio3Engine::EReadyAsynchronous);
PlayEnded(aError);
}
else
User::Invariant();
}
void CAsynchronousExampleStream::OpenFileL()
{
User::LeaveIfError(iSession.Connect());
User::LeaveIfError(iFile.Open(iSession,KFileName,EFileShareAny));
}
void CAsynchronousExampleStream::CloseFile()
{
iFile.Close();
iSession.Close();
}
void CAudio3Engine::UpdateMenu(CEikMenuPane* aMenuPane)
{
switch(iState)
{
case ENotReady:
aMenuPane->SetItemDimmed(ECmdSynchronous, EFalse);
aMenuPane->SetItemDimmed(ECmdAsynchronous, EFalse);
aMenuPane->SetItemDimmed(ECmdPlay, ETrue);
aMenuPane->SetItemDimmed(ECmdStop, ETrue);
break;
case EReadySynchronous:
aMenuPane->SetItemDimmed(ECmdSynchronous, ETrue);
aMenuPane->SetItemDimmed(ECmdAsynchronous, EFalse);
aMenuPane->SetItemDimmed(ECmdPlay, EFalse);
aMenuPane->SetItemDimmed(ECmdStop, ETrue);
break;
case EReadyAsynchronous:
aMenuPane->SetItemDimmed(ECmdSynchronous, EFalse);
aMenuPane->SetItemDimmed(ECmdAsynchronous, ETrue);
aMenuPane->SetItemDimmed(ECmdPlay, EFalse);
aMenuPane->SetItemDimmed(ECmdStop, ETrue);
break;
case EPlaying:
aMenuPane->SetItemDimmed(ECmdSynchronous, ETrue);
aMenuPane->SetItemDimmed(ECmdAsynchronous, ETrue);
aMenuPane->SetItemDimmed(ECmdPlay, ETrue);
aMenuPane->SetItemDimmed(ECmdStop, EFalse);
break;
}
}
#endif// __Audio3ENG_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -