📄 audio3engine.cpp
字号:
// CAudioEngine.cpp
//
// Copyright (c) 2004 Symbian Software Ltd. All rights reserved.
#include "CAudioEngine.h"
//
/************** CONSTRUCTION AND DESTRUCTION ******************/
//
CAudioEngine::CAudioEngine(RFs& aFs)
: iFs(aFs),
iState(CAudioEngine::EIdle),
iVolume(CAudioEngine::EMedium),
iGain(CAudioEngine::EGainMedium)
{
}
CAudioEngine::~CAudioEngine()
{
iEngineObserver = 0;
Stop();
}
//
/************** GENERAL FUNCTIONS ******************/
//
void CAudioEngine::SetObserver(MExEngineObserver& aObserver)
{
iEngineObserver = &aObserver;
}
// Terminate current operation
void CAudioEngine::Stop()
{
// Take appropriate action depending on what the state is
switch (iState)
{
case ETonePrepare:
iAudioToneUtility->CancelPrepare();
ToneCleanup();
break;
case ETonePlaying:
iAudioToneUtility->CancelPlay();
ToneCleanup();
break;
case EPlayFilePrepare:
PlayCleanup();
break;
case EPlayFilePlaying:
iAudioPlayUtility->Stop();
PlayCleanup();
break;
case ERecordPrepare:
RecordCleanup();
break;
case ERecording:
iAudioRecorderUtility -> Stop();
RecordCleanup();
break;
case EStreamPrepare:
StreamCleanup();
break;
case EStreamStarted:
case ENextStreamBuf:
iAudioStreamPlayer -> Stop(); // Calls MaoscBufferCopied() with KErrAbort
iState = EStreamStopping;
break;
default:
break;
};
// And tell the UI about the state change
if (iEngineObserver)
iEngineObserver -> HandleEngineState(iState,KErrNone);
}
// Get volume
CAudioEngine::TExVolume CAudioEngine::Volume() const
{
return iVolume;
}
// Set volume
void CAudioEngine::SetVolume(CAudioEngine::TExVolume aVolume)
{
iVolume = aVolume;
if (iAudioStreamPlayer) // Streaming is in progress
{
iAudioStreamPlayer -> SetVolume(iAudioStreamPlayer -> MaxVolume()/iVolume);
}
}
// Get gain
CAudioEngine::TExRecordGain CAudioEngine::Gain() const
{
return iGain;
}
// Set gain
void CAudioEngine::SetGain(CAudioEngine::TExRecordGain aGain)
{
iGain = aGain;
}
// Get state
CAudioEngine::TState CAudioEngine::Status() const
{
return iState;
}
// Panic
void CAudioEngine::Panic(CAudioEngine::TPanics aPanic)
{
_LIT(KPanic,"MEDIA_AUDIO_EX");
User::Panic(KPanic,aPanic);
}
//
/************** TONE PLAYING ******************/
//
// Play a tone of specified frequency and duration
void CAudioEngine::PlayL(TInt aFrequency,
const TTimeIntervalMicroSeconds& aDuration)
{
__ASSERT_DEBUG(iState == CAudioEngine::EIdle, Panic(CAudioEngine::EInProgress));
iAudioToneUtility = CMdaAudioToneUtility::NewL(*this);
iState = ETonePrepare;
iAudioToneUtility -> PrepareToPlayTone(aFrequency, aDuration);
// This will call back MatoPrepareComplete() when preparation is complete
}
// Audio is now prepared to play a tone
// from MMdaAudioToneObserver
void CAudioEngine::MatoPrepareComplete(TInt aError)
{
__ASSERT_DEBUG(iEngineObserver, Panic(CAudioEngine::ENullObserver));
__ASSERT_DEBUG(iAudioToneUtility, Panic(CAudioEngine::ENullTonePlayer));
__ASSERT_DEBUG(iState == ETonePrepare, Panic(CAudioEngine::EWrongState));
// If preparation was sucessful, play the tone
if (aError)
ToneCleanup();
else
{
iState = ETonePlaying;
iAudioToneUtility -> SetVolume(iAudioToneUtility -> MaxVolume()/iVolume);
iAudioToneUtility -> Play();
}
iEngineObserver -> HandleEngineState(iState,aError);
}
// Audio has completed playing the tone
// from MMdaAudioToneObserver
void CAudioEngine::MatoPlayComplete(TInt aError)
{
__ASSERT_DEBUG(iState == ETonePlaying, Panic(CAudioEngine::ENotReady));
__ASSERT_DEBUG(iEngineObserver, Panic(CAudioEngine::ENullObserver));
// Tell observer sound is complete, and clean up
ToneCleanup();
iEngineObserver -> HandleEngineState(iState,aError);
}
// Clean up
void CAudioEngine::ToneCleanup()
{
iState = EIdle;
delete iAudioToneUtility;
iAudioToneUtility = NULL;
}
//
/************** SOUND FILE PLAYING ******************/
//
// Play a sound file
void CAudioEngine::PlayL(const TDesC &aFile)
{
__ASSERT_DEBUG(iState == CAudioEngine::EIdle, Panic(CAudioEngine::EInProgress));
iAudioPlayUtility = CMdaAudioPlayerUtility::NewFilePlayerL(aFile, *this);
iState = EPlayFilePrepare;
// This will call back MapcInitComplete() when preparation is complete
}
// Audio is now prepared to play a file
// from MMdaAudioPlayerCallback
void CAudioEngine::MapcInitComplete(TInt aError,
const TTimeIntervalMicroSeconds& /*aDuration*/)
{
__ASSERT_DEBUG(iEngineObserver, Panic(CAudioEngine::ENullObserver));
__ASSERT_DEBUG(iAudioPlayUtility, Panic(CAudioEngine::ENullPlayerUtility));
__ASSERT_DEBUG(iState == EPlayFilePrepare, Panic(CAudioEngine::EWrongState));
// If preparation was sucessful, play the tone
if (aError)
PlayCleanup();
else
{
iState = EPlayFilePlaying;
iAudioPlayUtility -> SetVolume(iAudioPlayUtility -> MaxVolume()/iVolume);
iAudioPlayUtility->Play();
}
iEngineObserver -> HandleEngineState(iState,aError);
}
// Audio has completed playing the tone
// from MMdaAudioPlayerCallback
void CAudioEngine::MapcPlayComplete(TInt aError)
{
__ASSERT_DEBUG(iState == EPlayFilePlaying, Panic(CAudioEngine::ENotReady));
__ASSERT_DEBUG(iEngineObserver, Panic(CAudioEngine::ENullObserver));
// Tell observer sound is complete, and clean up
PlayCleanup();
iEngineObserver -> HandleEngineState(iState,aError);
}
// Clean up
void CAudioEngine::PlayCleanup()
{
iState = EIdle;
delete iAudioPlayUtility;
iAudioPlayUtility = NULL;
}
//
/************** SOUND RECORDING ******************/
//
// Record into a file
void CAudioEngine::RecordL(const TDesC &aFile)
{
__ASSERT_DEBUG(iState == CAudioEngine::EIdle, Panic(CAudioEngine::EInProgress));
// Set up file to record to, the recording format, codec, and settings
iLocation.iName = aFile;
iCodec.iBits = TMdaPcmWavCodec::E8BitPcm;
iSettings.iSampleRate=TMdaAudioDataSettings::ESampleRate8000Hz;
iSettings.iChannels=TMdaAudioDataSettings::EChannelsMono;
iAudioRecorderUtility = CMdaAudioRecorderUtility::NewL(*this);
iAudioRecorderUtility -> OpenL(&iLocation, &iFormat, &iCodec, &iSettings);
iState = ERecordPrepare;
iEngineObserver -> HandleEngineState(iState,KErrNone);
// This will call back MoscoStateChangeEvent() when preparation is complete
}
// Audio is now prepared to record a file
// from MMdaObjectStateChangeObserver
void CAudioEngine::MoscoStateChangeEvent(CBase* aObject,
TInt /*aPreviousState*/, TInt aCurrentState, TInt aErrorCode)
{
__ASSERT_ALWAYS(aObject == iAudioRecorderUtility, Panic(CAudioEngine::EWrongState));
__ASSERT_DEBUG(iState == CAudioEngine::ERecordPrepare || iState == CAudioEngine::ERecording,
Panic(CAudioEngine::EWrongState));
if (aErrorCode)
RecordCleanup();
else if (aCurrentState == CMdaAudioClipUtility::EOpen)
{
// Set the gain
iAudioRecorderUtility -> SetGain(iAudioRecorderUtility -> MaxGain()/iGain);
// And mode
iAudioRecorderUtility -> SetAudioDeviceMode(CMdaAudioRecorderUtility::ELocal);
// and start recording
iAudioRecorderUtility -> RecordL();
iState = ERecording;
}
iEngineObserver -> HandleEngineState(iState,aErrorCode);
}
// Clean up
void CAudioEngine::RecordCleanup()
{
iState = EIdle;
delete iAudioRecorderUtility;
iAudioRecorderUtility = NULL;
}
//
/************** STREAM PLAYING ******************/
//
// Play a stream
void CAudioEngine::PlayL()
{
__ASSERT_DEBUG(iState == CAudioEngine::EIdle, Panic(CAudioEngine::EInProgress));
iAudioStreamPlayer = CMdaAudioOutputStream::NewL(*this);
iAudioStreamPlayer -> Open(&iSettings);
// indicates whether the example will use a list to store the sound fragments
iState = EStreamPrepare;
iEngineObserver -> HandleEngineState(iState,KErrNone);
// This will call back MaoscOpenComplete() when preparation is complete
}
// Audio is now prepared to play a stream
void CAudioEngine::MaoscOpenComplete(TInt aError)
{
if (aError)
{
StreamCleanup();
}
else
{
iAudioStreamPlayer -> SetVolume(iAudioStreamPlayer -> MaxVolume()/iVolume);
iAudioStreamPlayer -> SetAudioPropertiesL(
TMdaAudioDataSettings::ESampleRate8000Hz,
TMdaAudioDataSettings::EChannelsMono);
iState = EStreamStarted;
}
iEngineObserver -> HandleEngineState(iState,aError);
// Caller can now write data into the stream by calling WriteToStreamL()
}
// Audio has accepted data: call back observer so they know to write more data.
// If using a list, just delete the first element
void CAudioEngine::MaoscBufferCopied(TInt aError, const TDesC8& /*aBuffer*/)
{
// Check for error
if (aError)
iState = EStreamError;
else
iState = ENextStreamBuf;
iEngineObserver -> HandleEngineState(iState,aError);
}
// Stream has finished
void CAudioEngine::MaoscPlayComplete(TInt aError)
{
if (aError == KErrUnderflow) aError=KErrNone;
StreamCleanup();
iEngineObserver -> HandleEngineState(iState,aError);
}
// Write data to the stream
void CAudioEngine::WriteToStreamL(const TDesC8& aData)
{
if (iState == CAudioEngine::EStreamStarted
|| iState == CAudioEngine::ENextStreamBuf)
iAudioStreamPlayer -> WriteL(aData);
// This will call back MaoscBufferCopied() when the data is copied
}
// Clean up
void CAudioEngine::StreamCleanup()
{
iState = EIdle;
delete iAudioStreamPlayer;
iAudioStreamPlayer = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -